From 19d282a61876885d3081cdf1c4d90bbc3a651000 Mon Sep 17 00:00:00 2001 From: "Peter (Somogyvari) Metz" Date: Wed, 16 Jan 2019 11:03:58 -0800 Subject: [PATCH 1/4] New: adds flag to skip loading rules from FS This is a backward compatible change of the public API. By default the Linter class' constructor instantiates a Rules instance which (in its own constructor) has the side-effect of reading from the file-system to obtain rule files that may have been put there by the user. The issue with this is side-effect is that it breaks WebPack bundles (at runtime they crash when the Linter gets instantiated). The particular use-case that spawned this PR is running ESLint inside a WebPack bundle deployed onto an AWS Lambda function. The Linter is configured inline so there is no need to load any rules from the file-system. --- docs/developer-guide/nodejs-api.md | 13 +++++++++++++ lib/linter.js | 4 ++-- lib/rules.js | 6 ++++-- tests/lib/linter.js | 10 ++++++++++ 4 files changed, 29 insertions(+), 4 deletions(-) diff --git a/docs/developer-guide/nodejs-api.md b/docs/developer-guide/nodejs-api.md index 7b9ecb6ecfb..49726b3b245 100644 --- a/docs/developer-guide/nodejs-api.md +++ b/docs/developer-guide/nodejs-api.md @@ -9,6 +9,7 @@ While ESLint is designed to be run on the command line, it's possible to use ESL * [SourceCode](#sourcecode) * [splitLines()](#sourcecodesplitlines) * [Linter](#linter) + * [constructor()](#linterconstructor) * [verify()](#linterverify) * [verifyAndFix()](#linterverifyandfix) * [defineRule()](#linterdefinerule) @@ -87,6 +88,18 @@ var Linter = require("eslint").Linter; var linter = new Linter(); ``` +### Linter#constructor + +* `config.skipRulesLoadFromFs` If you are using the ESLint NodeJS API in code that you are packaging with a module bundler such as WebPack, then you may experience crashes when instantiating the Linter class due to it's default behavior to attempt to read the rule files from the file-system from a directory that does not exist (because the ESLint code was moved out of context by the bundler software). In a scenario like the above one, you will need to configure your Linter instance to skip the loading of the rules which you can do so by setting the `config.skipRulesLoadFromFs` parameter to boolean `true`. This allows you to configure your Linter instance "manually" from code. Note that you can still configure your Linter instance this way if your execution environment does have the correct directories in place on the file-system. + +```js +const Linter = require("eslint").Linter; +const config = { + skipRulesLoadFromFs: true // causes the Linter to skip reading rules from the file-system. +}; +const linter = new Linter(config); +``` + ### Linter#verify The most important method on `Linter` is `verify()`, which initiates linting of the given text. This method accepts three arguments: diff --git a/lib/linter.js b/lib/linter.js index d6d635f9f47..f8435f12fa5 100644 --- a/lib/linter.js +++ b/lib/linter.js @@ -769,12 +769,12 @@ const loadedParserMaps = new WeakMap(); */ module.exports = class Linter { - constructor() { + constructor(config = { skipRulesLoadFromFs: false }) { lastSourceCodes.set(this, null); loadedParserMaps.set(this, new Map()); this.version = pkg.version; - this.rules = new Rules(); + this.rules = new Rules({ skipRulesLoadFromFs: config.skipRulesLoadFromFs }); this.environments = new Environments(); } diff --git a/lib/rules.js b/lib/rules.js index 040f9db5059..dede42b8fd8 100644 --- a/lib/rules.js +++ b/lib/rules.js @@ -57,10 +57,12 @@ function normalizeRule(rule) { //------------------------------------------------------------------------------ class Rules { - constructor() { + constructor(config = { skipRulesLoadFromFs: false }) { this._rules = Object.create(null); - this.load(); + if (!config.skipRulesLoadFromFs) { + this.load(); + } } /** diff --git a/tests/lib/linter.js b/tests/lib/linter.js index ebe59625b05..9a3ecd90025 100644 --- a/tests/lib/linter.js +++ b/tests/lib/linter.js @@ -2771,6 +2771,16 @@ describe("Linter", () => { }); }); + describe("skips loading of rules from file-system if flag is passed in to constructor config", () => { + it("should return no loaded rules", () => { + const config = { skipRulesLoadFromFs: true }; + const emptyLinter = new Linter(config); + const rules = emptyLinter.getRules(); + + assert.strictEqual(rules.size, 0); + }); + }); + describe("when calling getRules", () => { it("should return all loaded rules", () => { const rules = linter.getRules(); From 7b5a2de59edc3b4da765f558d50cd7d41f0005cf Mon Sep 17 00:00:00 2001 From: "Peter (Somogyvari) Metz" Date: Thu, 17 Jan 2019 08:25:40 -0800 Subject: [PATCH 2/4] Revert "New: adds flag to skip loading rules from FS" This reverts commit cbfab0af5cffd25dbbaaedf81745a4fc4e8521bd. --- docs/developer-guide/nodejs-api.md | 13 ------------- lib/linter.js | 4 ++-- lib/rules.js | 6 ++---- tests/lib/linter.js | 10 ---------- 4 files changed, 4 insertions(+), 29 deletions(-) diff --git a/docs/developer-guide/nodejs-api.md b/docs/developer-guide/nodejs-api.md index 49726b3b245..7b9ecb6ecfb 100644 --- a/docs/developer-guide/nodejs-api.md +++ b/docs/developer-guide/nodejs-api.md @@ -9,7 +9,6 @@ While ESLint is designed to be run on the command line, it's possible to use ESL * [SourceCode](#sourcecode) * [splitLines()](#sourcecodesplitlines) * [Linter](#linter) - * [constructor()](#linterconstructor) * [verify()](#linterverify) * [verifyAndFix()](#linterverifyandfix) * [defineRule()](#linterdefinerule) @@ -88,18 +87,6 @@ var Linter = require("eslint").Linter; var linter = new Linter(); ``` -### Linter#constructor - -* `config.skipRulesLoadFromFs` If you are using the ESLint NodeJS API in code that you are packaging with a module bundler such as WebPack, then you may experience crashes when instantiating the Linter class due to it's default behavior to attempt to read the rule files from the file-system from a directory that does not exist (because the ESLint code was moved out of context by the bundler software). In a scenario like the above one, you will need to configure your Linter instance to skip the loading of the rules which you can do so by setting the `config.skipRulesLoadFromFs` parameter to boolean `true`. This allows you to configure your Linter instance "manually" from code. Note that you can still configure your Linter instance this way if your execution environment does have the correct directories in place on the file-system. - -```js -const Linter = require("eslint").Linter; -const config = { - skipRulesLoadFromFs: true // causes the Linter to skip reading rules from the file-system. -}; -const linter = new Linter(config); -``` - ### Linter#verify The most important method on `Linter` is `verify()`, which initiates linting of the given text. This method accepts three arguments: diff --git a/lib/linter.js b/lib/linter.js index f8435f12fa5..d6d635f9f47 100644 --- a/lib/linter.js +++ b/lib/linter.js @@ -769,12 +769,12 @@ const loadedParserMaps = new WeakMap(); */ module.exports = class Linter { - constructor(config = { skipRulesLoadFromFs: false }) { + constructor() { lastSourceCodes.set(this, null); loadedParserMaps.set(this, new Map()); this.version = pkg.version; - this.rules = new Rules({ skipRulesLoadFromFs: config.skipRulesLoadFromFs }); + this.rules = new Rules(); this.environments = new Environments(); } diff --git a/lib/rules.js b/lib/rules.js index dede42b8fd8..040f9db5059 100644 --- a/lib/rules.js +++ b/lib/rules.js @@ -57,12 +57,10 @@ function normalizeRule(rule) { //------------------------------------------------------------------------------ class Rules { - constructor(config = { skipRulesLoadFromFs: false }) { + constructor() { this._rules = Object.create(null); - if (!config.skipRulesLoadFromFs) { - this.load(); - } + this.load(); } /** diff --git a/tests/lib/linter.js b/tests/lib/linter.js index 9a3ecd90025..ebe59625b05 100644 --- a/tests/lib/linter.js +++ b/tests/lib/linter.js @@ -2771,16 +2771,6 @@ describe("Linter", () => { }); }); - describe("skips loading of rules from file-system if flag is passed in to constructor config", () => { - it("should return no loaded rules", () => { - const config = { skipRulesLoadFromFs: true }; - const emptyLinter = new Linter(config); - const rules = emptyLinter.getRules(); - - assert.strictEqual(rules.size, 0); - }); - }); - describe("when calling getRules", () => { it("should return all loaded rules", () => { const rules = linter.getRules(); From 449be8ce6fcf84815c76763e4859d9066d509ce1 Mon Sep 17 00:00:00 2001 From: "Peter (Somogyvari) Metz" Date: Thu, 17 Jan 2019 09:08:51 -0800 Subject: [PATCH 3/4] Update: requires built-in rules at build time See #11278 for details. --- Makefile.js | 11 ++ lib/built-in-rules-index.js | 277 ++++++++++++++++++++++++++++++++++++ lib/rules.js | 13 +- 3 files changed, 299 insertions(+), 2 deletions(-) create mode 100644 lib/built-in-rules-index.js diff --git a/Makefile.js b/Makefile.js index eb2c586a731..e2e83bdf36c 100644 --- a/Makefile.js +++ b/Makefile.js @@ -938,6 +938,17 @@ target.checkRuleFiles = function() { errors++; } + // 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); + + if (ruleEntryFromIndexIsMissing) { + console.error(`Missing rule from index (${builtInRulesIndexPath}.js): ${basename}. If you just added a ` + + "new rule then add an entry for it in the previously mentioned file."); + errors++; + } + // check for tests if (!test("-f", `tests/lib/rules/${basename}.js`)) { console.error("Missing tests for rule %s", basename); diff --git a/lib/built-in-rules-index.js b/lib/built-in-rules-index.js new file mode 100644 index 00000000000..aaf2f06eccd --- /dev/null +++ b/lib/built-in-rules-index.js @@ -0,0 +1,277 @@ +/** + * @fileoverview Collects the built-in rules into a map structure so that they can be imported all at once and without + * using the file-system directly. + * @author Peter (Somogyvari) Metz + */ + +"use strict"; + +/* eslint sort-keys: ["error", "asc"] */ + +module.exports = { + "accessor-pairs": require("./rules/accessor-pairs"), + "array-bracket-newline": require("./rules/array-bracket-newline"), + "array-bracket-spacing": require("./rules/array-bracket-spacing"), + "array-callback-return": require("./rules/array-callback-return"), + "array-element-newline": require("./rules/array-element-newline"), + "arrow-body-style": require("./rules/arrow-body-style"), + "arrow-parens": require("./rules/arrow-parens"), + "arrow-spacing": require("./rules/arrow-spacing"), + "block-scoped-var": require("./rules/block-scoped-var"), + "block-spacing": require("./rules/block-spacing"), + "brace-style": require("./rules/brace-style"), + "callback-return": require("./rules/callback-return"), + camelcase: require("./rules/camelcase"), + "capitalized-comments": require("./rules/capitalized-comments"), + "class-methods-use-this": require("./rules/class-methods-use-this"), + "comma-dangle": require("./rules/comma-dangle"), + "comma-spacing": require("./rules/comma-spacing"), + "comma-style": require("./rules/comma-style"), + complexity: require("./rules/complexity"), + "computed-property-spacing": require("./rules/computed-property-spacing"), + "consistent-return": require("./rules/consistent-return"), + "consistent-this": require("./rules/consistent-this"), + "constructor-super": require("./rules/constructor-super"), + curly: require("./rules/curly"), + "default-case": require("./rules/default-case"), + "dot-location": require("./rules/dot-location"), + "dot-notation": require("./rules/dot-notation"), + "eol-last": require("./rules/eol-last"), + eqeqeq: require("./rules/eqeqeq"), + "for-direction": require("./rules/for-direction"), + "func-call-spacing": require("./rules/func-call-spacing"), + "func-name-matching": require("./rules/func-name-matching"), + "func-names": require("./rules/func-names"), + "func-style": require("./rules/func-style"), + "function-paren-newline": require("./rules/function-paren-newline"), + "generator-star-spacing": require("./rules/generator-star-spacing"), + "getter-return": require("./rules/getter-return"), + "global-require": require("./rules/global-require"), + "guard-for-in": require("./rules/guard-for-in"), + "handle-callback-err": require("./rules/handle-callback-err"), + "id-blacklist": require("./rules/id-blacklist"), + "id-length": require("./rules/id-length"), + "id-match": require("./rules/id-match"), + "implicit-arrow-linebreak": require("./rules/implicit-arrow-linebreak"), + indent: require("./rules/indent"), + "indent-legacy": require("./rules/indent-legacy"), + "init-declarations": require("./rules/init-declarations"), + "jsx-quotes": require("./rules/jsx-quotes"), + "key-spacing": require("./rules/key-spacing"), + "keyword-spacing": require("./rules/keyword-spacing"), + "line-comment-position": require("./rules/line-comment-position"), + "linebreak-style": require("./rules/linebreak-style"), + "lines-around-comment": require("./rules/lines-around-comment"), + "lines-around-directive": require("./rules/lines-around-directive"), + "lines-between-class-members": require("./rules/lines-between-class-members"), + "max-classes-per-file": require("./rules/max-classes-per-file"), + "max-depth": require("./rules/max-depth"), + "max-len": require("./rules/max-len"), + "max-lines": require("./rules/max-lines"), + "max-lines-per-function": require("./rules/max-lines-per-function"), + "max-nested-callbacks": require("./rules/max-nested-callbacks"), + "max-params": require("./rules/max-params"), + "max-statements": require("./rules/max-statements"), + "max-statements-per-line": require("./rules/max-statements-per-line"), + "multiline-comment-style": require("./rules/multiline-comment-style"), + "multiline-ternary": require("./rules/multiline-ternary"), + "new-cap": require("./rules/new-cap"), + "new-parens": require("./rules/new-parens"), + "newline-after-var": require("./rules/newline-after-var"), + "newline-before-return": require("./rules/newline-before-return"), + "newline-per-chained-call": require("./rules/newline-per-chained-call"), + "no-alert": require("./rules/no-alert"), + "no-array-constructor": require("./rules/no-array-constructor"), + "no-async-promise-executor": require("./rules/no-async-promise-executor"), + "no-await-in-loop": require("./rules/no-await-in-loop"), + "no-bitwise": require("./rules/no-bitwise"), + "no-buffer-constructor": require("./rules/no-buffer-constructor"), + "no-caller": require("./rules/no-caller"), + "no-case-declarations": require("./rules/no-case-declarations"), + "no-catch-shadow": require("./rules/no-catch-shadow"), + "no-class-assign": require("./rules/no-class-assign"), + "no-compare-neg-zero": require("./rules/no-compare-neg-zero"), + "no-cond-assign": require("./rules/no-cond-assign"), + "no-confusing-arrow": require("./rules/no-confusing-arrow"), + "no-console": require("./rules/no-console"), + "no-const-assign": require("./rules/no-const-assign"), + "no-constant-condition": require("./rules/no-constant-condition"), + "no-continue": require("./rules/no-continue"), + "no-control-regex": require("./rules/no-control-regex"), + "no-debugger": require("./rules/no-debugger"), + "no-delete-var": require("./rules/no-delete-var"), + "no-div-regex": require("./rules/no-div-regex"), + "no-dupe-args": require("./rules/no-dupe-args"), + "no-dupe-class-members": require("./rules/no-dupe-class-members"), + "no-dupe-keys": require("./rules/no-dupe-keys"), + "no-duplicate-case": require("./rules/no-duplicate-case"), + "no-duplicate-imports": require("./rules/no-duplicate-imports"), + "no-else-return": require("./rules/no-else-return"), + "no-empty": require("./rules/no-empty"), + "no-empty-character-class": require("./rules/no-empty-character-class"), + "no-empty-function": require("./rules/no-empty-function"), + "no-empty-pattern": require("./rules/no-empty-pattern"), + "no-eq-null": require("./rules/no-eq-null"), + "no-eval": require("./rules/no-eval"), + "no-ex-assign": require("./rules/no-ex-assign"), + "no-extend-native": require("./rules/no-extend-native"), + "no-extra-bind": require("./rules/no-extra-bind"), + "no-extra-boolean-cast": require("./rules/no-extra-boolean-cast"), + "no-extra-label": require("./rules/no-extra-label"), + "no-extra-parens": require("./rules/no-extra-parens"), + "no-extra-semi": require("./rules/no-extra-semi"), + "no-fallthrough": require("./rules/no-fallthrough"), + "no-floating-decimal": require("./rules/no-floating-decimal"), + "no-func-assign": require("./rules/no-func-assign"), + "no-global-assign": require("./rules/no-global-assign"), + "no-implicit-coercion": require("./rules/no-implicit-coercion"), + "no-implicit-globals": require("./rules/no-implicit-globals"), + "no-implied-eval": require("./rules/no-implied-eval"), + "no-inline-comments": require("./rules/no-inline-comments"), + "no-inner-declarations": require("./rules/no-inner-declarations"), + "no-invalid-regexp": require("./rules/no-invalid-regexp"), + "no-invalid-this": require("./rules/no-invalid-this"), + "no-irregular-whitespace": require("./rules/no-irregular-whitespace"), + "no-iterator": require("./rules/no-iterator"), + "no-label-var": require("./rules/no-label-var"), + "no-labels": require("./rules/no-labels"), + "no-lone-blocks": require("./rules/no-lone-blocks"), + "no-lonely-if": require("./rules/no-lonely-if"), + "no-loop-func": require("./rules/no-loop-func"), + "no-magic-numbers": require("./rules/no-magic-numbers"), + "no-misleading-character-class": require("./rules/no-misleading-character-class"), + "no-mixed-operators": require("./rules/no-mixed-operators"), + "no-mixed-requires": require("./rules/no-mixed-requires"), + "no-mixed-spaces-and-tabs": require("./rules/no-mixed-spaces-and-tabs"), + "no-multi-assign": require("./rules/no-multi-assign"), + "no-multi-spaces": require("./rules/no-multi-spaces"), + "no-multi-str": require("./rules/no-multi-str"), + "no-multiple-empty-lines": require("./rules/no-multiple-empty-lines"), + "no-native-reassign": require("./rules/no-native-reassign"), + "no-negated-condition": require("./rules/no-negated-condition"), + "no-negated-in-lhs": require("./rules/no-negated-in-lhs"), + "no-nested-ternary": require("./rules/no-nested-ternary"), + "no-new": require("./rules/no-new"), + "no-new-func": require("./rules/no-new-func"), + "no-new-object": require("./rules/no-new-object"), + "no-new-require": require("./rules/no-new-require"), + "no-new-symbol": require("./rules/no-new-symbol"), + "no-new-wrappers": require("./rules/no-new-wrappers"), + "no-obj-calls": require("./rules/no-obj-calls"), + "no-octal": require("./rules/no-octal"), + "no-octal-escape": require("./rules/no-octal-escape"), + "no-param-reassign": require("./rules/no-param-reassign"), + "no-path-concat": require("./rules/no-path-concat"), + "no-plusplus": require("./rules/no-plusplus"), + "no-process-env": require("./rules/no-process-env"), + "no-process-exit": require("./rules/no-process-exit"), + "no-proto": require("./rules/no-proto"), + "no-prototype-builtins": require("./rules/no-prototype-builtins"), + "no-redeclare": require("./rules/no-redeclare"), + "no-regex-spaces": require("./rules/no-regex-spaces"), + "no-restricted-globals": require("./rules/no-restricted-globals"), + "no-restricted-imports": require("./rules/no-restricted-imports"), + "no-restricted-modules": require("./rules/no-restricted-modules"), + "no-restricted-properties": require("./rules/no-restricted-properties"), + "no-restricted-syntax": require("./rules/no-restricted-syntax"), + "no-return-assign": require("./rules/no-return-assign"), + "no-return-await": require("./rules/no-return-await"), + "no-script-url": require("./rules/no-script-url"), + "no-self-assign": require("./rules/no-self-assign"), + "no-self-compare": require("./rules/no-self-compare"), + "no-sequences": require("./rules/no-sequences"), + "no-shadow": require("./rules/no-shadow"), + "no-shadow-restricted-names": require("./rules/no-shadow-restricted-names"), + "no-spaced-func": require("./rules/no-spaced-func"), + "no-sparse-arrays": require("./rules/no-sparse-arrays"), + "no-sync": require("./rules/no-sync"), + "no-tabs": require("./rules/no-tabs"), + "no-template-curly-in-string": require("./rules/no-template-curly-in-string"), + "no-ternary": require("./rules/no-ternary"), + "no-this-before-super": require("./rules/no-this-before-super"), + "no-throw-literal": require("./rules/no-throw-literal"), + "no-trailing-spaces": require("./rules/no-trailing-spaces"), + "no-undef": require("./rules/no-undef"), + "no-undef-init": require("./rules/no-undef-init"), + "no-undefined": require("./rules/no-undefined"), + "no-underscore-dangle": require("./rules/no-underscore-dangle"), + "no-unexpected-multiline": require("./rules/no-unexpected-multiline"), + "no-unmodified-loop-condition": require("./rules/no-unmodified-loop-condition"), + "no-unneeded-ternary": require("./rules/no-unneeded-ternary"), + "no-unreachable": require("./rules/no-unreachable"), + "no-unsafe-finally": require("./rules/no-unsafe-finally"), + "no-unsafe-negation": require("./rules/no-unsafe-negation"), + "no-unused-expressions": require("./rules/no-unused-expressions"), + "no-unused-labels": require("./rules/no-unused-labels"), + "no-unused-vars": require("./rules/no-unused-vars"), + "no-use-before-define": require("./rules/no-use-before-define"), + "no-useless-call": require("./rules/no-useless-call"), + "no-useless-catch": require("./rules/no-useless-catch"), + "no-useless-computed-key": require("./rules/no-useless-computed-key"), + "no-useless-concat": require("./rules/no-useless-concat"), + "no-useless-constructor": require("./rules/no-useless-constructor"), + "no-useless-escape": require("./rules/no-useless-escape"), + "no-useless-rename": require("./rules/no-useless-rename"), + "no-useless-return": require("./rules/no-useless-return"), + "no-var": require("./rules/no-var"), + "no-void": require("./rules/no-void"), + "no-warning-comments": require("./rules/no-warning-comments"), + "no-whitespace-before-property": require("./rules/no-whitespace-before-property"), + "no-with": require("./rules/no-with"), + "nonblock-statement-body-position": require("./rules/nonblock-statement-body-position"), + "object-curly-newline": require("./rules/object-curly-newline"), + "object-curly-spacing": require("./rules/object-curly-spacing"), + "object-property-newline": require("./rules/object-property-newline"), + "object-shorthand": require("./rules/object-shorthand"), + "one-var": require("./rules/one-var"), + "one-var-declaration-per-line": require("./rules/one-var-declaration-per-line"), + "operator-assignment": require("./rules/operator-assignment"), + "operator-linebreak": require("./rules/operator-linebreak"), + "padded-blocks": require("./rules/padded-blocks"), + "padding-line-between-statements": require("./rules/padding-line-between-statements"), + "prefer-arrow-callback": require("./rules/prefer-arrow-callback"), + "prefer-const": require("./rules/prefer-const"), + "prefer-destructuring": require("./rules/prefer-destructuring"), + "prefer-numeric-literals": require("./rules/prefer-numeric-literals"), + "prefer-object-spread": require("./rules/prefer-object-spread"), + "prefer-promise-reject-errors": require("./rules/prefer-promise-reject-errors"), + "prefer-reflect": require("./rules/prefer-reflect"), + "prefer-rest-params": require("./rules/prefer-rest-params"), + "prefer-spread": require("./rules/prefer-spread"), + "prefer-template": require("./rules/prefer-template"), + "quote-props": require("./rules/quote-props"), + quotes: require("./rules/quotes"), + radix: require("./rules/radix"), + "require-atomic-updates": require("./rules/require-atomic-updates"), + "require-await": require("./rules/require-await"), + "require-jsdoc": require("./rules/require-jsdoc"), + "require-unicode-regexp": require("./rules/require-unicode-regexp"), + "require-yield": require("./rules/require-yield"), + "rest-spread-spacing": require("./rules/rest-spread-spacing"), + semi: require("./rules/semi"), + "semi-spacing": require("./rules/semi-spacing"), + "semi-style": require("./rules/semi-style"), + "sort-imports": require("./rules/sort-imports"), + "sort-keys": require("./rules/sort-keys"), + "sort-vars": require("./rules/sort-vars"), + "space-before-blocks": require("./rules/space-before-blocks"), + "space-before-function-paren": require("./rules/space-before-function-paren"), + "space-in-parens": require("./rules/space-in-parens"), + "space-infix-ops": require("./rules/space-infix-ops"), + "space-unary-ops": require("./rules/space-unary-ops"), + "spaced-comment": require("./rules/spaced-comment"), + strict: require("./rules/strict"), + "switch-colon-spacing": require("./rules/switch-colon-spacing"), + "symbol-description": require("./rules/symbol-description"), + "template-curly-spacing": require("./rules/template-curly-spacing"), + "template-tag-spacing": require("./rules/template-tag-spacing"), + "unicode-bom": require("./rules/unicode-bom"), + "use-isnan": require("./rules/use-isnan"), + "valid-jsdoc": require("./rules/valid-jsdoc"), + "valid-typeof": require("./rules/valid-typeof"), + "vars-on-top": require("./rules/vars-on-top"), + "wrap-iife": require("./rules/wrap-iife"), + "wrap-regex": require("./rules/wrap-regex"), + "yield-star-spacing": require("./rules/yield-star-spacing"), + yoda: require("./rules/yoda") +}; diff --git a/lib/rules.js b/lib/rules.js index 040f9db5059..ee747311e79 100644 --- a/lib/rules.js +++ b/lib/rules.js @@ -12,6 +12,7 @@ const lodash = require("lodash"); const loadRules = require("./load-rules"); const ruleReplacements = require("../conf/replacements").rules; +const builtInRules = require("./built-in-rules-index"); //------------------------------------------------------------------------------ // Helpers @@ -59,8 +60,7 @@ function normalizeRule(rule) { class Rules { constructor() { this._rules = Object.create(null); - - this.load(); + this.defineAll(builtInRules); } /** @@ -82,6 +82,15 @@ class Rules { load(rulesDir, cwd) { const newRules = loadRules(rulesDir, cwd); + this.defineAll(newRules); + } + + /** + * Pulls a Map of new rules to the defined ones of this instance. + * @param {Object} newRules Expects to have an object here that maps the rule ID to the rule definition. + * @returns {void} + */ + defineAll(newRules) { Object.keys(newRules).forEach(ruleId => { this.define(ruleId, newRules[ruleId]); }); From 65de2972ad83290a0a5f36e3be91e7b59e93251a Mon Sep 17 00:00:00 2001 From: Kevin Partington Date: Fri, 18 Jan 2019 08:16:10 -0800 Subject: [PATCH 4/4] Update Makefile.js Co-Authored-By: petermetz --- Makefile.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Makefile.js b/Makefile.js index e2e83bdf36c..f38de4e3738 100644 --- a/Makefile.js +++ b/Makefile.js @@ -945,7 +945,7 @@ target.checkRuleFiles = function() { if (ruleEntryFromIndexIsMissing) { console.error(`Missing rule from index (${builtInRulesIndexPath}.js): ${basename}. If you just added a ` + - "new rule then add an entry for it in the previously mentioned file."); + "new rule then add an entry for it in this file."); errors++; }