Skip to content

Commit

Permalink
Validate rule schemas
Browse files Browse the repository at this point in the history
  • Loading branch information
fvictorio committed Jan 30, 2020
1 parent e07b908 commit c2c1fb3
Show file tree
Hide file tree
Showing 42 changed files with 111 additions and 110 deletions.
11 changes: 3 additions & 8 deletions docs/architecture.md
Expand Up @@ -78,14 +78,9 @@ const meta = {
recommended: true,
defaultSetup: [DEFAULT_SEVERITY, DEFAULT_INDENTS],

schema: [
{
type: 'array',
items: [{ type: 'integer' }],
uniqueItems: true,
minItems: 2
}
]
schema: {
type: 'integer'
}
}

class IndentChecker {
Expand Down
2 changes: 1 addition & 1 deletion lib/config.js
Expand Up @@ -2,7 +2,7 @@ const _ = require('lodash')

module.exports = {
from(configVals) {
return _.assign(configVals, this)
return _.assign({ rules: {} }, configVals, this)
},

getNumberByPath(path, defaultValue) {
Expand Down
54 changes: 54 additions & 0 deletions lib/rules/base-checker.js
@@ -1,8 +1,62 @@
const chalk = require('chalk')
const Ajv = require('ajv')

const ruleConfigSchema = schema => {
const baseSchema = {
type: 'array',
items: [
{
enum: ['error', 'warn', 'off']
}
],
minItems: 1
}

if (schema) {
baseSchema.items.push(schema)
}

return baseSchema
}

class BaseChecker {
constructor(reporter, ruleId, meta) {
this.reporter = reporter
this.ruleId = ruleId
this.meta = meta

if (!reporter) {
return
}

// validate user's configuration of the rule
const userConfig = reporter.config[ruleId]
if (userConfig) {
if (Array.isArray(userConfig)) {
const ajv = new Ajv()
const schema = ruleConfigSchema(meta.schema)
const validate = ajv.compile(schema)
if (!validate(userConfig)) {
const messages = validate.errors
.map(e => e.message)
.filter(x => x) // filter out possible missing messages
.map(x => ` ${x}`) // indent
.join('\n')

console.warn(
chalk.yellow(
`[solhint] Warning: invalid configuration for rule '${ruleId}':\n${messages}`
)
)
}
} else if (!['off', 'warn', 'error'].includes(userConfig)) {
console.warn(
chalk.yellow(
`[solhint] Warning: rule '${ruleId}' level is '${userConfig}'; should be one of "error", "warn" or "off"`
)
)
}
}
}

error(ctx, message, fix) {
Expand Down
9 changes: 1 addition & 8 deletions lib/rules/best-practises/code-complexity.js
Expand Up @@ -40,14 +40,7 @@ const meta = {
recommended: false,
defaultSetup: [DEFAULT_SEVERITY, DEFAULT_COMPLEXITY],

schema: [
{
type: 'array',
items: [{ type: 'integer' }],
uniqueItems: true,
minItems: 2
}
]
schema: { type: 'integer' }
}

class CodeComplexityChecker extends BaseChecker {
Expand Down
12 changes: 4 additions & 8 deletions lib/rules/best-practises/function-max-lines.js
Expand Up @@ -26,14 +26,10 @@ const meta = {
recommended: false,
defaultSetup: [DEFAULT_SEVERITY, DEFAULT_MAX_LINES_COUNT],

schema: [
{
type: 'array',
items: [{ type: 'integer' }],
uniqueItems: true,
minItems: 2
}
]
schema: {
type: 'integer',
minimum: 1
}
}

class FunctionMaxLinesChecker extends BaseChecker {
Expand Down
9 changes: 1 addition & 8 deletions lib/rules/best-practises/max-line-length.js
Expand Up @@ -28,14 +28,7 @@ const meta = {
recommended: false,
defaultSetup: [DEFAULT_SEVERITY, DEFAULT_MAX_LINE_LENGTH],

schema: [
{
type: 'array',
items: [{ type: 'integer' }],
uniqueItems: true,
minItems: 2
}
]
schema: { type: 'integer', minimum: 1 }
}

class MaxLineLengthChecker extends BaseChecker {
Expand Down
9 changes: 1 addition & 8 deletions lib/rules/best-practises/max-states-count.js
Expand Up @@ -42,14 +42,7 @@ const meta = {
recommended: true,
defaultSetup: [DEFAULT_SEVERITY, DEFAULT_MAX_STATES_COUNT],

schema: [
{
type: 'array',
items: [{ type: 'integer' }],
uniqueItems: true,
minItems: 2
}
]
schema: { type: 'integer' }
}

class MaxStatesCountChecker extends BaseChecker {
Expand Down
2 changes: 1 addition & 1 deletion lib/rules/best-practises/no-empty-blocks.js
Expand Up @@ -14,7 +14,7 @@ const meta = {
recommended: true,
defaultSetup: 'warn',

schema: []
schema: null
}

class NoEmptyBlocksChecker extends BaseChecker {
Expand Down
2 changes: 1 addition & 1 deletion lib/rules/best-practises/no-unused-vars.js
Expand Up @@ -17,7 +17,7 @@ const meta = {
recommended: true,
defaultSetup: 'warn',

schema: []
schema: null
}

class NoUnusedVarsChecker extends BaseChecker {
Expand Down
2 changes: 1 addition & 1 deletion lib/rules/best-practises/payable-fallback.js
Expand Up @@ -28,7 +28,7 @@ const meta = {
recommended: true,
defaultSetup: 'warn',

schema: []
schema: null
}

class PayableFallbackChecker extends BaseChecker {
Expand Down
23 changes: 7 additions & 16 deletions lib/rules/best-practises/reason-string.js
Expand Up @@ -45,23 +45,14 @@ const meta = {
recommended: false,
defaultSetup: [DEFAULT_SEVERITY, DEFAULT_OPTION],

schema: [
{
type: 'array',
items: [
{
properties: {
maxLength: {
type: 'integer'
}
},
additionalProperties: false
}
],
uniqueItems: true,
minItems: 2
schema: {
type: 'object',
properties: {
maxLength: {
type: 'integer'
}
}
]
}
}

class ReasonStringChecker extends BaseChecker {
Expand Down
2 changes: 1 addition & 1 deletion lib/rules/deprecations/constructor-syntax.js
Expand Up @@ -13,7 +13,7 @@ const meta = {
recommended: false,
defaultSetup: 'warn',

schema: []
schema: null
}

class ConstructorSyntax extends BaseDeprecation {
Expand Down
2 changes: 1 addition & 1 deletion lib/rules/index.js
Expand Up @@ -11,7 +11,7 @@ const { validSeverityMap } = require('../config/config-validator')

module.exports = function checkers(reporter, configVals, inputSrc, tokens, fileName) {
const config = configObject.from(configVals)
const { rules = {} } = config
const { rules } = config
const meta = {
reporter,
config,
Expand Down
17 changes: 4 additions & 13 deletions lib/rules/miscellaneous/quotes.js
Expand Up @@ -41,19 +41,10 @@ const meta = {
recommended: true,
defaultSetup: [DEFAULT_SEVERITY, DEFAULT_QUOTES_TYPE],

schema: [
{
type: 'array',
items: [
{
type: 'string',
enum: QUOTE_TYPES
}
],
uniqueItems: true,
minItems: 2
}
]
schema: {
type: 'string',
enum: QUOTE_TYPES
}
}

class QuotesChecker extends BaseChecker {
Expand Down
2 changes: 1 addition & 1 deletion lib/rules/naming/const-name-snakecase.js
Expand Up @@ -14,7 +14,7 @@ const meta = {
recommended: true,
defaultSetup: 'warn',

schema: []
schema: null
}

class ConstNameSnakecaseChecker extends BaseChecker {
Expand Down
2 changes: 1 addition & 1 deletion lib/rules/naming/contract-name-camelcase.js
Expand Up @@ -14,7 +14,7 @@ const meta = {
recommended: true,
defaultSetup: 'warn',

schema: []
schema: null
}

class ContractNameCamelcaseChecker extends BaseChecker {
Expand Down
2 changes: 1 addition & 1 deletion lib/rules/naming/event-name-camelcase.js
Expand Up @@ -14,7 +14,7 @@ const meta = {
recommended: true,
defaultSetup: 'warn',

schema: []
schema: null
}

class EventNameCamelcaseChecker extends BaseChecker {
Expand Down
2 changes: 1 addition & 1 deletion lib/rules/naming/func-name-mixedcase.js
Expand Up @@ -14,7 +14,7 @@ const meta = {
recommended: true,
defaultSetup: 'warn',

schema: []
schema: null
}

class FuncNameMixedcaseChecker extends BaseChecker {
Expand Down
2 changes: 1 addition & 1 deletion lib/rules/naming/func-param-name-mixedcase.js
Expand Up @@ -14,7 +14,7 @@ const meta = {
recommended: false,
defaultSetup: 'warn',

schema: []
schema: null
}

class FunctionParamNameMixedcaseChecker extends BaseChecker {
Expand Down
2 changes: 1 addition & 1 deletion lib/rules/naming/modifier-name-mixedcase.js
Expand Up @@ -14,7 +14,7 @@ const meta = {
recommended: false,
defaultSetup: 'warn',

schema: []
schema: null
}

class ModifierNameMixedcase extends BaseChecker {
Expand Down
2 changes: 1 addition & 1 deletion lib/rules/naming/use-forbidden-name.js
Expand Up @@ -15,7 +15,7 @@ const meta = {
recommended: true,
defaultSetup: 'warn',

schema: []
schema: null
}

class UseForbiddenNameChecker extends BaseChecker {
Expand Down
2 changes: 1 addition & 1 deletion lib/rules/naming/var-name-mixedcase.js
Expand Up @@ -14,7 +14,7 @@ const meta = {
recommended: true,
defaultSetup: 'warn',

schema: []
schema: null
}

class VarNameMixedcaseChecker extends BaseChecker {
Expand Down
2 changes: 1 addition & 1 deletion lib/rules/order/func-order.js
Expand Up @@ -31,7 +31,7 @@ const meta = {
recommended: false,
defaultSetup: 'warn',

schema: []
schema: null
}

class FuncOrderChecker extends BaseChecker {
Expand Down
2 changes: 1 addition & 1 deletion lib/rules/order/imports-on-top.js
Expand Up @@ -13,7 +13,7 @@ const meta = {
recommended: true,
defaultSetup: 'warn',

schema: []
schema: null
}

class ImportsOnTopChecker extends BaseChecker {
Expand Down
2 changes: 1 addition & 1 deletion lib/rules/order/visibility-modifier-order.js
Expand Up @@ -27,7 +27,7 @@ const meta = {
recommended: true,
defaultSetup: 'warn',

schema: []
schema: null
}

class VisibilityModifierOrderChecker extends BaseChecker {
Expand Down
2 changes: 1 addition & 1 deletion lib/rules/security/avoid-call-value.js
Expand Up @@ -13,7 +13,7 @@ const meta = {
recommended: true,
defaultSetup: 'warn',

schema: []
schema: null
}

class AvoidCallValueChecker extends BaseChecker {
Expand Down
2 changes: 1 addition & 1 deletion lib/rules/security/avoid-low-level-calls.js
Expand Up @@ -22,7 +22,7 @@ const meta = {
recommended: true,
defaultSetup: 'warn',

schema: []
schema: null
}

class AvoidLowLevelCallsChecker extends BaseChecker {
Expand Down
2 changes: 1 addition & 1 deletion lib/rules/security/avoid-sha3.js
Expand Up @@ -14,7 +14,7 @@ const meta = {
defaultSetup: 'warn',
fixable: true,

schema: []
schema: null
}

class AvoidSha3Checker extends BaseChecker {
Expand Down
2 changes: 1 addition & 1 deletion lib/rules/security/avoid-suicide.js
Expand Up @@ -13,7 +13,7 @@ const meta = {
recommended: true,
defaultSetup: 'warn',

schema: []
schema: null
}

class AvoidSuicideChecker extends BaseChecker {
Expand Down

0 comments on commit c2c1fb3

Please sign in to comment.