eslint 7.29.0 → 7.30.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/CHANGELOG.md CHANGED
@@ -1,3 +1,17 @@
1
+ v7.30.0 - July 2, 2021
2
+
3
+ * [`5f74642`](https://github.com/eslint/eslint/commit/5f746420700d457b92dd86659de588d272937b79) Chore: don't check Program.start in SourceCode#getComments (refs #14744) (#14748) (Milos Djermanovic)
4
+ * [`19a871a`](https://github.com/eslint/eslint/commit/19a871a35ae9997ce352624b1081c96c54b73a9f) Docs: Suggest linting plugins for ESLint plugin developers (#14754) (Bryan Mishkin)
5
+ * [`aa87329`](https://github.com/eslint/eslint/commit/aa87329d919f569404ca573b439934552006572f) Docs: fix broken links (#14756) (Sam Chen)
6
+ * [`278813a`](https://github.com/eslint/eslint/commit/278813a6e759f6b5512ac64c7530c9c51732e692) Docs: fix and add more examples for new-cap rule (fixes #12874) (#14725) (Nitin Kumar)
7
+ * [`ed1da5d`](https://github.com/eslint/eslint/commit/ed1da5d96af2587b7211854e45cf8657ef808710) Update: ecmaVersion allows "latest" (#14720) (薛定谔的猫)
8
+ * [`104c0b5`](https://github.com/eslint/eslint/commit/104c0b592f203d315a108d311c58375357e40b24) Update: improve use-isnan rule to detect `Number.NaN` (fixes #14715) (#14718) (Nitin Kumar)
9
+ * [`b08170b`](https://github.com/eslint/eslint/commit/b08170b92beb22db6ec612ebdfff930f9e0582ab) Update: Implement FlatConfigArray (refs #13481) (#14321) (Nicholas C. Zakas)
10
+ * [`f113cdd`](https://github.com/eslint/eslint/commit/f113cdd872257d72bbd66d95e4eaf13623323b24) Chore: upgrade eslint-plugin-eslint-plugin (#14738) (薛定谔的猫)
11
+ * [`1b8997a`](https://github.com/eslint/eslint/commit/1b8997ab63781f4ebf87e3269400b2ef4c7d2973) Docs: Fix getRulesMetaForResults link syntax (#14723) (Brandon Mills)
12
+ * [`aada733`](https://github.com/eslint/eslint/commit/aada733d2aee830aa32cccb9828cd72db4ccd6bd) Docs: fix two broken links (#14726) (Sam Chen)
13
+ * [`8972529`](https://github.com/eslint/eslint/commit/8972529f82d13bd04059ee8852b4ebb9b5350962) Docs: Update README team and sponsors (ESLint Jenkins)
14
+
1
15
  v7.29.0 - June 18, 2021
2
16
 
3
17
  * [`bfbfe5c`](https://github.com/eslint/eslint/commit/bfbfe5c1fd4c39a06d5e159dbe48479ca4305fc0) New: Add only to RuleTester (refs eslint/rfcs#73) (#14677) (Brandon Mills)
package/README.md CHANGED
@@ -268,6 +268,11 @@ Anix
268
268
  <img src="https://github.com/yeonjuan.png?s=75" width="75" height="75"><br />
269
269
  YeonJuan
270
270
  </a>
271
+ </td><td align="center" valign="top" width="11%">
272
+ <a href="https://github.com/snitin315">
273
+ <img src="https://github.com/snitin315.png?s=75" width="75" height="75"><br />
274
+ Nitin Kumar
275
+ </a>
271
276
  </td></tr></tbody></table>
272
277
 
273
278
 
@@ -0,0 +1,52 @@
1
+ /**
2
+ * @fileoverview Default configuration
3
+ * @author Nicholas C. Zakas
4
+ */
5
+
6
+ "use strict";
7
+
8
+ //-----------------------------------------------------------------------------
9
+ // Requirements
10
+ //-----------------------------------------------------------------------------
11
+
12
+ const Rules = require("../rules");
13
+
14
+ //-----------------------------------------------------------------------------
15
+ // Helpers
16
+ //-----------------------------------------------------------------------------
17
+
18
+
19
+ exports.defaultConfig = [
20
+ {
21
+ plugins: {
22
+ "@": {
23
+ parsers: {
24
+ espree: require("espree")
25
+ },
26
+
27
+ /*
28
+ * Because we try to delay loading rules until absolutely
29
+ * necessary, a proxy allows us to hook into the lazy-loading
30
+ * aspect of the rules map while still keeping all of the
31
+ * relevant configuration inside of the config array.
32
+ */
33
+ rules: new Proxy({}, {
34
+ get(target, property) {
35
+ return Rules.get(property);
36
+ },
37
+
38
+ has(target, property) {
39
+ return Rules.has(property);
40
+ }
41
+ })
42
+ }
43
+ },
44
+ ignores: [
45
+ "**/node_modules/**",
46
+ ".git/**"
47
+ ],
48
+ languageOptions: {
49
+ parser: "@/espree"
50
+ }
51
+ }
52
+ ];
@@ -0,0 +1,125 @@
1
+ /**
2
+ * @fileoverview Flat Config Array
3
+ * @author Nicholas C. Zakas
4
+ */
5
+
6
+ "use strict";
7
+
8
+ //-----------------------------------------------------------------------------
9
+ // Requirements
10
+ //-----------------------------------------------------------------------------
11
+
12
+ const { ConfigArray, ConfigArraySymbol } = require("@humanwhocodes/config-array");
13
+ const { flatConfigSchema } = require("./flat-config-schema");
14
+ const { RuleValidator } = require("./rule-validator");
15
+ const { defaultConfig } = require("./default-config");
16
+ const recommendedConfig = require("../../conf/eslint-recommended");
17
+ const allConfig = require("../../conf/eslint-all");
18
+
19
+ //-----------------------------------------------------------------------------
20
+ // Helpers
21
+ //-----------------------------------------------------------------------------
22
+
23
+ const ruleValidator = new RuleValidator();
24
+
25
+ /**
26
+ * Splits a plugin identifier in the form a/b/c into two parts: a/b and c.
27
+ * @param {string} identifier The identifier to parse.
28
+ * @returns {{objectName: string, pluginName: string}} The parts of the plugin
29
+ * name.
30
+ */
31
+ function splitPluginIdentifier(identifier) {
32
+ const parts = identifier.split("/");
33
+
34
+ return {
35
+ objectName: parts.pop(),
36
+ pluginName: parts.join("/")
37
+ };
38
+ }
39
+
40
+ //-----------------------------------------------------------------------------
41
+ // Exports
42
+ //-----------------------------------------------------------------------------
43
+
44
+ /**
45
+ * Represents an array containing configuration information for ESLint.
46
+ */
47
+ class FlatConfigArray extends ConfigArray {
48
+
49
+ /**
50
+ * Creates a new instance.
51
+ * @param {*[]} configs An array of configuration information.
52
+ * @param {{basePath: string, baseConfig: FlatConfig}} options The options
53
+ * to use for the config array instance.
54
+ */
55
+ constructor(configs, { basePath, baseConfig = defaultConfig }) {
56
+ super(configs, {
57
+ basePath,
58
+ schema: flatConfigSchema
59
+ });
60
+
61
+ this.unshift(baseConfig);
62
+ }
63
+
64
+ /* eslint-disable class-methods-use-this */
65
+ /**
66
+ * Replaces a config with another config to allow us to put strings
67
+ * in the config array that will be replaced by objects before
68
+ * normalization.
69
+ * @param {Object} config The config to preprocess.
70
+ * @returns {Object} The preprocessed config.
71
+ */
72
+ [ConfigArraySymbol.preprocessConfig](config) {
73
+ if (config === "eslint:recommended") {
74
+ return recommendedConfig;
75
+ }
76
+
77
+ if (config === "eslint:all") {
78
+ return allConfig;
79
+ }
80
+
81
+ return config;
82
+ }
83
+
84
+ /**
85
+ * Finalizes the config by replacing plugin references with their objects
86
+ * and validating rule option schemas.
87
+ * @param {Object} config The config to finalize.
88
+ * @returns {Object} The finalized config.
89
+ * @throws {TypeError} If the config is invalid.
90
+ */
91
+ [ConfigArraySymbol.finalizeConfig](config) {
92
+
93
+ const { plugins, languageOptions, processor } = config;
94
+
95
+ // Check parser value
96
+ if (languageOptions && languageOptions.parser && typeof languageOptions.parser === "string") {
97
+ const { pluginName, objectName: parserName } = splitPluginIdentifier(languageOptions.parser);
98
+
99
+ if (!plugins || !plugins[pluginName] || !plugins[pluginName].parsers || !plugins[pluginName].parsers[parserName]) {
100
+ throw new TypeError(`Key "parser": Could not find "${parserName}" in plugin "${pluginName}".`);
101
+ }
102
+
103
+ languageOptions.parser = plugins[pluginName].parsers[parserName];
104
+ }
105
+
106
+ // Check processor value
107
+ if (processor && typeof processor === "string") {
108
+ const { pluginName, objectName: processorName } = splitPluginIdentifier(processor);
109
+
110
+ if (!plugins || !plugins[pluginName] || !plugins[pluginName].processors || !plugins[pluginName].processors[processorName]) {
111
+ throw new TypeError(`Key "processor": Could not find "${processorName}" in plugin "${pluginName}".`);
112
+ }
113
+
114
+ config.processor = plugins[pluginName].processors[processorName];
115
+ }
116
+
117
+ ruleValidator.validate(config);
118
+
119
+ return config;
120
+ }
121
+ /* eslint-enable class-methods-use-this */
122
+
123
+ }
124
+
125
+ exports.FlatConfigArray = FlatConfigArray;
@@ -0,0 +1,452 @@
1
+ /**
2
+ * @fileoverview Flat config schema
3
+ * @author Nicholas C. Zakas
4
+ */
5
+
6
+ "use strict";
7
+
8
+ //-----------------------------------------------------------------------------
9
+ // Type Definitions
10
+ //-----------------------------------------------------------------------------
11
+
12
+ /**
13
+ * @typedef ObjectPropertySchema
14
+ * @property {Function|string} merge The function or name of the function to call
15
+ * to merge multiple objects with this property.
16
+ * @property {Function|string} validate The function or name of the function to call
17
+ * to validate the value of this property.
18
+ */
19
+
20
+ //-----------------------------------------------------------------------------
21
+ // Helpers
22
+ //-----------------------------------------------------------------------------
23
+
24
+ const ruleSeverities = new Map([
25
+ [0, 0], ["off", 0],
26
+ [1, 1], ["warn", 1],
27
+ [2, 2], ["error", 2]
28
+ ]);
29
+
30
+ const globalVariablesValues = new Set([
31
+ true, "true", "writable", "writeable",
32
+ false, "false", "readonly", "readable", null,
33
+ "off"
34
+ ]);
35
+
36
+ /**
37
+ * Check if a value is a non-null object.
38
+ * @param {any} value The value to check.
39
+ * @returns {boolean} `true` if the value is a non-null object.
40
+ */
41
+ function isNonNullObject(value) {
42
+ return typeof value === "object" && value !== null;
43
+ }
44
+
45
+ /**
46
+ * Check if a value is undefined.
47
+ * @param {any} value The value to check.
48
+ * @returns {boolean} `true` if the value is undefined.
49
+ */
50
+ function isUndefined(value) {
51
+ return typeof value === "undefined";
52
+ }
53
+
54
+ /**
55
+ * Deeply merges two objects.
56
+ * @param {Object} first The base object.
57
+ * @param {Object} second The overrides object.
58
+ * @returns {Object} An object with properties from both first and second.
59
+ */
60
+ function deepMerge(first = {}, second = {}) {
61
+
62
+ /*
63
+ * If the second value is an array, just return it. We don't merge
64
+ * arrays because order matters and we can't know the correct order.
65
+ */
66
+ if (Array.isArray(second)) {
67
+ return second;
68
+ }
69
+
70
+ /*
71
+ * First create a result object where properties from the second object
72
+ * overwrite properties from the first. This sets up a baseline to use
73
+ * later rather than needing to inspect and change every property
74
+ * individually.
75
+ */
76
+ const result = {
77
+ ...first,
78
+ ...second
79
+ };
80
+
81
+ for (const key of Object.keys(second)) {
82
+
83
+ // avoid hairy edge case
84
+ if (key === "__proto__") {
85
+ continue;
86
+ }
87
+
88
+ const firstValue = first[key];
89
+ const secondValue = second[key];
90
+
91
+ if (isNonNullObject(firstValue)) {
92
+ result[key] = deepMerge(firstValue, secondValue);
93
+ } else if (isUndefined(firstValue)) {
94
+ if (isNonNullObject(secondValue)) {
95
+ result[key] = deepMerge(
96
+ Array.isArray(secondValue) ? [] : {},
97
+ secondValue
98
+ );
99
+ } else if (!isUndefined(secondValue)) {
100
+ result[key] = secondValue;
101
+ }
102
+ }
103
+ }
104
+
105
+ return result;
106
+
107
+ }
108
+
109
+ /**
110
+ * Normalizes the rule options config for a given rule by ensuring that
111
+ * it is an array and that the first item is 0, 1, or 2.
112
+ * @param {Array|string|number} ruleOptions The rule options config.
113
+ * @returns {Array} An array of rule options.
114
+ */
115
+ function normalizeRuleOptions(ruleOptions) {
116
+
117
+ const finalOptions = Array.isArray(ruleOptions)
118
+ ? ruleOptions.slice(0)
119
+ : [ruleOptions];
120
+
121
+ finalOptions[0] = ruleSeverities.get(finalOptions[0]);
122
+ return finalOptions;
123
+ }
124
+
125
+ //-----------------------------------------------------------------------------
126
+ // Assertions
127
+ //-----------------------------------------------------------------------------
128
+
129
+ /**
130
+ * Validates that a value is a valid rule options entry.
131
+ * @param {any} value The value to check.
132
+ * @returns {void}
133
+ * @throws {TypeError} If the value isn't a valid rule options.
134
+ */
135
+ function assertIsRuleOptions(value) {
136
+
137
+ if (typeof value !== "string" && typeof value !== "number" && !Array.isArray(value)) {
138
+ throw new TypeError("Expected a string, number, or array.");
139
+ }
140
+ }
141
+
142
+ /**
143
+ * Validates that a value is valid rule severity.
144
+ * @param {any} value The value to check.
145
+ * @returns {void}
146
+ * @throws {TypeError} If the value isn't a valid rule severity.
147
+ */
148
+ function assertIsRuleSeverity(value) {
149
+ const severity = typeof value === "string"
150
+ ? ruleSeverities.get(value.toLowerCase())
151
+ : ruleSeverities.get(value);
152
+
153
+ if (typeof severity === "undefined") {
154
+ throw new TypeError("Expected severity of \"off\", 0, \"warn\", 1, \"error\", or 2.");
155
+ }
156
+ }
157
+
158
+ /**
159
+ * Validates that a given string is the form pluginName/objectName.
160
+ * @param {string} value The string to check.
161
+ * @returns {void}
162
+ * @throws {TypeError} If the string isn't in the correct format.
163
+ */
164
+ function assertIsPluginMemberName(value) {
165
+ if (!/[@a-z0-9-_$]+(?:\/(?:[a-z0-9-_$]+))+$/iu.test(value)) {
166
+ throw new TypeError(`Expected string in the form "pluginName/objectName" but found "${value}".`);
167
+ }
168
+ }
169
+
170
+ /**
171
+ * Validates that a value is an object.
172
+ * @param {any} value The value to check.
173
+ * @returns {void}
174
+ * @throws {TypeError} If the value isn't an object.
175
+ */
176
+ function assertIsObject(value) {
177
+ if (!isNonNullObject(value)) {
178
+ throw new TypeError("Expected an object.");
179
+ }
180
+ }
181
+
182
+ /**
183
+ * Validates that a value is an object or a string.
184
+ * @param {any} value The value to check.
185
+ * @returns {void}
186
+ * @throws {TypeError} If the value isn't an object or a string.
187
+ */
188
+ function assertIsObjectOrString(value) {
189
+ if ((!value || typeof value !== "object") && typeof value !== "string") {
190
+ throw new TypeError("Expected an object or string.");
191
+ }
192
+ }
193
+
194
+ //-----------------------------------------------------------------------------
195
+ // Low-Level Schemas
196
+ //-----------------------------------------------------------------------------
197
+
198
+
199
+ /** @type {ObjectPropertySchema} */
200
+ const numberSchema = {
201
+ merge: "replace",
202
+ validate: "number"
203
+ };
204
+
205
+ /** @type {ObjectPropertySchema} */
206
+ const booleanSchema = {
207
+ merge: "replace",
208
+ validate: "boolean"
209
+ };
210
+
211
+ /** @type {ObjectPropertySchema} */
212
+ const deepObjectAssignSchema = {
213
+ merge(first = {}, second = {}) {
214
+ return deepMerge(first, second);
215
+ },
216
+ validate: "object"
217
+ };
218
+
219
+ //-----------------------------------------------------------------------------
220
+ // High-Level Schemas
221
+ //-----------------------------------------------------------------------------
222
+
223
+ /** @type {ObjectPropertySchema} */
224
+ const globalsSchema = {
225
+ merge: "assign",
226
+ validate(value) {
227
+
228
+ assertIsObject(value);
229
+
230
+ for (const key of Object.keys(value)) {
231
+
232
+ // avoid hairy edge case
233
+ if (key === "__proto__") {
234
+ continue;
235
+ }
236
+
237
+ if (key !== key.trim()) {
238
+ throw new TypeError(`Global "${key}" has leading or trailing whitespace.`);
239
+ }
240
+
241
+ if (!globalVariablesValues.has(value[key])) {
242
+ throw new TypeError(`Key "${key}": Expected "readonly", "writable", or "off".`);
243
+ }
244
+ }
245
+ }
246
+ };
247
+
248
+ /** @type {ObjectPropertySchema} */
249
+ const parserSchema = {
250
+ merge: "replace",
251
+ validate(value) {
252
+ assertIsObjectOrString(value);
253
+
254
+ if (typeof value === "object" && typeof value.parse !== "function" && typeof value.parseForESLint !== "function") {
255
+ throw new TypeError("Expected object to have a parse() or parseForESLint() method.");
256
+ }
257
+
258
+ if (typeof value === "string") {
259
+ assertIsPluginMemberName(value);
260
+ }
261
+ }
262
+ };
263
+
264
+ /** @type {ObjectPropertySchema} */
265
+ const pluginsSchema = {
266
+ merge(first = {}, second = {}) {
267
+ const keys = new Set([...Object.keys(first), ...Object.keys(second)]);
268
+ const result = {};
269
+
270
+ // manually validate that plugins are not redefined
271
+ for (const key of keys) {
272
+
273
+ // avoid hairy edge case
274
+ if (key === "__proto__") {
275
+ continue;
276
+ }
277
+
278
+ if (key in first && key in second && first[key] !== second[key]) {
279
+ throw new TypeError(`Cannot redefine plugin "${key}".`);
280
+ }
281
+
282
+ result[key] = second[key] || first[key];
283
+ }
284
+
285
+ return result;
286
+ },
287
+ validate(value) {
288
+
289
+ // first check the value to be sure it's an object
290
+ if (value === null || typeof value !== "object") {
291
+ throw new TypeError("Expected an object.");
292
+ }
293
+
294
+ // second check the keys to make sure they are objects
295
+ for (const key of Object.keys(value)) {
296
+
297
+ // avoid hairy edge case
298
+ if (key === "__proto__") {
299
+ continue;
300
+ }
301
+
302
+ if (value[key] === null || typeof value[key] !== "object") {
303
+ throw new TypeError(`Key "${key}": Expected an object.`);
304
+ }
305
+ }
306
+ }
307
+ };
308
+
309
+ /** @type {ObjectPropertySchema} */
310
+ const processorSchema = {
311
+ merge: "replace",
312
+ validate(value) {
313
+ if (typeof value === "string") {
314
+ assertIsPluginMemberName(value);
315
+ } else if (value && typeof value === "object") {
316
+ if (typeof value.preprocess !== "function" || typeof value.postprocess !== "function") {
317
+ throw new TypeError("Object must have a preprocess() and a postprocess() method.");
318
+ }
319
+ } else {
320
+ throw new TypeError("Expected an object or a string.");
321
+ }
322
+ }
323
+ };
324
+
325
+ /** @type {ObjectPropertySchema} */
326
+ const rulesSchema = {
327
+ merge(first = {}, second = {}) {
328
+
329
+ const result = {
330
+ ...first,
331
+ ...second
332
+ };
333
+
334
+ for (const ruleId of Object.keys(result)) {
335
+
336
+ // avoid hairy edge case
337
+ if (ruleId === "__proto__") {
338
+
339
+ /* eslint-disable-next-line no-proto */
340
+ delete result.__proto__;
341
+ continue;
342
+ }
343
+
344
+ result[ruleId] = normalizeRuleOptions(result[ruleId]);
345
+
346
+ /*
347
+ * If either rule config is missing, then the correct
348
+ * config is already present and we just need to normalize
349
+ * the severity.
350
+ */
351
+ if (!(ruleId in first) || !(ruleId in second)) {
352
+ continue;
353
+ }
354
+
355
+ const firstRuleOptions = normalizeRuleOptions(first[ruleId]);
356
+ const secondRuleOptions = normalizeRuleOptions(second[ruleId]);
357
+
358
+ /*
359
+ * If the second rule config only has a severity (length of 1),
360
+ * then use that severity and keep the rest of the options from
361
+ * the first rule config.
362
+ */
363
+ if (secondRuleOptions.length === 1) {
364
+ result[ruleId] = [secondRuleOptions[0], ...firstRuleOptions.slice(1)];
365
+ continue;
366
+ }
367
+
368
+ /*
369
+ * In any other situation, then the second rule config takes
370
+ * precedence. That means the value at `result[ruleId]` is
371
+ * already correct and no further work is necessary.
372
+ */
373
+ }
374
+
375
+ return result;
376
+ },
377
+
378
+ validate(value) {
379
+ assertIsObject(value);
380
+
381
+ let lastRuleId;
382
+
383
+ // Performance: One try-catch has less overhead than one per loop iteration
384
+ try {
385
+
386
+ /*
387
+ * We are not checking the rule schema here because there is no
388
+ * guarantee that the rule definition is present at this point. Instead
389
+ * we wait and check the rule schema during the finalization step
390
+ * of calculating a config.
391
+ */
392
+ for (const ruleId of Object.keys(value)) {
393
+
394
+ // avoid hairy edge case
395
+ if (ruleId === "__proto__") {
396
+ continue;
397
+ }
398
+
399
+ lastRuleId = ruleId;
400
+
401
+ const ruleOptions = value[ruleId];
402
+
403
+ assertIsRuleOptions(ruleOptions);
404
+
405
+ if (Array.isArray(ruleOptions)) {
406
+ assertIsRuleSeverity(ruleOptions[0]);
407
+ } else {
408
+ assertIsRuleSeverity(ruleOptions);
409
+ }
410
+ }
411
+ } catch (error) {
412
+ error.message = `Key "${lastRuleId}": ${error.message}`;
413
+ throw error;
414
+ }
415
+ }
416
+ };
417
+
418
+ /** @type {ObjectPropertySchema} */
419
+ const sourceTypeSchema = {
420
+ merge: "replace",
421
+ validate(value) {
422
+ if (typeof value !== "string" || !/^(?:script|module|commonjs)$/u.test(value)) {
423
+ throw new TypeError("Expected \"script\", \"module\", or \"commonjs\".");
424
+ }
425
+ }
426
+ };
427
+
428
+ //-----------------------------------------------------------------------------
429
+ // Full schema
430
+ //-----------------------------------------------------------------------------
431
+
432
+ exports.flatConfigSchema = {
433
+ settings: deepObjectAssignSchema,
434
+ linterOptions: {
435
+ schema: {
436
+ noInlineConfig: booleanSchema,
437
+ reportUnusedDisableDirectives: booleanSchema
438
+ }
439
+ },
440
+ languageOptions: {
441
+ schema: {
442
+ ecmaVersion: numberSchema,
443
+ sourceType: sourceTypeSchema,
444
+ globals: globalsSchema,
445
+ parser: parserSchema,
446
+ parserOptions: deepObjectAssignSchema
447
+ }
448
+ },
449
+ processor: processorSchema,
450
+ plugins: pluginsSchema,
451
+ rules: rulesSchema
452
+ };
@@ -0,0 +1,169 @@
1
+ /**
2
+ * @fileoverview Rule Validator
3
+ * @author Nicholas C. Zakas
4
+ */
5
+
6
+ "use strict";
7
+
8
+ //-----------------------------------------------------------------------------
9
+ // Requirements
10
+ //-----------------------------------------------------------------------------
11
+
12
+ const ajv = require("../shared/ajv")();
13
+
14
+ //-----------------------------------------------------------------------------
15
+ // Helpers
16
+ //-----------------------------------------------------------------------------
17
+
18
+ /**
19
+ * Finds a rule with the given ID in the given config.
20
+ * @param {string} ruleId The ID of the rule to find.
21
+ * @param {Object} config The config to search in.
22
+ * @returns {{create: Function, schema: (Array|null)}} THe rule object.
23
+ */
24
+ function findRuleDefinition(ruleId, config) {
25
+ const ruleIdParts = ruleId.split("/");
26
+ let pluginName, ruleName;
27
+
28
+ // built-in rule
29
+ if (ruleIdParts.length === 1) {
30
+ pluginName = "@";
31
+ ruleName = ruleIdParts[0];
32
+ } else {
33
+ ruleName = ruleIdParts.pop();
34
+ pluginName = ruleIdParts.join("/");
35
+ }
36
+
37
+ if (!config.plugins || !config.plugins[pluginName]) {
38
+ throw new TypeError(`Key "rules": Key "${ruleId}": Could not find plugin "${pluginName}".`);
39
+ }
40
+
41
+ if (!config.plugins[pluginName].rules || !config.plugins[pluginName].rules[ruleName]) {
42
+ throw new TypeError(`Key "rules": Key "${ruleId}": Could not find "${ruleName}" in plugin "${pluginName}".`);
43
+ }
44
+
45
+ return config.plugins[pluginName].rules[ruleName];
46
+
47
+ }
48
+
49
+ /**
50
+ * Gets a complete options schema for a rule.
51
+ * @param {{create: Function, schema: (Array|null)}} rule A new-style rule object
52
+ * @returns {Object} JSON Schema for the rule's options.
53
+ */
54
+ function getRuleOptionsSchema(rule) {
55
+
56
+ if (!rule) {
57
+ return null;
58
+ }
59
+
60
+ const schema = rule.schema || rule.meta && rule.meta.schema;
61
+
62
+ if (Array.isArray(schema)) {
63
+ if (schema.length) {
64
+ return {
65
+ type: "array",
66
+ items: schema,
67
+ minItems: 0,
68
+ maxItems: schema.length
69
+ };
70
+ }
71
+ return {
72
+ type: "array",
73
+ minItems: 0,
74
+ maxItems: 0
75
+ };
76
+
77
+ }
78
+
79
+ // Given a full schema, leave it alone
80
+ return schema || null;
81
+ }
82
+
83
+ //-----------------------------------------------------------------------------
84
+ // Exports
85
+ //-----------------------------------------------------------------------------
86
+
87
+ /**
88
+ * Implements validation functionality for the rules portion of a config.
89
+ */
90
+ class RuleValidator {
91
+
92
+ /**
93
+ * Creates a new instance.
94
+ */
95
+ constructor() {
96
+
97
+ /**
98
+ * A collection of compiled validators for rules that have already
99
+ * been validated.
100
+ * @type {WeakMap}
101
+ * @property validators
102
+ */
103
+ this.validators = new WeakMap();
104
+ }
105
+
106
+ /**
107
+ * Validates all of the rule configurations in a config against each
108
+ * rule's schema.
109
+ * @param {Object} config The full config to validate. This object must
110
+ * contain both the rules section and the plugins section.
111
+ * @returns {void}
112
+ * @throws {Error} If a rule's configuration does not match its schema.
113
+ */
114
+ validate(config) {
115
+
116
+ if (!config.rules) {
117
+ return;
118
+ }
119
+
120
+ for (const [ruleId, ruleOptions] of Object.entries(config.rules)) {
121
+
122
+ // check for edge case
123
+ if (ruleId === "__proto__") {
124
+ continue;
125
+ }
126
+
127
+ /*
128
+ * If a rule is disabled, we don't do any validation. This allows
129
+ * users to safely set any value to 0 or "off" without worrying
130
+ * that it will cause a validation error.
131
+ *
132
+ * Note: ruleOptions is always an array at this point because
133
+ * this validation occurs after FlatConfigArray has merged and
134
+ * normalized values.
135
+ */
136
+ if (ruleOptions[0] === 0) {
137
+ continue;
138
+ }
139
+
140
+ const rule = findRuleDefinition(ruleId, config);
141
+
142
+ // Precompile and cache validator the first time
143
+ if (!this.validators.has(rule)) {
144
+ const schema = getRuleOptionsSchema(rule);
145
+
146
+ if (schema) {
147
+ this.validators.set(rule, ajv.compile(schema));
148
+ }
149
+ }
150
+
151
+ const validateRule = this.validators.get(rule);
152
+
153
+ if (validateRule) {
154
+
155
+ validateRule(ruleOptions.slice(1));
156
+
157
+ if (validateRule.errors) {
158
+ throw new Error(`Key "rules": Key "${ruleId}": ${
159
+ validateRule.errors.map(
160
+ error => `\tValue ${JSON.stringify(error.data)} ${error.message}.\n`
161
+ ).join("")
162
+ }`);
163
+ }
164
+ }
165
+ }
166
+ }
167
+ }
168
+
169
+ exports.RuleValidator = RuleValidator;
@@ -37,8 +37,10 @@ const
37
37
  const debug = require("debug")("eslint:linter");
38
38
  const MAX_AUTOFIX_PASSES = 10;
39
39
  const DEFAULT_PARSER_NAME = "espree";
40
+ const DEFAULT_ECMA_VERSION = 5;
40
41
  const commentParser = new ConfigCommentParser();
41
42
  const DEFAULT_ERROR_LOC = { start: { line: 1, column: 0 }, end: { line: 1, column: 1 } };
43
+ const parserSymbol = Symbol.for("eslint.RuleTester.parser");
42
44
 
43
45
  //------------------------------------------------------------------------------
44
46
  // Typedefs
@@ -432,10 +434,16 @@ function getDirectiveComments(filename, ast, ruleMapper, warnInlineConfig) {
432
434
 
433
435
  /**
434
436
  * Normalize ECMAScript version from the initial config
435
- * @param {number} ecmaVersion ECMAScript version from the initial config
437
+ * @param {Parser} parser The parser which uses this options.
438
+ * @param {number} ecmaVersion ECMAScript version from the initial config
436
439
  * @returns {number} normalized ECMAScript version
437
440
  */
438
- function normalizeEcmaVersion(ecmaVersion) {
441
+ function normalizeEcmaVersion(parser, ecmaVersion) {
442
+ if ((parser[parserSymbol] || parser) === espree) {
443
+ if (ecmaVersion === "latest") {
444
+ return espree.latestEcmaVersion;
445
+ }
446
+ }
439
447
 
440
448
  /*
441
449
  * Calculate ECMAScript edition number from official year version starting with
@@ -521,12 +529,13 @@ function normalizeVerifyOptions(providedOptions, config) {
521
529
 
522
530
  /**
523
531
  * Combines the provided parserOptions with the options from environments
524
- * @param {string} parserName The parser name which uses this options.
532
+ * @param {Parser} parser The parser which uses this options.
525
533
  * @param {ParserOptions} providedOptions The provided 'parserOptions' key in a config
526
534
  * @param {Environment[]} enabledEnvironments The environments enabled in configuration and with inline comments
527
535
  * @returns {ParserOptions} Resulting parser options after merge
528
536
  */
529
- function resolveParserOptions(parserName, providedOptions, enabledEnvironments) {
537
+ function resolveParserOptions(parser, providedOptions, enabledEnvironments) {
538
+
530
539
  const parserOptionsFromEnv = enabledEnvironments
531
540
  .filter(env => env.parserOptions)
532
541
  .reduce((parserOptions, env) => merge(parserOptions, env.parserOptions), {});
@@ -542,12 +551,7 @@ function resolveParserOptions(parserName, providedOptions, enabledEnvironments)
542
551
  mergedParserOptions.ecmaFeatures = Object.assign({}, mergedParserOptions.ecmaFeatures, { globalReturn: false });
543
552
  }
544
553
 
545
- /*
546
- * TODO: @aladdin-add
547
- * 1. for a 3rd-party parser, do not normalize parserOptions
548
- * 2. for espree, no need to do this (espree will do it)
549
- */
550
- mergedParserOptions.ecmaVersion = normalizeEcmaVersion(mergedParserOptions.ecmaVersion);
554
+ mergedParserOptions.ecmaVersion = normalizeEcmaVersion(parser, mergedParserOptions.ecmaVersion);
551
555
 
552
556
  return mergedParserOptions;
553
557
  }
@@ -606,7 +610,7 @@ function getRuleOptions(ruleConfig) {
606
610
  */
607
611
  function analyzeScope(ast, parserOptions, visitorKeys) {
608
612
  const ecmaFeatures = parserOptions.ecmaFeatures || {};
609
- const ecmaVersion = parserOptions.ecmaVersion || 5;
613
+ const ecmaVersion = parserOptions.ecmaVersion || DEFAULT_ECMA_VERSION;
610
614
 
611
615
  return eslintScope.analyze(ast, {
612
616
  ignoreEval: true,
@@ -1123,7 +1127,7 @@ class Linter {
1123
1127
  .map(envName => getEnv(slots, envName))
1124
1128
  .filter(env => env);
1125
1129
 
1126
- const parserOptions = resolveParserOptions(parserName, config.parserOptions || {}, enabledEnvs);
1130
+ const parserOptions = resolveParserOptions(parser, config.parserOptions || {}, enabledEnvs);
1127
1131
  const configuredGlobals = resolveGlobals(config.globals || {}, enabledEnvs);
1128
1132
  const settings = config.settings || {};
1129
1133
 
@@ -53,6 +53,7 @@ const
53
53
  const ajv = require("../shared/ajv")({ strictDefaults: true });
54
54
 
55
55
  const espreePath = require.resolve("espree");
56
+ const parserSymbol = Symbol.for("eslint.RuleTester.parser");
56
57
 
57
58
  //------------------------------------------------------------------------------
58
59
  // Typedefs
@@ -239,6 +240,7 @@ function defineStartEndAsError(objName, node) {
239
240
  });
240
241
  }
241
242
 
243
+
242
244
  /**
243
245
  * Define `start`/`end` properties of all nodes of the given AST as throwing error.
244
246
  * @param {ASTNode} ast The root node to errorize `start`/`end` properties.
@@ -258,8 +260,10 @@ function defineStartEndAsErrorInTree(ast, visitorKeys) {
258
260
  * @returns {Parser} Wrapped parser object.
259
261
  */
260
262
  function wrapParser(parser) {
263
+
261
264
  if (typeof parser.parseForESLint === "function") {
262
265
  return {
266
+ [parserSymbol]: parser,
263
267
  parseForESLint(...args) {
264
268
  const ret = parser.parseForESLint(...args);
265
269
 
@@ -268,7 +272,9 @@ function wrapParser(parser) {
268
272
  }
269
273
  };
270
274
  }
275
+
271
276
  return {
277
+ [parserSymbol]: parser,
272
278
  parse(...args) {
273
279
  const ast = parser.parse(...args);
274
280
 
@@ -94,7 +94,7 @@ module.exports = {
94
94
 
95
95
  // Don't perform any fixes if there are comments inside the brackets.
96
96
  if (sourceCode.commentsExistBetween(leftBracket, rightBracket)) {
97
- return; // eslint-disable-line eslint-plugin/fixer-return -- false positive
97
+ return;
98
98
  }
99
99
 
100
100
  // Replace the brackets by an identifier.
@@ -154,12 +154,12 @@ module.exports = {
154
154
 
155
155
  // A statement that starts with `let[` is parsed as a destructuring variable declaration, not a MemberExpression.
156
156
  if (node.object.type === "Identifier" && node.object.name === "let" && !node.optional) {
157
- return; // eslint-disable-line eslint-plugin/fixer-return -- false positive
157
+ return;
158
158
  }
159
159
 
160
160
  // Don't perform any fixes if there are comments between the dot and the property name.
161
161
  if (sourceCode.commentsExistBetween(dotToken, node.property)) {
162
- return; // eslint-disable-line eslint-plugin/fixer-return -- false positive
162
+ return;
163
163
  }
164
164
 
165
165
  // Replace the identifier to brackets.
@@ -295,7 +295,7 @@ module.exports = {
295
295
  * If the callback function has duplicates in its list of parameters (possible in sloppy mode),
296
296
  * don't replace it with an arrow function, because this is a SyntaxError with arrow functions.
297
297
  */
298
- return; // eslint-disable-line eslint-plugin/fixer-return -- false positive
298
+ return;
299
299
  }
300
300
 
301
301
  // Remove `.bind(this)` if exists.
@@ -307,7 +307,7 @@ module.exports = {
307
307
  * E.g. `(foo || function(){}).bind(this)`
308
308
  */
309
309
  if (memberNode.type !== "MemberExpression") {
310
- return; // eslint-disable-line eslint-plugin/fixer-return -- false positive
310
+ return;
311
311
  }
312
312
 
313
313
  const callNode = memberNode.parent;
@@ -320,12 +320,12 @@ module.exports = {
320
320
  * ^^^^^^^^^^^^
321
321
  */
322
322
  if (astUtils.isParenthesised(sourceCode, memberNode)) {
323
- return; // eslint-disable-line eslint-plugin/fixer-return -- false positive
323
+ return;
324
324
  }
325
325
 
326
326
  // If comments exist in the `.bind(this)`, don't remove those.
327
327
  if (sourceCode.commentsExistBetween(firstTokenToRemove, lastTokenToRemove)) {
328
- return; // eslint-disable-line eslint-plugin/fixer-return -- false positive
328
+ return;
329
329
  }
330
330
 
331
331
  yield fixer.removeRange([firstTokenToRemove.range[0], lastTokenToRemove.range[1]]);
@@ -21,7 +21,10 @@ const astUtils = require("./utils/ast-utils");
21
21
  * @returns {boolean} `true` if the node is 'NaN' identifier.
22
22
  */
23
23
  function isNaNIdentifier(node) {
24
- return Boolean(node) && node.type === "Identifier" && node.name === "NaN";
24
+ return Boolean(node) && (
25
+ astUtils.isSpecificId(node, "NaN") ||
26
+ astUtils.isSpecificMemberAccess(node, "Number", "NaN")
27
+ );
25
28
  }
26
29
 
27
30
  //------------------------------------------------------------------------------
@@ -349,7 +349,7 @@ class SourceCode extends TokenStore {
349
349
  let currentToken = this.getTokenBefore(node, { includeComments: true });
350
350
 
351
351
  while (currentToken && isCommentToken(currentToken)) {
352
- if (node.parent && (currentToken.start < node.parent.start)) {
352
+ if (node.parent && node.parent.type !== "Program" && (currentToken.start < node.parent.start)) {
353
353
  break;
354
354
  }
355
355
  comments.leading.push(currentToken);
@@ -361,7 +361,7 @@ class SourceCode extends TokenStore {
361
361
  currentToken = this.getTokenAfter(node, { includeComments: true });
362
362
 
363
363
  while (currentToken && isCommentToken(currentToken)) {
364
- if (node.parent && (currentToken.end > node.parent.end)) {
364
+ if (node.parent && node.parent.type !== "Program" && (currentToken.end > node.parent.end)) {
365
365
  break;
366
366
  }
367
367
  comments.trailing.push(currentToken);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "eslint",
3
- "version": "7.29.0",
3
+ "version": "7.30.0",
4
4
  "author": "Nicholas C. Zakas <nicholas+npm@nczconsulting.com>",
5
5
  "description": "An AST-based pattern checker for JavaScript.",
6
6
  "bin": {
@@ -45,6 +45,7 @@
45
45
  "dependencies": {
46
46
  "@babel/code-frame": "7.12.11",
47
47
  "@eslint/eslintrc": "^0.4.2",
48
+ "@humanwhocodes/config-array": "^0.5.0",
48
49
  "ajv": "^6.10.0",
49
50
  "chalk": "^4.0.0",
50
51
  "cross-spawn": "^7.0.2",
@@ -95,7 +96,7 @@
95
96
  "ejs": "^3.0.2",
96
97
  "eslint": "file:.",
97
98
  "eslint-config-eslint": "file:packages/eslint-config-eslint",
98
- "eslint-plugin-eslint-plugin": "^3.0.3",
99
+ "eslint-plugin-eslint-plugin": "^3.2.0",
99
100
  "eslint-plugin-internal-rules": "file:tools/internal-rules",
100
101
  "eslint-plugin-jsdoc": "^25.4.3",
101
102
  "eslint-plugin-node": "^11.1.0",