eslint 9.9.0 → 9.10.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 +43 -38
- package/lib/config/config.js +278 -0
- package/lib/config/flat-config-array.js +3 -204
- package/lib/eslint/eslint.js +24 -11
- package/lib/languages/js/source-code/source-code.js +29 -94
- package/lib/linter/apply-disable-directives.js +17 -28
- package/lib/linter/file-context.js +134 -0
- package/lib/linter/linter.js +49 -87
- package/lib/rules/id-length.js +1 -0
- package/lib/rules/no-invalid-regexp.js +34 -18
- package/lib/rules/require-unicode-regexp.js +95 -14
- package/lib/rules/utils/regular-expressions.js +11 -3
- package/lib/services/parser-service.js +65 -0
- package/lib/types/index.d.ts +1635 -0
- package/lib/types/rules/best-practices.d.ts +1075 -0
- package/lib/types/rules/deprecated.d.ts +294 -0
- package/lib/types/rules/ecmascript-6.d.ts +561 -0
- package/lib/types/rules/index.d.ts +50 -0
- package/lib/types/rules/node-commonjs.d.ts +160 -0
- package/lib/types/rules/possible-errors.d.ts +598 -0
- package/lib/types/rules/strict-mode.d.ts +38 -0
- package/lib/types/rules/stylistic-issues.d.ts +1932 -0
- package/lib/types/rules/variables.d.ts +221 -0
- package/lib/types/use-at-your-own-risk.d.ts +85 -0
- package/package.json +34 -23
- package/lib/linter/config-comment-parser.js +0 -169
package/README.md
CHANGED
@@ -21,25 +21,26 @@
|
|
21
21
|
|
22
22
|
ESLint is a tool for identifying and reporting on patterns found in ECMAScript/JavaScript code. In many ways, it is similar to JSLint and JSHint with a few exceptions:
|
23
23
|
|
24
|
-
* ESLint uses [Espree](https://github.com/eslint/espree) for JavaScript parsing.
|
24
|
+
* ESLint uses [Espree](https://github.com/eslint/js/tree/main/packages/espree) for JavaScript parsing.
|
25
25
|
* ESLint uses an AST to evaluate patterns in code.
|
26
26
|
* ESLint is completely pluggable, every single rule is a plugin and you can add more at runtime.
|
27
27
|
|
28
28
|
## Table of Contents
|
29
29
|
|
30
30
|
1. [Installation and Usage](#installation-and-usage)
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
31
|
+
1. [Configuration](#configuration)
|
32
|
+
1. [Version Support](#version-support)
|
33
|
+
1. [Code of Conduct](#code-of-conduct)
|
34
|
+
1. [Filing Issues](#filing-issues)
|
35
|
+
1. [Frequently Asked Questions](#frequently-asked-questions)
|
36
|
+
1. [Releases](#releases)
|
37
|
+
1. [Security Policy](#security-policy)
|
38
|
+
1. [Semantic Versioning Policy](#semantic-versioning-policy)
|
39
|
+
1. [Stylistic Rule Updates](#stylistic-rule-updates)
|
40
|
+
1. [License](#license)
|
41
|
+
1. [Team](#team)
|
42
|
+
1. [Sponsors](#sponsors)
|
43
|
+
1. [Technology Sponsors](#technology-sponsors) <!-- markdownlint-disable-line MD051 -->
|
43
44
|
|
44
45
|
## Installation and Usage
|
45
46
|
|
@@ -54,19 +55,26 @@ npm init @eslint/config@latest
|
|
54
55
|
After that, you can run ESLint on any file or directory like this:
|
55
56
|
|
56
57
|
```shell
|
57
|
-
|
58
|
+
npx eslint yourfile.js
|
58
59
|
```
|
59
60
|
|
60
61
|
## Configuration
|
61
62
|
|
62
|
-
|
63
|
+
You can configure rules in your `eslint.config.js` files as in this example:
|
63
64
|
|
64
65
|
```js
|
65
|
-
|
66
|
-
|
66
|
+
export default [
|
67
|
+
{
|
68
|
+
files: ["**/*.js", "**/*.cjs", "**/*.mjs"],
|
69
|
+
rules: {
|
70
|
+
"prefer-const": "warn",
|
71
|
+
"no-constant-binary-expression": "error"
|
72
|
+
}
|
73
|
+
}
|
74
|
+
];
|
67
75
|
```
|
68
76
|
|
69
|
-
The names `"
|
77
|
+
The names `"prefer-const"` and `"no-constant-binary-expression"` are the names of [rules](https://eslint.org/docs/rules) in ESLint. The first value is the error level of the rule and can be one of these values:
|
70
78
|
|
71
79
|
* `"off"` or `0` - turn the rule off
|
72
80
|
* `"warn"` or `1` - turn the rule on as a warning (doesn't affect exit code)
|
@@ -74,9 +82,17 @@ The names `"semi"` and `"quotes"` are the names of [rules](https://eslint.org/do
|
|
74
82
|
|
75
83
|
The three error levels allow you fine-grained control over how ESLint applies rules (for more configuration options and details, see the [configuration docs](https://eslint.org/docs/latest/use/configure)).
|
76
84
|
|
85
|
+
## Version Support
|
86
|
+
|
87
|
+
The ESLint team provides ongoing support for the current version and six months of limited support for the previous version. Limited support includes critical bug fixes, security issues, and compatibility issues only.
|
88
|
+
|
89
|
+
ESLint offers commercial support for both current and previous versions through our partners, [Tidelift][tidelift] and [HeroDevs][herodevs].
|
90
|
+
|
91
|
+
See [Version Support](https://eslint.org/version-support) for more details.
|
92
|
+
|
77
93
|
## Code of Conduct
|
78
94
|
|
79
|
-
ESLint adheres to the [
|
95
|
+
ESLint adheres to the [OpenJS Foundation Code of Conduct](https://eslint.org/conduct).
|
80
96
|
|
81
97
|
## Filing Issues
|
82
98
|
|
@@ -89,28 +105,14 @@ Before filing an issue, please be sure to read the guidelines for what you're re
|
|
89
105
|
|
90
106
|
## Frequently Asked Questions
|
91
107
|
|
92
|
-
###
|
93
|
-
|
94
|
-
Yes. [JSCS has reached end of life](https://eslint.org/blog/2016/07/jscs-end-of-life) and is no longer supported.
|
95
|
-
|
96
|
-
We have prepared a [migration guide](https://eslint.org/docs/latest/use/migrating-from-jscs) to help you convert your JSCS settings to an ESLint configuration.
|
108
|
+
### Does ESLint support JSX?
|
97
109
|
|
98
|
-
|
110
|
+
Yes, ESLint natively supports parsing JSX syntax (this must be enabled in [configuration](https://eslint.org/docs/latest/use/configure)). Please note that supporting JSX syntax *is not* the same as supporting React. React applies specific semantics to JSX syntax that ESLint doesn't recognize. We recommend using [eslint-plugin-react](https://www.npmjs.com/package/eslint-plugin-react) if you are using React and want React semantics.
|
99
111
|
|
100
112
|
### Does Prettier replace ESLint?
|
101
113
|
|
102
114
|
No, ESLint and Prettier have different jobs: ESLint is a linter (looking for problematic patterns) and Prettier is a code formatter. Using both tools is common, refer to [Prettier's documentation](https://prettier.io/docs/en/install#eslint-and-other-linters) to learn how to configure them to work well with each other.
|
103
115
|
|
104
|
-
### Why can't ESLint find my plugins?
|
105
|
-
|
106
|
-
* Make sure your plugins (and ESLint) are both in your project's `package.json` as devDependencies (or dependencies, if your project uses ESLint at runtime).
|
107
|
-
* Make sure you have run `npm install` and all your dependencies are installed.
|
108
|
-
* Make sure your plugins' peerDependencies have been installed as well. You can use `npm view eslint-plugin-myplugin peerDependencies` to see what peer dependencies `eslint-plugin-myplugin` has.
|
109
|
-
|
110
|
-
### Does ESLint support JSX?
|
111
|
-
|
112
|
-
Yes, ESLint natively supports parsing JSX syntax (this must be enabled in [configuration](https://eslint.org/docs/latest/use/configure)). Please note that supporting JSX syntax *is not* the same as supporting React. React applies specific semantics to JSX syntax that ESLint doesn't recognize. We recommend using [eslint-plugin-react](https://www.npmjs.com/package/eslint-plugin-react) if you are using React and want React semantics.
|
113
|
-
|
114
116
|
### What ECMAScript versions does ESLint support?
|
115
117
|
|
116
118
|
ESLint has full support for ECMAScript 3, 5, and every year from 2015 up until the most recent stage 4 specification (the default). You can set your desired ECMAScript syntax and other settings (like global variables) through [configuration](https://eslint.org/docs/latest/use/configure).
|
@@ -295,12 +297,15 @@ The following companies, organizations, and individuals support ESLint's ongoing
|
|
295
297
|
<!--sponsorsstart-->
|
296
298
|
<h3>Platinum Sponsors</h3>
|
297
299
|
<p><a href="https://automattic.com"><img src="https://images.opencollective.com/automattic/d0ef3e1/logo.png" alt="Automattic" height="128"></a> <a href="https://www.airbnb.com/"><img src="https://images.opencollective.com/airbnb/d327d66/logo.png" alt="Airbnb" height="128"></a></p><h3>Gold Sponsors</h3>
|
298
|
-
<p><a href="
|
300
|
+
<p><a href="https://trunk.io/"><img src="https://images.opencollective.com/trunkio/fb92d60/avatar.png" alt="trunk.io" height="96"></a> <a href="https://opensource.siemens.com"><img src="https://avatars.githubusercontent.com/u/624020?v=4" alt="Siemens" height="96"></a></p><h3>Silver Sponsors</h3>
|
299
301
|
<p><a href="https://www.jetbrains.com/"><img src="https://images.opencollective.com/jetbrains/fe76f99/logo.png" alt="JetBrains" height="64"></a> <a href="https://liftoff.io/"><img src="https://images.opencollective.com/liftoff/5c4fa84/logo.png" alt="Liftoff" height="64"></a> <a href="https://americanexpress.io"><img src="https://avatars.githubusercontent.com/u/3853301?v=4" alt="American Express" height="64"></a> <a href="https://www.workleap.com"><img src="https://avatars.githubusercontent.com/u/53535748?u=d1e55d7661d724bf2281c1bfd33cb8f99fe2465f&v=4" alt="Workleap" height="64"></a></p><h3>Bronze Sponsors</h3>
|
300
|
-
<p><a href="https://www.
|
302
|
+
<p><a href="https://www.crosswordsolver.org/anagram-solver/"><img src="https://images.opencollective.com/anagram-solver/2666271/logo.png" alt="Anagram Solver" height="32"></a> <a href="https://icons8.com/"><img src="https://images.opencollective.com/icons8/7fa1641/logo.png" alt="Icons8" height="32"></a> <a href="https://discord.com"><img src="https://images.opencollective.com/discordapp/f9645d9/logo.png" alt="Discord" height="32"></a> <a href="https://www.ignitionapp.com"><img src="https://avatars.githubusercontent.com/u/5753491?v=4" alt="Ignition" height="32"></a> <a href="https://nx.dev"><img src="https://avatars.githubusercontent.com/u/23692104?v=4" alt="Nx" height="32"></a> <a href="https://herocoders.com"><img src="https://avatars.githubusercontent.com/u/37549774?v=4" alt="HeroCoders" height="32"></a> <a href="https://usenextbase.com"><img src="https://avatars.githubusercontent.com/u/145838380?v=4" alt="Nextbase Starter Kit" height="32"></a></p>
|
301
303
|
<!--sponsorsend-->
|
302
304
|
|
303
305
|
<!--techsponsorsstart-->
|
304
306
|
<h2>Technology Sponsors</h2>
|
305
307
|
<p><a href="https://netlify.com"><img src="https://raw.githubusercontent.com/eslint/eslint.org/main/src/assets/images/techsponsors/netlify-icon.svg" alt="Netlify" height="32"></a> <a href="https://algolia.com"><img src="https://raw.githubusercontent.com/eslint/eslint.org/main/src/assets/images/techsponsors/algolia-icon.svg" alt="Algolia" height="32"></a> <a href="https://1password.com"><img src="https://raw.githubusercontent.com/eslint/eslint.org/main/src/assets/images/techsponsors/1password-icon.svg" alt="1Password" height="32"></a></p>
|
306
|
-
<!--techsponsorsend-->
|
308
|
+
<!--techsponsorsend-->
|
309
|
+
|
310
|
+
[tidelift]: https://tidelift.com/funding/github/npm/eslint
|
311
|
+
[herodevs]: https://www.herodevs.com/support/eslint-nes?utm_source=ESLintWebsite&utm_medium=ESLintWebsite&utm_campaign=ESLintNES&utm_id=ESLintNES
|
@@ -0,0 +1,278 @@
|
|
1
|
+
/**
|
2
|
+
* @fileoverview The `Config` class
|
3
|
+
* @author Nicholas C. Zakas
|
4
|
+
*/
|
5
|
+
|
6
|
+
"use strict";
|
7
|
+
|
8
|
+
//-----------------------------------------------------------------------------
|
9
|
+
// Requirements
|
10
|
+
//-----------------------------------------------------------------------------
|
11
|
+
|
12
|
+
const { RuleValidator } = require("./rule-validator");
|
13
|
+
const { flatConfigSchema, hasMethod } = require("./flat-config-schema");
|
14
|
+
const { ObjectSchema } = require("@eslint/config-array");
|
15
|
+
|
16
|
+
//-----------------------------------------------------------------------------
|
17
|
+
// Helpers
|
18
|
+
//-----------------------------------------------------------------------------
|
19
|
+
|
20
|
+
const ruleValidator = new RuleValidator();
|
21
|
+
|
22
|
+
const severities = new Map([
|
23
|
+
[0, 0],
|
24
|
+
[1, 1],
|
25
|
+
[2, 2],
|
26
|
+
["off", 0],
|
27
|
+
["warn", 1],
|
28
|
+
["error", 2]
|
29
|
+
]);
|
30
|
+
|
31
|
+
/**
|
32
|
+
* Splits a plugin identifier in the form a/b/c into two parts: a/b and c.
|
33
|
+
* @param {string} identifier The identifier to parse.
|
34
|
+
* @returns {{objectName: string, pluginName: string}} The parts of the plugin
|
35
|
+
* name.
|
36
|
+
*/
|
37
|
+
function splitPluginIdentifier(identifier) {
|
38
|
+
const parts = identifier.split("/");
|
39
|
+
|
40
|
+
return {
|
41
|
+
objectName: parts.pop(),
|
42
|
+
pluginName: parts.join("/")
|
43
|
+
};
|
44
|
+
}
|
45
|
+
|
46
|
+
/**
|
47
|
+
* Returns the name of an object in the config by reading its `meta` key.
|
48
|
+
* @param {Object} object The object to check.
|
49
|
+
* @returns {string?} The name of the object if found or `null` if there
|
50
|
+
* is no name.
|
51
|
+
*/
|
52
|
+
function getObjectId(object) {
|
53
|
+
|
54
|
+
// first check old-style name
|
55
|
+
let name = object.name;
|
56
|
+
|
57
|
+
if (!name) {
|
58
|
+
|
59
|
+
if (!object.meta) {
|
60
|
+
return null;
|
61
|
+
}
|
62
|
+
|
63
|
+
name = object.meta.name;
|
64
|
+
|
65
|
+
if (!name) {
|
66
|
+
return null;
|
67
|
+
}
|
68
|
+
}
|
69
|
+
|
70
|
+
// now check for old-style version
|
71
|
+
let version = object.version;
|
72
|
+
|
73
|
+
if (!version) {
|
74
|
+
version = object.meta && object.meta.version;
|
75
|
+
}
|
76
|
+
|
77
|
+
// if there's a version then append that
|
78
|
+
if (version) {
|
79
|
+
return `${name}@${version}`;
|
80
|
+
}
|
81
|
+
|
82
|
+
return name;
|
83
|
+
}
|
84
|
+
|
85
|
+
/**
|
86
|
+
* Converts a languageOptions object to a JSON representation.
|
87
|
+
* @param {Record<string, any>} languageOptions The options to create a JSON
|
88
|
+
* representation of.
|
89
|
+
* @param {string} objectKey The key of the object being converted.
|
90
|
+
* @returns {Record<string, any>} The JSON representation of the languageOptions.
|
91
|
+
* @throws {TypeError} If a function is found in the languageOptions.
|
92
|
+
*/
|
93
|
+
function languageOptionsToJSON(languageOptions, objectKey = "languageOptions") {
|
94
|
+
|
95
|
+
const result = {};
|
96
|
+
|
97
|
+
for (const [key, value] of Object.entries(languageOptions)) {
|
98
|
+
if (value) {
|
99
|
+
if (typeof value === "object") {
|
100
|
+
const name = getObjectId(value);
|
101
|
+
|
102
|
+
if (name && hasMethod(value)) {
|
103
|
+
result[key] = name;
|
104
|
+
} else {
|
105
|
+
result[key] = languageOptionsToJSON(value, key);
|
106
|
+
}
|
107
|
+
continue;
|
108
|
+
}
|
109
|
+
|
110
|
+
if (typeof value === "function") {
|
111
|
+
throw new TypeError(`Cannot serialize key "${key}" in ${objectKey}: Function values are not supported.`);
|
112
|
+
}
|
113
|
+
|
114
|
+
}
|
115
|
+
|
116
|
+
result[key] = value;
|
117
|
+
}
|
118
|
+
|
119
|
+
return result;
|
120
|
+
}
|
121
|
+
|
122
|
+
/**
|
123
|
+
* Normalizes the rules configuration. Ensure that each rule config is
|
124
|
+
* an array and that the severity is a number. This function modifies the
|
125
|
+
* rulesConfig.
|
126
|
+
* @param {Record<string, any>} rulesConfig The rules configuration to normalize.
|
127
|
+
* @returns {void}
|
128
|
+
*/
|
129
|
+
function normalizeRulesConfig(rulesConfig) {
|
130
|
+
|
131
|
+
for (const [ruleId, ruleConfig] of Object.entries(rulesConfig)) {
|
132
|
+
|
133
|
+
// ensure rule config is an array
|
134
|
+
if (!Array.isArray(ruleConfig)) {
|
135
|
+
rulesConfig[ruleId] = [ruleConfig];
|
136
|
+
}
|
137
|
+
|
138
|
+
// normalize severity
|
139
|
+
rulesConfig[ruleId][0] = severities.get(rulesConfig[ruleId][0]);
|
140
|
+
}
|
141
|
+
|
142
|
+
}
|
143
|
+
|
144
|
+
|
145
|
+
//-----------------------------------------------------------------------------
|
146
|
+
// Exports
|
147
|
+
//-----------------------------------------------------------------------------
|
148
|
+
|
149
|
+
/**
|
150
|
+
* Represents a normalized configuration object.
|
151
|
+
*/
|
152
|
+
class Config {
|
153
|
+
|
154
|
+
/**
|
155
|
+
* The name to use for the language when serializing to JSON.
|
156
|
+
* @type {string|undefined}
|
157
|
+
*/
|
158
|
+
#languageName;
|
159
|
+
|
160
|
+
/**
|
161
|
+
* The name to use for the processor when serializing to JSON.
|
162
|
+
* @type {string|undefined}
|
163
|
+
*/
|
164
|
+
#processorName;
|
165
|
+
|
166
|
+
/**
|
167
|
+
* Creates a new instance.
|
168
|
+
* @param {Object} config The configuration object.
|
169
|
+
*/
|
170
|
+
constructor(config) {
|
171
|
+
|
172
|
+
const { plugins, language, languageOptions, processor, ...otherKeys } = config;
|
173
|
+
|
174
|
+
// Validate config object
|
175
|
+
const schema = new ObjectSchema(flatConfigSchema);
|
176
|
+
|
177
|
+
schema.validate(config);
|
178
|
+
|
179
|
+
// first, copy all the other keys over
|
180
|
+
Object.assign(this, otherKeys);
|
181
|
+
|
182
|
+
// ensure that a language is specified
|
183
|
+
if (!language) {
|
184
|
+
throw new TypeError("Key 'language' is required.");
|
185
|
+
}
|
186
|
+
|
187
|
+
// copy the rest over
|
188
|
+
this.plugins = plugins;
|
189
|
+
this.language = language;
|
190
|
+
|
191
|
+
if (languageOptions) {
|
192
|
+
this.languageOptions = languageOptions;
|
193
|
+
}
|
194
|
+
|
195
|
+
// Check language value
|
196
|
+
const { pluginName: languagePluginName, objectName: localLanguageName } = splitPluginIdentifier(language);
|
197
|
+
|
198
|
+
this.#languageName = language;
|
199
|
+
|
200
|
+
if (!plugins || !plugins[languagePluginName] || !plugins[languagePluginName].languages || !plugins[languagePluginName].languages[localLanguageName]) {
|
201
|
+
throw new TypeError(`Key "language": Could not find "${localLanguageName}" in plugin "${languagePluginName}".`);
|
202
|
+
}
|
203
|
+
|
204
|
+
this.language = plugins[languagePluginName].languages[localLanguageName];
|
205
|
+
|
206
|
+
// Validate language options
|
207
|
+
if (this.languageOptions) {
|
208
|
+
try {
|
209
|
+
this.language.validateLanguageOptions(this.languageOptions);
|
210
|
+
} catch (error) {
|
211
|
+
throw new TypeError(`Key "languageOptions": ${error.message}`, { cause: error });
|
212
|
+
}
|
213
|
+
}
|
214
|
+
|
215
|
+
// Check processor value
|
216
|
+
if (processor) {
|
217
|
+
this.processor = processor;
|
218
|
+
|
219
|
+
if (typeof processor === "string") {
|
220
|
+
const { pluginName, objectName: localProcessorName } = splitPluginIdentifier(processor);
|
221
|
+
|
222
|
+
this.#processorName = processor;
|
223
|
+
|
224
|
+
if (!plugins || !plugins[pluginName] || !plugins[pluginName].processors || !plugins[pluginName].processors[localProcessorName]) {
|
225
|
+
throw new TypeError(`Key "processor": Could not find "${localProcessorName}" in plugin "${pluginName}".`);
|
226
|
+
}
|
227
|
+
|
228
|
+
this.processor = plugins[pluginName].processors[localProcessorName];
|
229
|
+
} else if (typeof processor === "object") {
|
230
|
+
this.#processorName = getObjectId(processor);
|
231
|
+
this.processor = processor;
|
232
|
+
} else {
|
233
|
+
throw new TypeError("Key 'processor' must be a string or an object.");
|
234
|
+
}
|
235
|
+
}
|
236
|
+
|
237
|
+
// Process the rules
|
238
|
+
if (this.rules) {
|
239
|
+
normalizeRulesConfig(this.rules);
|
240
|
+
ruleValidator.validate(this);
|
241
|
+
}
|
242
|
+
}
|
243
|
+
|
244
|
+
/**
|
245
|
+
* Converts the configuration to a JSON representation.
|
246
|
+
* @returns {Record<string, any>} The JSON representation of the configuration.
|
247
|
+
* @throws {Error} If the configuration cannot be serialized.
|
248
|
+
*/
|
249
|
+
toJSON() {
|
250
|
+
|
251
|
+
if (this.processor && !this.#processorName) {
|
252
|
+
throw new Error("Could not serialize processor object (missing 'meta' object).");
|
253
|
+
}
|
254
|
+
|
255
|
+
if (!this.#languageName) {
|
256
|
+
throw new Error("Could not serialize language object (missing 'meta' object).");
|
257
|
+
}
|
258
|
+
|
259
|
+
return {
|
260
|
+
...this,
|
261
|
+
plugins: Object.entries(this.plugins).map(([namespace, plugin]) => {
|
262
|
+
|
263
|
+
const pluginId = getObjectId(plugin);
|
264
|
+
|
265
|
+
if (!pluginId) {
|
266
|
+
return namespace;
|
267
|
+
}
|
268
|
+
|
269
|
+
return `${namespace}:${pluginId}`;
|
270
|
+
}),
|
271
|
+
language: this.#languageName,
|
272
|
+
languageOptions: languageOptionsToJSON(this.languageOptions),
|
273
|
+
processor: this.#processorName
|
274
|
+
};
|
275
|
+
}
|
276
|
+
}
|
277
|
+
|
278
|
+
module.exports = { Config };
|
@@ -10,9 +10,9 @@
|
|
10
10
|
//-----------------------------------------------------------------------------
|
11
11
|
|
12
12
|
const { ConfigArray, ConfigArraySymbol } = require("@eslint/config-array");
|
13
|
-
const { flatConfigSchema
|
14
|
-
const { RuleValidator } = require("./rule-validator");
|
13
|
+
const { flatConfigSchema } = require("./flat-config-schema");
|
15
14
|
const { defaultConfig } = require("./default-config");
|
15
|
+
const { Config } = require("./config");
|
16
16
|
|
17
17
|
//-----------------------------------------------------------------------------
|
18
18
|
// Helpers
|
@@ -23,62 +23,6 @@ const { defaultConfig } = require("./default-config");
|
|
23
23
|
*/
|
24
24
|
const META_FIELDS = new Set(["name"]);
|
25
25
|
|
26
|
-
const ruleValidator = new RuleValidator();
|
27
|
-
|
28
|
-
/**
|
29
|
-
* Splits a plugin identifier in the form a/b/c into two parts: a/b and c.
|
30
|
-
* @param {string} identifier The identifier to parse.
|
31
|
-
* @returns {{objectName: string, pluginName: string}} The parts of the plugin
|
32
|
-
* name.
|
33
|
-
*/
|
34
|
-
function splitPluginIdentifier(identifier) {
|
35
|
-
const parts = identifier.split("/");
|
36
|
-
|
37
|
-
return {
|
38
|
-
objectName: parts.pop(),
|
39
|
-
pluginName: parts.join("/")
|
40
|
-
};
|
41
|
-
}
|
42
|
-
|
43
|
-
/**
|
44
|
-
* Returns the name of an object in the config by reading its `meta` key.
|
45
|
-
* @param {Object} object The object to check.
|
46
|
-
* @returns {string?} The name of the object if found or `null` if there
|
47
|
-
* is no name.
|
48
|
-
*/
|
49
|
-
function getObjectId(object) {
|
50
|
-
|
51
|
-
// first check old-style name
|
52
|
-
let name = object.name;
|
53
|
-
|
54
|
-
if (!name) {
|
55
|
-
|
56
|
-
if (!object.meta) {
|
57
|
-
return null;
|
58
|
-
}
|
59
|
-
|
60
|
-
name = object.meta.name;
|
61
|
-
|
62
|
-
if (!name) {
|
63
|
-
return null;
|
64
|
-
}
|
65
|
-
}
|
66
|
-
|
67
|
-
// now check for old-style version
|
68
|
-
let version = object.version;
|
69
|
-
|
70
|
-
if (!version) {
|
71
|
-
version = object.meta && object.meta.version;
|
72
|
-
}
|
73
|
-
|
74
|
-
// if there's a version then append that
|
75
|
-
if (version) {
|
76
|
-
return `${name}@${version}`;
|
77
|
-
}
|
78
|
-
|
79
|
-
return name;
|
80
|
-
}
|
81
|
-
|
82
26
|
/**
|
83
27
|
* Wraps a config error with details about where the error occurred.
|
84
28
|
* @param {Error} error The original error.
|
@@ -123,42 +67,6 @@ function wrapConfigErrorWithDetails(error, originalLength, baseLength) {
|
|
123
67
|
);
|
124
68
|
}
|
125
69
|
|
126
|
-
/**
|
127
|
-
* Converts a languageOptions object to a JSON representation.
|
128
|
-
* @param {Record<string, any>} languageOptions The options to create a JSON
|
129
|
-
* representation of.
|
130
|
-
* @param {string} objectKey The key of the object being converted.
|
131
|
-
* @returns {Record<string, any>} The JSON representation of the languageOptions.
|
132
|
-
* @throws {TypeError} If a function is found in the languageOptions.
|
133
|
-
*/
|
134
|
-
function languageOptionsToJSON(languageOptions, objectKey = "languageOptions") {
|
135
|
-
|
136
|
-
const result = {};
|
137
|
-
|
138
|
-
for (const [key, value] of Object.entries(languageOptions)) {
|
139
|
-
if (value) {
|
140
|
-
if (typeof value === "object") {
|
141
|
-
const name = getObjectId(value);
|
142
|
-
|
143
|
-
if (name && hasMethod(value)) {
|
144
|
-
result[key] = name;
|
145
|
-
} else {
|
146
|
-
result[key] = languageOptionsToJSON(value, key);
|
147
|
-
}
|
148
|
-
continue;
|
149
|
-
}
|
150
|
-
|
151
|
-
if (typeof value === "function") {
|
152
|
-
throw new TypeError(`Cannot serialize key "${key}" in ${objectKey}: Function values are not supported.`);
|
153
|
-
}
|
154
|
-
|
155
|
-
}
|
156
|
-
|
157
|
-
result[key] = value;
|
158
|
-
}
|
159
|
-
|
160
|
-
return result;
|
161
|
-
}
|
162
70
|
|
163
71
|
const originalBaseConfig = Symbol("originalBaseConfig");
|
164
72
|
const originalLength = Symbol("originalLength");
|
@@ -305,116 +213,7 @@ class FlatConfigArray extends ConfigArray {
|
|
305
213
|
* @throws {TypeError} If the config is invalid.
|
306
214
|
*/
|
307
215
|
[ConfigArraySymbol.finalizeConfig](config) {
|
308
|
-
|
309
|
-
const { plugins, language, languageOptions, processor } = config;
|
310
|
-
let parserName, processorName, languageName;
|
311
|
-
let invalidParser = false,
|
312
|
-
invalidProcessor = false,
|
313
|
-
invalidLanguage = false;
|
314
|
-
|
315
|
-
// Check parser value
|
316
|
-
if (languageOptions && languageOptions.parser) {
|
317
|
-
const { parser } = languageOptions;
|
318
|
-
|
319
|
-
if (typeof parser === "object") {
|
320
|
-
parserName = getObjectId(parser);
|
321
|
-
|
322
|
-
if (!parserName) {
|
323
|
-
invalidParser = true;
|
324
|
-
}
|
325
|
-
|
326
|
-
} else {
|
327
|
-
invalidParser = true;
|
328
|
-
}
|
329
|
-
}
|
330
|
-
|
331
|
-
// Check language value
|
332
|
-
if (language) {
|
333
|
-
if (typeof language === "string") {
|
334
|
-
const { pluginName, objectName: localLanguageName } = splitPluginIdentifier(language);
|
335
|
-
|
336
|
-
languageName = language;
|
337
|
-
|
338
|
-
if (!plugins || !plugins[pluginName] || !plugins[pluginName].languages || !plugins[pluginName].languages[localLanguageName]) {
|
339
|
-
throw new TypeError(`Key "language": Could not find "${localLanguageName}" in plugin "${pluginName}".`);
|
340
|
-
}
|
341
|
-
|
342
|
-
config.language = plugins[pluginName].languages[localLanguageName];
|
343
|
-
} else {
|
344
|
-
invalidLanguage = true;
|
345
|
-
}
|
346
|
-
|
347
|
-
try {
|
348
|
-
config.language.validateLanguageOptions(config.languageOptions);
|
349
|
-
} catch (error) {
|
350
|
-
throw new TypeError(`Key "languageOptions": ${error.message}`, { cause: error });
|
351
|
-
}
|
352
|
-
}
|
353
|
-
|
354
|
-
// Check processor value
|
355
|
-
if (processor) {
|
356
|
-
if (typeof processor === "string") {
|
357
|
-
const { pluginName, objectName: localProcessorName } = splitPluginIdentifier(processor);
|
358
|
-
|
359
|
-
processorName = processor;
|
360
|
-
|
361
|
-
if (!plugins || !plugins[pluginName] || !plugins[pluginName].processors || !plugins[pluginName].processors[localProcessorName]) {
|
362
|
-
throw new TypeError(`Key "processor": Could not find "${localProcessorName}" in plugin "${pluginName}".`);
|
363
|
-
}
|
364
|
-
|
365
|
-
config.processor = plugins[pluginName].processors[localProcessorName];
|
366
|
-
} else if (typeof processor === "object") {
|
367
|
-
processorName = getObjectId(processor);
|
368
|
-
|
369
|
-
if (!processorName) {
|
370
|
-
invalidProcessor = true;
|
371
|
-
}
|
372
|
-
|
373
|
-
} else {
|
374
|
-
invalidProcessor = true;
|
375
|
-
}
|
376
|
-
}
|
377
|
-
|
378
|
-
ruleValidator.validate(config);
|
379
|
-
|
380
|
-
// apply special logic for serialization into JSON
|
381
|
-
/* eslint-disable object-shorthand -- shorthand would change "this" value */
|
382
|
-
Object.defineProperty(config, "toJSON", {
|
383
|
-
value: function() {
|
384
|
-
|
385
|
-
if (invalidParser) {
|
386
|
-
throw new Error("Could not serialize parser object (missing 'meta' object).");
|
387
|
-
}
|
388
|
-
|
389
|
-
if (invalidProcessor) {
|
390
|
-
throw new Error("Could not serialize processor object (missing 'meta' object).");
|
391
|
-
}
|
392
|
-
|
393
|
-
if (invalidLanguage) {
|
394
|
-
throw new Error("Caching is not supported when language is an object.");
|
395
|
-
}
|
396
|
-
|
397
|
-
return {
|
398
|
-
...this,
|
399
|
-
plugins: Object.entries(plugins).map(([namespace, plugin]) => {
|
400
|
-
|
401
|
-
const pluginId = getObjectId(plugin);
|
402
|
-
|
403
|
-
if (!pluginId) {
|
404
|
-
return namespace;
|
405
|
-
}
|
406
|
-
|
407
|
-
return `${namespace}:${pluginId}`;
|
408
|
-
}),
|
409
|
-
language: languageName,
|
410
|
-
languageOptions: languageOptionsToJSON(languageOptions),
|
411
|
-
processor: processorName
|
412
|
-
};
|
413
|
-
}
|
414
|
-
});
|
415
|
-
/* eslint-enable object-shorthand -- ok to enable now */
|
416
|
-
|
417
|
-
return config;
|
216
|
+
return new Config(config);
|
418
217
|
}
|
419
218
|
/* eslint-enable class-methods-use-this -- Desired as instance method */
|
420
219
|
|