Skip to content

Commit

Permalink
Breaking: fix bugs in config loading
Browse files Browse the repository at this point in the history
  • Loading branch information
mysticatea committed Apr 5, 2019
1 parent 4e7cdca commit 2fc4464
Show file tree
Hide file tree
Showing 117 changed files with 10,201 additions and 7,405 deletions.
4 changes: 2 additions & 2 deletions Makefile.js
Expand Up @@ -26,7 +26,7 @@ const lodash = require("lodash"),
ejs = require("ejs"),
loadPerf = require("load-perf"),
yaml = require("js-yaml"),
CLIEngine = require("./lib/cli-engine");
{ CLIEngine } = require("./lib/cli-engine");

const { cat, cd, cp, echo, exec, exit, find, ls, mkdir, pwd, rm, test } = require("shelljs");

Expand Down Expand Up @@ -871,7 +871,7 @@ target.checkRuleFiles = function() {
// check parity between rules index file and rules directory
const builtInRulesIndexPath = "./lib/built-in-rules-index";
const ruleIdsInIndex = require(builtInRulesIndexPath);
const ruleEntryFromIndexIsMissing = !(basename in ruleIdsInIndex);
const ruleEntryFromIndexIsMissing = !ruleIdsInIndex.has(basename);

if (ruleEntryFromIndexIsMissing) {
console.error(`Missing rule from index (${builtInRulesIndexPath}.js): ${basename}. If you just added a ` +
Expand Down
46 changes: 46 additions & 0 deletions conf/config-schema.js
Expand Up @@ -5,6 +5,52 @@

"use strict";

// Define types for VSCode IntelliSense.
/** @typedef {boolean|"off"|"readable"|"readonly"|"writable"|"writeable"} GlobalConf */
/** @typedef {0|1|2|"off"|"warn"|"error"} SeverityConf */
/** @typedef {SeverityConf|[SeverityConf, ...any[]]} RuleConf */

/**
* @typedef {Object} EcmaFeatures
* @property {boolean} [globalReturn] Enabling `return` statements at the top-level.
* @property {boolean} [jsx] Enabling JSX syntax.
* @property {boolean} [impliedStrict] Enabling strict mode always.
*/

/**
* @typedef {Object} ParserOptions
* @property {EcmaFeatures} [ecmaFeatures] The optional features.
* @property {3|5|6|7|8|9|10|2015|2016|2017|2018|2019} [ecmaVersion] The ECMAScript version (or revision number).
* @property {"script"|"module"} [sourceType] The source code type.
*/

/**
* @typedef {Object} ConfigData
* @property {Record<string, boolean>} [env] The environment settings.
* @property {string|string[]} [extends] The path to other config files or the package name of shareable configs.
* @property {Record<string, GlobalConf>} [globals] The global variable settings.
* @property {OverrideConfigData[]} [overrides] The override settings per kind of files.
* @property {string} [parser] The path to a parser or the package name of a parser.
* @property {ParserOptions} [parserOptions] The parser options.
* @property {string[]} [plugins] The plugin specifiers.
* @property {boolean} [root] The root flag.
* @property {Record<string, RuleConf>} [rules] The rule settings.
* @property {Object} [settings] The shared settings.
*/

/**
* @typedef {Object} OverrideConfigData
* @property {Record<string, boolean>} [env] The environment settings.
* @property {string|string[]} [excludedFiles] The glob pattarns for excluded files.
* @property {string|string[]} files The glob pattarns for target files.
* @property {Record<string, GlobalConf>} [globals] The global variable settings.
* @property {string} [parser] The path to a parser or the package name of a parser.
* @property {ParserOptions} [parserOptions] The parser options.
* @property {string[]} [plugins] The plugin specifiers.
* @property {Record<string, RuleConf>} [rules] The rule settings.
* @property {Object} [settings] The shared settings.
*/

const baseConfigProperties = {
env: { type: "object" },
globals: { type: "object" },
Expand Down
11 changes: 9 additions & 2 deletions conf/environments.js
Expand Up @@ -14,7 +14,14 @@ const globals = require("globals");
// Public Interface
//------------------------------------------------------------------------------

module.exports = {
/**
* @typedef {Object} Environment
* @property {Record<string, GlobalConf>} [globals] The definition of global variables.
* @property {Object} [parserOptions] The parser options that will be enabled under this environment.
*/

/** @type {Map<string, Environment>} */
module.exports = new Map(Object.entries({
builtin: {
globals: globals.es5
},
Expand Down Expand Up @@ -106,4 +113,4 @@ module.exports = {
greasemonkey: {
globals: globals.greasemonkey
}
};
}));
14 changes: 8 additions & 6 deletions conf/eslint-all.js
Expand Up @@ -15,15 +15,17 @@ const builtInRules = require("../lib/built-in-rules-index");
// Helpers
//------------------------------------------------------------------------------

const enabledRules = Object.keys(builtInRules).reduce((result, ruleId) => {
if (!builtInRules[ruleId].meta.deprecated) {
result[ruleId] = "error";
const allRules = {};

for (const [ruleId, rule] of builtInRules) {
if (!rule.meta.deprecated) {
allRules[ruleId] = "error";
}
return result;
}, {});
}

//------------------------------------------------------------------------------
// Public Interface
//------------------------------------------------------------------------------

module.exports = { rules: enabledRules };
/** @type {import("./config-schema").ConfigData} */
module.exports = { rules: allRules };
17 changes: 9 additions & 8 deletions conf/eslint-recommended.js
Expand Up @@ -7,12 +7,13 @@
"use strict";

const builtInRules = require("../lib/built-in-rules-index");
const recommendedRules = {};

module.exports = {
rules: Object.assign(
{},
...Object.keys(builtInRules)
.filter(ruleId => builtInRules[ruleId].meta.docs.recommended)
.map(ruleId => ({ [ruleId]: "error" }))
)
};
for (const [ruleId, rule] of builtInRules) {
if (rule.meta.docs.recommended) {
recommendedRules[ruleId] = "error";
}
}

/** @type {import("./config-schema").ConfigData} */
module.exports = { rules: recommendedRules };
4 changes: 2 additions & 2 deletions lib/api.js
Expand Up @@ -5,11 +5,11 @@

"use strict";

const Linter = require("./linter");
const { Linter } = require("./linter");

module.exports = {
Linter,
CLIEngine: require("./cli-engine"),
CLIEngine: require("./cli-engine").CLIEngine,
RuleTester: require("./testers/rule-tester"),
SourceCode: require("./util/source-code")
};
Expand Down
30 changes: 28 additions & 2 deletions lib/built-in-rules-index.js
Expand Up @@ -8,7 +8,33 @@

/* eslint sort-keys: ["error", "asc"] */

module.exports = {
/**
* @typedef {Object} RuleMetaDocs
* @property {string} category The category of the rule.
* @property {string} description The description of the rule.
* @property {boolean} recommended If `true` then the rule is included in `eslint:recommended` preset.
* @property {string} url The URL of the rule documentation.
*/

/**
* @typedef {Object} RuleMeta
* @property {boolean} [deprecated] If `true` then the rule has been deprecated.
* @property {RuleMetaDocs} docs The document information of the rule.
* @property {"code"|"whitespace"} [fixable] The autofix type.
* @property {Record<string,string>} [messages] The messages the rule reports.
* @property {string[]} [replacedBy] The IDs of the alternative rules.
* @property {Array|Object} schema The option schema of the rule.
* @property {"problem"|"suggestion"|"layout"} type The rule type.
*/

/**
* @typedef {Object} Rule
* @property {Function} create The factory of the rule.
* @property {RuleMeta} meta The meta data of the rule.
*/

/** @type {Map<string, Rule>} */
module.exports = new Map(Object.entries({
"accessor-pairs": require("./rules/accessor-pairs"),
"array-bracket-newline": require("./rules/array-bracket-newline"),
"array-bracket-spacing": require("./rules/array-bracket-spacing"),
Expand Down Expand Up @@ -275,4 +301,4 @@ module.exports = {
"wrap-regex": require("./rules/wrap-regex"),
"yield-star-spacing": require("./rules/yield-star-spacing"),
yoda: require("./rules/yoda")
};
}));

0 comments on commit 2fc4464

Please sign in to comment.