eslint 7.25.0 → 7.29.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 +55 -0
- package/README.md +7 -7
- package/bin/eslint.js +2 -12
- package/lib/cli-engine/cli-engine.js +2 -7
- package/lib/cli-engine/file-enumerator.js +1 -1
- package/lib/cli-engine/formatters/html.js +193 -9
- package/lib/eslint/eslint.js +38 -2
- package/lib/init/autoconfig.js +2 -2
- package/lib/init/config-file.js +1 -0
- package/lib/init/config-initializer.js +14 -1
- package/lib/init/npm-utils.js +2 -2
- package/lib/linter/apply-disable-directives.js +15 -3
- package/lib/linter/linter.js +15 -9
- package/lib/linter/node-event-generator.js +43 -6
- package/lib/rule-tester/rule-tester.js +83 -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/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/radix.js +19 -3
- package/lib/rules/require-atomic-updates.js +23 -20
- package/lib/rules/spaced-comment.js +2 -2
- 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 +6 -5
- 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 +8 -8
- 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
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"),
|
@@ -444,7 +444,7 @@ function normalizeEcmaVersion(ecmaVersion) {
|
|
444
444
|
return ecmaVersion >= 2015 ? ecmaVersion - 2009 : ecmaVersion;
|
445
445
|
}
|
446
446
|
|
447
|
-
const eslintEnvPattern = /\/\*\s*eslint-env\s(.+?)\*\//
|
447
|
+
const eslintEnvPattern = /\/\*\s*eslint-env\s(.+?)\*\//gsu;
|
448
448
|
|
449
449
|
/**
|
450
450
|
* Checks whether or not there is a comment which has "eslint-env *" in a given text.
|
@@ -529,8 +529,8 @@ function normalizeVerifyOptions(providedOptions, config) {
|
|
529
529
|
function resolveParserOptions(parserName, providedOptions, enabledEnvironments) {
|
530
530
|
const parserOptionsFromEnv = enabledEnvironments
|
531
531
|
.filter(env => env.parserOptions)
|
532
|
-
.reduce((parserOptions, env) =>
|
533
|
-
const mergedParserOptions =
|
532
|
+
.reduce((parserOptions, env) => merge(parserOptions, env.parserOptions), {});
|
533
|
+
const mergedParserOptions = merge(parserOptionsFromEnv, providedOptions || {});
|
534
534
|
const isModule = mergedParserOptions.sourceType === "module";
|
535
535
|
|
536
536
|
if (isModule) {
|
@@ -828,9 +828,10 @@ const BASE_TRAVERSAL_CONTEXT = Object.freeze(
|
|
828
828
|
* @param {string} filename The reported filename of the code
|
829
829
|
* @param {boolean} disableFixes If true, it doesn't make `fix` properties.
|
830
830
|
* @param {string | undefined} cwd cwd of the cli
|
831
|
+
* @param {string} physicalFilename The full path of the file on disk without any code block information
|
831
832
|
* @returns {Problem[]} An array of reported problems
|
832
833
|
*/
|
833
|
-
function runRules(sourceCode, configuredRules, ruleMapper, parserOptions, parserName, settings, filename, disableFixes, cwd) {
|
834
|
+
function runRules(sourceCode, configuredRules, ruleMapper, parserOptions, parserName, settings, filename, disableFixes, cwd, physicalFilename) {
|
834
835
|
const emitter = createEmitter();
|
835
836
|
const nodeQueue = [];
|
836
837
|
let currentNode = sourceCode.ast;
|
@@ -859,6 +860,7 @@ function runRules(sourceCode, configuredRules, ruleMapper, parserOptions, parser
|
|
859
860
|
getDeclaredVariables: sourceCode.scopeManager.getDeclaredVariables.bind(sourceCode.scopeManager),
|
860
861
|
getCwd: () => cwd,
|
861
862
|
getFilename: () => filename,
|
863
|
+
getPhysicalFilename: () => physicalFilename || filename,
|
862
864
|
getScope: () => getScope(sourceCode.scopeManager, currentNode),
|
863
865
|
getSourceCode: () => sourceCode,
|
864
866
|
markVariableAsUsed: name => markVariableAsUsed(sourceCode.scopeManager, currentNode, parserOptions, name),
|
@@ -1181,7 +1183,8 @@ class Linter {
|
|
1181
1183
|
settings,
|
1182
1184
|
options.filename,
|
1183
1185
|
options.disableFixes,
|
1184
|
-
slots.cwd
|
1186
|
+
slots.cwd,
|
1187
|
+
providedOptions.physicalFilename
|
1185
1188
|
);
|
1186
1189
|
} catch (err) {
|
1187
1190
|
err.message += `\nOccurred while linting ${options.filename}`;
|
@@ -1284,9 +1287,12 @@ class Linter {
|
|
1284
1287
|
_verifyWithProcessor(textOrSourceCode, config, options, configForRecursive) {
|
1285
1288
|
const filename = options.filename || "<input>";
|
1286
1289
|
const filenameToExpose = normalizeFilename(filename);
|
1290
|
+
const physicalFilename = options.physicalFilename || filenameToExpose;
|
1287
1291
|
const text = ensureText(textOrSourceCode);
|
1288
1292
|
const preprocess = options.preprocess || (rawText => [rawText]);
|
1289
|
-
|
1293
|
+
|
1294
|
+
// TODO(stephenwade): Replace this with array.flat() when we drop support for Node v10
|
1295
|
+
const postprocess = options.postprocess || (array => [].concat(...array));
|
1290
1296
|
const filterCodeBlock =
|
1291
1297
|
options.filterCodeBlock ||
|
1292
1298
|
(blockFilename => blockFilename.endsWith(".js"));
|
@@ -1314,7 +1320,7 @@ class Linter {
|
|
1314
1320
|
return this._verifyWithConfigArray(
|
1315
1321
|
blockText,
|
1316
1322
|
configForRecursive,
|
1317
|
-
{ ...options, filename: blockName }
|
1323
|
+
{ ...options, filename: blockName, physicalFilename }
|
1318
1324
|
);
|
1319
1325
|
}
|
1320
1326
|
|
@@ -1322,7 +1328,7 @@ class Linter {
|
|
1322
1328
|
return this._verifyWithoutProcessors(
|
1323
1329
|
blockText,
|
1324
1330
|
config,
|
1325
|
-
{ ...options, filename: blockName }
|
1331
|
+
{ ...options, filename: blockName, physicalFilename }
|
1326
1332
|
);
|
1327
1333
|
});
|
1328
1334
|
|
@@ -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");
|
@@ -70,6 +71,7 @@ const espreePath = require.resolve("espree");
|
|
70
71
|
* @property {{ [name: string]: any }} [parserOptions] Options for the parser.
|
71
72
|
* @property {{ [name: string]: "readonly" | "writable" | "off" }} [globals] The additional global variables.
|
72
73
|
* @property {{ [name: string]: boolean }} [env] Environments for the test case.
|
74
|
+
* @property {boolean} [only] Run only this test case or the subset of test cases with this property.
|
73
75
|
*/
|
74
76
|
|
75
77
|
/**
|
@@ -85,6 +87,7 @@ const espreePath = require.resolve("espree");
|
|
85
87
|
* @property {{ [name: string]: any }} [parserOptions] Options for the parser.
|
86
88
|
* @property {{ [name: string]: "readonly" | "writable" | "off" }} [globals] The additional global variables.
|
87
89
|
* @property {{ [name: string]: boolean }} [env] Environments for the test case.
|
90
|
+
* @property {boolean} [only] Run only this test case or the subset of test cases with this property.
|
88
91
|
*/
|
89
92
|
|
90
93
|
/**
|
@@ -120,7 +123,8 @@ const RuleTesterParameters = [
|
|
120
123
|
"filename",
|
121
124
|
"options",
|
122
125
|
"errors",
|
123
|
-
"output"
|
126
|
+
"output",
|
127
|
+
"only"
|
124
128
|
];
|
125
129
|
|
126
130
|
/*
|
@@ -281,6 +285,7 @@ function wrapParser(parser) {
|
|
281
285
|
// default separators for testing
|
282
286
|
const DESCRIBE = Symbol("describe");
|
283
287
|
const IT = Symbol("it");
|
288
|
+
const IT_ONLY = Symbol("itOnly");
|
284
289
|
|
285
290
|
/**
|
286
291
|
* This is `it` default handler if `it` don't exist.
|
@@ -324,10 +329,9 @@ class RuleTester {
|
|
324
329
|
* configuration and the default configuration.
|
325
330
|
* @type {Object}
|
326
331
|
*/
|
327
|
-
this.testerConfig =
|
328
|
-
|
329
|
-
|
330
|
-
lodash.cloneDeep(defaultConfig),
|
332
|
+
this.testerConfig = merge(
|
333
|
+
{},
|
334
|
+
defaultConfig,
|
331
335
|
testerConfig,
|
332
336
|
{ rules: { "rule-tester/validate-ast": "error" } }
|
333
337
|
);
|
@@ -369,7 +373,7 @@ class RuleTester {
|
|
369
373
|
* @returns {void}
|
370
374
|
*/
|
371
375
|
static resetDefaultConfig() {
|
372
|
-
defaultConfig =
|
376
|
+
defaultConfig = merge({}, testerDefaultConfig);
|
373
377
|
}
|
374
378
|
|
375
379
|
|
@@ -400,6 +404,46 @@ class RuleTester {
|
|
400
404
|
this[IT] = value;
|
401
405
|
}
|
402
406
|
|
407
|
+
/**
|
408
|
+
* Adds the `only` property to a test to run it in isolation.
|
409
|
+
* @param {string | ValidTestCase | InvalidTestCase} item A single test to run by itself.
|
410
|
+
* @returns {ValidTestCase | InvalidTestCase} The test with `only` set.
|
411
|
+
*/
|
412
|
+
static only(item) {
|
413
|
+
if (typeof item === "string") {
|
414
|
+
return { code: item, only: true };
|
415
|
+
}
|
416
|
+
|
417
|
+
return { ...item, only: true };
|
418
|
+
}
|
419
|
+
|
420
|
+
static get itOnly() {
|
421
|
+
if (typeof this[IT_ONLY] === "function") {
|
422
|
+
return this[IT_ONLY];
|
423
|
+
}
|
424
|
+
if (typeof this[IT] === "function" && typeof this[IT].only === "function") {
|
425
|
+
return Function.bind.call(this[IT].only, this[IT]);
|
426
|
+
}
|
427
|
+
if (typeof it === "function" && typeof it.only === "function") {
|
428
|
+
return Function.bind.call(it.only, it);
|
429
|
+
}
|
430
|
+
|
431
|
+
if (typeof this[DESCRIBE] === "function" || typeof this[IT] === "function") {
|
432
|
+
throw new Error(
|
433
|
+
"Set `RuleTester.itOnly` to use `only` with a custom test framework.\n" +
|
434
|
+
"See https://eslint.org/docs/developer-guide/nodejs-api#customizing-ruletester for more."
|
435
|
+
);
|
436
|
+
}
|
437
|
+
if (typeof it === "function") {
|
438
|
+
throw new Error("The current test framework does not support exclusive tests with `only`.");
|
439
|
+
}
|
440
|
+
throw new Error("To use `only`, use RuleTester with a test framework that provides `it.only()` like Mocha.");
|
441
|
+
}
|
442
|
+
|
443
|
+
static set itOnly(value) {
|
444
|
+
this[IT_ONLY] = value;
|
445
|
+
}
|
446
|
+
|
403
447
|
/**
|
404
448
|
* Define a rule for one particular run of tests.
|
405
449
|
* @param {string} name The name of the rule to define.
|
@@ -465,7 +509,7 @@ class RuleTester {
|
|
465
509
|
* @private
|
466
510
|
*/
|
467
511
|
function runRuleForItem(item) {
|
468
|
-
let config =
|
512
|
+
let config = merge({}, testerConfig),
|
469
513
|
code, filename, output, beforeAST, afterAST;
|
470
514
|
|
471
515
|
if (typeof item === "string") {
|
@@ -477,13 +521,17 @@ class RuleTester {
|
|
477
521
|
* Assumes everything on the item is a config except for the
|
478
522
|
* parameters used by this tester
|
479
523
|
*/
|
480
|
-
const itemConfig =
|
524
|
+
const itemConfig = { ...item };
|
525
|
+
|
526
|
+
for (const parameter of RuleTesterParameters) {
|
527
|
+
delete itemConfig[parameter];
|
528
|
+
}
|
481
529
|
|
482
530
|
/*
|
483
531
|
* Create the config object from the tester config and this item
|
484
532
|
* specific configurations.
|
485
533
|
*/
|
486
|
-
config =
|
534
|
+
config = merge(
|
487
535
|
config,
|
488
536
|
itemConfig
|
489
537
|
);
|
@@ -589,7 +637,7 @@ class RuleTester {
|
|
589
637
|
* @private
|
590
638
|
*/
|
591
639
|
function assertASTDidntChange(beforeAST, afterAST) {
|
592
|
-
if (!
|
640
|
+
if (!equal(beforeAST, afterAST)) {
|
593
641
|
assert.fail("Rule should not modify AST.");
|
594
642
|
}
|
595
643
|
}
|
@@ -606,7 +654,8 @@ class RuleTester {
|
|
606
654
|
const messages = result.messages;
|
607
655
|
|
608
656
|
assert.strictEqual(messages.length, 0, util.format("Should have no errors but had %d: %s",
|
609
|
-
messages.length,
|
657
|
+
messages.length,
|
658
|
+
util.inspect(messages)));
|
610
659
|
|
611
660
|
assertASTDidntChange(result.beforeAST, result.afterAST);
|
612
661
|
}
|
@@ -661,13 +710,18 @@ class RuleTester {
|
|
661
710
|
}
|
662
711
|
|
663
712
|
assert.strictEqual(messages.length, item.errors, util.format("Should have %d error%s but had %d: %s",
|
664
|
-
item.errors,
|
713
|
+
item.errors,
|
714
|
+
item.errors === 1 ? "" : "s",
|
715
|
+
messages.length,
|
716
|
+
util.inspect(messages)));
|
665
717
|
} else {
|
666
718
|
assert.strictEqual(
|
667
|
-
messages.length, item.errors.length,
|
668
|
-
util.format(
|
719
|
+
messages.length, item.errors.length, util.format(
|
669
720
|
"Should have %d error%s but had %d: %s",
|
670
|
-
item.errors.length,
|
721
|
+
item.errors.length,
|
722
|
+
item.errors.length === 1 ? "" : "s",
|
723
|
+
messages.length,
|
724
|
+
util.inspect(messages)
|
671
725
|
)
|
672
726
|
);
|
673
727
|
|
@@ -881,23 +935,29 @@ class RuleTester {
|
|
881
935
|
RuleTester.describe(ruleName, () => {
|
882
936
|
RuleTester.describe("valid", () => {
|
883
937
|
test.valid.forEach(valid => {
|
884
|
-
RuleTester.
|
885
|
-
|
886
|
-
|
938
|
+
RuleTester[valid.only ? "itOnly" : "it"](
|
939
|
+
sanitize(typeof valid === "object" ? valid.code : valid),
|
940
|
+
() => {
|
941
|
+
testValidTemplate(valid);
|
942
|
+
}
|
943
|
+
);
|
887
944
|
});
|
888
945
|
});
|
889
946
|
|
890
947
|
RuleTester.describe("invalid", () => {
|
891
948
|
test.invalid.forEach(invalid => {
|
892
|
-
RuleTester
|
893
|
-
|
894
|
-
|
949
|
+
RuleTester[invalid.only ? "itOnly" : "it"](
|
950
|
+
sanitize(invalid.code),
|
951
|
+
() => {
|
952
|
+
testInvalidTemplate(invalid);
|
953
|
+
}
|
954
|
+
);
|
895
955
|
});
|
896
956
|
});
|
897
957
|
});
|
898
958
|
}
|
899
959
|
}
|
900
960
|
|
901
|
-
RuleTester[DESCRIBE] = RuleTester[IT] = null;
|
961
|
+
RuleTester[DESCRIBE] = RuleTester[IT] = RuleTester[IT_ONLY] = null;
|
902
962
|
|
903
963
|
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) {
|
package/lib/rules/complexity.js
CHANGED
@@ -10,9 +10,8 @@
|
|
10
10
|
// Requirements
|
11
11
|
//------------------------------------------------------------------------------
|
12
12
|
|
13
|
-
const lodash = require("lodash");
|
14
|
-
|
15
13
|
const astUtils = require("./utils/ast-utils");
|
14
|
+
const { upperCaseFirst } = require("../shared/string-utils");
|
16
15
|
|
17
16
|
//------------------------------------------------------------------------------
|
18
17
|
// Rule Definition
|
@@ -95,7 +94,7 @@ module.exports = {
|
|
95
94
|
* @private
|
96
95
|
*/
|
97
96
|
function endFunction(node) {
|
98
|
-
const name =
|
97
|
+
const name = upperCaseFirst(astUtils.getFunctionNameWithKind(node));
|
99
98
|
const complexity = fns.pop();
|
100
99
|
|
101
100
|
if (complexity > THRESHOLD) {
|
@@ -8,8 +8,8 @@
|
|
8
8
|
// Requirements
|
9
9
|
//------------------------------------------------------------------------------
|
10
10
|
|
11
|
-
const lodash = require("lodash");
|
12
11
|
const astUtils = require("./utils/ast-utils");
|
12
|
+
const { upperCaseFirst } = require("../shared/string-utils");
|
13
13
|
|
14
14
|
//------------------------------------------------------------------------------
|
15
15
|
// Helpers
|
@@ -164,7 +164,7 @@ module.exports = {
|
|
164
164
|
funcInfo.data = {
|
165
165
|
name: funcInfo.node.type === "Program"
|
166
166
|
? "Program"
|
167
|
-
:
|
167
|
+
: upperCaseFirst(astUtils.getFunctionNameWithKind(funcInfo.node))
|
168
168
|
};
|
169
169
|
} else if (funcInfo.hasReturnValue !== hasReturnValue) {
|
170
170
|
context.report({
|
package/lib/rules/eol-last.js
CHANGED
@@ -4,12 +4,6 @@
|
|
4
4
|
*/
|
5
5
|
"use strict";
|
6
6
|
|
7
|
-
//------------------------------------------------------------------------------
|
8
|
-
// Requirements
|
9
|
-
//------------------------------------------------------------------------------
|
10
|
-
|
11
|
-
const lodash = require("lodash");
|
12
|
-
|
13
7
|
//------------------------------------------------------------------------------
|
14
8
|
// Rule Definition
|
15
9
|
//------------------------------------------------------------------------------
|
@@ -48,8 +42,9 @@ module.exports = {
|
|
48
42
|
Program: function checkBadEOF(node) {
|
49
43
|
const sourceCode = context.getSourceCode(),
|
50
44
|
src = sourceCode.getText(),
|
45
|
+
lastLine = sourceCode.lines[sourceCode.lines.length - 1],
|
51
46
|
location = {
|
52
|
-
column:
|
47
|
+
column: lastLine.length,
|
53
48
|
line: sourceCode.lines.length
|
54
49
|
},
|
55
50
|
LF = "\n",
|
package/lib/rules/indent.js
CHANGED
@@ -12,10 +12,10 @@
|
|
12
12
|
// Requirements
|
13
13
|
//------------------------------------------------------------------------------
|
14
14
|
|
15
|
-
const lodash = require("lodash");
|
16
|
-
const astUtils = require("./utils/ast-utils");
|
17
15
|
const createTree = require("functional-red-black-tree");
|
18
16
|
|
17
|
+
const astUtils = require("./utils/ast-utils");
|
18
|
+
|
19
19
|
//------------------------------------------------------------------------------
|
20
20
|
// Rule Definition
|
21
21
|
//------------------------------------------------------------------------------
|
@@ -1068,7 +1068,7 @@ module.exports = {
|
|
1068
1068
|
const baseOffsetListeners = {
|
1069
1069
|
"ArrayExpression, ArrayPattern"(node) {
|
1070
1070
|
const openingBracket = sourceCode.getFirstToken(node);
|
1071
|
-
const closingBracket = sourceCode.getTokenAfter(
|
1071
|
+
const closingBracket = sourceCode.getTokenAfter([...node.elements].reverse().find(_ => _) || openingBracket, astUtils.isClosingBracketToken);
|
1072
1072
|
|
1073
1073
|
addElementListIndent(node.elements, openingBracket, closingBracket, options.ArrayExpression);
|
1074
1074
|
},
|
@@ -1177,8 +1177,7 @@ module.exports = {
|
|
1177
1177
|
offsets.setDesiredOffset(questionMarkToken, firstToken, 1);
|
1178
1178
|
offsets.setDesiredOffset(colonToken, firstToken, 1);
|
1179
1179
|
|
1180
|
-
offsets.setDesiredOffset(firstConsequentToken, firstToken,
|
1181
|
-
firstConsequentToken.type === "Punctuator" &&
|
1180
|
+
offsets.setDesiredOffset(firstConsequentToken, firstToken, firstConsequentToken.type === "Punctuator" &&
|
1182
1181
|
options.offsetTernaryExpressions ? 2 : 1);
|
1183
1182
|
|
1184
1183
|
/*
|
@@ -1204,8 +1203,7 @@ module.exports = {
|
|
1204
1203
|
* If `baz` were aligned with `bar` rather than being offset by 1 from `foo`, `baz` would end up
|
1205
1204
|
* having no expected indentation.
|
1206
1205
|
*/
|
1207
|
-
offsets.setDesiredOffset(firstAlternateToken, firstToken,
|
1208
|
-
firstAlternateToken.type === "Punctuator" &&
|
1206
|
+
offsets.setDesiredOffset(firstAlternateToken, firstToken, firstAlternateToken.type === "Punctuator" &&
|
1209
1207
|
options.offsetTernaryExpressions ? 2 : 1);
|
1210
1208
|
}
|
1211
1209
|
}
|
@@ -1560,8 +1558,9 @@ module.exports = {
|
|
1560
1558
|
* 2. Don't set any offsets against the first token of the node.
|
1561
1559
|
* 3. Call `ignoreNode` on the node sometime after exiting it and before validating offsets.
|
1562
1560
|
*/
|
1563
|
-
const offsetListeners =
|
1564
|
-
|
1561
|
+
const offsetListeners = {};
|
1562
|
+
|
1563
|
+
for (const [selector, listener] of Object.entries(baseOffsetListeners)) {
|
1565
1564
|
|
1566
1565
|
/*
|
1567
1566
|
* Offset listener calls are deferred until traversal is finished, and are called as
|
@@ -1579,10 +1578,8 @@ module.exports = {
|
|
1579
1578
|
* To avoid this, the `Identifier` listener isn't called until traversal finishes and all
|
1580
1579
|
* ignored nodes are known.
|
1581
1580
|
*/
|
1582
|
-
listener
|
1583
|
-
|
1584
|
-
listenerCallQueue.push({ listener, node })
|
1585
|
-
);
|
1581
|
+
offsetListeners[selector] = node => listenerCallQueue.push({ listener, node });
|
1582
|
+
}
|
1586
1583
|
|
1587
1584
|
// For each ignored node selector, set up a listener to collect it into the `ignoredNodes` set.
|
1588
1585
|
const ignoredNodes = new Set();
|