2 * @fileoverview Main API Class
4 * @author Toru Nagashima
9 //------------------------------------------------------------------------------
11 //------------------------------------------------------------------------------
13 const path
= require("path");
14 const fs
= require("fs");
15 const { promisify
} = require("util");
16 const { CLIEngine
, getCLIEngineInternalSlots
} = require("../cli-engine/cli-engine");
17 const BuiltinRules
= require("../rules");
24 } = require("@eslint/eslintrc");
25 const { version
} = require("../../package.json");
27 //------------------------------------------------------------------------------
29 //------------------------------------------------------------------------------
31 /** @typedef {import("../cli-engine/cli-engine").LintReport} CLIEngineLintReport */
32 /** @typedef {import("../shared/types").DeprecatedRuleInfo} DeprecatedRuleInfo */
33 /** @typedef {import("../shared/types").ConfigData} ConfigData */
34 /** @typedef {import("../shared/types").LintMessage} LintMessage */
35 /** @typedef {import("../shared/types").SuppressedLintMessage} SuppressedLintMessage */
36 /** @typedef {import("../shared/types").Plugin} Plugin */
37 /** @typedef {import("../shared/types").Rule} Rule */
38 /** @typedef {import("../shared/types").LintResult} LintResult */
41 * The main formatter object.
42 * @typedef LoadedFormatter
43 * @property {function(LintResult[]): string | Promise<string>} format format function.
47 * The options with which to configure the ESLint instance.
48 * @typedef {Object} ESLintOptions
49 * @property {boolean} [allowInlineConfig] Enable or disable inline configuration comments.
50 * @property {ConfigData} [baseConfig] Base config object, extended by all configs used with this instance
51 * @property {boolean} [cache] Enable result caching.
52 * @property {string} [cacheLocation] The cache file to use instead of .eslintcache.
53 * @property {"metadata" | "content"} [cacheStrategy] The strategy used to detect changed files.
54 * @property {string} [cwd] The value to use for the current working directory.
55 * @property {boolean} [errorOnUnmatchedPattern] If `false` then `ESLint#lintFiles()` doesn't throw even if no target files found. Defaults to `true`.
56 * @property {string[]} [extensions] An array of file extensions to check.
57 * @property {boolean|Function} [fix] Execute in autofix mode. If a function, should return a boolean.
58 * @property {string[]} [fixTypes] Array of rule types to apply fixes for.
59 * @property {boolean} [globInputPaths] Set to false to skip glob resolution of input file paths to lint (default: true). If false, each input file paths is assumed to be a non-glob path to an existing file.
60 * @property {boolean} [ignore] False disables use of .eslintignore.
61 * @property {string} [ignorePath] The ignore file to use instead of .eslintignore.
62 * @property {ConfigData} [overrideConfig] Override config object, overrides all configs used with this instance
63 * @property {string} [overrideConfigFile] The configuration file to use.
64 * @property {Record<string,Plugin>|null} [plugins] Preloaded plugins. This is a map-like object, keys are plugin IDs and each value is implementation.
65 * @property {"error" | "warn" | "off"} [reportUnusedDisableDirectives] the severity to report unused eslint-disable directives.
66 * @property {string} [resolvePluginsRelativeTo] The folder where plugins should be resolved from, defaulting to the CWD.
67 * @property {string[]} [rulePaths] An array of directories to load custom rules from.
68 * @property {boolean} [useEslintrc] False disables looking for .eslintrc.* files.
72 * A rules metadata object.
73 * @typedef {Object} RulesMeta
74 * @property {string} id The plugin ID.
75 * @property {Object} definition The plugin definition.
79 * Private members for the `ESLint` instance.
80 * @typedef {Object} ESLintPrivateMembers
81 * @property {CLIEngine} cliEngine The wrapped CLIEngine instance.
82 * @property {ESLintOptions} options The options used to instantiate the ESLint instance.
85 //------------------------------------------------------------------------------
87 //------------------------------------------------------------------------------
89 const writeFile
= promisify(fs
.writeFile
);
92 * The map with which to store private class members.
93 * @type {WeakMap<ESLint, ESLintPrivateMembers>}
95 const privateMembersMap
= new WeakMap();
98 * Check if a given value is a non-empty string or not.
99 * @param {any} x The value to check.
100 * @returns {boolean} `true` if `x` is a non-empty string.
102 function isNonEmptyString(x
) {
103 return typeof x
=== "string" && x
.trim() !== "";
107 * Check if a given value is an array of non-empty strings or not.
108 * @param {any} x The value to check.
109 * @returns {boolean} `true` if `x` is an array of non-empty strings.
111 function isArrayOfNonEmptyString(x
) {
112 return Array
.isArray(x
) && x
.every(isNonEmptyString
);
116 * Check if a given value is a valid fix type or not.
117 * @param {any} x The value to check.
118 * @returns {boolean} `true` if `x` is valid fix type.
120 function isFixType(x
) {
121 return x
=== "directive" || x
=== "problem" || x
=== "suggestion" || x
=== "layout";
125 * Check if a given value is an array of fix types or not.
126 * @param {any} x The value to check.
127 * @returns {boolean} `true` if `x` is an array of fix types.
129 function isFixTypeArray(x
) {
130 return Array
.isArray(x
) && x
.every(isFixType
);
134 * The error for invalid options.
136 class ESLintInvalidOptionsError
extends Error
{
137 constructor(messages
) {
138 super(`Invalid Options:\n- ${messages.join("\n- ")}`);
139 this.code
= "ESLINT_INVALID_OPTIONS";
140 Error
.captureStackTrace(this, ESLintInvalidOptionsError
);
145 * Validates and normalizes options for the wrapped CLIEngine instance.
146 * @param {ESLintOptions} options The options to process.
147 * @throws {ESLintInvalidOptionsError} If of any of a variety of type errors.
148 * @returns {ESLintOptions} The normalized options.
150 function processOptions({
151 allowInlineConfig
= true, // ← we cannot use `overrideConfig.noInlineConfig` instead because `allowInlineConfig` has side-effect that suppress warnings that show inline configs are ignored.
154 cacheLocation
= ".eslintcache",
155 cacheStrategy
= "metadata",
157 errorOnUnmatchedPattern
= true,
158 extensions
= null, // ← should be null by default because if it's an array then it suppresses RFC20 feature.
160 fixTypes
= null, // ← should be null by default because if it's an array then it suppresses rules that don't have the `meta.type` property.
161 globInputPaths
= true,
163 ignorePath
= null, // ← should be null by default because if it's a string then it may throw ENOENT.
164 overrideConfig
= null,
165 overrideConfigFile
= null,
167 reportUnusedDisableDirectives
= null, // ← should be null by default because if it's a string then it overrides the 'reportUnusedDisableDirectives' setting in config files. And we cannot use `overrideConfig.reportUnusedDisableDirectives` instead because we cannot configure the `error` severity with that.
168 resolvePluginsRelativeTo
= null, // ← should be null by default because if it's a string then it suppresses RFC47 feature.
174 const unknownOptionKeys
= Object
.keys(unknownOptions
);
176 if (unknownOptionKeys
.length
>= 1) {
177 errors
.push(`Unknown options: ${unknownOptionKeys.join(", ")}`);
178 if (unknownOptionKeys
.includes("cacheFile")) {
179 errors
.push("'cacheFile' has been removed. Please use the 'cacheLocation' option instead.");
181 if (unknownOptionKeys
.includes("configFile")) {
182 errors
.push("'configFile' has been removed. Please use the 'overrideConfigFile' option instead.");
184 if (unknownOptionKeys
.includes("envs")) {
185 errors
.push("'envs' has been removed. Please use the 'overrideConfig.env' option instead.");
187 if (unknownOptionKeys
.includes("globals")) {
188 errors
.push("'globals' has been removed. Please use the 'overrideConfig.globals' option instead.");
190 if (unknownOptionKeys
.includes("ignorePattern")) {
191 errors
.push("'ignorePattern' has been removed. Please use the 'overrideConfig.ignorePatterns' option instead.");
193 if (unknownOptionKeys
.includes("parser")) {
194 errors
.push("'parser' has been removed. Please use the 'overrideConfig.parser' option instead.");
196 if (unknownOptionKeys
.includes("parserOptions")) {
197 errors
.push("'parserOptions' has been removed. Please use the 'overrideConfig.parserOptions' option instead.");
199 if (unknownOptionKeys
.includes("rules")) {
200 errors
.push("'rules' has been removed. Please use the 'overrideConfig.rules' option instead.");
203 if (typeof allowInlineConfig
!== "boolean") {
204 errors
.push("'allowInlineConfig' must be a boolean.");
206 if (typeof baseConfig
!== "object") {
207 errors
.push("'baseConfig' must be an object or null.");
209 if (typeof cache
!== "boolean") {
210 errors
.push("'cache' must be a boolean.");
212 if (!isNonEmptyString(cacheLocation
)) {
213 errors
.push("'cacheLocation' must be a non-empty string.");
216 cacheStrategy
!== "metadata" &&
217 cacheStrategy
!== "content"
219 errors
.push("'cacheStrategy' must be any of \"metadata\", \"content\".");
221 if (!isNonEmptyString(cwd
) || !path
.isAbsolute(cwd
)) {
222 errors
.push("'cwd' must be an absolute path.");
224 if (typeof errorOnUnmatchedPattern
!== "boolean") {
225 errors
.push("'errorOnUnmatchedPattern' must be a boolean.");
227 if (!isArrayOfNonEmptyString(extensions
) && extensions
!== null) {
228 errors
.push("'extensions' must be an array of non-empty strings or null.");
230 if (typeof fix
!== "boolean" && typeof fix
!== "function") {
231 errors
.push("'fix' must be a boolean or a function.");
233 if (fixTypes
!== null && !isFixTypeArray(fixTypes
)) {
234 errors
.push("'fixTypes' must be an array of any of \"directive\", \"problem\", \"suggestion\", and \"layout\".");
236 if (typeof globInputPaths
!== "boolean") {
237 errors
.push("'globInputPaths' must be a boolean.");
239 if (typeof ignore
!== "boolean") {
240 errors
.push("'ignore' must be a boolean.");
242 if (!isNonEmptyString(ignorePath
) && ignorePath
!== null) {
243 errors
.push("'ignorePath' must be a non-empty string or null.");
245 if (typeof overrideConfig
!== "object") {
246 errors
.push("'overrideConfig' must be an object or null.");
248 if (!isNonEmptyString(overrideConfigFile
) && overrideConfigFile
!== null) {
249 errors
.push("'overrideConfigFile' must be a non-empty string or null.");
251 if (typeof plugins
!== "object") {
252 errors
.push("'plugins' must be an object or null.");
253 } else if (plugins
!== null && Object
.keys(plugins
).includes("")) {
254 errors
.push("'plugins' must not include an empty string.");
256 if (Array
.isArray(plugins
)) {
257 errors
.push("'plugins' doesn't add plugins to configuration to load. Please use the 'overrideConfig.plugins' option instead.");
260 reportUnusedDisableDirectives
!== "error" &&
261 reportUnusedDisableDirectives
!== "warn" &&
262 reportUnusedDisableDirectives
!== "off" &&
263 reportUnusedDisableDirectives
!== null
265 errors
.push("'reportUnusedDisableDirectives' must be any of \"error\", \"warn\", \"off\", and null.");
268 !isNonEmptyString(resolvePluginsRelativeTo
) &&
269 resolvePluginsRelativeTo
!== null
271 errors
.push("'resolvePluginsRelativeTo' must be a non-empty string or null.");
273 if (!isArrayOfNonEmptyString(rulePaths
)) {
274 errors
.push("'rulePaths' must be an array of non-empty strings.");
276 if (typeof useEslintrc
!== "boolean") {
277 errors
.push("'useEslintrc' must be a boolean.");
280 if (errors
.length
> 0) {
281 throw new ESLintInvalidOptionsError(errors
);
290 configFile
: overrideConfigFile
,
292 errorOnUnmatchedPattern
,
299 reportUnusedDisableDirectives
,
300 resolvePluginsRelativeTo
,
307 * Check if a value has one or more properties and that value is not undefined.
308 * @param {any} obj The value to check.
309 * @returns {boolean} `true` if `obj` has one or more properties that that value is not undefined.
311 function hasDefinedProperty(obj
) {
312 if (typeof obj
=== "object" && obj
!== null) {
313 for (const key
in obj
) {
314 if (typeof obj
[key
] !== "undefined") {
323 * Create rulesMeta object.
324 * @param {Map<string,Rule>} rules a map of rules from which to generate the object.
325 * @returns {Object} metadata for all enabled rules.
327 function createRulesMeta(rules
) {
328 return Array
.from(rules
).reduce((retVal
, [id
, rule
]) => {
329 retVal
[id
] = rule
.meta
;
334 /** @type {WeakMap<ExtractedConfig, DeprecatedRuleInfo[]>} */
335 const usedDeprecatedRulesCache
= new WeakMap();
338 * Create used deprecated rule list.
339 * @param {CLIEngine} cliEngine The CLIEngine instance.
340 * @param {string} maybeFilePath The absolute path to a lint target file or `"<text>"`.
341 * @returns {DeprecatedRuleInfo[]} The used deprecated rule list.
343 function getOrFindUsedDeprecatedRules(cliEngine
, maybeFilePath
) {
347 } = getCLIEngineInternalSlots(cliEngine
);
348 const filePath
= path
.isAbsolute(maybeFilePath
)
350 : path
.join(cwd
, "__placeholder__.js");
351 const configArray
= configArrayFactory
.getConfigArrayForFile(filePath
);
352 const config
= configArray
.extractConfig(filePath
);
354 // Most files use the same config, so cache it.
355 if (!usedDeprecatedRulesCache
.has(config
)) {
356 const pluginRules
= configArray
.pluginRules
;
359 for (const [ruleId
, ruleConf
] of Object
.entries(config
.rules
)) {
360 if (getRuleSeverity(ruleConf
) === 0) {
363 const rule
= pluginRules
.get(ruleId
) || BuiltinRules
.get(ruleId
);
364 const meta
= rule
&& rule
.meta
;
366 if (meta
&& meta
.deprecated
) {
367 retv
.push({ ruleId
, replacedBy
: meta
.replacedBy
|| [] });
371 usedDeprecatedRulesCache
.set(config
, Object
.freeze(retv
));
374 return usedDeprecatedRulesCache
.get(config
);
378 * Processes the linting results generated by a CLIEngine linting report to
379 * match the ESLint class's API.
380 * @param {CLIEngine} cliEngine The CLIEngine instance.
381 * @param {CLIEngineLintReport} report The CLIEngine linting report to process.
382 * @returns {LintResult[]} The processed linting results.
384 function processCLIEngineLintReport(cliEngine
, { results
}) {
389 return getOrFindUsedDeprecatedRules(cliEngine
, this.filePath
);
393 for (const result
of results
) {
394 Object
.defineProperty(result
, "usedDeprecatedRules", descriptor
);
401 * An Array.prototype.sort() compatible compare function to order results by their file path.
402 * @param {LintResult} a The first lint result.
403 * @param {LintResult} b The second lint result.
404 * @returns {number} An integer representing the order in which the two results should occur.
406 function compareResultsByFilePath(a
, b
) {
407 if (a
.filePath
< b
.filePath
) {
411 if (a
.filePath
> b
.filePath
) {
424 * Creates a new instance of the main ESLint API.
425 * @param {ESLintOptions} options The options for this instance.
427 constructor(options
= {}) {
428 const processedOptions
= processOptions(options
);
429 const cliEngine
= new CLIEngine(processedOptions
, { preloadedPlugins
: options
.plugins
});
433 } = getCLIEngineInternalSlots(cliEngine
);
437 * Address `overrideConfig` to set override config.
438 * Operate the `configArrayFactory` internal slot directly because this
439 * functionality doesn't exist as the public API of CLIEngine.
441 if (hasDefinedProperty(options
.overrideConfig
)) {
442 configArrayFactory
.setOverrideConfig(options
.overrideConfig
);
448 configArrayFactory
.clearCache();
449 lastConfigArrays
[0] = configArrayFactory
.getConfigArrayForFile();
452 // Initialize private properties.
453 privateMembersMap
.set(this, {
455 options
: processedOptions
463 static get version() {
468 * Outputs fixes from the given results to files.
469 * @param {LintResult[]} results The lint results.
470 * @returns {Promise<void>} Returns a promise that is used to track side effects.
472 static async
outputFixes(results
) {
473 if (!Array
.isArray(results
)) {
474 throw new Error("'results' must be an array");
480 if (typeof result
!== "object" || result
=== null) {
481 throw new Error("'results' must include only objects");
484 typeof result
.output
=== "string" &&
485 path
.isAbsolute(result
.filePath
)
488 .map(r
=> writeFile(r
.filePath
, r
.output
))
493 * Returns results that only contains errors.
494 * @param {LintResult[]} results The results to filter.
495 * @returns {LintResult[]} The filtered results.
497 static getErrorResults(results
) {
498 return CLIEngine
.getErrorResults(results
);
502 * Returns meta objects for each rule represented in the lint results.
503 * @param {LintResult[]} results The results to fetch rules meta for.
504 * @returns {Object} A mapping of ruleIds to rule meta objects.
506 getRulesMetaForResults(results
) {
508 const resultRuleIds
= new Set();
510 // first gather all ruleIds from all results
512 for (const result
of results
) {
513 for (const { ruleId
} of result
.messages
) {
514 resultRuleIds
.add(ruleId
);
516 for (const { ruleId
} of result
.suppressedMessages
) {
517 resultRuleIds
.add(ruleId
);
521 // create a map of all rules in the results
523 const { cliEngine
} = privateMembersMap
.get(this);
524 const rules
= cliEngine
.getRules();
525 const resultRules
= new Map();
527 for (const [ruleId
, rule
] of rules
) {
528 if (resultRuleIds
.has(ruleId
)) {
529 resultRules
.set(ruleId
, rule
);
533 return createRulesMeta(resultRules
);
538 * Executes the current configuration on an array of file and directory names.
539 * @param {string[]} patterns An array of file and directory names.
540 * @returns {Promise<LintResult[]>} The results of linting the file patterns given.
542 async
lintFiles(patterns
) {
543 if (!isNonEmptyString(patterns
) && !isArrayOfNonEmptyString(patterns
)) {
544 throw new Error("'patterns' must be a non-empty string or an array of non-empty strings");
546 const { cliEngine
} = privateMembersMap
.get(this);
548 return processCLIEngineLintReport(
550 cliEngine
.executeOnFiles(patterns
)
555 * Executes the current configuration on text.
556 * @param {string} code A string of JavaScript code to lint.
557 * @param {Object} [options] The options.
558 * @param {string} [options.filePath] The path to the file of the source code.
559 * @param {boolean} [options.warnIgnored] When set to true, warn if given filePath is an ignored path.
560 * @returns {Promise<LintResult[]>} The results of linting the string of code given.
562 async
lintText(code
, options
= {}) {
563 if (typeof code
!== "string") {
564 throw new Error("'code' must be a string");
566 if (typeof options
!== "object") {
567 throw new Error("'options' must be an object, null, or undefined");
575 const unknownOptionKeys
= Object
.keys(unknownOptions
);
577 if (unknownOptionKeys
.length
> 0) {
578 throw new Error(`'options' must not include the unknown option(s): ${unknownOptionKeys.join(", ")}`);
581 if (filePath
!== void 0 && !isNonEmptyString(filePath
)) {
582 throw new Error("'options.filePath' must be a non-empty string or undefined");
584 if (typeof warnIgnored
!== "boolean") {
585 throw new Error("'options.warnIgnored' must be a boolean or undefined");
588 const { cliEngine
} = privateMembersMap
.get(this);
590 return processCLIEngineLintReport(
592 cliEngine
.executeOnText(code
, filePath
, warnIgnored
)
597 * Returns the formatter representing the given formatter name.
598 * @param {string} [name] The name of the formatter to load.
599 * The following values are allowed:
600 * - `undefined` ... Load `stylish` builtin formatter.
601 * - A builtin formatter name ... Load the builtin formatter.
602 * - A third-party formatter name:
603 * - `foo` → `eslint-formatter-foo`
604 * - `@foo` → `@foo/eslint-formatter`
605 * - `@foo/bar` → `@foo/eslint-formatter-bar`
606 * - A file path ... Load the file.
607 * @returns {Promise<LoadedFormatter>} A promise resolving to the formatter object.
608 * This promise will be rejected if the given formatter was not found or not
611 async
loadFormatter(name
= "stylish") {
612 if (typeof name
!== "string") {
613 throw new Error("'name' must be a string");
616 const { cliEngine
, options
} = privateMembersMap
.get(this);
617 const formatter
= cliEngine
.getFormatter(name
);
619 if (typeof formatter
!== "function") {
620 throw new Error(`Formatter must be a function, but got a ${typeof formatter}.`);
626 * The main formatter method.
627 * @param {LintResult[]} results The lint results to format.
628 * @returns {string | Promise<string>} The formatted lint results.
631 let rulesMeta
= null;
633 results
.sort(compareResultsByFilePath
);
635 return formatter(results
, {
641 rulesMeta
= createRulesMeta(cliEngine
.getRules());
652 * Returns a configuration object for the given file based on the CLI options.
653 * This is the same logic used by the ESLint CLI executable to determine
654 * configuration for each file it processes.
655 * @param {string} filePath The path of the file to retrieve a config object for.
656 * @returns {Promise<ConfigData>} A configuration object for the file.
658 async
calculateConfigForFile(filePath
) {
659 if (!isNonEmptyString(filePath
)) {
660 throw new Error("'filePath' must be a non-empty string");
662 const { cliEngine
} = privateMembersMap
.get(this);
664 return cliEngine
.getConfigForFile(filePath
);
668 * Checks if a given path is ignored by ESLint.
669 * @param {string} filePath The path of the file to check.
670 * @returns {Promise<boolean>} Whether or not the given path is ignored.
672 async
isPathIgnored(filePath
) {
673 if (!isNonEmptyString(filePath
)) {
674 throw new Error("'filePath' must be a non-empty string");
676 const { cliEngine
} = privateMembersMap
.get(this);
678 return cliEngine
.isPathIgnored(filePath
);
682 //------------------------------------------------------------------------------
684 //------------------------------------------------------------------------------
690 * Get the private class members of a given ESLint instance for tests.
691 * @param {ESLint} instance The ESLint instance to get.
692 * @returns {ESLintPrivateMembers} The instance's private class members.
694 getESLintPrivateMembers(instance
) {
695 return privateMembersMap
.get(instance
);