2 * @fileoverview Main CLI object.
3 * @author Nicholas C. Zakas
9 * The CLI object should *not* call process.exit() directly. It should only return
10 * exit codes. This allows other programs to use the CLI object and still control
11 * when the program exits.
14 //------------------------------------------------------------------------------
16 //------------------------------------------------------------------------------
18 const fs
= require("fs");
19 const path
= require("path");
20 const defaultOptions
= require("../../conf/default-cli-options");
21 const pkg
= require("../../package.json");
22 const ConfigOps
= require("../shared/config-ops");
23 const naming
= require("../shared/naming");
24 const ModuleResolver
= require("../shared/relative-module-resolver");
25 const { Linter
} = require("../linter");
26 const builtInRules
= require("../rules");
27 const { CascadingConfigArrayFactory
} = require("./cascading-config-array-factory");
28 const { IgnorePattern
, getUsedExtractedConfigs
} = require("./config-array");
29 const { FileEnumerator
} = require("./file-enumerator");
30 const hash
= require("./hash");
31 const LintResultCache
= require("./lint-result-cache");
33 const debug
= require("debug")("eslint:cli-engine");
34 const validFixTypes
= new Set(["problem", "suggestion", "layout"]);
36 //------------------------------------------------------------------------------
38 //------------------------------------------------------------------------------
40 // For VSCode IntelliSense
41 /** @typedef {import("../shared/types").ConfigData} ConfigData */
42 /** @typedef {import("../shared/types").LintMessage} LintMessage */
43 /** @typedef {import("../shared/types").ParserOptions} ParserOptions */
44 /** @typedef {import("../shared/types").Plugin} Plugin */
45 /** @typedef {import("../shared/types").RuleConf} RuleConf */
46 /** @typedef {import("../shared/types").Rule} Rule */
47 /** @typedef {ReturnType<CascadingConfigArrayFactory["getConfigArrayForFile"]>} ConfigArray */
48 /** @typedef {ReturnType<ConfigArray["extractConfig"]>} ExtractedConfig */
51 * The options to configure a CLI engine with.
52 * @typedef {Object} CLIEngineOptions
53 * @property {boolean} allowInlineConfig Enable or disable inline configuration comments.
54 * @property {ConfigData} baseConfig Base config object, extended by all configs used with this CLIEngine instance
55 * @property {boolean} cache Enable result caching.
56 * @property {string} cacheLocation The cache file to use instead of .eslintcache.
57 * @property {string} configFile The configuration file to use.
58 * @property {string} cwd The value to use for the current working directory.
59 * @property {string[]} envs An array of environments to load.
60 * @property {string[]|null} extensions An array of file extensions to check.
61 * @property {boolean|Function} fix Execute in autofix mode. If a function, should return a boolean.
62 * @property {string[]} fixTypes Array of rule types to apply fixes for.
63 * @property {string[]} globals An array of global variables to declare.
64 * @property {boolean} ignore False disables use of .eslintignore.
65 * @property {string} ignorePath The ignore file to use instead of .eslintignore.
66 * @property {string|string[]} ignorePattern One or more glob patterns to ignore.
67 * @property {boolean} useEslintrc False disables looking for .eslintrc
68 * @property {string} parser The name of the parser to use.
69 * @property {ParserOptions} parserOptions An object of parserOption settings to use.
70 * @property {string[]} plugins An array of plugins to load.
71 * @property {Record<string,RuleConf>} rules An object of rules to use.
72 * @property {string[]} rulePaths An array of directories to load custom rules from.
73 * @property {boolean} reportUnusedDisableDirectives `true` adds reports for unused eslint-disable directives
74 * @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.
75 * @property {string} resolvePluginsRelativeTo The folder where plugins should be resolved from, defaulting to the CWD
80 * @typedef {Object} LintResult
81 * @property {string} filePath The path to the file that was linted.
82 * @property {LintMessage[]} messages All of the messages for the result.
83 * @property {number} errorCount Number of errors for the result.
84 * @property {number} warningCount Number of warnings for the result.
85 * @property {number} fixableErrorCount Number of fixable errors for the result.
86 * @property {number} fixableWarningCount Number of fixable warnings for the result.
87 * @property {string} [source] The source code of the file that was linted.
88 * @property {string} [output] The source code of the file that was linted, with as many fixes applied as possible.
92 * Information of deprecated rules.
93 * @typedef {Object} DeprecatedRuleInfo
94 * @property {string} ruleId The rule ID.
95 * @property {string[]} replacedBy The rule IDs that replace this deprecated rule.
100 * @typedef {Object} LintReport
101 * @property {LintResult[]} results All of the result.
102 * @property {number} errorCount Number of errors for the result.
103 * @property {number} warningCount Number of warnings for the result.
104 * @property {number} fixableErrorCount Number of fixable errors for the result.
105 * @property {number} fixableWarningCount Number of fixable warnings for the result.
106 * @property {DeprecatedRuleInfo[]} usedDeprecatedRules The list of used deprecated rules.
110 * Private data for CLIEngine.
111 * @typedef {Object} CLIEngineInternalSlots
112 * @property {Map<string, Plugin>} additionalPluginPool The map for additional plugins.
113 * @property {string} cacheFilePath The path to the cache of lint results.
114 * @property {CascadingConfigArrayFactory} configArrayFactory The factory of configs.
115 * @property {(filePath: string) => boolean} defaultIgnores The default predicate function to check if a file ignored or not.
116 * @property {FileEnumerator} fileEnumerator The file enumerator.
117 * @property {ConfigArray[]} lastConfigArrays The list of config arrays that the last `executeOnFiles` or `executeOnText` used.
118 * @property {LintResultCache|null} lintResultCache The cache of lint results.
119 * @property {Linter} linter The linter instance which has loaded rules.
120 * @property {CLIEngineOptions} options The normalized options of this instance.
123 //------------------------------------------------------------------------------
125 //------------------------------------------------------------------------------
127 /** @type {WeakMap<CLIEngine, CLIEngineInternalSlots>} */
128 const internalSlotsMap
= new WeakMap();
131 * Determines if each fix type in an array is supported by ESLint and throws
133 * @param {string[]} fixTypes An array of fix types to check.
135 * @throws {Error} If an invalid fix type is found.
137 function validateFixTypes(fixTypes
) {
138 for (const fixType
of fixTypes
) {
139 if (!validFixTypes
.has(fixType
)) {
140 throw new Error(`Invalid fix type "${fixType}" found.`);
146 * It will calculate the error and warning count for collection of messages per file
147 * @param {LintMessage[]} messages Collection of messages
148 * @returns {Object} Contains the stats
151 function calculateStatsPerFile(messages
) {
152 return messages
.reduce((stat
, message
) => {
153 if (message
.fatal
|| message
.severity
=== 2) {
156 stat
.fixableErrorCount
++;
161 stat
.fixableWarningCount
++;
168 fixableErrorCount
: 0,
169 fixableWarningCount
: 0
174 * It will calculate the error and warning count for collection of results from all files
175 * @param {LintResult[]} results Collection of messages from all the files
176 * @returns {Object} Contains the stats
179 function calculateStatsPerRun(results
) {
180 return results
.reduce((stat
, result
) => {
181 stat
.errorCount
+= result
.errorCount
;
182 stat
.warningCount
+= result
.warningCount
;
183 stat
.fixableErrorCount
+= result
.fixableErrorCount
;
184 stat
.fixableWarningCount
+= result
.fixableWarningCount
;
189 fixableErrorCount
: 0,
190 fixableWarningCount
: 0
195 * Processes an source code using ESLint.
196 * @param {Object} config The config object.
197 * @param {string} config.text The source code to verify.
198 * @param {string} config.cwd The path to the current working directory.
199 * @param {string|undefined} config.filePath The path to the file of `text`. If this is undefined, it uses `<text>`.
200 * @param {ConfigArray} config.config The config.
201 * @param {boolean} config.fix If `true` then it does fix.
202 * @param {boolean} config.allowInlineConfig If `true` then it uses directive comments.
203 * @param {boolean} config.reportUnusedDisableDirectives If `true` then it reports unused `eslint-disable` comments.
204 * @param {FileEnumerator} config.fileEnumerator The file enumerator to check if a path is a target or not.
205 * @param {Linter} config.linter The linter instance to verify.
206 * @returns {LintResult} The result of linting.
209 function verifyText({
212 filePath
: providedFilePath
,
216 reportUnusedDisableDirectives
,
220 const filePath
= providedFilePath
|| "<text>";
222 debug(`Lint ${filePath}`);
226 * `config.extractConfig(filePath)` requires an absolute path, but `linter`
227 * doesn't know CWD, so it gives `linter` an absolute path always.
229 const filePathToVerify
= filePath
=== "<text>" ? path
.join(cwd
, filePath
) : filePath
;
230 const { fixed
, messages
, output
} = linter
.verifyAndFix(
235 filename
: filePathToVerify
,
237 reportUnusedDisableDirectives
,
240 * Check if the linter should adopt a given code block or not.
241 * @param {string} blockFilename The virtual filename of a code block.
242 * @returns {boolean} `true` if the linter should adopt the code block.
244 filterCodeBlock(blockFilename
) {
245 return fileEnumerator
.isTargetPath(blockFilename
);
254 ...calculateStatsPerFile(messages
)
258 result
.output
= output
;
261 result
.errorCount
+ result
.warningCount
> 0 &&
262 typeof result
.output
=== "undefined"
264 result
.source
= text
;
271 * Returns result with warning by ignore settings
272 * @param {string} filePath File path of checked code
273 * @param {string} baseDir Absolute path of base directory
274 * @returns {LintResult} Result with single warning
277 function createIgnoreResult(filePath
, baseDir
) {
279 const isHidden
= filePath
.split(path
.sep
)
280 .find(segment
=> /^\./u.test(segment
));
281 const isInNodeModules
= baseDir
&& path
.relative(baseDir
, filePath
).startsWith("node_modules");
284 message
= "File ignored by default. Use a negated ignore pattern (like \"--ignore-pattern '!<relative/path/to/filename>'\") to override.";
285 } else if (isInNodeModules
) {
286 message
= "File ignored by default. Use \"--ignore-pattern '!node_modules/*'\" to override.";
288 message
= "File ignored because of a matching ignore pattern. Use \"--no-ignore\" to override.";
292 filePath
: path
.resolve(filePath
),
302 fixableErrorCount
: 0,
303 fixableWarningCount
: 0
309 * @param {string} ruleId The rule ID to get.
310 * @param {ConfigArray[]} configArrays The config arrays that have plugin rules.
311 * @returns {Rule|null} The rule or null.
313 function getRule(ruleId
, configArrays
) {
314 for (const configArray
of configArrays
) {
315 const rule
= configArray
.pluginRules
.get(ruleId
);
321 return builtInRules
.get(ruleId
) || null;
325 * Collect used deprecated rules.
326 * @param {ConfigArray[]} usedConfigArrays The config arrays which were used.
327 * @returns {IterableIterator<DeprecatedRuleInfo>} Used deprecated rules.
329 function *iterateRuleDeprecationWarnings(usedConfigArrays
) {
330 const processedRuleIds
= new Set();
332 // Flatten used configs.
333 /** @type {ExtractedConfig[]} */
334 const configs
= [].concat(
335 ...usedConfigArrays
.map(getUsedExtractedConfigs
)
338 // Traverse rule configs.
339 for (const config
of configs
) {
340 for (const [ruleId
, ruleConfig
] of Object
.entries(config
.rules
)) {
342 // Skip if it was processed.
343 if (processedRuleIds
.has(ruleId
)) {
346 processedRuleIds
.add(ruleId
);
348 // Skip if it's not used.
349 if (!ConfigOps
.getRuleSeverity(ruleConfig
)) {
352 const rule
= getRule(ruleId
, usedConfigArrays
);
354 // Skip if it's not deprecated.
355 if (!(rule
&& rule
.meta
&& rule
.meta
.deprecated
)) {
359 // This rule was used and deprecated.
362 replacedBy
: rule
.meta
.replacedBy
|| []
369 * Checks if the given message is an error message.
370 * @param {LintMessage} message The message to check.
371 * @returns {boolean} Whether or not the message is an error message.
374 function isErrorMessage(message
) {
375 return message
.severity
=== 2;
380 * return the cacheFile to be used by eslint, based on whether the provided parameter is
381 * a directory or looks like a directory (ends in `path.sep`), in which case the file
382 * name will be the `cacheFile/.cache_hashOfCWD`
384 * if cacheFile points to a file or looks like a file then in will just use that file
385 * @param {string} cacheFile The name of file to be used to store the cache
386 * @param {string} cwd Current working directory
387 * @returns {string} the resolved path to the cache file
389 function getCacheFile(cacheFile
, cwd
) {
392 * make sure the path separators are normalized for the environment/os
393 * keeping the trailing path separator if present
395 const normalizedCacheFile
= path
.normalize(cacheFile
);
397 const resolvedCacheFile
= path
.resolve(cwd
, normalizedCacheFile
);
398 const looksLikeADirectory
= normalizedCacheFile
.slice(-1) === path
.sep
;
401 * return the name for the cache file in case the provided parameter is a directory
402 * @returns {string} the resolved path to the cacheFile
404 function getCacheFileForDirectory() {
405 return path
.join(resolvedCacheFile
, `.cache_${hash(cwd)}`);
411 fileStats
= fs
.lstatSync(resolvedCacheFile
);
418 * in case the file exists we need to verify if the provided path
419 * is a directory or a file. If it is a directory we want to create a file
420 * inside that directory
425 * is a directory or is a file, but the original file the user provided
426 * looks like a directory but `path.resolve` removed the `last path.sep`
427 * so we need to still treat this like a directory
429 if (fileStats
.isDirectory() || looksLikeADirectory
) {
430 return getCacheFileForDirectory();
433 // is file so just use that file
434 return resolvedCacheFile
;
438 * here we known the file or directory doesn't exist,
439 * so we will try to infer if its a directory if it looks like a directory
440 * for the current operating system.
443 // if the last character passed is a path separator we assume is a directory
444 if (looksLikeADirectory
) {
445 return getCacheFileForDirectory();
448 return resolvedCacheFile
;
452 * Convert a string array to a boolean map.
453 * @param {string[]|null} keys The keys to assign true.
454 * @param {boolean} defaultValue The default value for each property.
455 * @param {string} displayName The property name which is used in error message.
456 * @returns {Record<string,boolean>} The boolean map.
458 function toBooleanMap(keys
, defaultValue
, displayName
) {
459 if (keys
&& !Array
.isArray(keys
)) {
460 throw new Error(`${displayName} must be an array.`);
462 if (keys
&& keys
.length
> 0) {
463 return keys
.reduce((map
, def
) => {
464 const [key
, value
] = def
.split(":");
466 if (key
!== "__proto__") {
467 map
[key
] = value
=== void 0
479 * Create a config data from CLI options.
480 * @param {CLIEngineOptions} options The options
481 * @returns {ConfigData|null} The created config data.
483 function createConfigDataFromOptions(options
) {
491 const env
= toBooleanMap(options
.envs
, true, "envs");
492 const globals
= toBooleanMap(options
.globals
, false, "globals");
496 globals
=== void 0 &&
497 (ignorePattern
=== void 0 || ignorePattern
.length
=== 0) &&
499 parserOptions
=== void 0 &&
500 plugins
=== void 0 &&
508 ignorePatterns
: ignorePattern
,
517 * Checks whether a directory exists at the given location
518 * @param {string} resolvedPath A path from the CWD
519 * @returns {boolean} `true` if a directory exists
521 function directoryExists(resolvedPath
) {
523 return fs
.statSync(resolvedPath
).isDirectory();
525 if (error
&& error
.code
=== "ENOENT") {
532 //------------------------------------------------------------------------------
534 //------------------------------------------------------------------------------
539 * Creates a new instance of the core CLI engine.
540 * @param {CLIEngineOptions} providedOptions The options for this instance.
542 constructor(providedOptions
) {
543 const options
= Object
.assign(
546 { cwd
: process
.cwd() },
550 if (options
.fix
=== void 0) {
554 const additionalPluginPool
= new Map();
555 const cacheFilePath
= getCacheFile(
556 options
.cacheLocation
|| options
.cacheFile
,
559 const configArrayFactory
= new CascadingConfigArrayFactory({
560 additionalPluginPool
,
561 baseConfig
: options
.baseConfig
|| null,
562 cliConfig
: createConfigDataFromOptions(options
),
564 ignorePath
: options
.ignorePath
,
565 resolvePluginsRelativeTo
: options
.resolvePluginsRelativeTo
,
566 rulePaths
: options
.rulePaths
,
567 specificConfigPath
: options
.configFile
,
568 useEslintrc
: options
.useEslintrc
570 const fileEnumerator
= new FileEnumerator({
573 extensions
: options
.extensions
,
574 globInputPaths
: options
.globInputPaths
,
575 errorOnUnmatchedPattern
: options
.errorOnUnmatchedPattern
,
576 ignore
: options
.ignore
578 const lintResultCache
=
579 options
.cache
? new LintResultCache(cacheFilePath
) : null;
580 const linter
= new Linter({ cwd
: options
.cwd
});
582 /** @type {ConfigArray[]} */
583 const lastConfigArrays
= [configArrayFactory
.getConfigArrayForFile()];
585 // Store private data.
586 internalSlotsMap
.set(this, {
587 additionalPluginPool
,
590 defaultIgnores
: IgnorePattern
.createDefaultIgnore(options
.cwd
),
598 // setup special filter for fixes
599 if (options
.fix
&& options
.fixTypes
&& options
.fixTypes
.length
> 0) {
600 debug(`Using fix types ${options.fixTypes}`);
602 // throw an error if any invalid fix types are found
603 validateFixTypes(options
.fixTypes
);
605 // convert to Set for faster lookup
606 const fixTypes
= new Set(options
.fixTypes
);
608 // save original value of options.fix in case it's a function
609 const originalFix
= (typeof options
.fix
=== "function")
610 ? options
.fix
: () => true;
612 options
.fix
= message
=> {
613 const rule
= message
.ruleId
&& getRule(message
.ruleId
, lastConfigArrays
);
614 const matches
= rule
&& rule
.meta
&& fixTypes
.has(rule
.meta
.type
);
616 return matches
&& originalFix(message
);
622 const { lastConfigArrays
} = internalSlotsMap
.get(this);
624 return new Map(function *() {
627 for (const configArray
of lastConfigArrays
) {
628 yield* configArray
.pluginRules
;
634 * Returns results that only contains errors.
635 * @param {LintResult[]} results The results to filter.
636 * @returns {LintResult[]} The filtered results.
638 static getErrorResults(results
) {
641 results
.forEach(result
=> {
642 const filteredMessages
= result
.messages
.filter(isErrorMessage
);
644 if (filteredMessages
.length
> 0) {
647 messages
: filteredMessages
,
648 errorCount
: filteredMessages
.length
,
650 fixableErrorCount
: result
.fixableErrorCount
,
651 fixableWarningCount
: 0
660 * Outputs fixes from the given results to files.
661 * @param {LintReport} report The report object created by CLIEngine.
664 static outputFixes(report
) {
665 report
.results
.filter(result
=> Object
.prototype.hasOwnProperty
.call(result
, "output")).forEach(result
=> {
666 fs
.writeFileSync(result
.filePath
, result
.output
);
672 * Add a plugin by passing its configuration
673 * @param {string} name Name of the plugin.
674 * @param {Plugin} pluginObject Plugin configuration object.
677 addPlugin(name
, pluginObject
) {
679 additionalPluginPool
,
682 } = internalSlotsMap
.get(this);
684 additionalPluginPool
.set(name
, pluginObject
);
685 configArrayFactory
.clearCache();
686 lastConfigArrays
.length
= 1;
687 lastConfigArrays
[0] = configArrayFactory
.getConfigArrayForFile();
691 * Resolves the patterns passed into executeOnFiles() into glob-based patterns
692 * for easier handling.
693 * @param {string[]} patterns The file patterns passed on the command line.
694 * @returns {string[]} The equivalent glob patterns.
696 resolveFileGlobPatterns(patterns
) {
697 const { options
} = internalSlotsMap
.get(this);
699 if (options
.globInputPaths
=== false) {
700 return patterns
.filter(Boolean
);
703 const extensions
= (options
.extensions
|| [".js"]).map(ext
=> ext
.replace(/^\./u, ""));
704 const dirSuffix
= `/**/*.{${extensions.join(",")}}`;
706 return patterns
.filter(Boolean
).map(pathname
=> {
707 const resolvedPath
= path
.resolve(options
.cwd
, pathname
);
708 const newPath
= directoryExists(resolvedPath
)
709 ? pathname
.replace(/[/\\]$/u, "") + dirSuffix
712 return path
.normalize(newPath
).replace(/\\/gu, "/");
717 * Executes the current configuration on an array of file and directory names.
718 * @param {string[]} patterns An array of file and directory names.
719 * @returns {LintReport} The results for all files that were linted.
721 executeOnFiles(patterns) {
733 reportUnusedDisableDirectives
735 } = internalSlotsMap.get(this);
737 const startTime = Date.now();
739 // Clear the last used config arrays.
740 lastConfigArrays.length = 0;
742 // Delete cache file; should this do here?
745 fs.unlinkSync(cacheFilePath);
747 const errorCode = error && error.code;
749 // Ignore errors when no such file exists or file system is read only (and cache file does not exist)
750 if (errorCode !== "ENOENT
" && !(errorCode === "EROFS
" && !fs.existsSync(cacheFilePath))) {
756 // Iterate source code files.
757 for (const { config, filePath, ignored } of fileEnumerator.iterateFiles(patterns)) {
759 results.push(createIgnoreResult(filePath, cwd));
764 * Store used configs for:
765 * - this method uses to collect used deprecated rules.
766 * - `getRules()` method uses to collect all loaded rules.
767 * - `--fix-type` option uses to get the loaded rule's meta data.
769 if (!lastConfigArrays.includes(config)) {
770 lastConfigArrays.push(config);
773 // Skip if there is cached result.
774 if (lintResultCache) {
776 lintResultCache.getCachedLintResults(filePath, config);
780 cachedResult.messages &&
781 cachedResult.messages.length > 0;
783 if (hadMessages && fix) {
784 debug(`Reprocessing cached file to allow autofix: ${filePath}`);
786 debug(`Skipping file since it hasn't changed: ${filePath}`);
787 results.push(cachedResult);
794 const result = verifyText({
795 text: fs.readFileSync(filePath, "utf8
"),
801 reportUnusedDisableDirectives,
806 results.push(result);
809 * Store the lint result in the LintResultCache.
810 * NOTE: The LintResultCache will remove the file source and any
811 * other properties that are difficult to serialize, and will
812 * hydrate those properties back in on future lint runs.
814 if (lintResultCache) {
815 lintResultCache.setCachedLintResults(filePath, config, result);
819 // Persist the cache to disk.
820 if (lintResultCache) {
821 lintResultCache.reconcile();
824 // Collect used deprecated rules.
825 const usedDeprecatedRules = Array.from(
826 iterateRuleDeprecationWarnings(lastConfigArrays)
829 debug(`Linting complete in: ${Date.now() - startTime}ms`);
832 ...calculateStatsPerRun(results),
838 * Executes the current configuration on text.
839 * @param {string} text A string of JavaScript code to lint.
840 * @param {string} [filename] An optional string representing the texts filename.
841 * @param {boolean} [warnIgnored] Always warn when a file is ignored
842 * @returns {LintReport} The results for the linting.
844 executeOnText(text, filename, warnIgnored) {
854 reportUnusedDisableDirectives
856 } = internalSlotsMap.get(this);
858 const startTime = Date.now();
859 const resolvedFilename = filename && path.resolve(cwd, filename);
861 // Clear the last used config arrays.
862 lastConfigArrays.length = 0;
864 if (resolvedFilename && this.isPathIgnored(resolvedFilename)) {
866 results.push(createIgnoreResult(resolvedFilename, cwd));
869 const config = configArrayFactory.getConfigArrayForFile(
870 resolvedFilename || "__placeholder__
.js
"
874 * Store used configs for:
875 * - this method uses to collect used deprecated rules.
876 * - `getRules()` method uses to collect all loaded rules.
877 * - `--fix-type` option uses to get the loaded rule's meta data.
879 lastConfigArrays.push(config);
882 results.push(verifyText({
884 filePath: resolvedFilename,
889 reportUnusedDisableDirectives,
895 // Collect used deprecated rules.
896 const usedDeprecatedRules = Array.from(
897 iterateRuleDeprecationWarnings(lastConfigArrays)
900 debug(`Linting complete in: ${Date.now() - startTime}ms`);
903 ...calculateStatsPerRun(results),
909 * Returns a configuration object for the given file based on the CLI options.
910 * This is the same logic used by the ESLint CLI executable to determine
911 * configuration for each file it processes.
912 * @param {string} filePath The path of the file to retrieve a config object for.
913 * @returns {ConfigData} A configuration object for the file.
915 getConfigForFile(filePath) {
916 const { configArrayFactory, options } = internalSlotsMap.get(this);
917 const absolutePath = path.resolve(options.cwd, filePath);
919 if (directoryExists(absolutePath)) {
921 new Error("'filePath' should not be a directory path
."),
922 { messageTemplate: "print
-config
-with-directory
-path
" }
926 return configArrayFactory
927 .getConfigArrayForFile(absolutePath)
928 .extractConfig(absolutePath)
929 .toCompatibleObjectAsConfigFileContent();
933 * Checks if a given path is ignored by ESLint.
934 * @param {string} filePath The path of the file to check.
935 * @returns {boolean} Whether or not the given path is ignored.
937 isPathIgnored(filePath) {
941 options: { cwd, ignore }
942 } = internalSlotsMap.get(this);
943 const absolutePath = path.resolve(cwd, filePath);
946 const config = configArrayFactory
947 .getConfigArrayForFile(absolutePath)
948 .extractConfig(absolutePath);
949 const ignores = config.ignores || defaultIgnores;
951 return ignores(absolutePath);
954 return defaultIgnores(absolutePath);
958 * Returns the formatter representing the given format or null if no formatter
959 * with the given name can be found.
960 * @param {string} [format] The name of the format to load or the path to a
962 * @returns {Function} The formatter function or null if not found.
964 getFormatter(format) {
966 // default is stylish
967 const resolvedFormatName = format || "stylish
";
969 // only strings are valid formatters
970 if (typeof resolvedFormatName === "string
") {
972 // replace \ with / for Windows compatibility
973 const normalizedFormatName = resolvedFormatName.replace(/\\/gu, "/");
975 const slots = internalSlotsMap.get(this);
976 const cwd = slots ? slots.options.cwd : process.cwd();
977 const namespace = naming.getNamespaceFromTerm(normalizedFormatName);
981 // if there's a slash, then it's a file (TODO: this check seems dubious for scoped npm packages)
982 if (!namespace && normalizedFormatName.indexOf("/") > -1) {
983 formatterPath = path.resolve(cwd, normalizedFormatName);
986 const npmFormat = naming.normalizePackageName(normalizedFormatName, "eslint
-formatter
");
988 formatterPath = ModuleResolver.resolve(npmFormat, path.join(cwd, "__placeholder__
.js
"));
990 formatterPath = path.resolve(__dirname, "formatters
", normalizedFormatName);
995 return require(formatterPath);
997 ex.message = `There was a problem loading formatter: ${formatterPath}\nError: ${ex.message}`;
1007 CLIEngine.version = pkg.version;
1008 CLIEngine.getFormatter = CLIEngine.prototype.getFormatter;
1014 * Get the internal slots of a given CLIEngine instance for tests.
1015 * @param {CLIEngine} instance The CLIEngine instance to get.
1016 * @returns {CLIEngineInternalSlots} The internal slots.
1018 getCLIEngineInternalSlots(instance) {
1019 return internalSlotsMap.get(instance);