eslint 7.26.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 +58 -0
- package/README.md +12 -7
- package/bin/eslint.js +2 -12
- package/lib/cli-engine/file-enumerator.js +1 -1
- package/lib/cli-engine/formatters/html.js +193 -9
- package/lib/config/default-config.js +52 -0
- package/lib/config/flat-config-array.js +125 -0
- package/lib/config/flat-config-schema.js +452 -0
- package/lib/config/rule-validator.js +169 -0
- package/lib/eslint/eslint.js +38 -2
- package/lib/init/autoconfig.js +2 -2
- package/lib/init/npm-utils.js +1 -2
- package/lib/linter/apply-disable-directives.js +15 -3
- package/lib/linter/linter.js +31 -21
- package/lib/linter/node-event-generator.js +43 -6
- package/lib/rule-tester/rule-tester.js +89 -23
- package/lib/rules/arrow-body-style.js +21 -11
- package/lib/rules/comma-dangle.js +16 -7
- package/lib/rules/comma-spacing.js +1 -1
- package/lib/rules/comma-style.js +1 -2
- package/lib/rules/complexity.js +2 -3
- package/lib/rules/consistent-return.js +2 -2
- package/lib/rules/dot-notation.js +3 -3
- package/lib/rules/eol-last.js +2 -7
- package/lib/rules/indent.js +10 -13
- package/lib/rules/max-lines-per-function.js +2 -3
- package/lib/rules/max-lines.js +32 -7
- package/lib/rules/max-params.js +2 -3
- package/lib/rules/max-statements.js +2 -3
- package/lib/rules/no-duplicate-imports.js +214 -66
- package/lib/rules/no-fallthrough.js +18 -13
- package/lib/rules/no-implicit-coercion.js +21 -2
- package/lib/rules/no-restricted-imports.js +61 -24
- package/lib/rules/no-unused-vars.js +40 -10
- package/lib/rules/no-useless-backreference.js +1 -2
- package/lib/rules/no-useless-computed-key.js +8 -2
- package/lib/rules/no-warning-comments.js +1 -1
- package/lib/rules/object-curly-newline.js +19 -4
- package/lib/rules/prefer-arrow-callback.js +4 -4
- package/lib/rules/spaced-comment.js +2 -2
- package/lib/rules/use-isnan.js +4 -1
- package/lib/rules/utils/ast-utils.js +2 -2
- package/lib/shared/deprecation-warnings.js +12 -3
- package/lib/shared/string-utils.js +22 -0
- package/lib/source-code/source-code.js +8 -7
- package/lib/source-code/token-store/utils.js +4 -12
- package/messages/{all-files-ignored.txt → all-files-ignored.js} +10 -2
- package/messages/extend-config-missing.js +13 -0
- package/messages/failed-to-read-json.js +11 -0
- package/messages/file-not-found.js +10 -0
- package/messages/{no-config-found.txt → no-config-found.js} +9 -1
- package/messages/plugin-conflict.js +22 -0
- package/messages/plugin-invalid.js +16 -0
- package/messages/plugin-missing.js +19 -0
- package/messages/{print-config-with-directory-path.txt → print-config-with-directory-path.js} +6 -0
- package/messages/whitespace-found.js +11 -0
- package/package.json +9 -7
- package/lib/cli-engine/formatters/html-template-message.html +0 -8
- package/lib/cli-engine/formatters/html-template-page.html +0 -115
- package/lib/cli-engine/formatters/html-template-result.html +0 -6
- package/messages/extend-config-missing.txt +0 -5
- package/messages/failed-to-read-json.txt +0 -3
- package/messages/file-not-found.txt +0 -2
- package/messages/plugin-conflict.txt +0 -7
- package/messages/plugin-invalid.txt +0 -8
- package/messages/plugin-missing.txt +0 -11
- package/messages/whitespace-found.txt +0 -3
@@ -5,8 +5,6 @@
|
|
5
5
|
|
6
6
|
"use strict";
|
7
7
|
|
8
|
-
const lodash = require("lodash");
|
9
|
-
|
10
8
|
/**
|
11
9
|
* Compares the locations of two objects in a source file
|
12
10
|
* @param {{line: number, column: number}} itemA The first object
|
@@ -124,7 +122,21 @@ module.exports = ({ directives, problems, reportUnusedDisableDirectives = "off"
|
|
124
122
|
.map(directive => Object.assign({}, directive, { unprocessedDirective: directive }))
|
125
123
|
.sort(compareLocations);
|
126
124
|
|
127
|
-
|
125
|
+
/**
|
126
|
+
* Returns a new array formed by applying a given callback function to each element of the array, and then flattening the result by one level.
|
127
|
+
* TODO(stephenwade): Replace this with array.flatMap when we drop support for Node v10
|
128
|
+
* @param {any[]} array The array to process
|
129
|
+
* @param {Function} fn The function to use
|
130
|
+
* @returns {any[]} The result array
|
131
|
+
*/
|
132
|
+
function flatMap(array, fn) {
|
133
|
+
const mapped = array.map(fn);
|
134
|
+
const flattened = [].concat(...mapped);
|
135
|
+
|
136
|
+
return flattened;
|
137
|
+
}
|
138
|
+
|
139
|
+
const lineDirectives = flatMap(directives, directive => {
|
128
140
|
switch (directive.type) {
|
129
141
|
case "disable":
|
130
142
|
case "enable":
|
package/lib/linter/linter.js
CHANGED
@@ -15,7 +15,7 @@ const
|
|
15
15
|
eslintScope = require("eslint-scope"),
|
16
16
|
evk = require("eslint-visitor-keys"),
|
17
17
|
espree = require("espree"),
|
18
|
-
|
18
|
+
merge = require("lodash.merge"),
|
19
19
|
BuiltInEnvironments = require("@eslint/eslintrc/conf/environments"),
|
20
20
|
pkg = require("../../package.json"),
|
21
21
|
astUtils = require("../shared/ast-utils"),
|
@@ -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
|
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
|
@@ -444,7 +452,7 @@ function normalizeEcmaVersion(ecmaVersion) {
|
|
444
452
|
return ecmaVersion >= 2015 ? ecmaVersion - 2009 : ecmaVersion;
|
445
453
|
}
|
446
454
|
|
447
|
-
const eslintEnvPattern = /\/\*\s*eslint-env\s(.+?)\*\//
|
455
|
+
const eslintEnvPattern = /\/\*\s*eslint-env\s(.+?)\*\//gsu;
|
448
456
|
|
449
457
|
/**
|
450
458
|
* Checks whether or not there is a comment which has "eslint-env *" in a given text.
|
@@ -521,16 +529,17 @@ function normalizeVerifyOptions(providedOptions, config) {
|
|
521
529
|
|
522
530
|
/**
|
523
531
|
* Combines the provided parserOptions with the options from environments
|
524
|
-
* @param {
|
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(
|
537
|
+
function resolveParserOptions(parser, providedOptions, enabledEnvironments) {
|
538
|
+
|
530
539
|
const parserOptionsFromEnv = enabledEnvironments
|
531
540
|
.filter(env => env.parserOptions)
|
532
|
-
.reduce((parserOptions, env) =>
|
533
|
-
const mergedParserOptions =
|
541
|
+
.reduce((parserOptions, env) => merge(parserOptions, env.parserOptions), {});
|
542
|
+
const mergedParserOptions = merge(parserOptionsFromEnv, providedOptions || {});
|
534
543
|
const isModule = mergedParserOptions.sourceType === "module";
|
535
544
|
|
536
545
|
if (isModule) {
|
@@ -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 ||
|
613
|
+
const ecmaVersion = parserOptions.ecmaVersion || DEFAULT_ECMA_VERSION;
|
610
614
|
|
611
615
|
return eslintScope.analyze(ast, {
|
612
616
|
ignoreEval: true,
|
@@ -828,9 +832,10 @@ const BASE_TRAVERSAL_CONTEXT = Object.freeze(
|
|
828
832
|
* @param {string} filename The reported filename of the code
|
829
833
|
* @param {boolean} disableFixes If true, it doesn't make `fix` properties.
|
830
834
|
* @param {string | undefined} cwd cwd of the cli
|
835
|
+
* @param {string} physicalFilename The full path of the file on disk without any code block information
|
831
836
|
* @returns {Problem[]} An array of reported problems
|
832
837
|
*/
|
833
|
-
function runRules(sourceCode, configuredRules, ruleMapper, parserOptions, parserName, settings, filename, disableFixes, cwd) {
|
838
|
+
function runRules(sourceCode, configuredRules, ruleMapper, parserOptions, parserName, settings, filename, disableFixes, cwd, physicalFilename) {
|
834
839
|
const emitter = createEmitter();
|
835
840
|
const nodeQueue = [];
|
836
841
|
let currentNode = sourceCode.ast;
|
@@ -859,6 +864,7 @@ function runRules(sourceCode, configuredRules, ruleMapper, parserOptions, parser
|
|
859
864
|
getDeclaredVariables: sourceCode.scopeManager.getDeclaredVariables.bind(sourceCode.scopeManager),
|
860
865
|
getCwd: () => cwd,
|
861
866
|
getFilename: () => filename,
|
867
|
+
getPhysicalFilename: () => physicalFilename || filename,
|
862
868
|
getScope: () => getScope(sourceCode.scopeManager, currentNode),
|
863
869
|
getSourceCode: () => sourceCode,
|
864
870
|
markVariableAsUsed: name => markVariableAsUsed(sourceCode.scopeManager, currentNode, parserOptions, name),
|
@@ -1121,7 +1127,7 @@ class Linter {
|
|
1121
1127
|
.map(envName => getEnv(slots, envName))
|
1122
1128
|
.filter(env => env);
|
1123
1129
|
|
1124
|
-
const parserOptions = resolveParserOptions(
|
1130
|
+
const parserOptions = resolveParserOptions(parser, config.parserOptions || {}, enabledEnvs);
|
1125
1131
|
const configuredGlobals = resolveGlobals(config.globals || {}, enabledEnvs);
|
1126
1132
|
const settings = config.settings || {};
|
1127
1133
|
|
@@ -1181,7 +1187,8 @@ class Linter {
|
|
1181
1187
|
settings,
|
1182
1188
|
options.filename,
|
1183
1189
|
options.disableFixes,
|
1184
|
-
slots.cwd
|
1190
|
+
slots.cwd,
|
1191
|
+
providedOptions.physicalFilename
|
1185
1192
|
);
|
1186
1193
|
} catch (err) {
|
1187
1194
|
err.message += `\nOccurred while linting ${options.filename}`;
|
@@ -1284,9 +1291,12 @@ class Linter {
|
|
1284
1291
|
_verifyWithProcessor(textOrSourceCode, config, options, configForRecursive) {
|
1285
1292
|
const filename = options.filename || "<input>";
|
1286
1293
|
const filenameToExpose = normalizeFilename(filename);
|
1294
|
+
const physicalFilename = options.physicalFilename || filenameToExpose;
|
1287
1295
|
const text = ensureText(textOrSourceCode);
|
1288
1296
|
const preprocess = options.preprocess || (rawText => [rawText]);
|
1289
|
-
|
1297
|
+
|
1298
|
+
// TODO(stephenwade): Replace this with array.flat() when we drop support for Node v10
|
1299
|
+
const postprocess = options.postprocess || (array => [].concat(...array));
|
1290
1300
|
const filterCodeBlock =
|
1291
1301
|
options.filterCodeBlock ||
|
1292
1302
|
(blockFilename => blockFilename.endsWith(".js"));
|
@@ -1314,7 +1324,7 @@ class Linter {
|
|
1314
1324
|
return this._verifyWithConfigArray(
|
1315
1325
|
blockText,
|
1316
1326
|
configForRecursive,
|
1317
|
-
{ ...options, filename: blockName }
|
1327
|
+
{ ...options, filename: blockName, physicalFilename }
|
1318
1328
|
);
|
1319
1329
|
}
|
1320
1330
|
|
@@ -1322,7 +1332,7 @@ class Linter {
|
|
1322
1332
|
return this._verifyWithoutProcessors(
|
1323
1333
|
blockText,
|
1324
1334
|
config,
|
1325
|
-
{ ...options, filename: blockName }
|
1335
|
+
{ ...options, filename: blockName, physicalFilename }
|
1326
1336
|
);
|
1327
1337
|
});
|
1328
1338
|
|
@@ -10,7 +10,6 @@
|
|
10
10
|
//------------------------------------------------------------------------------
|
11
11
|
|
12
12
|
const esquery = require("esquery");
|
13
|
-
const lodash = require("lodash");
|
14
13
|
|
15
14
|
//------------------------------------------------------------------------------
|
16
15
|
// Typedefs
|
@@ -32,6 +31,35 @@ const lodash = require("lodash");
|
|
32
31
|
// Helpers
|
33
32
|
//------------------------------------------------------------------------------
|
34
33
|
|
34
|
+
/**
|
35
|
+
* Computes the union of one or more arrays
|
36
|
+
* @param {...any[]} arrays One or more arrays to union
|
37
|
+
* @returns {any[]} The union of the input arrays
|
38
|
+
*/
|
39
|
+
function union(...arrays) {
|
40
|
+
|
41
|
+
// TODO(stephenwade): Replace this with arrays.flat() when we drop support for Node v10
|
42
|
+
return [...new Set([].concat(...arrays))];
|
43
|
+
}
|
44
|
+
|
45
|
+
/**
|
46
|
+
* Computes the intersection of one or more arrays
|
47
|
+
* @param {...any[]} arrays One or more arrays to intersect
|
48
|
+
* @returns {any[]} The intersection of the input arrays
|
49
|
+
*/
|
50
|
+
function intersection(...arrays) {
|
51
|
+
if (arrays.length === 0) {
|
52
|
+
return [];
|
53
|
+
}
|
54
|
+
|
55
|
+
let result = [...new Set(arrays[0])];
|
56
|
+
|
57
|
+
for (const array of arrays.slice(1)) {
|
58
|
+
result = result.filter(x => array.includes(x));
|
59
|
+
}
|
60
|
+
return result;
|
61
|
+
}
|
62
|
+
|
35
63
|
/**
|
36
64
|
* Gets the possible types of a selector
|
37
65
|
* @param {Object} parsedSelector An object (from esquery) describing the matching behavior of the selector
|
@@ -46,7 +74,7 @@ function getPossibleTypes(parsedSelector) {
|
|
46
74
|
const typesForComponents = parsedSelector.selectors.map(getPossibleTypes);
|
47
75
|
|
48
76
|
if (typesForComponents.every(Boolean)) {
|
49
|
-
return
|
77
|
+
return union(...typesForComponents);
|
50
78
|
}
|
51
79
|
return null;
|
52
80
|
}
|
@@ -63,7 +91,7 @@ function getPossibleTypes(parsedSelector) {
|
|
63
91
|
* If at least one of the components could only match a particular type, the compound could only match
|
64
92
|
* the intersection of those types.
|
65
93
|
*/
|
66
|
-
return
|
94
|
+
return intersection(...typesForComponents);
|
67
95
|
}
|
68
96
|
|
69
97
|
case "child":
|
@@ -166,15 +194,21 @@ function tryParseSelector(rawSelector) {
|
|
166
194
|
}
|
167
195
|
}
|
168
196
|
|
197
|
+
const selectorCache = new Map();
|
198
|
+
|
169
199
|
/**
|
170
200
|
* Parses a raw selector string, and returns the parsed selector along with specificity and type information.
|
171
201
|
* @param {string} rawSelector A raw AST selector
|
172
202
|
* @returns {ASTSelector} A selector descriptor
|
173
203
|
*/
|
174
|
-
|
204
|
+
function parseSelector(rawSelector) {
|
205
|
+
if (selectorCache.has(rawSelector)) {
|
206
|
+
return selectorCache.get(rawSelector);
|
207
|
+
}
|
208
|
+
|
175
209
|
const parsedSelector = tryParseSelector(rawSelector);
|
176
210
|
|
177
|
-
|
211
|
+
const result = {
|
178
212
|
rawSelector,
|
179
213
|
isExit: rawSelector.endsWith(":exit"),
|
180
214
|
parsedSelector,
|
@@ -182,7 +216,10 @@ const parseSelector = lodash.memoize(rawSelector => {
|
|
182
216
|
attributeCount: countClassAttributes(parsedSelector),
|
183
217
|
identifierCount: countIdentifiers(parsedSelector)
|
184
218
|
};
|
185
|
-
|
219
|
+
|
220
|
+
selectorCache.set(rawSelector, result);
|
221
|
+
return result;
|
222
|
+
}
|
186
223
|
|
187
224
|
//------------------------------------------------------------------------------
|
188
225
|
// Public Interface
|
@@ -44,7 +44,8 @@ const
|
|
44
44
|
assert = require("assert"),
|
45
45
|
path = require("path"),
|
46
46
|
util = require("util"),
|
47
|
-
|
47
|
+
merge = require("lodash.merge"),
|
48
|
+
equal = require("fast-deep-equal"),
|
48
49
|
Traverser = require("../../lib/shared/traverser"),
|
49
50
|
{ getRuleOptionsSchema, validate } = require("../shared/config-validator"),
|
50
51
|
{ Linter, SourceCodeFixer, interpolate } = require("../linter");
|
@@ -52,6 +53,7 @@ const
|
|
52
53
|
const ajv = require("../shared/ajv")({ strictDefaults: true });
|
53
54
|
|
54
55
|
const espreePath = require.resolve("espree");
|
56
|
+
const parserSymbol = Symbol.for("eslint.RuleTester.parser");
|
55
57
|
|
56
58
|
//------------------------------------------------------------------------------
|
57
59
|
// Typedefs
|
@@ -70,6 +72,7 @@ const espreePath = require.resolve("espree");
|
|
70
72
|
* @property {{ [name: string]: any }} [parserOptions] Options for the parser.
|
71
73
|
* @property {{ [name: string]: "readonly" | "writable" | "off" }} [globals] The additional global variables.
|
72
74
|
* @property {{ [name: string]: boolean }} [env] Environments for the test case.
|
75
|
+
* @property {boolean} [only] Run only this test case or the subset of test cases with this property.
|
73
76
|
*/
|
74
77
|
|
75
78
|
/**
|
@@ -85,6 +88,7 @@ const espreePath = require.resolve("espree");
|
|
85
88
|
* @property {{ [name: string]: any }} [parserOptions] Options for the parser.
|
86
89
|
* @property {{ [name: string]: "readonly" | "writable" | "off" }} [globals] The additional global variables.
|
87
90
|
* @property {{ [name: string]: boolean }} [env] Environments for the test case.
|
91
|
+
* @property {boolean} [only] Run only this test case or the subset of test cases with this property.
|
88
92
|
*/
|
89
93
|
|
90
94
|
/**
|
@@ -120,7 +124,8 @@ const RuleTesterParameters = [
|
|
120
124
|
"filename",
|
121
125
|
"options",
|
122
126
|
"errors",
|
123
|
-
"output"
|
127
|
+
"output",
|
128
|
+
"only"
|
124
129
|
];
|
125
130
|
|
126
131
|
/*
|
@@ -235,6 +240,7 @@ function defineStartEndAsError(objName, node) {
|
|
235
240
|
});
|
236
241
|
}
|
237
242
|
|
243
|
+
|
238
244
|
/**
|
239
245
|
* Define `start`/`end` properties of all nodes of the given AST as throwing error.
|
240
246
|
* @param {ASTNode} ast The root node to errorize `start`/`end` properties.
|
@@ -254,8 +260,10 @@ function defineStartEndAsErrorInTree(ast, visitorKeys) {
|
|
254
260
|
* @returns {Parser} Wrapped parser object.
|
255
261
|
*/
|
256
262
|
function wrapParser(parser) {
|
263
|
+
|
257
264
|
if (typeof parser.parseForESLint === "function") {
|
258
265
|
return {
|
266
|
+
[parserSymbol]: parser,
|
259
267
|
parseForESLint(...args) {
|
260
268
|
const ret = parser.parseForESLint(...args);
|
261
269
|
|
@@ -264,7 +272,9 @@ function wrapParser(parser) {
|
|
264
272
|
}
|
265
273
|
};
|
266
274
|
}
|
275
|
+
|
267
276
|
return {
|
277
|
+
[parserSymbol]: parser,
|
268
278
|
parse(...args) {
|
269
279
|
const ast = parser.parse(...args);
|
270
280
|
|
@@ -281,6 +291,7 @@ function wrapParser(parser) {
|
|
281
291
|
// default separators for testing
|
282
292
|
const DESCRIBE = Symbol("describe");
|
283
293
|
const IT = Symbol("it");
|
294
|
+
const IT_ONLY = Symbol("itOnly");
|
284
295
|
|
285
296
|
/**
|
286
297
|
* This is `it` default handler if `it` don't exist.
|
@@ -324,10 +335,9 @@ class RuleTester {
|
|
324
335
|
* configuration and the default configuration.
|
325
336
|
* @type {Object}
|
326
337
|
*/
|
327
|
-
this.testerConfig =
|
328
|
-
|
329
|
-
|
330
|
-
lodash.cloneDeep(defaultConfig),
|
338
|
+
this.testerConfig = merge(
|
339
|
+
{},
|
340
|
+
defaultConfig,
|
331
341
|
testerConfig,
|
332
342
|
{ rules: { "rule-tester/validate-ast": "error" } }
|
333
343
|
);
|
@@ -369,7 +379,7 @@ class RuleTester {
|
|
369
379
|
* @returns {void}
|
370
380
|
*/
|
371
381
|
static resetDefaultConfig() {
|
372
|
-
defaultConfig =
|
382
|
+
defaultConfig = merge({}, testerDefaultConfig);
|
373
383
|
}
|
374
384
|
|
375
385
|
|
@@ -400,6 +410,46 @@ class RuleTester {
|
|
400
410
|
this[IT] = value;
|
401
411
|
}
|
402
412
|
|
413
|
+
/**
|
414
|
+
* Adds the `only` property to a test to run it in isolation.
|
415
|
+
* @param {string | ValidTestCase | InvalidTestCase} item A single test to run by itself.
|
416
|
+
* @returns {ValidTestCase | InvalidTestCase} The test with `only` set.
|
417
|
+
*/
|
418
|
+
static only(item) {
|
419
|
+
if (typeof item === "string") {
|
420
|
+
return { code: item, only: true };
|
421
|
+
}
|
422
|
+
|
423
|
+
return { ...item, only: true };
|
424
|
+
}
|
425
|
+
|
426
|
+
static get itOnly() {
|
427
|
+
if (typeof this[IT_ONLY] === "function") {
|
428
|
+
return this[IT_ONLY];
|
429
|
+
}
|
430
|
+
if (typeof this[IT] === "function" && typeof this[IT].only === "function") {
|
431
|
+
return Function.bind.call(this[IT].only, this[IT]);
|
432
|
+
}
|
433
|
+
if (typeof it === "function" && typeof it.only === "function") {
|
434
|
+
return Function.bind.call(it.only, it);
|
435
|
+
}
|
436
|
+
|
437
|
+
if (typeof this[DESCRIBE] === "function" || typeof this[IT] === "function") {
|
438
|
+
throw new Error(
|
439
|
+
"Set `RuleTester.itOnly` to use `only` with a custom test framework.\n" +
|
440
|
+
"See https://eslint.org/docs/developer-guide/nodejs-api#customizing-ruletester for more."
|
441
|
+
);
|
442
|
+
}
|
443
|
+
if (typeof it === "function") {
|
444
|
+
throw new Error("The current test framework does not support exclusive tests with `only`.");
|
445
|
+
}
|
446
|
+
throw new Error("To use `only`, use RuleTester with a test framework that provides `it.only()` like Mocha.");
|
447
|
+
}
|
448
|
+
|
449
|
+
static set itOnly(value) {
|
450
|
+
this[IT_ONLY] = value;
|
451
|
+
}
|
452
|
+
|
403
453
|
/**
|
404
454
|
* Define a rule for one particular run of tests.
|
405
455
|
* @param {string} name The name of the rule to define.
|
@@ -465,7 +515,7 @@ class RuleTester {
|
|
465
515
|
* @private
|
466
516
|
*/
|
467
517
|
function runRuleForItem(item) {
|
468
|
-
let config =
|
518
|
+
let config = merge({}, testerConfig),
|
469
519
|
code, filename, output, beforeAST, afterAST;
|
470
520
|
|
471
521
|
if (typeof item === "string") {
|
@@ -477,13 +527,17 @@ class RuleTester {
|
|
477
527
|
* Assumes everything on the item is a config except for the
|
478
528
|
* parameters used by this tester
|
479
529
|
*/
|
480
|
-
const itemConfig =
|
530
|
+
const itemConfig = { ...item };
|
531
|
+
|
532
|
+
for (const parameter of RuleTesterParameters) {
|
533
|
+
delete itemConfig[parameter];
|
534
|
+
}
|
481
535
|
|
482
536
|
/*
|
483
537
|
* Create the config object from the tester config and this item
|
484
538
|
* specific configurations.
|
485
539
|
*/
|
486
|
-
config =
|
540
|
+
config = merge(
|
487
541
|
config,
|
488
542
|
itemConfig
|
489
543
|
);
|
@@ -589,7 +643,7 @@ class RuleTester {
|
|
589
643
|
* @private
|
590
644
|
*/
|
591
645
|
function assertASTDidntChange(beforeAST, afterAST) {
|
592
|
-
if (!
|
646
|
+
if (!equal(beforeAST, afterAST)) {
|
593
647
|
assert.fail("Rule should not modify AST.");
|
594
648
|
}
|
595
649
|
}
|
@@ -606,7 +660,8 @@ class RuleTester {
|
|
606
660
|
const messages = result.messages;
|
607
661
|
|
608
662
|
assert.strictEqual(messages.length, 0, util.format("Should have no errors but had %d: %s",
|
609
|
-
messages.length,
|
663
|
+
messages.length,
|
664
|
+
util.inspect(messages)));
|
610
665
|
|
611
666
|
assertASTDidntChange(result.beforeAST, result.afterAST);
|
612
667
|
}
|
@@ -661,13 +716,18 @@ class RuleTester {
|
|
661
716
|
}
|
662
717
|
|
663
718
|
assert.strictEqual(messages.length, item.errors, util.format("Should have %d error%s but had %d: %s",
|
664
|
-
item.errors,
|
719
|
+
item.errors,
|
720
|
+
item.errors === 1 ? "" : "s",
|
721
|
+
messages.length,
|
722
|
+
util.inspect(messages)));
|
665
723
|
} else {
|
666
724
|
assert.strictEqual(
|
667
|
-
messages.length, item.errors.length,
|
668
|
-
util.format(
|
725
|
+
messages.length, item.errors.length, util.format(
|
669
726
|
"Should have %d error%s but had %d: %s",
|
670
|
-
item.errors.length,
|
727
|
+
item.errors.length,
|
728
|
+
item.errors.length === 1 ? "" : "s",
|
729
|
+
messages.length,
|
730
|
+
util.inspect(messages)
|
671
731
|
)
|
672
732
|
);
|
673
733
|
|
@@ -881,23 +941,29 @@ class RuleTester {
|
|
881
941
|
RuleTester.describe(ruleName, () => {
|
882
942
|
RuleTester.describe("valid", () => {
|
883
943
|
test.valid.forEach(valid => {
|
884
|
-
RuleTester.
|
885
|
-
|
886
|
-
|
944
|
+
RuleTester[valid.only ? "itOnly" : "it"](
|
945
|
+
sanitize(typeof valid === "object" ? valid.code : valid),
|
946
|
+
() => {
|
947
|
+
testValidTemplate(valid);
|
948
|
+
}
|
949
|
+
);
|
887
950
|
});
|
888
951
|
});
|
889
952
|
|
890
953
|
RuleTester.describe("invalid", () => {
|
891
954
|
test.invalid.forEach(invalid => {
|
892
|
-
RuleTester
|
893
|
-
|
894
|
-
|
955
|
+
RuleTester[invalid.only ? "itOnly" : "it"](
|
956
|
+
sanitize(invalid.code),
|
957
|
+
() => {
|
958
|
+
testInvalidTemplate(invalid);
|
959
|
+
}
|
960
|
+
);
|
895
961
|
});
|
896
962
|
});
|
897
963
|
});
|
898
964
|
}
|
899
965
|
}
|
900
966
|
|
901
|
-
RuleTester[DESCRIBE] = RuleTester[IT] = null;
|
967
|
+
RuleTester[DESCRIBE] = RuleTester[IT] = RuleTester[IT_ONLY] = null;
|
902
968
|
|
903
969
|
module.exports = RuleTester;
|
@@ -87,17 +87,17 @@ module.exports = {
|
|
87
87
|
}
|
88
88
|
|
89
89
|
/**
|
90
|
-
* Gets the closing parenthesis
|
91
|
-
* @param {
|
90
|
+
* Gets the closing parenthesis by the given node.
|
91
|
+
* @param {ASTNode} node first node after an opening parenthesis.
|
92
92
|
* @returns {Token} The found closing parenthesis token.
|
93
93
|
*/
|
94
|
-
function findClosingParen(
|
95
|
-
let
|
94
|
+
function findClosingParen(node) {
|
95
|
+
let nodeToCheck = node;
|
96
96
|
|
97
|
-
while (!astUtils.isParenthesised(sourceCode,
|
98
|
-
|
97
|
+
while (!astUtils.isParenthesised(sourceCode, nodeToCheck)) {
|
98
|
+
nodeToCheck = nodeToCheck.parent;
|
99
99
|
}
|
100
|
-
return sourceCode.getTokenAfter(
|
100
|
+
return sourceCode.getTokenAfter(nodeToCheck);
|
101
101
|
}
|
102
102
|
|
103
103
|
/**
|
@@ -226,12 +226,22 @@ module.exports = {
|
|
226
226
|
const arrowToken = sourceCode.getTokenBefore(arrowBody, astUtils.isArrowToken);
|
227
227
|
const [firstTokenAfterArrow, secondTokenAfterArrow] = sourceCode.getTokensAfter(arrowToken, { count: 2 });
|
228
228
|
const lastToken = sourceCode.getLastToken(node);
|
229
|
-
|
229
|
+
|
230
|
+
let parenthesisedObjectLiteral = null;
|
231
|
+
|
232
|
+
if (
|
230
233
|
astUtils.isOpeningParenToken(firstTokenAfterArrow) &&
|
231
|
-
astUtils.isOpeningBraceToken(secondTokenAfterArrow)
|
234
|
+
astUtils.isOpeningBraceToken(secondTokenAfterArrow)
|
235
|
+
) {
|
236
|
+
const braceNode = sourceCode.getNodeByRangeIndex(secondTokenAfterArrow.range[0]);
|
237
|
+
|
238
|
+
if (braceNode.type === "ObjectExpression") {
|
239
|
+
parenthesisedObjectLiteral = braceNode;
|
240
|
+
}
|
241
|
+
}
|
232
242
|
|
233
243
|
// If the value is object literal, remove parentheses which were forced by syntax.
|
234
|
-
if (
|
244
|
+
if (parenthesisedObjectLiteral) {
|
235
245
|
const openingParenToken = firstTokenAfterArrow;
|
236
246
|
const openingBraceToken = secondTokenAfterArrow;
|
237
247
|
|
@@ -247,7 +257,7 @@ module.exports = {
|
|
247
257
|
}
|
248
258
|
|
249
259
|
// Closing paren for the object doesn't have to be lastToken, e.g.: () => ({}).foo()
|
250
|
-
fixes.push(fixer.remove(findClosingParen(
|
260
|
+
fixes.push(fixer.remove(findClosingParen(parenthesisedObjectLiteral)));
|
251
261
|
fixes.push(fixer.insertTextAfter(lastToken, "}"));
|
252
262
|
|
253
263
|
} else {
|
@@ -9,7 +9,6 @@
|
|
9
9
|
// Requirements
|
10
10
|
//------------------------------------------------------------------------------
|
11
11
|
|
12
|
-
const lodash = require("lodash");
|
13
12
|
const astUtils = require("./utils/ast-utils");
|
14
13
|
|
15
14
|
//------------------------------------------------------------------------------
|
@@ -144,23 +143,33 @@ module.exports = {
|
|
144
143
|
* @returns {ASTNode|null} The last node or null.
|
145
144
|
*/
|
146
145
|
function getLastItem(node) {
|
146
|
+
|
147
|
+
/**
|
148
|
+
* Returns the last element of an array
|
149
|
+
* @param {any[]} array The input array
|
150
|
+
* @returns {any} The last element
|
151
|
+
*/
|
152
|
+
function last(array) {
|
153
|
+
return array[array.length - 1];
|
154
|
+
}
|
155
|
+
|
147
156
|
switch (node.type) {
|
148
157
|
case "ObjectExpression":
|
149
158
|
case "ObjectPattern":
|
150
|
-
return
|
159
|
+
return last(node.properties);
|
151
160
|
case "ArrayExpression":
|
152
161
|
case "ArrayPattern":
|
153
|
-
return
|
162
|
+
return last(node.elements);
|
154
163
|
case "ImportDeclaration":
|
155
164
|
case "ExportNamedDeclaration":
|
156
|
-
return
|
165
|
+
return last(node.specifiers);
|
157
166
|
case "FunctionDeclaration":
|
158
167
|
case "FunctionExpression":
|
159
168
|
case "ArrowFunctionExpression":
|
160
|
-
return
|
169
|
+
return last(node.params);
|
161
170
|
case "CallExpression":
|
162
171
|
case "NewExpression":
|
163
|
-
return
|
172
|
+
return last(node.arguments);
|
164
173
|
default:
|
165
174
|
return null;
|
166
175
|
}
|
@@ -316,7 +325,7 @@ module.exports = {
|
|
316
325
|
"always-multiline": forceTrailingCommaIfMultiline,
|
317
326
|
"only-multiline": allowTrailingCommaIfMultiline,
|
318
327
|
never: forbidTrailingComma,
|
319
|
-
ignore:
|
328
|
+
ignore: () => {}
|
320
329
|
};
|
321
330
|
|
322
331
|
return {
|
@@ -181,7 +181,7 @@ module.exports = {
|
|
181
181
|
|
182
182
|
validateCommaItemSpacing({
|
183
183
|
comma: token,
|
184
|
-
left: astUtils.isCommaToken(previousToken) || commaTokensToIgnore.
|
184
|
+
left: astUtils.isCommaToken(previousToken) || commaTokensToIgnore.includes(token) ? null : previousToken,
|
185
185
|
right: astUtils.isCommaToken(nextToken) ? null : nextToken
|
186
186
|
}, token);
|
187
187
|
});
|
package/lib/rules/comma-style.js
CHANGED
@@ -207,8 +207,7 @@ module.exports = {
|
|
207
207
|
* they are always valid regardless of an undefined item.
|
208
208
|
*/
|
209
209
|
if (astUtils.isCommaToken(commaToken)) {
|
210
|
-
validateCommaItemSpacing(previousItemToken, commaToken,
|
211
|
-
currentItemToken, reportItem);
|
210
|
+
validateCommaItemSpacing(previousItemToken, commaToken, currentItemToken, reportItem);
|
212
211
|
}
|
213
212
|
|
214
213
|
if (item) {
|