eslint 9.25.1 → 9.27.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +44 -39
- package/bin/eslint.js +15 -0
- package/conf/rule-type-list.json +2 -1
- package/lib/cli-engine/cli-engine.js +8 -8
- package/lib/cli.js +6 -5
- package/lib/config/config-loader.js +10 -18
- package/lib/config/config.js +328 -5
- package/lib/eslint/eslint-helpers.js +3 -1
- package/lib/eslint/eslint.js +31 -17
- package/lib/eslint/legacy-eslint.js +7 -7
- package/lib/languages/js/index.js +4 -3
- package/lib/languages/js/source-code/source-code.js +10 -6
- package/lib/linter/apply-disable-directives.js +1 -1
- package/lib/linter/esquery.js +329 -0
- package/lib/linter/file-context.js +11 -0
- package/lib/linter/linter.js +81 -89
- package/lib/linter/node-event-generator.js +94 -251
- package/lib/linter/report-translator.js +2 -1
- package/lib/options.js +11 -0
- package/lib/rule-tester/rule-tester.js +17 -9
- package/lib/rules/eqeqeq.js +31 -8
- package/lib/rules/index.js +2 -1
- package/lib/rules/max-params.js +32 -7
- package/lib/rules/no-array-constructor.js +51 -1
- package/lib/rules/no-shadow-restricted-names.js +25 -2
- package/lib/rules/no-unassigned-vars.js +72 -0
- package/lib/rules/no-unused-expressions.js +7 -1
- package/lib/rules/no-useless-escape.js +24 -2
- package/lib/rules/prefer-named-capture-group.js +7 -1
- package/lib/rules/utils/lazy-loading-rule-map.js +2 -2
- package/lib/services/processor-service.js +1 -2
- package/lib/services/suppressions-service.js +5 -3
- package/lib/shared/flags.js +1 -0
- package/lib/shared/serialization.js +29 -6
- package/lib/types/index.d.ts +126 -6
- package/lib/types/rules.d.ts +33 -2
- package/package.json +7 -6
- package/lib/config/flat-config-helpers.js +0 -128
- package/lib/config/rule-validator.js +0 -199
- package/lib/shared/types.js +0 -246
package/lib/config/config.js
CHANGED
@@ -10,16 +10,31 @@
|
|
10
10
|
//-----------------------------------------------------------------------------
|
11
11
|
|
12
12
|
const { deepMergeArrays } = require("../shared/deep-merge-arrays");
|
13
|
-
const { getRuleFromConfig } = require("./flat-config-helpers");
|
14
13
|
const { flatConfigSchema, hasMethod } = require("./flat-config-schema");
|
15
|
-
const { RuleValidator } = require("./rule-validator");
|
16
14
|
const { ObjectSchema } = require("@eslint/config-array");
|
15
|
+
const ajvImport = require("../shared/ajv");
|
16
|
+
const ajv = ajvImport();
|
17
|
+
const ruleReplacements = require("../../conf/replacements.json");
|
17
18
|
|
18
19
|
//-----------------------------------------------------------------------------
|
19
|
-
//
|
20
|
+
// Typedefs
|
21
|
+
//-----------------------------------------------------------------------------
|
22
|
+
|
23
|
+
/**
|
24
|
+
* @import { RuleDefinition } from "@eslint/core";
|
25
|
+
* @import { Linter } from "eslint";
|
26
|
+
*/
|
27
|
+
|
20
28
|
//-----------------------------------------------------------------------------
|
29
|
+
// Private Members
|
30
|
+
//------------------------------------------------------------------------------
|
21
31
|
|
22
|
-
|
32
|
+
// JSON schema that disallows passing any options
|
33
|
+
const noOptionsSchema = Object.freeze({
|
34
|
+
type: "array",
|
35
|
+
minItems: 0,
|
36
|
+
maxItems: 0,
|
37
|
+
});
|
23
38
|
|
24
39
|
const severities = new Map([
|
25
40
|
[0, 0],
|
@@ -30,6 +45,174 @@ const severities = new Map([
|
|
30
45
|
["error", 2],
|
31
46
|
]);
|
32
47
|
|
48
|
+
/**
|
49
|
+
* A collection of compiled validators for rules that have already
|
50
|
+
* been validated.
|
51
|
+
* @type {WeakMap}
|
52
|
+
*/
|
53
|
+
const validators = new WeakMap();
|
54
|
+
|
55
|
+
//-----------------------------------------------------------------------------
|
56
|
+
// Helpers
|
57
|
+
//-----------------------------------------------------------------------------
|
58
|
+
|
59
|
+
/**
|
60
|
+
* Throws a helpful error when a rule cannot be found.
|
61
|
+
* @param {Object} ruleId The rule identifier.
|
62
|
+
* @param {string} ruleId.pluginName The ID of the rule to find.
|
63
|
+
* @param {string} ruleId.ruleName The ID of the rule to find.
|
64
|
+
* @param {Object} config The config to search in.
|
65
|
+
* @throws {TypeError} For missing plugin or rule.
|
66
|
+
* @returns {void}
|
67
|
+
*/
|
68
|
+
function throwRuleNotFoundError({ pluginName, ruleName }, config) {
|
69
|
+
const ruleId = pluginName === "@" ? ruleName : `${pluginName}/${ruleName}`;
|
70
|
+
|
71
|
+
const errorMessageHeader = `Key "rules": Key "${ruleId}"`;
|
72
|
+
|
73
|
+
let errorMessage = `${errorMessageHeader}: Could not find plugin "${pluginName}" in configuration.`;
|
74
|
+
|
75
|
+
const missingPluginErrorMessage = errorMessage;
|
76
|
+
|
77
|
+
// if the plugin exists then we need to check if the rule exists
|
78
|
+
if (config.plugins && config.plugins[pluginName]) {
|
79
|
+
const replacementRuleName = ruleReplacements.rules[ruleName];
|
80
|
+
|
81
|
+
if (pluginName === "@" && replacementRuleName) {
|
82
|
+
errorMessage = `${errorMessageHeader}: Rule "${ruleName}" was removed and replaced by "${replacementRuleName}".`;
|
83
|
+
} else {
|
84
|
+
errorMessage = `${errorMessageHeader}: Could not find "${ruleName}" in plugin "${pluginName}".`;
|
85
|
+
|
86
|
+
// otherwise, let's see if we can find the rule name elsewhere
|
87
|
+
for (const [otherPluginName, otherPlugin] of Object.entries(
|
88
|
+
config.plugins,
|
89
|
+
)) {
|
90
|
+
if (otherPlugin.rules && otherPlugin.rules[ruleName]) {
|
91
|
+
errorMessage += ` Did you mean "${otherPluginName}/${ruleName}"?`;
|
92
|
+
break;
|
93
|
+
}
|
94
|
+
}
|
95
|
+
}
|
96
|
+
|
97
|
+
// falls through to throw error
|
98
|
+
}
|
99
|
+
|
100
|
+
const error = new TypeError(errorMessage);
|
101
|
+
|
102
|
+
if (errorMessage === missingPluginErrorMessage) {
|
103
|
+
error.messageTemplate = "config-plugin-missing";
|
104
|
+
error.messageData = { pluginName, ruleId };
|
105
|
+
}
|
106
|
+
|
107
|
+
throw error;
|
108
|
+
}
|
109
|
+
|
110
|
+
/**
|
111
|
+
* The error type when a rule has an invalid `meta.schema`.
|
112
|
+
*/
|
113
|
+
class InvalidRuleOptionsSchemaError extends Error {
|
114
|
+
/**
|
115
|
+
* Creates a new instance.
|
116
|
+
* @param {string} ruleId Id of the rule that has an invalid `meta.schema`.
|
117
|
+
* @param {Error} processingError Error caught while processing the `meta.schema`.
|
118
|
+
*/
|
119
|
+
constructor(ruleId, processingError) {
|
120
|
+
super(
|
121
|
+
`Error while processing options validation schema of rule '${ruleId}': ${processingError.message}`,
|
122
|
+
{ cause: processingError },
|
123
|
+
);
|
124
|
+
this.code = "ESLINT_INVALID_RULE_OPTIONS_SCHEMA";
|
125
|
+
}
|
126
|
+
}
|
127
|
+
|
128
|
+
/**
|
129
|
+
* Parses a ruleId into its plugin and rule parts.
|
130
|
+
* @param {string} ruleId The rule ID to parse.
|
131
|
+
* @returns {{pluginName:string,ruleName:string}} The plugin and rule
|
132
|
+
* parts of the ruleId;
|
133
|
+
*/
|
134
|
+
function parseRuleId(ruleId) {
|
135
|
+
let pluginName, ruleName;
|
136
|
+
|
137
|
+
// distinguish between core rules and plugin rules
|
138
|
+
if (ruleId.includes("/")) {
|
139
|
+
// mimic scoped npm packages
|
140
|
+
if (ruleId.startsWith("@")) {
|
141
|
+
pluginName = ruleId.slice(0, ruleId.lastIndexOf("/"));
|
142
|
+
} else {
|
143
|
+
pluginName = ruleId.slice(0, ruleId.indexOf("/"));
|
144
|
+
}
|
145
|
+
|
146
|
+
ruleName = ruleId.slice(pluginName.length + 1);
|
147
|
+
} else {
|
148
|
+
pluginName = "@";
|
149
|
+
ruleName = ruleId;
|
150
|
+
}
|
151
|
+
|
152
|
+
return {
|
153
|
+
pluginName,
|
154
|
+
ruleName,
|
155
|
+
};
|
156
|
+
}
|
157
|
+
|
158
|
+
/**
|
159
|
+
* Retrieves a rule instance from a given config based on the ruleId.
|
160
|
+
* @param {string} ruleId The rule ID to look for.
|
161
|
+
* @param {Linter.Config} config The config to search.
|
162
|
+
* @returns {RuleDefinition|undefined} The rule if found
|
163
|
+
* or undefined if not.
|
164
|
+
*/
|
165
|
+
function getRuleFromConfig(ruleId, config) {
|
166
|
+
const { pluginName, ruleName } = parseRuleId(ruleId);
|
167
|
+
|
168
|
+
return config.plugins?.[pluginName]?.rules?.[ruleName];
|
169
|
+
}
|
170
|
+
|
171
|
+
/**
|
172
|
+
* Gets a complete options schema for a rule.
|
173
|
+
* @param {RuleDefinition} rule A rule object
|
174
|
+
* @throws {TypeError} If `meta.schema` is specified but is not an array, object or `false`.
|
175
|
+
* @returns {Object|null} JSON Schema for the rule's options. `null` if `meta.schema` is `false`.
|
176
|
+
*/
|
177
|
+
function getRuleOptionsSchema(rule) {
|
178
|
+
if (!rule.meta) {
|
179
|
+
return { ...noOptionsSchema }; // default if `meta.schema` is not specified
|
180
|
+
}
|
181
|
+
|
182
|
+
const schema = rule.meta.schema;
|
183
|
+
|
184
|
+
if (typeof schema === "undefined") {
|
185
|
+
return { ...noOptionsSchema }; // default if `meta.schema` is not specified
|
186
|
+
}
|
187
|
+
|
188
|
+
// `schema:false` is an allowed explicit opt-out of options validation for the rule
|
189
|
+
if (schema === false) {
|
190
|
+
return null;
|
191
|
+
}
|
192
|
+
|
193
|
+
if (typeof schema !== "object" || schema === null) {
|
194
|
+
throw new TypeError("Rule's `meta.schema` must be an array or object");
|
195
|
+
}
|
196
|
+
|
197
|
+
// ESLint-specific array form needs to be converted into a valid JSON Schema definition
|
198
|
+
if (Array.isArray(schema)) {
|
199
|
+
if (schema.length) {
|
200
|
+
return {
|
201
|
+
type: "array",
|
202
|
+
items: schema,
|
203
|
+
minItems: 0,
|
204
|
+
maxItems: schema.length,
|
205
|
+
};
|
206
|
+
}
|
207
|
+
|
208
|
+
// `schema:[]` is an explicit way to specify that the rule does not accept any options
|
209
|
+
return { ...noOptionsSchema };
|
210
|
+
}
|
211
|
+
|
212
|
+
// `schema:<object>` is assumed to be a valid JSON Schema definition
|
213
|
+
return schema;
|
214
|
+
}
|
215
|
+
|
33
216
|
/**
|
34
217
|
* Splits a plugin identifier in the form a/b/c into two parts: a/b and c.
|
35
218
|
* @param {string} identifier The identifier to parse.
|
@@ -124,6 +307,29 @@ function languageOptionsToJSON(languageOptions, objectKey = "languageOptions") {
|
|
124
307
|
return result;
|
125
308
|
}
|
126
309
|
|
310
|
+
/**
|
311
|
+
* Gets or creates a validator for a rule.
|
312
|
+
* @param {Object} rule The rule to get a validator for.
|
313
|
+
* @param {string} ruleId The ID of the rule (for error reporting).
|
314
|
+
* @returns {Function|null} A validation function or null if no validation is needed.
|
315
|
+
* @throws {InvalidRuleOptionsSchemaError} If a rule's `meta.schema` is invalid.
|
316
|
+
*/
|
317
|
+
function getOrCreateValidator(rule, ruleId) {
|
318
|
+
if (!validators.has(rule)) {
|
319
|
+
try {
|
320
|
+
const schema = getRuleOptionsSchema(rule);
|
321
|
+
|
322
|
+
if (schema) {
|
323
|
+
validators.set(rule, ajv.compile(schema));
|
324
|
+
}
|
325
|
+
} catch (err) {
|
326
|
+
throw new InvalidRuleOptionsSchemaError(ruleId, err);
|
327
|
+
}
|
328
|
+
}
|
329
|
+
|
330
|
+
return validators.get(rule);
|
331
|
+
}
|
332
|
+
|
127
333
|
//-----------------------------------------------------------------------------
|
128
334
|
// Exports
|
129
335
|
//-----------------------------------------------------------------------------
|
@@ -252,7 +458,7 @@ class Config {
|
|
252
458
|
// Process the rules
|
253
459
|
if (this.rules) {
|
254
460
|
this.#normalizeRulesConfig();
|
255
|
-
|
461
|
+
this.validateRulesConfig(this.rules);
|
256
462
|
}
|
257
463
|
}
|
258
464
|
|
@@ -291,6 +497,15 @@ class Config {
|
|
291
497
|
};
|
292
498
|
}
|
293
499
|
|
500
|
+
/**
|
501
|
+
* Gets a rule configuration by its ID.
|
502
|
+
* @param {string} ruleId The ID of the rule to get.
|
503
|
+
* @returns {RuleDefinition|undefined} The rule definition from the plugin, or `undefined` if the rule is not found.
|
504
|
+
*/
|
505
|
+
getRuleDefinition(ruleId) {
|
506
|
+
return getRuleFromConfig(ruleId, this);
|
507
|
+
}
|
508
|
+
|
294
509
|
/**
|
295
510
|
* Normalizes the rules configuration. Ensures that each rule config is
|
296
511
|
* an array and that the severity is a number. Applies meta.defaultOptions.
|
@@ -323,6 +538,114 @@ class Config {
|
|
323
538
|
this.rules[ruleId] = ruleConfig;
|
324
539
|
}
|
325
540
|
}
|
541
|
+
|
542
|
+
/**
|
543
|
+
* Validates all of the rule configurations in the given rules config
|
544
|
+
* against the plugins in this instance. This is used primarily to
|
545
|
+
* validate inline configuration rules while inting.
|
546
|
+
* @param {Object} rulesConfig The rules config to validate.
|
547
|
+
* @returns {void}
|
548
|
+
* @throws {Error} If a rule's configuration does not match its schema.
|
549
|
+
* @throws {TypeError} If the rulesConfig is not provided or is invalid.
|
550
|
+
* @throws {InvalidRuleOptionsSchemaError} If a rule's `meta.schema` is invalid.
|
551
|
+
* @throws {TypeError} If a rule is not found in the plugins.
|
552
|
+
*/
|
553
|
+
validateRulesConfig(rulesConfig) {
|
554
|
+
if (!rulesConfig) {
|
555
|
+
throw new TypeError("Config is required for validation.");
|
556
|
+
}
|
557
|
+
|
558
|
+
for (const [ruleId, ruleOptions] of Object.entries(rulesConfig)) {
|
559
|
+
// check for edge case
|
560
|
+
if (ruleId === "__proto__") {
|
561
|
+
continue;
|
562
|
+
}
|
563
|
+
|
564
|
+
/*
|
565
|
+
* If a rule is disabled, we don't do any validation. This allows
|
566
|
+
* users to safely set any value to 0 or "off" without worrying
|
567
|
+
* that it will cause a validation error.
|
568
|
+
*
|
569
|
+
* Note: ruleOptions is always an array at this point because
|
570
|
+
* this validation occurs after FlatConfigArray has merged and
|
571
|
+
* normalized values.
|
572
|
+
*/
|
573
|
+
if (ruleOptions[0] === 0) {
|
574
|
+
continue;
|
575
|
+
}
|
576
|
+
|
577
|
+
const rule = getRuleFromConfig(ruleId, this);
|
578
|
+
|
579
|
+
if (!rule) {
|
580
|
+
throwRuleNotFoundError(parseRuleId(ruleId), this);
|
581
|
+
}
|
582
|
+
|
583
|
+
const validateRule = getOrCreateValidator(rule, ruleId);
|
584
|
+
|
585
|
+
if (validateRule) {
|
586
|
+
validateRule(ruleOptions.slice(1));
|
587
|
+
|
588
|
+
if (validateRule.errors) {
|
589
|
+
throw new Error(
|
590
|
+
`Key "rules": Key "${ruleId}":\n${validateRule.errors
|
591
|
+
.map(error => {
|
592
|
+
if (
|
593
|
+
error.keyword === "additionalProperties" &&
|
594
|
+
error.schema === false &&
|
595
|
+
typeof error.parentSchema?.properties ===
|
596
|
+
"object" &&
|
597
|
+
typeof error.params?.additionalProperty ===
|
598
|
+
"string"
|
599
|
+
) {
|
600
|
+
const expectedProperties = Object.keys(
|
601
|
+
error.parentSchema.properties,
|
602
|
+
).map(property => `"${property}"`);
|
603
|
+
|
604
|
+
return `\tValue ${JSON.stringify(error.data)} ${error.message}.\n\t\tUnexpected property "${error.params.additionalProperty}". Expected properties: ${expectedProperties.join(", ")}.\n`;
|
605
|
+
}
|
606
|
+
|
607
|
+
return `\tValue ${JSON.stringify(error.data)} ${error.message}.\n`;
|
608
|
+
})
|
609
|
+
.join("")}`,
|
610
|
+
);
|
611
|
+
}
|
612
|
+
}
|
613
|
+
}
|
614
|
+
}
|
615
|
+
|
616
|
+
/**
|
617
|
+
* Gets a complete options schema for a rule.
|
618
|
+
* @param {RuleDefinition} ruleDefinition A rule definition object.
|
619
|
+
* @throws {TypeError} If `meta.schema` is specified but is not an array, object or `false`.
|
620
|
+
* @returns {Object|null} JSON Schema for the rule's options. `null` if `meta.schema` is `false`.
|
621
|
+
*/
|
622
|
+
static getRuleOptionsSchema(ruleDefinition) {
|
623
|
+
return getRuleOptionsSchema(ruleDefinition);
|
624
|
+
}
|
625
|
+
|
626
|
+
/**
|
627
|
+
* Normalizes the severity value of a rule's configuration to a number
|
628
|
+
* @param {(number|string|[number, ...*]|[string, ...*])} ruleConfig A rule's configuration value, generally
|
629
|
+
* received from the user. A valid config value is either 0, 1, 2, the string "off" (treated the same as 0),
|
630
|
+
* the string "warn" (treated the same as 1), the string "error" (treated the same as 2), or an array
|
631
|
+
* whose first element is one of the above values. Strings are matched case-insensitively.
|
632
|
+
* @returns {(0|1|2)} The numeric severity value if the config value was valid, otherwise 0.
|
633
|
+
*/
|
634
|
+
static getRuleNumericSeverity(ruleConfig) {
|
635
|
+
const severityValue = Array.isArray(ruleConfig)
|
636
|
+
? ruleConfig[0]
|
637
|
+
: ruleConfig;
|
638
|
+
|
639
|
+
if (severities.has(severityValue)) {
|
640
|
+
return severities.get(severityValue);
|
641
|
+
}
|
642
|
+
|
643
|
+
if (typeof severityValue === "string") {
|
644
|
+
return severities.get(severityValue.toLowerCase()) ?? 0;
|
645
|
+
}
|
646
|
+
|
647
|
+
return 0;
|
648
|
+
}
|
326
649
|
}
|
327
650
|
|
328
651
|
module.exports = { Config };
|
@@ -30,10 +30,12 @@ const MINIMATCH_OPTIONS = { dot: true };
|
|
30
30
|
|
31
31
|
/**
|
32
32
|
* @import { ESLintOptions } from "./eslint.js";
|
33
|
-
* @import { LintMessage, LintResult } from "../shared/types.js";
|
34
33
|
* @import { ConfigLoader, LegacyConfigLoader } from "../config/config-loader.js";
|
35
34
|
*/
|
36
35
|
|
36
|
+
/** @typedef {import("../types").Linter.LintMessage} LintMessage */
|
37
|
+
/** @typedef {import("../types").ESLint.LintResult} LintResult */
|
38
|
+
|
37
39
|
/**
|
38
40
|
* @typedef {Object} GlobSearch
|
39
41
|
* @property {Array<string>} patterns The normalized patterns to use for a search.
|
package/lib/eslint/eslint.js
CHANGED
@@ -14,7 +14,6 @@ const { existsSync } = require("node:fs");
|
|
14
14
|
const path = require("node:path");
|
15
15
|
const { version } = require("../../package.json");
|
16
16
|
const { Linter } = require("../linter");
|
17
|
-
const { getRuleFromConfig } = require("../config/flat-config-helpers");
|
18
17
|
const { defaultConfig } = require("../config/default-config");
|
19
18
|
const {
|
20
19
|
Legacy: {
|
@@ -57,16 +56,21 @@ const { ConfigLoader, LegacyConfigLoader } = require("../config/config-loader");
|
|
57
56
|
* @import { CLIEngineLintReport } from "./legacy-eslint.js";
|
58
57
|
* @import { FlatConfigArray } from "../config/flat-config-array.js";
|
59
58
|
* @import { RuleDefinition } from "@eslint/core";
|
60
|
-
* @import { ConfigData, DeprecatedRuleInfo, LintMessage, LintResult, Plugin, ResultsMeta } from "../shared/types.js";
|
61
59
|
*/
|
62
60
|
|
63
61
|
/** @typedef {ReturnType<ConfigArray.extractConfig>} ExtractedConfig */
|
62
|
+
/** @typedef {import("../types").Linter.Config} Config */
|
63
|
+
/** @typedef {import("../types").ESLint.DeprecatedRuleUse} DeprecatedRuleInfo */
|
64
|
+
/** @typedef {import("../types").Linter.LintMessage} LintMessage */
|
65
|
+
/** @typedef {import("../types").ESLint.LintResult} LintResult */
|
66
|
+
/** @typedef {import("../types").ESLint.Plugin} Plugin */
|
67
|
+
/** @typedef {import("../types").ESLint.ResultsMeta} ResultsMeta */
|
64
68
|
|
65
69
|
/**
|
66
70
|
* The options with which to configure the ESLint instance.
|
67
71
|
* @typedef {Object} ESLintOptions
|
68
72
|
* @property {boolean} [allowInlineConfig] Enable or disable inline configuration comments.
|
69
|
-
* @property {
|
73
|
+
* @property {Config|Array<Config>} [baseConfig] Base config, extended by all configs used with this instance
|
70
74
|
* @property {boolean} [cache] Enable result caching.
|
71
75
|
* @property {string} [cacheLocation] The cache file to use instead of .eslintcache.
|
72
76
|
* @property {"metadata" | "content"} [cacheStrategy] The strategy used to detect changed files.
|
@@ -78,7 +82,7 @@ const { ConfigLoader, LegacyConfigLoader } = require("../config/config-loader");
|
|
78
82
|
* @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.
|
79
83
|
* @property {boolean} [ignore] False disables all ignore patterns except for the default ones.
|
80
84
|
* @property {string[]} [ignorePatterns] Ignore file patterns to use in addition to config ignores. These patterns are relative to `cwd`.
|
81
|
-
* @property {
|
85
|
+
* @property {Config|Array<Config>} [overrideConfig] Override config, overrides all configs used with this instance
|
82
86
|
* @property {boolean|string} [overrideConfigFile] Searches for default config file when falsy;
|
83
87
|
* doesn't do any config file lookup when `true`; considered to be a config filename
|
84
88
|
* when a string.
|
@@ -158,7 +162,7 @@ function getOrFindUsedDeprecatedRules(eslint, maybeFilePath) {
|
|
158
162
|
if (getRuleSeverity(ruleConf) === 0) {
|
159
163
|
continue;
|
160
164
|
}
|
161
|
-
const rule =
|
165
|
+
const rule = config.getRuleDefinition(ruleId);
|
162
166
|
const meta = rule && rule.meta;
|
163
167
|
|
164
168
|
if (meta && meta.deprecated) {
|
@@ -352,7 +356,7 @@ function shouldMessageBeFixed(message, config, fixTypes) {
|
|
352
356
|
return fixTypes.has("directive");
|
353
357
|
}
|
354
358
|
|
355
|
-
const rule = message.ruleId &&
|
359
|
+
const rule = message.ruleId && config.getRuleDefinition(message.ruleId);
|
356
360
|
|
357
361
|
return Boolean(rule && rule.meta && fixTypes.has(rule.meta.type));
|
358
362
|
}
|
@@ -386,6 +390,20 @@ function getFixerForFixTypes(fix, fixTypesSet, config) {
|
|
386
390
|
originalFix(message);
|
387
391
|
}
|
388
392
|
|
393
|
+
/**
|
394
|
+
* Retrieves flags from the environment variable ESLINT_FLAGS.
|
395
|
+
* @param {string[]} flags The flags defined via the API.
|
396
|
+
* @returns {string[]} The merged flags to use.
|
397
|
+
*/
|
398
|
+
function mergeEnvironmentFlags(flags) {
|
399
|
+
if (!process.env.ESLINT_FLAGS) {
|
400
|
+
return flags;
|
401
|
+
}
|
402
|
+
|
403
|
+
const envFlags = process.env.ESLINT_FLAGS.trim().split(/\s*,\s*/gu);
|
404
|
+
return Array.from(new Set([...envFlags, ...flags]));
|
405
|
+
}
|
406
|
+
|
389
407
|
//-----------------------------------------------------------------------------
|
390
408
|
// Main API
|
391
409
|
//-----------------------------------------------------------------------------
|
@@ -416,7 +434,7 @@ class ESLint {
|
|
416
434
|
const linter = new Linter({
|
417
435
|
cwd: processedOptions.cwd,
|
418
436
|
configType: "flat",
|
419
|
-
flags: processedOptions.flags,
|
437
|
+
flags: mergeEnvironmentFlags(processedOptions.flags),
|
420
438
|
});
|
421
439
|
|
422
440
|
const cacheFilePath = getCacheFile(
|
@@ -610,7 +628,7 @@ class ESLint {
|
|
610
628
|
if (!config) {
|
611
629
|
throw createExtraneousResultsError();
|
612
630
|
}
|
613
|
-
const rule =
|
631
|
+
const rule = config.getRuleDefinition(ruleId);
|
614
632
|
|
615
633
|
// ignore unknown rules
|
616
634
|
if (rule) {
|
@@ -703,15 +721,11 @@ class ESLint {
|
|
703
721
|
debug(`Deleting cache file at ${cacheFilePath}`);
|
704
722
|
|
705
723
|
try {
|
706
|
-
|
724
|
+
if (existsSync(cacheFilePath)) {
|
725
|
+
await fs.unlink(cacheFilePath);
|
726
|
+
}
|
707
727
|
} catch (error) {
|
708
|
-
|
709
|
-
|
710
|
-
// Ignore errors when no such file exists or file system is read only (and cache file does not exist)
|
711
|
-
if (
|
712
|
-
errorCode !== "ENOENT" &&
|
713
|
-
!(errorCode === "EROFS" && !existsSync(cacheFilePath))
|
714
|
-
) {
|
728
|
+
if (existsSync(cacheFilePath)) {
|
715
729
|
throw error;
|
716
730
|
}
|
717
731
|
}
|
@@ -1071,7 +1085,7 @@ class ESLint {
|
|
1071
1085
|
* This is the same logic used by the ESLint CLI executable to determine
|
1072
1086
|
* configuration for each file it processes.
|
1073
1087
|
* @param {string} filePath The path of the file to retrieve a config object for.
|
1074
|
-
* @returns {Promise<
|
1088
|
+
* @returns {Promise<Config|undefined>} A configuration object for the file
|
1075
1089
|
* or `undefined` if there is no configuration data for the object.
|
1076
1090
|
*/
|
1077
1091
|
async calculateConfigForFile(filePath) {
|
@@ -30,14 +30,14 @@ const { version } = require("../../package.json");
|
|
30
30
|
//------------------------------------------------------------------------------
|
31
31
|
|
32
32
|
/** @typedef {import("../cli-engine/cli-engine").LintReport} CLIEngineLintReport */
|
33
|
-
/** @typedef {import("../
|
34
|
-
/** @typedef {import("../
|
35
|
-
/** @typedef {import("../
|
36
|
-
/** @typedef {import("../
|
37
|
-
/** @typedef {import("../
|
33
|
+
/** @typedef {import("../types").ESLint.ConfigData} ConfigData */
|
34
|
+
/** @typedef {import("../types").ESLint.DeprecatedRuleUse} DeprecatedRuleInfo */
|
35
|
+
/** @typedef {import("../types").Linter.LintMessage} LintMessage */
|
36
|
+
/** @typedef {import("../types").ESLint.LintResult} LintResult */
|
37
|
+
/** @typedef {import("../types").ESLint.Plugin} Plugin */
|
38
|
+
/** @typedef {import("../types").ESLint.ResultsMeta} ResultsMeta */
|
38
39
|
/** @typedef {import("../types").Rule.RuleModule} Rule */
|
39
|
-
/** @typedef {import("../
|
40
|
-
/** @typedef {import("../shared/types").ResultsMeta} ResultsMeta */
|
40
|
+
/** @typedef {import("../types").Linter.SuppressedLintMessage} SuppressedLintMessage */
|
41
41
|
|
42
42
|
/**
|
43
43
|
* The main formatter object.
|
@@ -25,6 +25,7 @@ const { LATEST_ECMA_VERSION } = require("../../../conf/ecma-version");
|
|
25
25
|
/** @typedef {import("@eslint/core").File} File */
|
26
26
|
/** @typedef {import("@eslint/core").Language} Language */
|
27
27
|
/** @typedef {import("@eslint/core").OkParseResult} OkParseResult */
|
28
|
+
/** @typedef {import("../../types").Linter.LanguageOptions} JSLanguageOptions */
|
28
29
|
|
29
30
|
//-----------------------------------------------------------------------------
|
30
31
|
// Helpers
|
@@ -37,7 +38,7 @@ const parserSymbol = Symbol.for("eslint.RuleTester.parser");
|
|
37
38
|
/**
|
38
39
|
* Analyze scope of the given AST.
|
39
40
|
* @param {ASTNode} ast The `Program` node to analyze.
|
40
|
-
* @param {
|
41
|
+
* @param {JSLanguageOptions} languageOptions The parser options.
|
41
42
|
* @param {Record<string, string[]>} visitorKeys The visitor keys.
|
42
43
|
* @returns {ScopeManager} The analysis result.
|
43
44
|
*/
|
@@ -230,7 +231,7 @@ module.exports = {
|
|
230
231
|
* Parses the given file into an AST.
|
231
232
|
* @param {File} file The virtual file to parse.
|
232
233
|
* @param {Object} options Additional options passed from ESLint.
|
233
|
-
* @param {
|
234
|
+
* @param {JSLanguageOptions} options.languageOptions The language options.
|
234
235
|
* @returns {Object} The result of parsing.
|
235
236
|
*/
|
236
237
|
parse(file, { languageOptions }) {
|
@@ -309,7 +310,7 @@ module.exports = {
|
|
309
310
|
* @param {File} file The virtual file to create a `SourceCode` object from.
|
310
311
|
* @param {OkParseResult} parseResult The result returned from `parse()`.
|
311
312
|
* @param {Object} options Additional options passed from ESLint.
|
312
|
-
* @param {
|
313
|
+
* @param {JSLanguageOptions} options.languageOptions The language options.
|
313
314
|
* @returns {SourceCode} The new `SourceCode` object.
|
314
315
|
*/
|
315
316
|
createSourceCode(file, parseResult, { languageOptions }) {
|
@@ -53,7 +53,7 @@ const CODE_PATH_EVENTS = [
|
|
53
53
|
/**
|
54
54
|
* Validates that the given AST has the required information.
|
55
55
|
* @param {ASTNode} ast The Program node of the AST to check.
|
56
|
-
* @throws {
|
56
|
+
* @throws {TypeError} If the AST doesn't contain the correct information.
|
57
57
|
* @returns {void}
|
58
58
|
* @private
|
59
59
|
*/
|
@@ -147,8 +147,8 @@ function sortedMerge(tokens, comments) {
|
|
147
147
|
* Normalizes a value for a global in a config
|
148
148
|
* @param {(boolean|string|null)} configuredValue The value given for a global in configuration or in
|
149
149
|
* a global directive comment
|
150
|
-
* @returns {("
|
151
|
-
* @throws Error if global value is invalid
|
150
|
+
* @returns {("readonly"|"writable"|"off")} The value normalized as a string
|
151
|
+
* @throws {Error} if global value is invalid
|
152
152
|
*/
|
153
153
|
function normalizeConfigGlobal(configuredValue) {
|
154
154
|
switch (configuredValue) {
|
@@ -471,6 +471,10 @@ class SourceCode extends TokenStore {
|
|
471
471
|
* @type {string[]}
|
472
472
|
*/
|
473
473
|
this.lines = [];
|
474
|
+
|
475
|
+
/**
|
476
|
+
* @type {number[]}
|
477
|
+
*/
|
474
478
|
this.lineStartIndices = [0];
|
475
479
|
|
476
480
|
const lineEndingPattern = astUtils.createGlobalLinebreakMatcher();
|
@@ -528,7 +532,7 @@ class SourceCode extends TokenStore {
|
|
528
532
|
|
529
533
|
/**
|
530
534
|
* Gets the entire source text split into an array of lines.
|
531
|
-
* @returns {
|
535
|
+
* @returns {string[]} The source text as an array of lines.
|
532
536
|
* @public
|
533
537
|
*/
|
534
538
|
getLines() {
|
@@ -687,8 +691,8 @@ class SourceCode extends TokenStore {
|
|
687
691
|
/**
|
688
692
|
* Converts a source text index into a (line, column) pair.
|
689
693
|
* @param {number} index The index of a character in a file
|
690
|
-
* @throws {TypeError} If non-numeric index or index out of range.
|
691
|
-
* @returns {
|
694
|
+
* @throws {TypeError|RangeError} If non-numeric index or index out of range.
|
695
|
+
* @returns {{line: number, column: number}} A {line, column} location object with a 0-indexed column
|
692
696
|
* @public
|
693
697
|
*/
|
694
698
|
getLocFromIndex(index) {
|
@@ -9,7 +9,7 @@
|
|
9
9
|
// Typedefs
|
10
10
|
//------------------------------------------------------------------------------
|
11
11
|
|
12
|
-
/** @typedef {import("../
|
12
|
+
/** @typedef {import("../types").Linter.LintMessage} LintMessage */
|
13
13
|
/** @typedef {import("@eslint/core").Language} Language */
|
14
14
|
/** @typedef {import("@eslint/core").Position} Position */
|
15
15
|
/** @typedef {import("@eslint/core").RulesConfig} RulesConfig */
|