diff --git a/.eslintrc.js b/.eslintrc.js index 6ab4510cb37..0ca92dab810 100644 --- a/.eslintrc.js +++ b/.eslintrc.js @@ -1,13 +1,44 @@ "use strict"; -const internalFiles = [ - "**/cli-engine/**/*", - "**/init/**/*", - "**/linter/**/*", - "**/rule-tester/**/*", - "**/rules/**/*", - "**/source-code/**/*" -]; +const path = require("path"); + +const INTERNAL_FILES = { + CLI_ENGINE_PATTERN: "lib/cli-engine/**/*", + INIT_PATTERN: "lib/init/**/*", + LINTER_PATTERN: "lib/linter/**/*", + RULE_TESTER_PATTERN: "lib/rule-tester/**/*", + RULES_PATTERN: "lib/rules/**/*", + SOURCE_CODE_PATTERN: "lib/source-code/**/*" +}; + +/** + * Resolve an absolute path or glob pattern. + * @param {string} pathOrPattern the path or glob pattern. + * @returns {string} The resolved path or glob pattern. + */ +function resolveAbsPath(pathOrPattern) { + return path.resolve(__dirname, pathOrPattern); +} + +/** + * Create an array of `no-restricted-require` entries for ESLint's core files. + * @param {string} [pattern] The glob pattern to create the entries for. + * @returns {Object[]} The array of `no-restricted-require` entries. + */ +function createInternalFilesPatterns(pattern = null) { + return Object.values(INTERNAL_FILES) + .filter(p => p !== pattern) + .map(p => ({ + name: [ + + // Disallow all children modules. + resolveAbsPath(p), + + // Allow the main `index.js` module. + `!${resolveAbsPath(p.replace(/\*\*\/\*$/u, "index.js"))}` + ] + })); +} module.exports = { root: true, @@ -84,106 +115,68 @@ module.exports = { { files: ["lib/*"], rules: { - "no-restricted-modules": ["error", { - patterns: [ - ...internalFiles - ] - }] + "node/no-restricted-require": ["error", [ + ...createInternalFilesPatterns() + ]] } }, { - files: ["lib/cli-engine/**/*"], + files: [INTERNAL_FILES.CLI_ENGINE_PATTERN], rules: { - "no-restricted-modules": ["error", { - patterns: [ - ...internalFiles, - "**/init" - ] - }] + "node/no-restricted-require": ["error", [ + ...createInternalFilesPatterns(INTERNAL_FILES.CLI_ENGINE_PATTERN) + ]] } }, { - files: ["lib/init/**/*"], + files: [INTERNAL_FILES.INIT_PATTERN], rules: { - "no-restricted-modules": ["error", { - patterns: [ - ...internalFiles, - "**/rule-tester" - ] - }] + "node/no-restricted-require": ["error", [ + ...createInternalFilesPatterns(INTERNAL_FILES.INIT_PATTERN) + ]] } }, { - files: ["lib/linter/**/*"], + files: [INTERNAL_FILES.LINTER_PATTERN], rules: { - "no-restricted-modules": ["error", { - patterns: [ - ...internalFiles, - "fs", - "**/cli-engine", - "**/init", - "**/rule-tester" - ] - }] + "node/no-restricted-require": ["error", [ + ...createInternalFilesPatterns(INTERNAL_FILES.LINTER_PATTERN), + "fs" + ]] } }, { - files: ["lib/rules/**/*"], + files: [INTERNAL_FILES.RULES_PATTERN], rules: { - "no-restricted-modules": ["error", { - patterns: [ - ...internalFiles, - "fs", - "**/cli-engine", - "**/init", - "**/linter", - "**/rule-tester", - "**/source-code" - ] - }] + "node/no-restricted-require": ["error", [ + ...createInternalFilesPatterns(INTERNAL_FILES.RULES_PATTERN), + "fs" + ]] } }, { files: ["lib/shared/**/*"], rules: { - "no-restricted-modules": ["error", { - patterns: [ - ...internalFiles, - "**/cli-engine", - "**/init", - "**/linter", - "**/rule-tester", - "**/source-code" - ] - }] + "node/no-restricted-require": ["error", [ + ...createInternalFilesPatterns() + ]] } }, { - files: ["lib/source-code/**/*"], + files: [INTERNAL_FILES.SOURCE_CODE_PATTERN], rules: { - "no-restricted-modules": ["error", { - patterns: [ - ...internalFiles, - "fs", - "**/cli-engine", - "**/init", - "**/linter", - "**/rule-tester", - "**/rules" - ] - }] + "node/no-restricted-require": ["error", [ + ...createInternalFilesPatterns(INTERNAL_FILES.SOURCE_CODE_PATTERN), + "fs" + ]] } }, { - files: ["lib/rule-tester/**/*"], + files: [INTERNAL_FILES.RULE_TESTER_PATTERN], rules: { - "no-restricted-modules": ["error", { - patterns: [ - ...internalFiles, - "**/cli-engine", - "**/init" - ] - }] + "node/no-restricted-require": ["error", [ + ...createInternalFilesPatterns(INTERNAL_FILES.RULE_TESTER_PATTERN) + ]] } } ] diff --git a/lib/init/source-code-utils.js b/lib/init/source-code-utils.js index c95f64f69d3..dca6541d1ed 100644 --- a/lib/init/source-code-utils.js +++ b/lib/init/source-code-utils.js @@ -23,7 +23,7 @@ const { CLIEngine } = require("../cli-engine"); * TODO1: Expose the API that enumerates target files. * TODO2: Extract the creation logic of `SourceCode` from `Linter` class. */ -const { getCLIEngineInternalSlots } = require("../cli-engine/cli-engine"); // eslint-disable-line no-restricted-modules +const { getCLIEngineInternalSlots } = require("../cli-engine/cli-engine"); // eslint-disable-line node/no-restricted-require const debug = require("debug")("eslint:source-code-utils");