Skip to content

Commit

Permalink
Update: use default keyword in JSON schema (fixes #9929) (#11288)
Browse files Browse the repository at this point in the history
  • Loading branch information
g-plane authored and aladdin-add committed Feb 13, 2019
1 parent 983c520 commit 550de1e
Show file tree
Hide file tree
Showing 97 changed files with 504 additions and 370 deletions.
3 changes: 3 additions & 0 deletions lib/cli-engine.js
Expand Up @@ -455,6 +455,9 @@ class CLIEngine {
if (this.options.rules && Object.keys(this.options.rules).length) {
const loadedRules = this.linter.getRules();

// Ajv validator with default schema will mutate original object, so we must clone it recursively.
this.options.rules = lodash.cloneDeep(this.options.rules);

Object.keys(this.options.rules).forEach(name => {
validator.validateRuleOptions(loadedRules.get(name), name, this.options.rules[name], "CLI");
});
Expand Down
6 changes: 4 additions & 2 deletions lib/rules/accessor-pairs.js
Expand Up @@ -85,10 +85,12 @@ module.exports = {
type: "object",
properties: {
getWithoutSet: {
type: "boolean"
type: "boolean",
default: false
},
setWithoutGet: {
type: "boolean"
type: "boolean",
default: true
}
},
additionalProperties: false
Expand Down
6 changes: 4 additions & 2 deletions lib/rules/array-bracket-newline.js
Expand Up @@ -34,11 +34,13 @@ module.exports = {
type: "object",
properties: {
multiline: {
type: "boolean"
type: "boolean",
default: true
},
minItems: {
type: ["integer", "null"],
minimum: 0
minimum: 0,
default: null
}
},
additionalProperties: false
Expand Down
3 changes: 2 additions & 1 deletion lib/rules/array-callback-return.js
Expand Up @@ -155,7 +155,8 @@ module.exports = {
type: "object",
properties: {
allowImplicit: {
type: "boolean"
type: "boolean",
default: false
}
},
additionalProperties: false
Expand Down
6 changes: 4 additions & 2 deletions lib/rules/array-element-newline.js
Expand Up @@ -34,11 +34,13 @@ module.exports = {
type: "object",
properties: {
multiline: {
type: "boolean"
type: "boolean",
default: false
},
minItems: {
type: ["integer", "null"],
minimum: 0
minimum: 0,
default: null
}
},
additionalProperties: false
Expand Down
2 changes: 1 addition & 1 deletion lib/rules/arrow-body-style.js
Expand Up @@ -46,7 +46,7 @@ module.exports = {
{
type: "object",
properties: {
requireReturnForObjectLiteral: { type: "boolean" }
requireReturnForObjectLiteral: { type: "boolean", default: false }
},
additionalProperties: false
}
Expand Down
3 changes: 2 additions & 1 deletion lib/rules/arrow-parens.js
Expand Up @@ -35,7 +35,8 @@ module.exports = {
type: "object",
properties: {
requireForBlockBody: {
type: "boolean"
type: "boolean",
default: false
}
},
additionalProperties: false
Expand Down
13 changes: 7 additions & 6 deletions lib/rules/arrow-spacing.js
Expand Up @@ -32,10 +32,12 @@ module.exports = {
type: "object",
properties: {
before: {
type: "boolean"
type: "boolean",
default: true
},
after: {
type: "boolean"
type: "boolean",
default: true
}
},
additionalProperties: false
Expand All @@ -54,11 +56,10 @@ module.exports = {
create(context) {

// merge rules with default
const rule = { before: true, after: true },
option = context.options[0] || {};
const rule = Object.assign({}, context.options[0]);

rule.before = option.before !== false;
rule.after = option.after !== false;
rule.before = rule.before !== false;
rule.after = rule.after !== false;

const sourceCode = context.getSourceCode();

Expand Down
3 changes: 2 additions & 1 deletion lib/rules/brace-style.js
Expand Up @@ -30,7 +30,8 @@ module.exports = {
type: "object",
properties: {
allowSingleLine: {
type: "boolean"
type: "boolean",
default: false
}
},
additionalProperties: false
Expand Down
5 changes: 3 additions & 2 deletions lib/rules/camelcase.js
Expand Up @@ -25,7 +25,8 @@ module.exports = {
type: "object",
properties: {
ignoreDestructuring: {
type: "boolean"
type: "boolean",
default: false
},
properties: {
enum: ["always", "never"]
Expand Down Expand Up @@ -54,7 +55,7 @@ module.exports = {

const options = context.options[0] || {};
let properties = options.properties || "";
const ignoreDestructuring = options.ignoreDestructuring || false;
const ignoreDestructuring = options.ignoreDestructuring;
const allow = options.allow || [];

if (properties !== "always" && properties !== "never") {
Expand Down
29 changes: 15 additions & 14 deletions lib/rules/capitalized-comments.js
Expand Up @@ -17,12 +17,7 @@ const astUtils = require("../util/ast-utils");

const DEFAULT_IGNORE_PATTERN = astUtils.COMMENTS_IGNORE_PATTERN,
WHITESPACE = /\s/g,
MAYBE_URL = /^\s*[^:/?#\s]+:\/\/[^?#]/, // TODO: Combine w/ max-len pattern?
DEFAULTS = {
ignorePattern: null,
ignoreInlineComments: false,
ignoreConsecutiveComments: false
};
MAYBE_URL = /^\s*[^:/?#\s]+:\/\/[^?#]/; // TODO: Combine w/ max-len pattern?

/*
* Base schema body for defining the basic capitalization rule, ignorePattern,
Expand All @@ -33,17 +28,27 @@ const SCHEMA_BODY = {
type: "object",
properties: {
ignorePattern: {
type: "string"
type: "string",
default: ""
},
ignoreInlineComments: {
type: "boolean"
type: "boolean",
default: false
},
ignoreConsecutiveComments: {
type: "boolean"
type: "boolean",
default: false
}
},
additionalProperties: false
};
const DEFAULTS = Object.keys(SCHEMA_BODY.properties).reduce(
(obj, current) => {
obj[current] = SCHEMA_BODY.properties[current].default;
return obj;
},
{}
);

/**
* Get normalized options for either block or line comments from the given
Expand All @@ -59,11 +64,7 @@ const SCHEMA_BODY = {
* @param {string} which Either "line" or "block".
* @returns {Object} The normalized options.
*/
function getNormalizedOptions(rawOptions, which) {
if (!rawOptions) {
return Object.assign({}, DEFAULTS);
}

function getNormalizedOptions(rawOptions = {}, which) {
return Object.assign({}, DEFAULTS, rawOptions[which] || rawOptions);
}

Expand Down
2 changes: 1 addition & 1 deletion lib/rules/class-methods-use-this.js
Expand Up @@ -38,7 +38,7 @@ module.exports = {
}
},
create(context) {
const config = context.options[0] ? Object.assign({}, context.options[0]) : {};
const config = Object.assign({}, context.options[0]);
const exceptMethods = new Set(config.exceptMethods || []);

const stack = [];
Expand Down
10 changes: 6 additions & 4 deletions lib/rules/comma-spacing.js
Expand Up @@ -28,10 +28,12 @@ module.exports = {
type: "object",
properties: {
before: {
type: "boolean"
type: "boolean",
default: false
},
after: {
type: "boolean"
type: "boolean",
default: true
}
},
additionalProperties: false
Expand All @@ -50,8 +52,8 @@ module.exports = {
const tokensAndComments = sourceCode.tokensAndComments;

const options = {
before: context.options[0] ? !!context.options[0].before : false,
after: context.options[0] ? !!context.options[0].after : true
before: context.options[0] ? context.options[0].before : false,
after: context.options[0] ? context.options[0].after : true
};

//--------------------------------------------------------------------------
Expand Down
16 changes: 7 additions & 9 deletions lib/rules/complexity.js
Expand Up @@ -41,11 +41,13 @@ module.exports = {
properties: {
maximum: {
type: "integer",
minimum: 0
minimum: 0,
default: 20
},
max: {
type: "integer",
minimum: 0
minimum: 0,
default: 20
}
},
additionalProperties: false
Expand All @@ -63,13 +65,9 @@ module.exports = {
const option = context.options[0];
let THRESHOLD = 20;

if (typeof option === "object" && Object.prototype.hasOwnProperty.call(option, "maximum") && typeof option.maximum === "number") {
THRESHOLD = option.maximum;
}
if (typeof option === "object" && Object.prototype.hasOwnProperty.call(option, "max") && typeof option.max === "number") {
THRESHOLD = option.max;
}
if (typeof option === "number") {
if (typeof option === "object") {
THRESHOLD = option.maximum || option.max;
} else if (typeof option === "number") {
THRESHOLD = option;
}

Expand Down
3 changes: 2 additions & 1 deletion lib/rules/consistent-return.js
Expand Up @@ -66,7 +66,8 @@ module.exports = {
type: "object",
properties: {
treatUndefinedAsUnspecified: {
type: "boolean"
type: "boolean",
default: false
}
},
additionalProperties: false
Expand Down
8 changes: 5 additions & 3 deletions lib/rules/dot-notation.js
Expand Up @@ -33,10 +33,12 @@ module.exports = {
type: "object",
properties: {
allowKeywords: {
type: "boolean"
type: "boolean",
default: true
},
allowPattern: {
type: "string"
type: "string",
default: ""
}
},
additionalProperties: false
Expand All @@ -53,7 +55,7 @@ module.exports = {

create(context) {
const options = context.options[0] || {};
const allowKeywords = options.allowKeywords === void 0 || !!options.allowKeywords;
const allowKeywords = options.allowKeywords === void 0 || options.allowKeywords;
const sourceCode = context.getSourceCode();

let allowPattern;
Expand Down
3 changes: 2 additions & 1 deletion lib/rules/eqeqeq.js
Expand Up @@ -38,7 +38,8 @@ module.exports = {
type: "object",
properties: {
null: {
enum: ["always", "never", "ignore"]
enum: ["always", "never", "ignore"],
default: "always"
}
},
additionalProperties: false
Expand Down
3 changes: 2 additions & 1 deletion lib/rules/func-call-spacing.js
Expand Up @@ -50,7 +50,8 @@ module.exports = {
type: "object",
properties: {
allowNewlines: {
type: "boolean"
type: "boolean",
default: false
}
},
additionalProperties: false
Expand Down
5 changes: 3 additions & 2 deletions lib/rules/func-style.js
Expand Up @@ -27,7 +27,8 @@ module.exports = {
type: "object",
properties: {
allowArrowFunctions: {
type: "boolean"
type: "boolean",
default: false
}
},
additionalProperties: false
Expand All @@ -43,7 +44,7 @@ module.exports = {
create(context) {

const style = context.options[0],
allowArrowFunctions = context.options[1] && context.options[1].allowArrowFunctions === true,
allowArrowFunctions = context.options[1] && context.options[1].allowArrowFunctions,
enforceDeclarations = (style === "declaration"),
stack = [];

Expand Down
3 changes: 2 additions & 1 deletion lib/rules/getter-return.js
Expand Up @@ -60,7 +60,8 @@ module.exports = {
type: "object",
properties: {
allowImplicit: {
type: "boolean"
type: "boolean",
default: false
}
},
additionalProperties: false
Expand Down
5 changes: 3 additions & 2 deletions lib/rules/id-length.js
Expand Up @@ -26,10 +26,11 @@ module.exports = {
type: "object",
properties: {
min: {
type: "number"
type: "integer",
default: 2
},
max: {
type: "number"
type: "integer"
},
exceptions: {
type: "array",
Expand Down
9 changes: 6 additions & 3 deletions lib/rules/id-match.js
Expand Up @@ -28,13 +28,16 @@ module.exports = {
type: "object",
properties: {
properties: {
type: "boolean"
type: "boolean",
default: false
},
onlyDeclarations: {
type: "boolean"
type: "boolean",
default: false
},
ignoreDestructuring: {
type: "boolean"
type: "boolean",
default: false
}
}
}
Expand Down

0 comments on commit 550de1e

Please sign in to comment.