Skip to content

Commit

Permalink
Merge pull request #76 from protofire/feature/add-whitelisting
Browse files Browse the repository at this point in the history
Switch from Blacklisting to Whitelisting
  • Loading branch information
Franco Victorio authored Dec 11, 2018
2 parents 4aa0704 + 3ad01bb commit 078441a
Show file tree
Hide file tree
Showing 126 changed files with 2,771 additions and 2,115 deletions.
27 changes: 0 additions & 27 deletions lib/config.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
const _ = require('lodash')
const { RULES } = require('./constants')

module.exports = {
from(configVals) {
Expand All @@ -13,31 +12,5 @@ module.exports = {

getNumber(ruleName, defaultValue) {
return this.getNumberByPath(`rules["${ruleName}"][1]`, defaultValue)
},

getRulesDisabledByDefault() {
const { PRETTIER } = RULES
return [PRETTIER]
},

isRuleDisabledByDefault(ruleId) {
const rules = module.exports.getRulesDisabledByDefault()
return rules.includes(ruleId)
},

validateDisabledByDefault(rulesConfig, rulesToCheck) {
const rulesArrayOfObjects = []

for (const ruleToCheck of rulesToCheck) {
const { rule, instance } = ruleToCheck

const isConfigured = rulesConfig && rulesConfig[rule]
const isEnabledByDefault = !module.exports.isRuleDisabledByDefault(rule)

if (isConfigured || isEnabledByDefault) {
rulesArrayOfObjects.push(instance)
}
}
return rulesArrayOfObjects
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
const BaseChecker = require('./../base-checker')
const { hasNoSpacesBefore } = require('./../../common/tokens')

class ArrayDeclarationChecker extends BaseChecker {
class ArrayDeclarationSpacesChecker extends BaseChecker {
constructor(reporter) {
super(reporter, 'array-declaration-spaces')
}

enterTypeName(ctx) {
this.validateSpaceBeforeBracket(ctx, '[')
this.validateSpaceBeforeBracket(ctx, ']')
Expand All @@ -23,8 +27,8 @@ class ArrayDeclarationChecker extends BaseChecker {
}

makeReport(ctx) {
this.error(ctx, 'array-declaration-spaces', 'Array declaration must not contains spaces')
this.error(ctx, 'Array declaration must not contains spaces')
}
}

module.exports = ArrayDeclarationChecker
module.exports = ArrayDeclarationSpacesChecker
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,11 @@ const BaseChecker = require('./../base-checker')
const { hasSpaceBefore, onSameLine, prevToken, startOf } = require('./../../common/tokens')
const { typeOf } = require('./../../common/tree-traversing')

class BracketsAlign extends BaseChecker {
class BracketAlign extends BaseChecker {
constructor(reporter) {
super(reporter, 'bracket-align')
}

enterBlock(ctx) {
this.validateBlock(ctx)
}
Expand Down Expand Up @@ -33,7 +37,7 @@ class BracketsAlign extends BaseChecker {
}

_error({ ctx, errorMessage }) {
this.error(ctx, 'bracket-align', errorMessage)
this.error(ctx, errorMessage)
}
}

Expand Down Expand Up @@ -95,4 +99,4 @@ class UsualOpenBracket extends OpenBracket {
}
}

module.exports = BracketsAlign
module.exports = BracketAlign
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,11 @@ const {
Rule
} = require('./../../common/statements-indent-validator')

class ExpressionAlignChecker extends BaseChecker {
class ExpressionIndentChecker extends BaseChecker {
constructor(reporter) {
super(reporter, 'expression-indent')
}

enterExpression(ctx) {
const expression = Rule.expression
const term = Term.term
Expand Down Expand Up @@ -67,8 +71,8 @@ class ExpressionAlignChecker extends BaseChecker {
}

_error(ctx, message) {
this.error(ctx, 'expression-indent', `Expression indentation is incorrect. ${message}`)
this.error(ctx, `Expression indentation is incorrect. ${message}`)
}
}

module.exports = ExpressionAlignChecker
module.exports = ExpressionIndentChecker
26 changes: 19 additions & 7 deletions lib/rules/align/indent.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,24 @@ const { columnOf, lineOf, stopLine } = require('./../../common/tokens')
class IndentChecker {
constructor(reporter, config) {
this.reporter = reporter
this.ruleId = 'indent'
this.linesWithError = []

const indent = this.parseConfig(config).indent || 4
const indentUnit = this.parseConfig(config).unit || 'spaces'

this.blockValidator = new BlockValidator(indent, indentUnit, reporter)
this.nestedSingleLineValidator = new NestedSingleLineValidator(indent, indentUnit, reporter)
this.baseIndentMultiplicityValidator = new BaseIndentMultiplicityValidator(indent, reporter)
this.blockValidator = new BlockValidator(indent, indentUnit, reporter, this.ruleId)
this.nestedSingleLineValidator = new NestedSingleLineValidator(
indent,
indentUnit,
reporter,
this.ruleId
)
this.baseIndentMultiplicityValidator = new BaseIndentMultiplicityValidator(
indent,
reporter,
this.ruleId
)
}

enterBlock(ctx) {
Expand Down Expand Up @@ -157,18 +167,19 @@ class Block {
}

class KnowLineValidator {
constructor(indent, indentUnit, reporter) {
constructor(indent, indentUnit, reporter, ruleId) {
this.indent = indent
this.indentUnit = indentUnit
this.reporter = reporter
this.linesWithError = []
this.ruleId = ruleId
}

makeReportCorrectLine(line, col, correctIndent) {
this.linesWithError.push(line)

const message = `Expected indentation of ${correctIndent} ${this.indentUnit} but found ${col}`
this.reporter.errorAt(line, col, 'indent', message)
this.reporter.errorAt(line, col, this.ruleId, message)
}
}

Expand Down Expand Up @@ -244,10 +255,11 @@ class NestedSingleLineValidator extends KnowLineValidator {
}

class BaseIndentMultiplicityValidator {
constructor(indent, reporter) {
constructor(indent, reporter, ruleId) {
this.reporter = reporter
this.indent = indent
this.firstIndent = new Map()
this.ruleId = ruleId
}

validate(linesWithError, ctx) {
Expand Down Expand Up @@ -283,7 +295,7 @@ class BaseIndentMultiplicityValidator {
}

error(line, col) {
this.reporter.errorAt(line, col, 'indent', 'Indentation is incorrect')
this.reporter.errorAt(line, col, this.ruleId, 'Indentation is incorrect')
}
}

Expand Down
24 changes: 12 additions & 12 deletions lib/rules/align/index.js
Original file line number Diff line number Diff line change
@@ -1,21 +1,21 @@
const NoMixTabAndSpacesChecker = require('./no-mix-tabs-and-spaces')
const ArrayDeclarationSpacesChecker = require('./array-declaration-spaces')
const BracketAlign = require('./bracket-align')
const ExpressionIndentChecker = require('./expression-indent')
const IndentChecker = require('./indent')
const BracketsAlign = require('./brackets-align')
const ArrayDeclarationChecker = require('./array-declaration')
const ExpressionAlignChecker = require('./expression-align')
const StatementsAlignChecker = require('./statements-align')
const SpaceAfterCommaChecker = require('./space-after-comma')
const NoMixTabsAndSpacesChecker = require('./no-mix-tabs-and-spaces')
const NoSpacesBeforeSemicolonChecker = require('./no-spaces-before-semicolon')
const SpaceAfterCommaChecker = require('./space-after-comma')
const StatementsAlignChecker = require('./statement-indent')

module.exports = function checkers(reporter, config) {
return [
new NoMixTabAndSpacesChecker(reporter, config),
new ArrayDeclarationSpacesChecker(reporter),
new BracketAlign(reporter),
new ExpressionIndentChecker(reporter),
new IndentChecker(reporter, config),
new BracketsAlign(reporter),
new ArrayDeclarationChecker(reporter),
new ExpressionAlignChecker(reporter),
new StatementsAlignChecker(reporter),
new NoMixTabsAndSpacesChecker(reporter, config),
new NoSpacesBeforeSemicolonChecker(reporter),
new SpaceAfterCommaChecker(reporter),
new NoSpacesBeforeSemicolonChecker(reporter)
new StatementsAlignChecker(reporter)
]
}
8 changes: 4 additions & 4 deletions lib/rules/align/no-mix-tabs-and-spaces.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
const BaseChecker = require('./../base-checker')

class NoMixTabAndSpacesChecker extends BaseChecker {
class NoMixTabsAndSpacesChecker extends BaseChecker {
constructor(reporter, config) {
super(reporter)
super(reporter, 'no-mix-tabs-and-spaces')

const configDefined = config.rules && config.rules.indent && config.rules.indent[1]
this.spacer = (configDefined && config.rules.indent[1] === 'tabs' && 'tabs') || 'spaces'
Expand All @@ -24,8 +24,8 @@ class NoMixTabAndSpacesChecker extends BaseChecker {

_error(line) {
const message = `Mixed tabs and spaces. Allowed only ${this.spacer}`
this.reporter.errorAt(line + 1, 0, 'no-mix-tabs-and-spaces', message)
this.errorAt(line + 1, 0, message)
}
}

module.exports = NoMixTabAndSpacesChecker
module.exports = NoMixTabsAndSpacesChecker
6 changes: 5 additions & 1 deletion lib/rules/align/no-spaces-before-semicolon.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@ const {
} = require('./../../common/tokens')

class NoSpacesBeforeSemicolonChecker extends BaseChecker {
constructor(reporter) {
super(reporter, 'no-spaces-before-semicolon')
}

exitSourceUnit(ctx) {
TokenList.from(ctx)
.semicolonTokens()
Expand All @@ -17,7 +21,7 @@ class NoSpacesBeforeSemicolonChecker extends BaseChecker {

_error(curToken) {
const message = 'Semicolon must not have spaces before'
this.reporter.errorAt(curToken.line, curToken.column, 'no-spaces-before-semicolon', message)
this.errorAt(curToken.line, curToken.column, message)
}
}

Expand Down
6 changes: 5 additions & 1 deletion lib/rules/align/space-after-comma.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@ const {
const { noSpaces, prevTokenFromToken } = require('./../../common/tokens')

class SpaceAfterCommaChecker extends BaseChecker {
constructor(reporter) {
super(reporter, 'space-after-comma')
}

exitSourceUnit(ctx) {
TokenList.from(ctx)
.commaTokens()
Expand All @@ -18,7 +22,7 @@ class SpaceAfterCommaChecker extends BaseChecker {

_error(curToken) {
const message = 'Comma must be separated from next element by space'
this.reporter.errorAt(curToken.line, curToken.column, 'space-after-comma', message)
this.errorAt(curToken.line, curToken.column, message)
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,11 @@ const { StatementsIndentValidator, Term } = require('./../../common/statements-i
const { onSameLine, stopOf, startOf } = require('./../../common/tokens')
const { typeOf } = require('./../../common/tree-traversing')

class StatementsAlignChecker extends BaseChecker {
class StatementIndentChecker extends BaseChecker {
constructor(reporter) {
super(reporter, 'statement-indent')
}

enterIfStatement(ctx) {
new StatementsIndentValidator(ctx)
.cases(ifWithoutElse(), ifElse())
Expand Down Expand Up @@ -75,7 +79,7 @@ class StatementsAlignChecker extends BaseChecker {
}

_error(ctx, message) {
this.error(ctx, 'statement-indent', `Statement indentation is incorrect. ${message}`)
this.error(ctx, `Statement indentation is incorrect. ${message}`)
}
}

Expand Down Expand Up @@ -151,4 +155,4 @@ function ifElse() {
.statement()
}

module.exports = StatementsAlignChecker
module.exports = StatementIndentChecker
15 changes: 8 additions & 7 deletions lib/rules/base-checker.js
Original file line number Diff line number Diff line change
@@ -1,18 +1,19 @@
class BaseChecker {
constructor(reporter) {
constructor(reporter, ruleId) {
this.reporter = reporter
this.ruleId = ruleId
}

error(ctx, ruleId, message) {
this.reporter.error(ctx, ruleId, message)
error(ctx, message) {
this.reporter.error(ctx, this.ruleId, message)
}

errorAt(line, column, ruleId, message) {
this.reporter.errorAt(line, column, ruleId, message)
errorAt(line, column, message) {
this.reporter.errorAt(line, column, this.ruleId, message)
}

warn(ctx, ruleId, message) {
this.reporter.warn(ctx, ruleId, message)
warn(ctx, message) {
this.reporter.warn(ctx, this.ruleId, message)
}
}

Expand Down
8 changes: 4 additions & 4 deletions lib/rules/best-practises/code-complexity.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ const BaseChecker = require('./../base-checker')

class CodeComplexityChecker extends BaseChecker {
constructor(reporter, config) {
super(reporter)
super(reporter, 'code-complexity')

this.maxComplexity = config.getNumber('code-complexity', 7)
}
Expand Down Expand Up @@ -54,16 +54,16 @@ class CodeComplexityChecker extends BaseChecker {
const scope = ComplexityScope.of(ctx)

if (scope && scope.complexity > this.maxComplexity) {
this.error(ctx, scope)
this._error(ctx, scope)
}
}

error(ctx, scope) {
_error(ctx, scope) {
const curComplexity = scope.complexity
const maxComplexity = this.maxComplexity

const message = `Function has cyclomatic complexity ${curComplexity} but allowed no more than ${maxComplexity}`
super.error(ctx, 'code-complexity', message)
this.error(ctx, message)
}
}

Expand Down
8 changes: 4 additions & 4 deletions lib/rules/best-practises/function-max-lines.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ const DEFAULT_MAX_LINES_COUNT = 50

class FunctionMaxLinesChecker extends BaseChecker {
constructor(reporter, config) {
super(reporter)
super(reporter, 'function-max-lines')

this.maxLines = config.getNumber('function-max-lines', DEFAULT_MAX_LINES_COUNT)
}
Expand All @@ -15,16 +15,16 @@ class FunctionMaxLinesChecker extends BaseChecker {
const block = Block.of(ctx)

if (block.linesCount() > this.maxLines) {
this.error(block)
this._error(block)
}
}

error(block) {
_error(block) {
const linesCount = block.linesCount()
const message = `Function body contains ${linesCount} lines but allowed no more than ${
this.maxLines
} lines`
super.error(block.ctx, 'function-max-lines', message)
this.error(block.ctx, message)
}
}

Expand Down
Loading

0 comments on commit 078441a

Please sign in to comment.