From 8c3008dc4cf0e04832e867687f3bf8964911d8c3 Mon Sep 17 00:00:00 2001 From: Brandon Mills Date: Tue, 7 Apr 2015 17:35:07 -0400 Subject: [PATCH] Simplify schemas --- lib/rules/brace-style.js | 30 +++++++--------- lib/rules/camelcase.js | 24 ++++++------- lib/rules/comma-dangle.js | 14 +++----- lib/rules/comma-spacing.js | 29 +++++++-------- lib/rules/curly.js | 14 +++----- lib/rules/dot-notation.js | 29 +++++++-------- lib/rules/eqeqeq.js | 14 +++----- lib/rules/key-spacing.js | 36 +++++++++---------- lib/rules/max-len.js | 28 +++++++-------- lib/rules/new-cap.js | 51 +++++++++++++-------------- lib/rules/no-cond-assign.js | 14 +++----- lib/rules/no-inner-declarations.js | 14 +++----- lib/rules/no-mixed-spaces-and-tabs.js | 13 +++---- lib/rules/no-multi-spaces.js | 37 +++++++++---------- lib/rules/no-unused-vars.js | 39 ++++++++++---------- lib/rules/no-use-before-define.js | 13 +++---- lib/rules/quotes.js | 24 +++++-------- lib/rules/semi-spacing.js | 29 +++++++-------- lib/rules/semi.js | 13 +++---- lib/rules/space-infix-ops.js | 25 ++++++------- lib/rules/space-unary-ops.js | 29 +++++++-------- lib/rules/strict.js | 14 +++----- lib/rules/yoda.js | 30 +++++++--------- lib/validate-options.js | 8 +++-- 24 files changed, 246 insertions(+), 325 deletions(-) diff --git a/lib/rules/brace-style.js b/lib/rules/brace-style.js index 5d639eac53d..b78e74bb00d 100644 --- a/lib/rules/brace-style.js +++ b/lib/rules/brace-style.js @@ -203,21 +203,17 @@ module.exports = function(context) { }; -module.exports.schema = { - "items": [ - { - "type": "string", - "enum": ["1tbs", "stroustrup"] +module.exports.schema = [ + { + "enum": ["1tbs", "stroustrup"] + }, + { + "type": "object", + "properties": { + "allowSingleLine": { + "type": "boolean" + } }, - { - "type": "object", - "properties": { - "allowSingleLine": { - "type": "boolean" - } - }, - "additionalProperties": false - } - ], - "maxItems": 2 -}; + "additionalProperties": false + } +]; diff --git a/lib/rules/camelcase.js b/lib/rules/camelcase.js index 679575e739e..3091b815ca7 100644 --- a/lib/rules/camelcase.js +++ b/lib/rules/camelcase.js @@ -98,18 +98,14 @@ module.exports = function(context) { }; -module.exports.schema = { - "items": [ - { - "type": "object", +module.exports.schema = [ + { + "type": "object", + "properties": { "properties": { - "properties": { - "type": "string", - "enum": ["always", "never"] - } - }, - "additionalProperties": false - } - ], - "maxItems": 1 -}; + "enum": ["always", "never"] + } + }, + "additionalProperties": false + } +]; diff --git a/lib/rules/comma-dangle.js b/lib/rules/comma-dangle.js index 2d87772b5f4..0ab85817cb2 100644 --- a/lib/rules/comma-dangle.js +++ b/lib/rules/comma-dangle.js @@ -57,12 +57,8 @@ module.exports = function (context) { }; }; -module.exports.schema = { - "items": [ - { - "type": "string", - "enum": ["always", "always-multiline", "never"] - } - ], - "maxItems": 1 -}; +module.exports.schema = [ + { + "enum": ["always", "always-multiline", "never"] + } +]; diff --git a/lib/rules/comma-spacing.js b/lib/rules/comma-spacing.js index 1327174f47a..f082bf860c7 100644 --- a/lib/rules/comma-spacing.js +++ b/lib/rules/comma-spacing.js @@ -158,20 +158,17 @@ module.exports = function(context) { }; -module.exports.schema = { - "items": [ - { - "type": "object", - "properties": { - "before": { - "type": "boolean" - }, - "after": { - "type": "boolean" - } +module.exports.schema = [ + { + "type": "object", + "properties": { + "before": { + "type": "boolean" }, - "additionalProperties": false - } - ], - "maxItems": 1 -}; + "after": { + "type": "boolean" + } + }, + "additionalProperties": false + } +]; diff --git a/lib/rules/curly.js b/lib/rules/curly.js index 57ebf099b05..df7ad4c5703 100644 --- a/lib/rules/curly.js +++ b/lib/rules/curly.js @@ -102,12 +102,8 @@ module.exports = function(context) { }; -module.exports.schema = { - "items": [ - { - "type": "string", - "enum": ["all", "multi", "multi-line"] - } - ], - "maxItems": 1 -}; +module.exports.schema = [ + { + "enum": ["all", "multi", "multi-line"] + } +]; diff --git a/lib/rules/dot-notation.js b/lib/rules/dot-notation.js index 1be329abcbd..2198cd75470 100644 --- a/lib/rules/dot-notation.js +++ b/lib/rules/dot-notation.js @@ -103,20 +103,17 @@ module.exports = function(context) { }; }; -module.exports.schema = { - "items": [ - { - "type": "object", - "properties": { - "allowKeywords": { - "type": "boolean" - }, - "allowPattern": { - "type": "string" - } +module.exports.schema = [ + { + "type": "object", + "properties": { + "allowKeywords": { + "type": "boolean" }, - "additionalProperties": false - } - ], - "maxItems": 1 -}; + "allowPattern": { + "type": "string" + } + }, + "additionalProperties": false + } +]; diff --git a/lib/rules/eqeqeq.js b/lib/rules/eqeqeq.js index 110bae15f88..1576d6c1ab5 100644 --- a/lib/rules/eqeqeq.js +++ b/lib/rules/eqeqeq.js @@ -89,12 +89,8 @@ module.exports = function(context) { }; -module.exports.schema = { - "items": [ - { - "type": "string", - "enum": ["smart", "allow-null"] - } - ], - "maxItems": 1 -}; +module.exports.schema = [ + { + "enum": ["smart", "allow-null"] + } +]; diff --git a/lib/rules/key-spacing.js b/lib/rules/key-spacing.js index 3a06b487126..ff63a2464ba 100644 --- a/lib/rules/key-spacing.js +++ b/lib/rules/key-spacing.js @@ -306,24 +306,20 @@ module.exports = function(context) { }; -module.exports.schema = { - "items": [ - { - "type": "object", - "properties": { - "align": { - "type": "string", - "enum": ["colon", "value"] - }, - "beforeColon": { - "type": "boolean" - }, - "afterColon": { - "type": "boolean" - } +module.exports.schema = [ + { + "type": "object", + "properties": { + "align": { + "enum": ["colon", "value"] }, - "additionalProperties": false - } - ], - "maxItems": 1 -}; + "beforeColon": { + "type": "boolean" + }, + "afterColon": { + "type": "boolean" + } + }, + "additionalProperties": false + } +]; diff --git a/lib/rules/max-len.js b/lib/rules/max-len.js index d058d943bd8..b50f8539319 100644 --- a/lib/rules/max-len.js +++ b/lib/rules/max-len.js @@ -32,9 +32,9 @@ module.exports = function(context) { return result; } - var tabWidth = context.options[1]; + var tabWidth = context.options[1] || 4; // TODO: Add tests for default - var maxLength = context.options[0], + var maxLength = context.options[0] || 80, tabString = stringRepeat(" ", tabWidth); //-------------------------------------------------------------------------- @@ -64,17 +64,13 @@ module.exports = function(context) { }; -module.exports.schema = { - "items": [ - { - "type": "integer", - "minimum": 0 - }, - { - "type": "integer", - "minimum": 0 - } - ], - "minItems": 2, - "maxItems": 2 -}; +module.exports.schema = [ + { + "type": "integer", + "minimum": 0 + }, + { + "type": "integer", + "minimum": 0 + } +]; diff --git a/lib/rules/new-cap.js b/lib/rules/new-cap.js index 469a1d9ba79..3279d22979e 100644 --- a/lib/rules/new-cap.js +++ b/lib/rules/new-cap.js @@ -196,32 +196,29 @@ module.exports = function(context) { return listeners; }; -module.exports.schema = { - "items": [ - { - "type": "object", - "properties": { - "newIsCap": { - "type": "boolean" - }, - "capIsNew": { - "type": "boolean" - }, - "newIsCapExceptions": { - "type": "array", - "items": { - "type": "string" - } - }, - "capIsNewExceptions": { - "type": "array", - "items": { - "type": "string" - } +module.exports.schema = [ + { + "type": "object", + "properties": { + "newIsCap": { + "type": "boolean" + }, + "capIsNew": { + "type": "boolean" + }, + "newIsCapExceptions": { + "type": "array", + "items": { + "type": "string" } }, - "additionalProperties": false - } - ], - "maxItems": 1 -}; + "capIsNewExceptions": { + "type": "array", + "items": { + "type": "string" + } + } + }, + "additionalProperties": false + } +]; diff --git a/lib/rules/no-cond-assign.js b/lib/rules/no-cond-assign.js index cbd157b163d..2dc6251e39e 100644 --- a/lib/rules/no-cond-assign.js +++ b/lib/rules/no-cond-assign.js @@ -116,12 +116,8 @@ module.exports = function(context) { }; -module.exports.schema = { - "items": [ - { - "type": "string", - "enum": ["except-parens", "always"] - } - ], - "maxItems": 1 -}; +module.exports.schema = [ + { + "enum": ["except-parens", "always"] + } +]; diff --git a/lib/rules/no-inner-declarations.js b/lib/rules/no-inner-declarations.js index 2f39a896da4..f5503eea8e7 100644 --- a/lib/rules/no-inner-declarations.js +++ b/lib/rules/no-inner-declarations.js @@ -71,12 +71,8 @@ module.exports = function(context) { }; -module.exports.schema = { - "items": [ - { - "type": "string", - "enum": ["functions", "both"] - } - ], - "maxItems": 1 -}; +module.exports.schema = [ + { + "enum": ["functions", "both"] + } +]; diff --git a/lib/rules/no-mixed-spaces-and-tabs.js b/lib/rules/no-mixed-spaces-and-tabs.js index 4dabb9b1f2a..b6e29bacf21 100644 --- a/lib/rules/no-mixed-spaces-and-tabs.js +++ b/lib/rules/no-mixed-spaces-and-tabs.js @@ -67,11 +67,8 @@ module.exports = function(context) { }; -module.exports.schema = { - "items": [ - { - "enum": ["smart-tabs", true, false] - } - ], - "maxItems": 1 -}; +module.exports.schema = [ + { + "enum": ["smart-tabs", true, false] + } +]; diff --git a/lib/rules/no-multi-spaces.js b/lib/rules/no-multi-spaces.js index 6fea63768ae..f4d469859bd 100644 --- a/lib/rules/no-multi-spaces.js +++ b/lib/rules/no-multi-spaces.js @@ -100,23 +100,20 @@ module.exports = function(context) { }; -module.exports.schema = { - "items": [ - { - "type": "object", - "properties": { - "exceptions": { - "type": "object", - "patternProperties": { - "^([A-Z][a-z]*)+$": { - "type": "boolean" - } - }, - "additionalProperties": false - } - }, - "additionalProperties": false - } - ], - "maxItems": 1 -}; +module.exports.schema = [ + { + "type": "object", + "properties": { + "exceptions": { + "type": "object", + "patternProperties": { + "^([A-Z][a-z]*)+$": { + "type": "boolean" + } + }, + "additionalProperties": false + } + }, + "additionalProperties": false + } +]; diff --git a/lib/rules/no-unused-vars.js b/lib/rules/no-unused-vars.js index 512f1cbd1f6..a07de61e22e 100644 --- a/lib/rules/no-unused-vars.js +++ b/lib/rules/no-unused-vars.js @@ -179,26 +179,23 @@ module.exports = function(context) { }; -module.exports.schema = { - "items": [ - { - "oneOf": [ - { - "enum": ["all", "local"] - }, - { - "type": "object", - "properties": { - "vars": { - "enum": ["all", "local"] - }, - "args": { - "enum": ["all", "after-used", "none"] - } +module.exports.schema = [ + { + "oneOf": [ + { + "enum": ["all", "local"] + }, + { + "type": "object", + "properties": { + "vars": { + "enum": ["all", "local"] + }, + "args": { + "enum": ["all", "after-used", "none"] } } - ] - } - ], - "maxItems": 1 -}; + } + ] + } +]; diff --git a/lib/rules/no-use-before-define.js b/lib/rules/no-use-before-define.js index 4f1c2908f97..86231a66052 100644 --- a/lib/rules/no-use-before-define.js +++ b/lib/rules/no-use-before-define.js @@ -66,11 +66,8 @@ module.exports = function(context) { }; }; -module.exports.schema = { - "items": [ - { - "enum": ["nofunc"] - } - ], - "maxItems": 1 -}; +module.exports.schema = [ + { + "enum": ["nofunc"] + } +]; diff --git a/lib/rules/quotes.js b/lib/rules/quotes.js index b1b21c4630d..34ff2dcbb41 100644 --- a/lib/rules/quotes.js +++ b/lib/rules/quotes.js @@ -57,7 +57,7 @@ module.exports = function(context) { var val = node.value, rawVal = node.raw, quoteOption = context.options[0], - settings = QUOTE_SETTINGS[quoteOption], + settings = QUOTE_SETTINGS[quoteOption || "double"], // TODO: Add tests for default avoidEscape = context.options[1] === AVOID_ESCAPE, isValid; @@ -77,17 +77,11 @@ module.exports = function(context) { }; -module.exports.schema = { - "items": [ - { - "type": "string", - "enum": ["single", "double"] - }, - { - "type": "string", - "enum": ["avoid-escape"] - } - ], - "minItems": 1, - "maxItems": 2 -}; +module.exports.schema = [ + { + "enum": ["single", "double"] + }, + { + "enum": ["avoid-escape"] + } +]; diff --git a/lib/rules/semi-spacing.js b/lib/rules/semi-spacing.js index a923970367b..a8b4de790bc 100644 --- a/lib/rules/semi-spacing.js +++ b/lib/rules/semi-spacing.js @@ -151,20 +151,17 @@ module.exports = function (context) { }; }; -module.exports.schema = { - "items": [ - { - "type": "object", - "properties": { - "before": { - "type": "boolean" - }, - "after": { - "type": "boolean" - } +module.exports.schema = [ + { + "type": "object", + "properties": { + "before": { + "type": "boolean" }, - "additionalProperties": false - } - ], - "maxItems": 1 -}; + "after": { + "type": "boolean" + } + }, + "additionalProperties": false + } +]; diff --git a/lib/rules/semi.js b/lib/rules/semi.js index a9ac45f3167..7cd19632f55 100644 --- a/lib/rules/semi.js +++ b/lib/rules/semi.js @@ -129,11 +129,8 @@ module.exports = function(context) { }; -module.exports.schema = { - "items": [ - { - "enum": ["always", "never"] - } - ], - "maxItems": 1 -}; +module.exports.schema = [ + { + "enum": ["always", "never"] + } +]; diff --git a/lib/rules/space-infix-ops.js b/lib/rules/space-infix-ops.js index 94c6d68fae4..cd24274a4a9 100644 --- a/lib/rules/space-infix-ops.js +++ b/lib/rules/space-infix-ops.js @@ -67,17 +67,14 @@ module.exports = function(context) { }; -module.exports.schema = { - "items": [ - { - "type": "object", - "properties": { - "int32Hint": { - "type": "boolean" - } - }, - "additionalProperties": false - } - ], - "maxItems": 1 -}; +module.exports.schema = [ + { + "type": "object", + "properties": { + "int32Hint": { + "type": "boolean" + } + }, + "additionalProperties": false + } +]; diff --git a/lib/rules/space-unary-ops.js b/lib/rules/space-unary-ops.js index 616a9d28a2f..26980d3aa94 100644 --- a/lib/rules/space-unary-ops.js +++ b/lib/rules/space-unary-ops.js @@ -117,20 +117,17 @@ module.exports = function(context) { }; -module.exports.schema = { - "items": [ - { - "type": "object", - "properties": { - "words": { - "type": "boolean" - }, - "nonwords": { - "type": "boolean" - } +module.exports.schema = [ + { + "type": "object", + "properties": { + "words": { + "type": "boolean" }, - "additionalProperties": false - } - ], - "maxItems": 1 -}; + "nonwords": { + "type": "boolean" + } + }, + "additionalProperties": false + } +]; diff --git a/lib/rules/strict.js b/lib/rules/strict.js index 4ade7971615..a398d7dbecf 100644 --- a/lib/rules/strict.js +++ b/lib/rules/strict.js @@ -235,12 +235,8 @@ module.exports = function(context) { }; -module.exports.schema = { - "items": [ - { - "type": "string", - "enum": ["never", "global", "function"] - } - ], - "maxItems": 1 -}; +module.exports.schema = [ + { + "enum": ["never", "global", "function"] + } +]; diff --git a/lib/rules/yoda.js b/lib/rules/yoda.js index 7e2ddb0a6de..7221951212e 100644 --- a/lib/rules/yoda.js +++ b/lib/rules/yoda.js @@ -209,21 +209,17 @@ module.exports = function (context) { }; -module.exports.schema = { - "items": [ - { - "type": "string", - "enum": ["always", "never"] +module.exports.schema = [ + { + "enum": ["always", "never"] + }, + { + "type": "object", + "properties": { + "exceptRange": { + "type": "boolean" + } }, - { - "type": "object", - "properties": { - "exceptRange": { - "type": "boolean" - } - }, - "additionalProperties": false - } - ], - "maxItems": 2 -}; + "additionalProperties": false + } +]; diff --git a/lib/validate-options.js b/lib/validate-options.js index 3db74a2553e..049cae92e68 100644 --- a/lib/validate-options.js +++ b/lib/validate-options.js @@ -17,6 +17,8 @@ var validators = Object.create(null); // Cache generated schema validators * @returns {object} Full schema ready for validation. */ function makeSchema(options) { + var items; + if (!options) { return { "type": "array", @@ -29,15 +31,17 @@ function makeSchema(options) { }; } + items = options.items || options; + return { "type": "array", "items": [ { "enum": [0, 1, 2] } - ].concat(options.items), + ].concat(items), "minItems": (options.minItems || 0) + 1, - "maxItems": options.items.length + 1 + "maxItems": items.length + 1 }; }