eslint 8.0.0-rc.0 → 8.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +3 -3
- package/lib/cli-engine/cli-engine.js +10 -21
- package/lib/eslint/eslint.js +2 -15
- package/lib/linter/config-comment-parser.js +1 -1
- package/lib/linter/linter.js +1 -1
- package/lib/linter/node-event-generator.js +7 -0
- package/lib/rule-tester/rule-tester.js +5 -2
- package/lib/rules/index.js +1 -0
- package/lib/rules/key-spacing.js +14 -13
- package/lib/rules/keyword-spacing.js +14 -1
- package/lib/rules/no-unused-private-class-members.js +194 -0
- package/lib/rules/no-use-before-define.js +152 -74
- package/lib/rules/require-atomic-updates.js +21 -8
- package/lib/shared/types.js +1 -1
- package/package.json +6 -6
package/README.md
CHANGED
@@ -242,7 +242,7 @@ Toru Nagashima
|
|
242
242
|
</td><td align="center" valign="top" width="11%">
|
243
243
|
<a href="https://github.com/aladdin-add">
|
244
244
|
<img src="https://github.com/aladdin-add.png?s=75" width="75" height="75"><br />
|
245
|
-
|
245
|
+
唯然
|
246
246
|
</a>
|
247
247
|
</td></tr></tbody></table>
|
248
248
|
|
@@ -297,8 +297,8 @@ The following companies, organizations, and individuals support ESLint's ongoing
|
|
297
297
|
<h3>Platinum Sponsors</h3>
|
298
298
|
<p><a href="https://automattic.com"><img src="https://images.opencollective.com/photomatt/d0ef3e1/logo.png" alt="Automattic" height="undefined"></a></p><h3>Gold Sponsors</h3>
|
299
299
|
<p><a href="https://nx.dev"><img src="https://images.opencollective.com/nx/0efbe42/logo.png" alt="Nx (by Nrwl)" height="96"></a> <a href="https://google.com/chrome"><img src="https://images.opencollective.com/chrome/dc55bd4/logo.png" alt="Chrome's Web Framework & Tools Performance Fund" height="96"></a> <a href="https://www.salesforce.com"><img src="https://images.opencollective.com/salesforce/ca8f997/logo.png" alt="Salesforce" height="96"></a> <a href="https://www.airbnb.com/"><img src="https://images.opencollective.com/airbnb/d327d66/logo.png" alt="Airbnb" height="96"></a> <a href="https://coinbase.com"><img src="https://avatars.githubusercontent.com/u/1885080?v=4" alt="Coinbase" height="96"></a> <a href="https://americanexpress.io"><img src="https://avatars.githubusercontent.com/u/3853301?v=4" alt="American Express" height="96"></a> <a href="https://substack.com/"><img src="https://avatars.githubusercontent.com/u/53023767?v=4" alt="Substack" height="96"></a></p><h3>Silver Sponsors</h3>
|
300
|
-
<p><a href="https://
|
301
|
-
<p><a href="https://
|
300
|
+
<p><a href="https://liftoff.io/"><img src="https://images.opencollective.com/liftoff/5c4fa84/logo.png" alt="Liftoff" height="64"></a></p><h3>Bronze Sponsors</h3>
|
301
|
+
<p><a href="https://launchdarkly.com"><img src="https://images.opencollective.com/launchdarkly/574bb9e/logo.png" alt="launchdarkly" height="32"></a> <a href="https://troypoint.com"><img src="https://images.opencollective.com/troypoint/080f96f/avatar.png" alt="TROYPOINT" height="32"></a> <a href="https://www.crosswordsolver.org/anagram-solver/"><img src="https://images.opencollective.com/anagram-solver/2666271/logo.png" alt="Anagram Solver" height="32"></a> <a href="https://www.vpsserver.com"><img src="https://images.opencollective.com/vpsservercom/logo.png" alt="VPS Server" height="32"></a> <a href="https://icons8.com"><img src="https://images.opencollective.com/icons8/7fa1641/logo.png" alt="Icons8: free icons, photos, illustrations, and music" height="32"></a> <a href="https://discord.com"><img src="https://images.opencollective.com/discordapp/f9645d9/logo.png" alt="Discord" height="32"></a> <a href="https://themeisle.com"><img src="https://images.opencollective.com/themeisle/d5592fe/logo.png" alt="ThemeIsle" height="32"></a> <a href="https://www.firesticktricks.com"><img src="https://images.opencollective.com/fire-stick-tricks/b8fbe2c/logo.png" alt="Fire Stick Tricks" height="32"></a> <a href="https://www.practiceignition.com"><img src="https://avatars.githubusercontent.com/u/5753491?v=4" alt="Practice Ignition" height="32"></a></p>
|
302
302
|
<!--sponsorsend-->
|
303
303
|
|
304
304
|
## <a name="technology-sponsors"></a>Technology Sponsors
|
@@ -570,8 +570,10 @@ class CLIEngine {
|
|
570
570
|
/**
|
571
571
|
* Creates a new instance of the core CLI engine.
|
572
572
|
* @param {CLIEngineOptions} providedOptions The options for this instance.
|
573
|
+
* @param {Object} [additionalData] Additional settings that are not CLIEngineOptions.
|
574
|
+
* @param {Record<string,Plugin>|null} [additionalData.preloadedPlugins] Preloaded plugins.
|
573
575
|
*/
|
574
|
-
constructor(providedOptions) {
|
576
|
+
constructor(providedOptions, { preloadedPlugins } = {}) {
|
575
577
|
const options = Object.assign(
|
576
578
|
Object.create(null),
|
577
579
|
defaultOptions,
|
@@ -584,6 +586,13 @@ class CLIEngine {
|
|
584
586
|
}
|
585
587
|
|
586
588
|
const additionalPluginPool = new Map();
|
589
|
+
|
590
|
+
if (preloadedPlugins) {
|
591
|
+
for (const [id, plugin] of Object.entries(preloadedPlugins)) {
|
592
|
+
additionalPluginPool.set(id, plugin);
|
593
|
+
}
|
594
|
+
}
|
595
|
+
|
587
596
|
const cacheFilePath = getCacheFile(
|
588
597
|
options.cacheLocation || options.cacheFile,
|
589
598
|
options.cwd
|
@@ -698,26 +707,6 @@ class CLIEngine {
|
|
698
707
|
});
|
699
708
|
}
|
700
709
|
|
701
|
-
|
702
|
-
/**
|
703
|
-
* Add a plugin by passing its configuration
|
704
|
-
* @param {string} name Name of the plugin.
|
705
|
-
* @param {Plugin} pluginObject Plugin configuration object.
|
706
|
-
* @returns {void}
|
707
|
-
*/
|
708
|
-
addPlugin(name, pluginObject) {
|
709
|
-
const {
|
710
|
-
additionalPluginPool,
|
711
|
-
configArrayFactory,
|
712
|
-
lastConfigArrays
|
713
|
-
} = internalSlotsMap.get(this);
|
714
|
-
|
715
|
-
additionalPluginPool.set(name, pluginObject);
|
716
|
-
configArrayFactory.clearCache();
|
717
|
-
lastConfigArrays.length = 1;
|
718
|
-
lastConfigArrays[0] = configArrayFactory.getConfigArrayForFile();
|
719
|
-
}
|
720
|
-
|
721
710
|
/**
|
722
711
|
* Resolves the patterns passed into executeOnFiles() into glob-based patterns
|
723
712
|
* for easier handling.
|
package/lib/eslint/eslint.js
CHANGED
@@ -54,7 +54,7 @@ const { version } = require("../../package.json");
|
|
54
54
|
* @property {string} [ignorePath] The ignore file to use instead of .eslintignore.
|
55
55
|
* @property {ConfigData} [overrideConfig] Override config object, overrides all configs used with this instance
|
56
56
|
* @property {string} [overrideConfigFile] The configuration file to use.
|
57
|
-
* @property {Record<string,Plugin
|
57
|
+
* @property {Record<string,Plugin>|null} [plugins] Preloaded plugins. This is a map-like object, keys are plugin IDs and each value is implementation.
|
58
58
|
* @property {"error" | "warn" | "off"} [reportUnusedDisableDirectives] the severity to report unused eslint-disable directives.
|
59
59
|
* @property {string} [resolvePluginsRelativeTo] The folder where plugins should be resolved from, defaulting to the CWD.
|
60
60
|
* @property {string[]} [rulePaths] An array of directories to load custom rules from.
|
@@ -433,26 +433,13 @@ class ESLint {
|
|
433
433
|
*/
|
434
434
|
constructor(options = {}) {
|
435
435
|
const processedOptions = processOptions(options);
|
436
|
-
const cliEngine = new CLIEngine(processedOptions);
|
436
|
+
const cliEngine = new CLIEngine(processedOptions, { preloadedPlugins: options.plugins });
|
437
437
|
const {
|
438
|
-
additionalPluginPool,
|
439
438
|
configArrayFactory,
|
440
439
|
lastConfigArrays
|
441
440
|
} = getCLIEngineInternalSlots(cliEngine);
|
442
441
|
let updated = false;
|
443
442
|
|
444
|
-
/*
|
445
|
-
* Address `plugins` to add plugin implementations.
|
446
|
-
* Operate the `additionalPluginPool` internal slot directly to avoid
|
447
|
-
* using `addPlugin(id, plugin)` method that resets cache everytime.
|
448
|
-
*/
|
449
|
-
if (options.plugins) {
|
450
|
-
for (const [id, plugin] of Object.entries(options.plugins)) {
|
451
|
-
additionalPluginPool.set(id, plugin);
|
452
|
-
updated = true;
|
453
|
-
}
|
454
|
-
}
|
455
|
-
|
456
443
|
/*
|
457
444
|
* Address `overrideConfig` to set override config.
|
458
445
|
* Operate the `configArrayFactory` internal slot directly because this
|
@@ -15,7 +15,7 @@ const levn = require("levn"),
|
|
15
15
|
Legacy: {
|
16
16
|
ConfigOps
|
17
17
|
}
|
18
|
-
} = require("@eslint/eslintrc/universal");
|
18
|
+
} = require("@eslint/eslintrc/universal");
|
19
19
|
|
20
20
|
const debug = require("debug")("eslint:config-comment-parser");
|
21
21
|
|
package/lib/linter/linter.js
CHANGED
@@ -24,7 +24,7 @@ const
|
|
24
24
|
ConfigValidator,
|
25
25
|
environments: BuiltInEnvironments
|
26
26
|
}
|
27
|
-
} = require("@eslint/eslintrc/universal"),
|
27
|
+
} = require("@eslint/eslintrc/universal"),
|
28
28
|
Traverser = require("../shared/traverser"),
|
29
29
|
{ SourceCode } = require("../source-code"),
|
30
30
|
CodePathAnalyzer = require("./code-path-analysis/code-path-analyzer"),
|
@@ -98,6 +98,13 @@ function getPossibleTypes(parsedSelector) {
|
|
98
98
|
case "adjacent":
|
99
99
|
return getPossibleTypes(parsedSelector.right);
|
100
100
|
|
101
|
+
case "class":
|
102
|
+
if (parsedSelector.name === "function") {
|
103
|
+
return ["FunctionDeclaration", "FunctionExpression", "ArrowFunctionExpression"];
|
104
|
+
}
|
105
|
+
|
106
|
+
return null;
|
107
|
+
|
101
108
|
default:
|
102
109
|
return null;
|
103
110
|
|
@@ -67,6 +67,7 @@ const { SourceCode } = require("../source-code");
|
|
67
67
|
/**
|
68
68
|
* A test case that is expected to pass lint.
|
69
69
|
* @typedef {Object} ValidTestCase
|
70
|
+
* @property {string} [name] Name for the test case.
|
70
71
|
* @property {string} code Code for the test case.
|
71
72
|
* @property {any[]} [options] Options for the test case.
|
72
73
|
* @property {{ [name: string]: any }} [settings] Settings for the test case.
|
@@ -81,6 +82,7 @@ const { SourceCode } = require("../source-code");
|
|
81
82
|
/**
|
82
83
|
* A test case that is expected to fail lint.
|
83
84
|
* @typedef {Object} InvalidTestCase
|
85
|
+
* @property {string} [name] Name for the test case.
|
84
86
|
* @property {string} code Code for the test case.
|
85
87
|
* @property {number | Array<TestCaseError | string | RegExp>} errors Expected errors.
|
86
88
|
* @property {string | null} [output] The expected code after autofixes are applied. If set to `null`, the test runner will assert that no autofix is suggested.
|
@@ -124,6 +126,7 @@ let defaultConfig = { rules: {} };
|
|
124
126
|
* configuration
|
125
127
|
*/
|
126
128
|
const RuleTesterParameters = [
|
129
|
+
"name",
|
127
130
|
"code",
|
128
131
|
"filename",
|
129
132
|
"options",
|
@@ -964,7 +967,7 @@ class RuleTester {
|
|
964
967
|
RuleTester.describe("valid", () => {
|
965
968
|
test.valid.forEach(valid => {
|
966
969
|
RuleTester[valid.only ? "itOnly" : "it"](
|
967
|
-
sanitize(typeof valid === "object" ? valid.code : valid),
|
970
|
+
sanitize(typeof valid === "object" ? valid.name || valid.code : valid),
|
968
971
|
() => {
|
969
972
|
testValidTemplate(valid);
|
970
973
|
}
|
@@ -975,7 +978,7 @@ class RuleTester {
|
|
975
978
|
RuleTester.describe("invalid", () => {
|
976
979
|
test.invalid.forEach(invalid => {
|
977
980
|
RuleTester[invalid.only ? "itOnly" : "it"](
|
978
|
-
sanitize(invalid.code),
|
981
|
+
sanitize(invalid.name || invalid.code),
|
979
982
|
() => {
|
980
983
|
testInvalidTemplate(invalid);
|
981
984
|
}
|
package/lib/rules/index.js
CHANGED
@@ -221,6 +221,7 @@ module.exports = new LazyLoadingRuleMap(Object.entries({
|
|
221
221
|
"no-unsafe-optional-chaining": () => require("./no-unsafe-optional-chaining"),
|
222
222
|
"no-unused-expressions": () => require("./no-unused-expressions"),
|
223
223
|
"no-unused-labels": () => require("./no-unused-labels"),
|
224
|
+
"no-unused-private-class-members": () => require("./no-unused-private-class-members"),
|
224
225
|
"no-unused-vars": () => require("./no-unused-vars"),
|
225
226
|
"no-use-before-define": () => require("./no-use-before-define"),
|
226
227
|
"no-useless-backreference": () => require("./no-useless-backreference"),
|
package/lib/rules/key-spacing.js
CHANGED
@@ -427,19 +427,7 @@ module.exports = {
|
|
427
427
|
* @returns {void}
|
428
428
|
*/
|
429
429
|
function report(property, side, whitespace, expected, mode) {
|
430
|
-
const diff = whitespace.length - expected
|
431
|
-
nextColon = getNextColon(property.key),
|
432
|
-
tokenBeforeColon = sourceCode.getTokenBefore(nextColon, { includeComments: true }),
|
433
|
-
tokenAfterColon = sourceCode.getTokenAfter(nextColon, { includeComments: true }),
|
434
|
-
isKeySide = side === "key",
|
435
|
-
isExtra = diff > 0,
|
436
|
-
diffAbs = Math.abs(diff),
|
437
|
-
spaces = Array(diffAbs + 1).join(" ");
|
438
|
-
|
439
|
-
const locStart = isKeySide ? tokenBeforeColon.loc.end : nextColon.loc.start;
|
440
|
-
const locEnd = isKeySide ? nextColon.loc.start : tokenAfterColon.loc.start;
|
441
|
-
const missingLoc = isKeySide ? tokenBeforeColon.loc : tokenAfterColon.loc;
|
442
|
-
const loc = isExtra ? { start: locStart, end: locEnd } : missingLoc;
|
430
|
+
const diff = whitespace.length - expected;
|
443
431
|
|
444
432
|
if ((
|
445
433
|
diff && mode === "strict" ||
|
@@ -447,6 +435,19 @@ module.exports = {
|
|
447
435
|
diff > 0 && !expected && mode === "minimum") &&
|
448
436
|
!(expected && containsLineTerminator(whitespace))
|
449
437
|
) {
|
438
|
+
const nextColon = getNextColon(property.key),
|
439
|
+
tokenBeforeColon = sourceCode.getTokenBefore(nextColon, { includeComments: true }),
|
440
|
+
tokenAfterColon = sourceCode.getTokenAfter(nextColon, { includeComments: true }),
|
441
|
+
isKeySide = side === "key",
|
442
|
+
isExtra = diff > 0,
|
443
|
+
diffAbs = Math.abs(diff),
|
444
|
+
spaces = Array(diffAbs + 1).join(" ");
|
445
|
+
|
446
|
+
const locStart = isKeySide ? tokenBeforeColon.loc.end : nextColon.loc.start;
|
447
|
+
const locEnd = isKeySide ? nextColon.loc.start : tokenAfterColon.loc.start;
|
448
|
+
const missingLoc = isKeySide ? tokenBeforeColon.loc : tokenAfterColon.loc;
|
449
|
+
const loc = isExtra ? { start: locStart, end: locEnd } : missingLoc;
|
450
|
+
|
450
451
|
let fix;
|
451
452
|
|
452
453
|
if (isExtra) {
|
@@ -109,6 +109,8 @@ module.exports = {
|
|
109
109
|
create(context) {
|
110
110
|
const sourceCode = context.getSourceCode();
|
111
111
|
|
112
|
+
const tokensToIgnore = new WeakSet();
|
113
|
+
|
112
114
|
/**
|
113
115
|
* Reports a given token if there are not space(s) before the token.
|
114
116
|
* @param {Token} token A token to report.
|
@@ -121,6 +123,7 @@ module.exports = {
|
|
121
123
|
if (prevToken &&
|
122
124
|
(CHECK_TYPE.test(prevToken.type) || pattern.test(prevToken.value)) &&
|
123
125
|
!isOpenParenOfTemplate(prevToken) &&
|
126
|
+
!tokensToIgnore.has(prevToken) &&
|
124
127
|
astUtils.isTokenOnSameLine(prevToken, token) &&
|
125
128
|
!sourceCode.isSpaceBetweenTokens(prevToken, token)
|
126
129
|
) {
|
@@ -147,6 +150,7 @@ module.exports = {
|
|
147
150
|
if (prevToken &&
|
148
151
|
(CHECK_TYPE.test(prevToken.type) || pattern.test(prevToken.value)) &&
|
149
152
|
!isOpenParenOfTemplate(prevToken) &&
|
153
|
+
!tokensToIgnore.has(prevToken) &&
|
150
154
|
astUtils.isTokenOnSameLine(prevToken, token) &&
|
151
155
|
sourceCode.isSpaceBetweenTokens(prevToken, token)
|
152
156
|
) {
|
@@ -173,6 +177,7 @@ module.exports = {
|
|
173
177
|
if (nextToken &&
|
174
178
|
(CHECK_TYPE.test(nextToken.type) || pattern.test(nextToken.value)) &&
|
175
179
|
!isCloseParenOfTemplate(nextToken) &&
|
180
|
+
!tokensToIgnore.has(nextToken) &&
|
176
181
|
astUtils.isTokenOnSameLine(token, nextToken) &&
|
177
182
|
!sourceCode.isSpaceBetweenTokens(token, nextToken)
|
178
183
|
) {
|
@@ -199,6 +204,7 @@ module.exports = {
|
|
199
204
|
if (nextToken &&
|
200
205
|
(CHECK_TYPE.test(nextToken.type) || pattern.test(nextToken.value)) &&
|
201
206
|
!isCloseParenOfTemplate(nextToken) &&
|
207
|
+
!tokensToIgnore.has(nextToken) &&
|
202
208
|
astUtils.isTokenOnSameLine(token, nextToken) &&
|
203
209
|
sourceCode.isSpaceBetweenTokens(token, nextToken)
|
204
210
|
) {
|
@@ -584,7 +590,14 @@ module.exports = {
|
|
584
590
|
ImportNamespaceSpecifier: checkSpacingForImportNamespaceSpecifier,
|
585
591
|
MethodDefinition: checkSpacingForProperty,
|
586
592
|
PropertyDefinition: checkSpacingForProperty,
|
587
|
-
Property: checkSpacingForProperty
|
593
|
+
Property: checkSpacingForProperty,
|
594
|
+
|
595
|
+
// To avoid conflicts with `space-infix-ops`, e.g. `a > this.b`
|
596
|
+
"BinaryExpression[operator='>']"(node) {
|
597
|
+
const operatorToken = sourceCode.getTokenBefore(node.right, astUtils.isNotOpeningParenToken);
|
598
|
+
|
599
|
+
tokensToIgnore.add(operatorToken);
|
600
|
+
}
|
588
601
|
};
|
589
602
|
}
|
590
603
|
};
|
@@ -0,0 +1,194 @@
|
|
1
|
+
/**
|
2
|
+
* @fileoverview Rule to flag declared but unused private class members
|
3
|
+
* @author Tim van der Lippe
|
4
|
+
*/
|
5
|
+
|
6
|
+
"use strict";
|
7
|
+
|
8
|
+
//------------------------------------------------------------------------------
|
9
|
+
// Rule Definition
|
10
|
+
//------------------------------------------------------------------------------
|
11
|
+
|
12
|
+
module.exports = {
|
13
|
+
meta: {
|
14
|
+
type: "problem",
|
15
|
+
|
16
|
+
docs: {
|
17
|
+
description: "disallow unused private class members",
|
18
|
+
recommended: false,
|
19
|
+
url: "https://eslint.org/docs/rules/no-unused-private-class-members"
|
20
|
+
},
|
21
|
+
|
22
|
+
schema: [],
|
23
|
+
|
24
|
+
messages: {
|
25
|
+
unusedPrivateClassMember: "'{{classMemberName}}' is defined but never used."
|
26
|
+
}
|
27
|
+
},
|
28
|
+
|
29
|
+
create(context) {
|
30
|
+
const trackedClasses = [];
|
31
|
+
|
32
|
+
/**
|
33
|
+
* Check whether the current node is in a write only assignment.
|
34
|
+
* @param {ASTNode} privateIdentifierNode Node referring to a private identifier
|
35
|
+
* @returns {boolean} Whether the node is in a write only assignment
|
36
|
+
* @private
|
37
|
+
*/
|
38
|
+
function isWriteOnlyAssignment(privateIdentifierNode) {
|
39
|
+
const parentStatement = privateIdentifierNode.parent.parent;
|
40
|
+
const isAssignmentExpression = parentStatement.type === "AssignmentExpression";
|
41
|
+
|
42
|
+
if (!isAssignmentExpression &&
|
43
|
+
parentStatement.type !== "ForInStatement" &&
|
44
|
+
parentStatement.type !== "ForOfStatement" &&
|
45
|
+
parentStatement.type !== "AssignmentPattern") {
|
46
|
+
return false;
|
47
|
+
}
|
48
|
+
|
49
|
+
// It is a write-only usage, since we still allow usages on the right for reads
|
50
|
+
if (parentStatement.left !== privateIdentifierNode.parent) {
|
51
|
+
return false;
|
52
|
+
}
|
53
|
+
|
54
|
+
// For any other operator (such as '+=') we still consider it a read operation
|
55
|
+
if (isAssignmentExpression && parentStatement.operator !== "=") {
|
56
|
+
|
57
|
+
/*
|
58
|
+
* However, if the read operation is "discarded" in an empty statement, then
|
59
|
+
* we consider it write only.
|
60
|
+
*/
|
61
|
+
return parentStatement.parent.type === "ExpressionStatement";
|
62
|
+
}
|
63
|
+
|
64
|
+
return true;
|
65
|
+
}
|
66
|
+
|
67
|
+
//--------------------------------------------------------------------------
|
68
|
+
// Public
|
69
|
+
//--------------------------------------------------------------------------
|
70
|
+
|
71
|
+
return {
|
72
|
+
|
73
|
+
// Collect all declared members up front and assume they are all unused
|
74
|
+
ClassBody(classBodyNode) {
|
75
|
+
const privateMembers = new Map();
|
76
|
+
|
77
|
+
trackedClasses.unshift(privateMembers);
|
78
|
+
for (const bodyMember of classBodyNode.body) {
|
79
|
+
if (bodyMember.type === "PropertyDefinition" || bodyMember.type === "MethodDefinition") {
|
80
|
+
if (bodyMember.key.type === "PrivateIdentifier") {
|
81
|
+
privateMembers.set(bodyMember.key.name, {
|
82
|
+
declaredNode: bodyMember,
|
83
|
+
isAccessor: bodyMember.type === "MethodDefinition" &&
|
84
|
+
(bodyMember.kind === "set" || bodyMember.kind === "get")
|
85
|
+
});
|
86
|
+
}
|
87
|
+
}
|
88
|
+
}
|
89
|
+
},
|
90
|
+
|
91
|
+
/*
|
92
|
+
* Process all usages of the private identifier and remove a member from
|
93
|
+
* `declaredAndUnusedPrivateMembers` if we deem it used.
|
94
|
+
*/
|
95
|
+
PrivateIdentifier(privateIdentifierNode) {
|
96
|
+
const classBody = trackedClasses.find(classProperties => classProperties.has(privateIdentifierNode.name));
|
97
|
+
|
98
|
+
// Can't happen, as it is a parser to have a missing class body, but let's code defensively here.
|
99
|
+
if (!classBody) {
|
100
|
+
return;
|
101
|
+
}
|
102
|
+
|
103
|
+
// In case any other usage was already detected, we can short circuit the logic here.
|
104
|
+
const memberDefinition = classBody.get(privateIdentifierNode.name);
|
105
|
+
|
106
|
+
if (memberDefinition.isUsed) {
|
107
|
+
return;
|
108
|
+
}
|
109
|
+
|
110
|
+
// The definition of the class member itself
|
111
|
+
if (privateIdentifierNode.parent.type === "PropertyDefinition" ||
|
112
|
+
privateIdentifierNode.parent.type === "MethodDefinition") {
|
113
|
+
return;
|
114
|
+
}
|
115
|
+
|
116
|
+
/*
|
117
|
+
* Any usage of an accessor is considered a read, as the getter/setter can have
|
118
|
+
* side-effects in its definition.
|
119
|
+
*/
|
120
|
+
if (memberDefinition.isAccessor) {
|
121
|
+
memberDefinition.isUsed = true;
|
122
|
+
return;
|
123
|
+
}
|
124
|
+
|
125
|
+
// Any assignments to this member, except for assignments that also read
|
126
|
+
if (isWriteOnlyAssignment(privateIdentifierNode)) {
|
127
|
+
return;
|
128
|
+
}
|
129
|
+
|
130
|
+
const wrappingExpressionType = privateIdentifierNode.parent.parent.type;
|
131
|
+
const parentOfWrappingExpressionType = privateIdentifierNode.parent.parent.parent.type;
|
132
|
+
|
133
|
+
// A statement which only increments (`this.#x++;`)
|
134
|
+
if (wrappingExpressionType === "UpdateExpression" &&
|
135
|
+
parentOfWrappingExpressionType === "ExpressionStatement") {
|
136
|
+
return;
|
137
|
+
}
|
138
|
+
|
139
|
+
/*
|
140
|
+
* ({ x: this.#usedInDestructuring } = bar);
|
141
|
+
*
|
142
|
+
* But should treat the following as a read:
|
143
|
+
* ({ [this.#x]: a } = foo);
|
144
|
+
*/
|
145
|
+
if (wrappingExpressionType === "Property" &&
|
146
|
+
parentOfWrappingExpressionType === "ObjectPattern" &&
|
147
|
+
privateIdentifierNode.parent.parent.value === privateIdentifierNode.parent) {
|
148
|
+
return;
|
149
|
+
}
|
150
|
+
|
151
|
+
// [...this.#unusedInRestPattern] = bar;
|
152
|
+
if (wrappingExpressionType === "RestElement") {
|
153
|
+
return;
|
154
|
+
}
|
155
|
+
|
156
|
+
// [this.#unusedInAssignmentPattern] = bar;
|
157
|
+
if (wrappingExpressionType === "ArrayPattern") {
|
158
|
+
return;
|
159
|
+
}
|
160
|
+
|
161
|
+
/*
|
162
|
+
* We can't delete the memberDefinition, as we need to keep track of which member we are marking as used.
|
163
|
+
* In the case of nested classes, we only mark the first member we encounter as used. If you were to delete
|
164
|
+
* the member, then any subsequent usage could incorrectly mark the member of an encapsulating parent class
|
165
|
+
* as used, which is incorrect.
|
166
|
+
*/
|
167
|
+
memberDefinition.isUsed = true;
|
168
|
+
},
|
169
|
+
|
170
|
+
/*
|
171
|
+
* Post-process the class members and report any remaining members.
|
172
|
+
* Since private members can only be accessed in the current class context,
|
173
|
+
* we can safely assume that all usages are within the current class body.
|
174
|
+
*/
|
175
|
+
"ClassBody:exit"() {
|
176
|
+
const unusedPrivateMembers = trackedClasses.shift();
|
177
|
+
|
178
|
+
for (const [classMemberName, { declaredNode, isUsed }] of unusedPrivateMembers.entries()) {
|
179
|
+
if (isUsed) {
|
180
|
+
continue;
|
181
|
+
}
|
182
|
+
context.report({
|
183
|
+
node: declaredNode,
|
184
|
+
loc: declaredNode.key.loc,
|
185
|
+
messageId: "unusedPrivateClassMember",
|
186
|
+
data: {
|
187
|
+
classMemberName: `#${classMemberName}`
|
188
|
+
}
|
189
|
+
});
|
190
|
+
}
|
191
|
+
}
|
192
|
+
};
|
193
|
+
}
|
194
|
+
};
|
@@ -34,52 +34,91 @@ function parseOptions(options) {
|
|
34
34
|
}
|
35
35
|
|
36
36
|
/**
|
37
|
-
* Checks whether or not a given
|
38
|
-
* @param {
|
39
|
-
* @
|
37
|
+
* Checks whether or not a given location is inside of the range of a given node.
|
38
|
+
* @param {ASTNode} node An node to check.
|
39
|
+
* @param {number} location A location to check.
|
40
|
+
* @returns {boolean} `true` if the location is inside of the range of the node.
|
40
41
|
*/
|
41
|
-
function
|
42
|
-
return
|
42
|
+
function isInRange(node, location) {
|
43
|
+
return node && node.range[0] <= location && location <= node.range[1];
|
43
44
|
}
|
44
45
|
|
45
46
|
/**
|
46
|
-
* Checks whether or not a given
|
47
|
-
* @param {
|
48
|
-
* @param {
|
49
|
-
* @returns {boolean} `true` if the
|
47
|
+
* Checks whether or not a given location is inside of the range of a class static initializer.
|
48
|
+
* @param {ASTNode} node `ClassBody` node to check static initializers.
|
49
|
+
* @param {number} location A location to check.
|
50
|
+
* @returns {boolean} `true` if the location is inside of a class static initializer.
|
50
51
|
*/
|
51
|
-
function
|
52
|
-
return (
|
53
|
-
|
54
|
-
|
55
|
-
|
52
|
+
function isInClassStaticInitializerRange(node, location) {
|
53
|
+
return node.body.some(classMember => (
|
54
|
+
classMember.type === "PropertyDefinition" &&
|
55
|
+
classMember.static &&
|
56
|
+
classMember.value &&
|
57
|
+
isInRange(classMember.value, location)
|
58
|
+
));
|
56
59
|
}
|
57
60
|
|
58
61
|
/**
|
59
|
-
* Checks whether
|
60
|
-
* @param {eslint-scope.
|
61
|
-
* @
|
62
|
-
* @returns {boolean} `true` if the variable is a variable declaration.
|
62
|
+
* Checks whether a given scope is the scope of a static class field initializer.
|
63
|
+
* @param {eslint-scope.Scope} scope A scope to check.
|
64
|
+
* @returns {boolean} `true` if the scope is a class static initializer scope.
|
63
65
|
*/
|
64
|
-
function
|
65
|
-
|
66
|
-
|
67
|
-
|
68
|
-
|
66
|
+
function isClassStaticInitializerScope(scope) {
|
67
|
+
if (scope.type === "class-field-initializer") {
|
68
|
+
|
69
|
+
// `scope.block` is PropertyDefinition#value node
|
70
|
+
const propertyDefinition = scope.block.parent;
|
71
|
+
|
72
|
+
return propertyDefinition.static;
|
73
|
+
}
|
74
|
+
|
75
|
+
return false;
|
69
76
|
}
|
70
77
|
|
71
78
|
/**
|
72
|
-
* Checks whether
|
73
|
-
*
|
74
|
-
*
|
75
|
-
*
|
79
|
+
* Checks whether a given reference is evaluated in an execution context
|
80
|
+
* that isn't the one where the variable it refers to is defined.
|
81
|
+
* Execution contexts are:
|
82
|
+
* - top-level
|
83
|
+
* - functions
|
84
|
+
* - class field initializers (implicit functions)
|
85
|
+
* Static class field initializers are automatically run during the class definition evaluation,
|
86
|
+
* and therefore we'll consider them as a part of the parent execution context.
|
87
|
+
* Example:
|
88
|
+
*
|
89
|
+
* const x = 1;
|
90
|
+
*
|
91
|
+
* x; // returns `false`
|
92
|
+
* () => x; // returns `true`
|
93
|
+
* class C {
|
94
|
+
* field = x; // returns `true`
|
95
|
+
* static field = x; // returns `false`
|
96
|
+
*
|
97
|
+
* method() {
|
98
|
+
* x; // returns `true`
|
99
|
+
* }
|
100
|
+
* }
|
101
|
+
* @param {eslint-scope.Reference} reference A reference to check.
|
102
|
+
* @returns {boolean} `true` if the reference is from a separate execution context.
|
76
103
|
*/
|
77
|
-
function
|
78
|
-
|
104
|
+
function isFromSeparateExecutionContext(reference) {
|
105
|
+
const variable = reference.resolved;
|
106
|
+
let scope = reference.from;
|
107
|
+
|
108
|
+
// Scope#variableScope represents execution context
|
109
|
+
while (variable.scope.variableScope !== scope.variableScope) {
|
110
|
+
if (isClassStaticInitializerScope(scope.variableScope)) {
|
111
|
+
scope = scope.variableScope.upper;
|
112
|
+
} else {
|
113
|
+
return true;
|
114
|
+
}
|
115
|
+
}
|
116
|
+
|
117
|
+
return false;
|
79
118
|
}
|
80
119
|
|
81
120
|
/**
|
82
|
-
* Checks whether or not a given reference is
|
121
|
+
* Checks whether or not a given reference is evaluated during the initialization of its variable.
|
83
122
|
*
|
84
123
|
* This returns `true` in the following cases:
|
85
124
|
*
|
@@ -88,17 +127,44 @@ function isInRange(node, location) {
|
|
88
127
|
* var {a = a} = obj
|
89
128
|
* for (var a in a) {}
|
90
129
|
* for (var a of a) {}
|
91
|
-
*
|
130
|
+
* var C = class { [C]; }
|
131
|
+
* var C = class { static foo = C; }
|
132
|
+
* class C extends C {}
|
133
|
+
* class C extends (class { static foo = C; }) {}
|
134
|
+
* class C { [C]; }
|
92
135
|
* @param {Reference} reference A reference to check.
|
93
|
-
* @returns {boolean} `true` if the reference is
|
136
|
+
* @returns {boolean} `true` if the reference is evaluated during the initialization.
|
94
137
|
*/
|
95
|
-
function
|
96
|
-
if (
|
138
|
+
function isEvaluatedDuringInitialization(reference) {
|
139
|
+
if (isFromSeparateExecutionContext(reference)) {
|
140
|
+
|
141
|
+
/*
|
142
|
+
* Even if the reference appears in the initializer, it isn't evaluated during the initialization.
|
143
|
+
* For example, `const x = () => x;` is valid.
|
144
|
+
*/
|
97
145
|
return false;
|
98
146
|
}
|
99
147
|
|
100
|
-
let node = variable.identifiers[0].parent;
|
101
148
|
const location = reference.identifier.range[1];
|
149
|
+
const definition = reference.resolved.defs[0];
|
150
|
+
|
151
|
+
if (definition.type === "ClassName") {
|
152
|
+
|
153
|
+
// `ClassDeclaration` or `ClassExpression`
|
154
|
+
const classDefinition = definition.node;
|
155
|
+
|
156
|
+
return (
|
157
|
+
isInRange(classDefinition, location) &&
|
158
|
+
|
159
|
+
/*
|
160
|
+
* Class binding is initialized before running static initializers.
|
161
|
+
* For example, `class C { static foo = C; }` is valid.
|
162
|
+
*/
|
163
|
+
!isInClassStaticInitializerRange(classDefinition.body, location)
|
164
|
+
);
|
165
|
+
}
|
166
|
+
|
167
|
+
let node = definition.name.parent;
|
102
168
|
|
103
169
|
while (node) {
|
104
170
|
if (node.type === "VariableDeclarator") {
|
@@ -167,65 +233,77 @@ module.exports = {
|
|
167
233
|
const options = parseOptions(context.options[0]);
|
168
234
|
|
169
235
|
/**
|
170
|
-
* Determines whether a given
|
171
|
-
*
|
172
|
-
*
|
173
|
-
*
|
236
|
+
* Determines whether a given reference should be checked.
|
237
|
+
*
|
238
|
+
* Returns `false` if the reference is:
|
239
|
+
* - initialization's (e.g., `let a = 1`).
|
240
|
+
* - referring to an undefined variable (i.e., if it's an unresolved reference).
|
241
|
+
* - referring to a variable that is defined, but not in the given source code
|
242
|
+
* (e.g., global environment variable or `arguments` in functions).
|
243
|
+
* - allowed by options.
|
244
|
+
* @param {eslint-scope.Reference} reference The reference
|
245
|
+
* @returns {boolean} `true` if the reference should be checked
|
174
246
|
*/
|
175
|
-
function
|
176
|
-
if (
|
177
|
-
return
|
247
|
+
function shouldCheck(reference) {
|
248
|
+
if (reference.init) {
|
249
|
+
return false;
|
250
|
+
}
|
251
|
+
|
252
|
+
const variable = reference.resolved;
|
253
|
+
|
254
|
+
if (!variable || variable.defs.length === 0) {
|
255
|
+
return false;
|
178
256
|
}
|
179
|
-
|
180
|
-
|
257
|
+
|
258
|
+
const definitionType = variable.defs[0].type;
|
259
|
+
|
260
|
+
if (!options.functions && definitionType === "FunctionName") {
|
261
|
+
return false;
|
181
262
|
}
|
182
|
-
|
183
|
-
|
263
|
+
|
264
|
+
if (
|
265
|
+
(
|
266
|
+
!options.variables && definitionType === "Variable" ||
|
267
|
+
!options.classes && definitionType === "ClassName"
|
268
|
+
) &&
|
269
|
+
|
270
|
+
// don't skip checking the reference if it's in the same execution context, because of TDZ
|
271
|
+
isFromSeparateExecutionContext(reference)
|
272
|
+
) {
|
273
|
+
return false;
|
184
274
|
}
|
275
|
+
|
185
276
|
return true;
|
186
277
|
}
|
187
278
|
|
188
279
|
/**
|
189
|
-
* Finds and validates all
|
190
|
-
* @param {Scope} scope The scope object.
|
280
|
+
* Finds and validates all references in a given scope and its child scopes.
|
281
|
+
* @param {eslint-scope.Scope} scope The scope object.
|
191
282
|
* @returns {void}
|
192
|
-
* @private
|
193
283
|
*/
|
194
|
-
function
|
195
|
-
scope.references.forEach(reference => {
|
284
|
+
function checkReferencesInScope(scope) {
|
285
|
+
scope.references.filter(shouldCheck).forEach(reference => {
|
196
286
|
const variable = reference.resolved;
|
287
|
+
const definitionIdentifier = variable.defs[0].name;
|
197
288
|
|
198
|
-
|
199
|
-
|
200
|
-
|
201
|
-
* - referring to an undefined variable.
|
202
|
-
* - referring to a global environment variable (there're no identifiers).
|
203
|
-
* - located preceded by the variable (except in initializers).
|
204
|
-
* - allowed by options.
|
205
|
-
*/
|
206
|
-
if (reference.init ||
|
207
|
-
!variable ||
|
208
|
-
variable.identifiers.length === 0 ||
|
209
|
-
(variable.identifiers[0].range[1] < reference.identifier.range[1] && !isInInitializer(variable, reference)) ||
|
210
|
-
!isForbidden(variable, reference)
|
289
|
+
if (
|
290
|
+
reference.identifier.range[1] < definitionIdentifier.range[1] ||
|
291
|
+
isEvaluatedDuringInitialization(reference)
|
211
292
|
) {
|
212
|
-
|
293
|
+
context.report({
|
294
|
+
node: reference.identifier,
|
295
|
+
messageId: "usedBeforeDefined",
|
296
|
+
data: reference.identifier
|
297
|
+
});
|
213
298
|
}
|
214
|
-
|
215
|
-
// Reports.
|
216
|
-
context.report({
|
217
|
-
node: reference.identifier,
|
218
|
-
messageId: "usedBeforeDefined",
|
219
|
-
data: reference.identifier
|
220
|
-
});
|
221
299
|
});
|
222
300
|
|
223
|
-
scope.childScopes.forEach(
|
301
|
+
scope.childScopes.forEach(checkReferencesInScope);
|
224
302
|
}
|
225
303
|
|
226
304
|
return {
|
227
305
|
Program() {
|
228
|
-
|
306
|
+
checkReferencesInScope(context.getScope());
|
229
307
|
}
|
230
308
|
};
|
231
309
|
}
|
@@ -179,7 +179,8 @@ module.exports = {
|
|
179
179
|
schema: [],
|
180
180
|
|
181
181
|
messages: {
|
182
|
-
nonAtomicUpdate: "Possible race condition: `{{value}}` might be reassigned based on an outdated value of `{{value}}`."
|
182
|
+
nonAtomicUpdate: "Possible race condition: `{{value}}` might be reassigned based on an outdated value of `{{value}}`.",
|
183
|
+
nonAtomicObjectUpdate: "Possible race condition: `{{value}}` might be assigned based on an outdated state of `{{object}}`."
|
183
184
|
}
|
184
185
|
},
|
185
186
|
|
@@ -275,13 +276,25 @@ module.exports = {
|
|
275
276
|
const variable = reference.resolved;
|
276
277
|
|
277
278
|
if (segmentInfo.isOutdated(codePath.currentSegments, variable)) {
|
278
|
-
|
279
|
-
|
280
|
-
|
281
|
-
|
282
|
-
|
283
|
-
|
284
|
-
|
279
|
+
if (node.parent.left === reference.identifier) {
|
280
|
+
context.report({
|
281
|
+
node: node.parent,
|
282
|
+
messageId: "nonAtomicUpdate",
|
283
|
+
data: {
|
284
|
+
value: variable.name
|
285
|
+
}
|
286
|
+
});
|
287
|
+
} else {
|
288
|
+
context.report({
|
289
|
+
node: node.parent,
|
290
|
+
messageId: "nonAtomicObjectUpdate",
|
291
|
+
data: {
|
292
|
+
value: sourceCode.getText(node.parent.left),
|
293
|
+
object: variable.name
|
294
|
+
}
|
295
|
+
});
|
296
|
+
}
|
297
|
+
|
285
298
|
}
|
286
299
|
}
|
287
300
|
}
|
package/lib/shared/types.js
CHANGED
@@ -21,7 +21,7 @@ module.exports = {};
|
|
21
21
|
/**
|
22
22
|
* @typedef {Object} ParserOptions
|
23
23
|
* @property {EcmaFeatures} [ecmaFeatures] The optional features.
|
24
|
-
* @property {3|5|6|7|8|9|10|11|12|2015|2016|2017|2018|2019|2020|2021} [ecmaVersion] The ECMAScript version (or revision number).
|
24
|
+
* @property {3|5|6|7|8|9|10|11|12|13|2015|2016|2017|2018|2019|2020|2021|2022} [ecmaVersion] The ECMAScript version (or revision number).
|
25
25
|
* @property {"script"|"module"} [sourceType] The source code type.
|
26
26
|
*/
|
27
27
|
|
package/package.json
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
{
|
2
2
|
"name": "eslint",
|
3
|
-
"version": "8.
|
3
|
+
"version": "8.2.0",
|
4
4
|
"author": "Nicholas C. Zakas <nicholas+npm@nczconsulting.com>",
|
5
5
|
"description": "An AST-based pattern checker for JavaScript.",
|
6
6
|
"bin": {
|
@@ -47,7 +47,7 @@
|
|
47
47
|
"homepage": "https://eslint.org",
|
48
48
|
"bugs": "https://github.com/eslint/eslint/issues/",
|
49
49
|
"dependencies": {
|
50
|
-
"@eslint/eslintrc": "^1.0.
|
50
|
+
"@eslint/eslintrc": "^1.0.4",
|
51
51
|
"@humanwhocodes/config-array": "^0.6.0",
|
52
52
|
"ajv": "^6.10.0",
|
53
53
|
"chalk": "^4.0.0",
|
@@ -81,7 +81,7 @@
|
|
81
81
|
"progress": "^2.0.0",
|
82
82
|
"regexpp": "^3.2.0",
|
83
83
|
"semver": "^7.2.1",
|
84
|
-
"strip-ansi": "^6.0.
|
84
|
+
"strip-ansi": "^6.0.1",
|
85
85
|
"strip-json-comments": "^3.1.0",
|
86
86
|
"text-table": "^0.2.0",
|
87
87
|
"v8-compile-cache": "^2.0.3"
|
@@ -99,11 +99,11 @@
|
|
99
99
|
"eslint": "file:.",
|
100
100
|
"eslint-config-eslint": "file:packages/eslint-config-eslint",
|
101
101
|
"eslint-plugin-eslint-comments": "^3.2.0",
|
102
|
-
"eslint-plugin-eslint-plugin": "^
|
102
|
+
"eslint-plugin-eslint-plugin": "^4.0.1",
|
103
103
|
"eslint-plugin-internal-rules": "file:tools/internal-rules",
|
104
|
-
"eslint-plugin-jsdoc": "^
|
104
|
+
"eslint-plugin-jsdoc": "^37.0.0",
|
105
105
|
"eslint-plugin-node": "^11.1.0",
|
106
|
-
"eslint-release": "^3.
|
106
|
+
"eslint-release": "^3.2.0",
|
107
107
|
"eslump": "^3.0.0",
|
108
108
|
"esprima": "^4.0.1",
|
109
109
|
"fs-teardown": "^0.1.3",
|