eslint 8.0.0 → 8.3.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 +8 -2
- package/lib/cli-engine/cli-engine.js +10 -21
- package/lib/eslint/eslint.js +2 -15
- package/lib/linter/code-path-analysis/code-path-analyzer.js +6 -1
- package/lib/linter/code-path-analysis/code-path.js +1 -1
- package/lib/linter/config-comment-parser.js +1 -1
- package/lib/linter/linter.js +2 -2
- package/lib/linter/node-event-generator.js +7 -0
- package/lib/rule-tester/rule-tester.js +5 -2
- package/lib/rules/block-scoped-var.js +2 -0
- package/lib/rules/block-spacing.js +10 -3
- package/lib/rules/brace-style.js +6 -0
- package/lib/rules/class-methods-use-this.js +10 -1
- package/lib/rules/complexity.js +14 -6
- package/lib/rules/indent.js +21 -0
- package/lib/rules/index.js +1 -0
- package/lib/rules/key-spacing.js +14 -13
- package/lib/rules/keyword-spacing.js +15 -1
- package/lib/rules/lines-around-comment.js +54 -7
- package/lib/rules/max-depth.js +2 -0
- package/lib/rules/max-statements.js +10 -0
- package/lib/rules/no-eval.js +2 -0
- package/lib/rules/no-extra-semi.js +1 -1
- package/lib/rules/no-inner-declarations.js +26 -4
- package/lib/rules/no-invalid-this.js +4 -0
- package/lib/rules/no-lone-blocks.js +8 -2
- package/lib/rules/no-redeclare.js +2 -0
- package/lib/rules/no-unused-expressions.js +6 -0
- package/lib/rules/no-unused-private-class-members.js +194 -0
- package/lib/rules/no-use-before-define.js +175 -74
- package/lib/rules/one-var.js +5 -1
- package/lib/rules/padded-blocks.js +8 -0
- package/lib/rules/padding-line-between-statements.js +2 -0
- package/lib/rules/prefer-const.js +1 -1
- package/lib/rules/require-atomic-updates.js +14 -2
- package/lib/rules/semi-style.js +8 -2
- package/lib/rules/semi.js +18 -9
- package/lib/rules/utils/ast-utils.js +15 -3
- package/lib/rules/vars-on-top.js +25 -12
- package/lib/shared/types.js +1 -1
- package/package.json +9 -9
@@ -123,6 +123,14 @@ module.exports = {
|
|
123
123
|
function endFunction(node) {
|
124
124
|
const count = functionStack.pop();
|
125
125
|
|
126
|
+
/*
|
127
|
+
* This rule does not apply to class static blocks, but we have to track them so
|
128
|
+
* that stataments in them do not count as statements in the enclosing function.
|
129
|
+
*/
|
130
|
+
if (node.type === "StaticBlock") {
|
131
|
+
return;
|
132
|
+
}
|
133
|
+
|
126
134
|
if (ignoreTopLevelFunctions && functionStack.length === 0) {
|
127
135
|
topLevelFunctions.push({ node, count });
|
128
136
|
} else {
|
@@ -148,12 +156,14 @@ module.exports = {
|
|
148
156
|
FunctionDeclaration: startFunction,
|
149
157
|
FunctionExpression: startFunction,
|
150
158
|
ArrowFunctionExpression: startFunction,
|
159
|
+
StaticBlock: startFunction,
|
151
160
|
|
152
161
|
BlockStatement: countStatements,
|
153
162
|
|
154
163
|
"FunctionDeclaration:exit": endFunction,
|
155
164
|
"FunctionExpression:exit": endFunction,
|
156
165
|
"ArrowFunctionExpression:exit": endFunction,
|
166
|
+
"StaticBlock:exit": endFunction,
|
157
167
|
|
158
168
|
"Program:exit"() {
|
159
169
|
if (topLevelFunctions.length === 1) {
|
package/lib/rules/no-eval.js
CHANGED
@@ -248,6 +248,8 @@ module.exports = {
|
|
248
248
|
"ArrowFunctionExpression:exit": exitVarScope,
|
249
249
|
"PropertyDefinition > *.value": enterVarScope,
|
250
250
|
"PropertyDefinition > *.value:exit": exitVarScope,
|
251
|
+
StaticBlock: enterVarScope,
|
252
|
+
"StaticBlock:exit": exitVarScope,
|
251
253
|
|
252
254
|
ThisExpression(node) {
|
253
255
|
if (!isMember(node.parent, "eval")) {
|
@@ -116,7 +116,7 @@ module.exports = {
|
|
116
116
|
* @param {Node} node A MethodDefinition node of the start point.
|
117
117
|
* @returns {void}
|
118
118
|
*/
|
119
|
-
"MethodDefinition, PropertyDefinition"(node) {
|
119
|
+
"MethodDefinition, PropertyDefinition, StaticBlock"(node) {
|
120
120
|
checkForPartOfClassBody(sourceCode.getTokenAfter(node));
|
121
121
|
}
|
122
122
|
};
|
@@ -15,9 +15,33 @@ const astUtils = require("./utils/ast-utils");
|
|
15
15
|
// Rule Definition
|
16
16
|
//------------------------------------------------------------------------------
|
17
17
|
|
18
|
-
const validParent = new Set(["Program", "ExportNamedDeclaration", "ExportDefaultDeclaration"]);
|
18
|
+
const validParent = new Set(["Program", "StaticBlock", "ExportNamedDeclaration", "ExportDefaultDeclaration"]);
|
19
19
|
const validBlockStatementParent = new Set(["FunctionDeclaration", "FunctionExpression", "ArrowFunctionExpression"]);
|
20
20
|
|
21
|
+
/**
|
22
|
+
* Finds the nearest enclosing context where this rule allows declarations and returns its description.
|
23
|
+
* @param {ASTNode} node Node to search from.
|
24
|
+
* @returns {string} Description. One of "program", "function body", "class static block body".
|
25
|
+
*/
|
26
|
+
function getAllowedBodyDescription(node) {
|
27
|
+
let { parent } = node;
|
28
|
+
|
29
|
+
while (parent) {
|
30
|
+
|
31
|
+
if (parent.type === "StaticBlock") {
|
32
|
+
return "class static block body";
|
33
|
+
}
|
34
|
+
|
35
|
+
if (astUtils.isFunction(parent)) {
|
36
|
+
return "function body";
|
37
|
+
}
|
38
|
+
|
39
|
+
({ parent } = parent);
|
40
|
+
}
|
41
|
+
|
42
|
+
return "program";
|
43
|
+
}
|
44
|
+
|
21
45
|
module.exports = {
|
22
46
|
meta: {
|
23
47
|
type: "problem",
|
@@ -59,14 +83,12 @@ module.exports = {
|
|
59
83
|
return;
|
60
84
|
}
|
61
85
|
|
62
|
-
const upperFunction = astUtils.getUpperFunction(parent);
|
63
|
-
|
64
86
|
context.report({
|
65
87
|
node,
|
66
88
|
messageId: "moveDeclToRoot",
|
67
89
|
data: {
|
68
90
|
type: (node.type === "FunctionDeclaration" ? "function" : "variable"),
|
69
|
-
body: (
|
91
|
+
body: getAllowedBodyDescription(node)
|
70
92
|
}
|
71
93
|
});
|
72
94
|
}
|
@@ -132,6 +132,10 @@ module.exports = {
|
|
132
132
|
"PropertyDefinition > *.value": enterFunction,
|
133
133
|
"PropertyDefinition > *.value:exit": exitFunction,
|
134
134
|
|
135
|
+
// Class static blocks are implicit functions.
|
136
|
+
StaticBlock: enterFunction,
|
137
|
+
"StaticBlock:exit": exitFunction,
|
138
|
+
|
135
139
|
// Reports if `this` of the current context is invalid.
|
136
140
|
ThisExpression(node) {
|
137
141
|
const current = stack.getCurrent();
|
@@ -39,7 +39,9 @@ module.exports = {
|
|
39
39
|
* @returns {void}
|
40
40
|
*/
|
41
41
|
function report(node) {
|
42
|
-
const messageId = node.parent.type === "BlockStatement"
|
42
|
+
const messageId = node.parent.type === "BlockStatement" || node.parent.type === "StaticBlock"
|
43
|
+
? "redundantNestedBlock"
|
44
|
+
: "redundantBlock";
|
43
45
|
|
44
46
|
context.report({
|
45
47
|
node,
|
@@ -54,6 +56,7 @@ module.exports = {
|
|
54
56
|
*/
|
55
57
|
function isLoneBlock(node) {
|
56
58
|
return node.parent.type === "BlockStatement" ||
|
59
|
+
node.parent.type === "StaticBlock" ||
|
57
60
|
node.parent.type === "Program" ||
|
58
61
|
|
59
62
|
// Don't report blocks in switch cases if the block is the only statement of the case.
|
@@ -99,7 +102,10 @@ module.exports = {
|
|
99
102
|
loneBlocks.pop();
|
100
103
|
report(node);
|
101
104
|
} else if (
|
102
|
-
|
105
|
+
(
|
106
|
+
node.parent.type === "BlockStatement" ||
|
107
|
+
node.parent.type === "StaticBlock"
|
108
|
+
) &&
|
103
109
|
node.parent.body.length === 1
|
104
110
|
) {
|
105
111
|
report(node);
|
@@ -115,6 +115,12 @@ module.exports = {
|
|
115
115
|
const parent = ancestors[ancestors.length - 1],
|
116
116
|
grandparent = ancestors[ancestors.length - 2];
|
117
117
|
|
118
|
+
/**
|
119
|
+
* https://tc39.es/ecma262/#directive-prologue
|
120
|
+
*
|
121
|
+
* Only `FunctionBody`, `ScriptBody` and `ModuleBody` can have directive prologue.
|
122
|
+
* Class static blocks do not have directive prologue.
|
123
|
+
*/
|
118
124
|
return (parent.type === "Program" || parent.type === "BlockStatement" &&
|
119
125
|
(/Function/u.test(grandparent.type))) &&
|
120
126
|
directives(parent).indexOf(node) >= 0;
|
@@ -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,113 @@ 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
|
-
*
|
48
|
-
* @param {
|
49
|
-
* @
|
47
|
+
* Checks whether or not a given location is inside of the range of a class static initializer.
|
48
|
+
* Static initializers are static blocks and initializers of static fields.
|
49
|
+
* @param {ASTNode} node `ClassBody` node to check static initializers.
|
50
|
+
* @param {number} location A location to check.
|
51
|
+
* @returns {boolean} `true` if the location is inside of a class static initializer.
|
50
52
|
*/
|
51
|
-
function
|
52
|
-
return (
|
53
|
-
|
54
|
-
|
55
|
-
|
53
|
+
function isInClassStaticInitializerRange(node, location) {
|
54
|
+
return node.body.some(classMember => (
|
55
|
+
(
|
56
|
+
classMember.type === "StaticBlock" &&
|
57
|
+
isInRange(classMember, location)
|
58
|
+
) ||
|
59
|
+
(
|
60
|
+
classMember.type === "PropertyDefinition" &&
|
61
|
+
classMember.static &&
|
62
|
+
classMember.value &&
|
63
|
+
isInRange(classMember.value, location)
|
64
|
+
)
|
65
|
+
));
|
56
66
|
}
|
57
67
|
|
58
68
|
/**
|
59
|
-
* Checks whether
|
60
|
-
*
|
61
|
-
* @param {eslint-scope.
|
62
|
-
* @returns {boolean} `true` if the
|
69
|
+
* Checks whether a given scope is the scope of a a class static initializer.
|
70
|
+
* Static initializers are static blocks and initializers of static fields.
|
71
|
+
* @param {eslint-scope.Scope} scope A scope to check.
|
72
|
+
* @returns {boolean} `true` if the scope is a class static initializer scope.
|
63
73
|
*/
|
64
|
-
function
|
65
|
-
|
66
|
-
|
67
|
-
|
68
|
-
|
74
|
+
function isClassStaticInitializerScope(scope) {
|
75
|
+
if (scope.type === "class-static-block") {
|
76
|
+
return true;
|
77
|
+
}
|
78
|
+
|
79
|
+
if (scope.type === "class-field-initializer") {
|
80
|
+
|
81
|
+
// `scope.block` is PropertyDefinition#value node
|
82
|
+
const propertyDefinition = scope.block.parent;
|
83
|
+
|
84
|
+
return propertyDefinition.static;
|
85
|
+
}
|
86
|
+
|
87
|
+
return false;
|
69
88
|
}
|
70
89
|
|
71
90
|
/**
|
72
|
-
* Checks whether
|
73
|
-
*
|
74
|
-
*
|
75
|
-
*
|
91
|
+
* Checks whether a given reference is evaluated in an execution context
|
92
|
+
* that isn't the one where the variable it refers to is defined.
|
93
|
+
* Execution contexts are:
|
94
|
+
* - top-level
|
95
|
+
* - functions
|
96
|
+
* - class field initializers (implicit functions)
|
97
|
+
* - class static blocks (implicit functions)
|
98
|
+
* Static class field initializers and class static blocks are automatically run during the class definition evaluation,
|
99
|
+
* and therefore we'll consider them as a part of the parent execution context.
|
100
|
+
* Example:
|
101
|
+
*
|
102
|
+
* const x = 1;
|
103
|
+
*
|
104
|
+
* x; // returns `false`
|
105
|
+
* () => x; // returns `true`
|
106
|
+
*
|
107
|
+
* class C {
|
108
|
+
* field = x; // returns `true`
|
109
|
+
* static field = x; // returns `false`
|
110
|
+
*
|
111
|
+
* method() {
|
112
|
+
* x; // returns `true`
|
113
|
+
* }
|
114
|
+
*
|
115
|
+
* static method() {
|
116
|
+
* x; // returns `true`
|
117
|
+
* }
|
118
|
+
*
|
119
|
+
* static {
|
120
|
+
* x; // returns `false`
|
121
|
+
* }
|
122
|
+
* }
|
123
|
+
* @param {eslint-scope.Reference} reference A reference to check.
|
124
|
+
* @returns {boolean} `true` if the reference is from a separate execution context.
|
76
125
|
*/
|
77
|
-
function
|
78
|
-
|
126
|
+
function isFromSeparateExecutionContext(reference) {
|
127
|
+
const variable = reference.resolved;
|
128
|
+
let scope = reference.from;
|
129
|
+
|
130
|
+
// Scope#variableScope represents execution context
|
131
|
+
while (variable.scope.variableScope !== scope.variableScope) {
|
132
|
+
if (isClassStaticInitializerScope(scope.variableScope)) {
|
133
|
+
scope = scope.variableScope.upper;
|
134
|
+
} else {
|
135
|
+
return true;
|
136
|
+
}
|
137
|
+
}
|
138
|
+
|
139
|
+
return false;
|
79
140
|
}
|
80
141
|
|
81
142
|
/**
|
82
|
-
* Checks whether or not a given reference is
|
143
|
+
* Checks whether or not a given reference is evaluated during the initialization of its variable.
|
83
144
|
*
|
84
145
|
* This returns `true` in the following cases:
|
85
146
|
*
|
@@ -88,17 +149,45 @@ function isInRange(node, location) {
|
|
88
149
|
* var {a = a} = obj
|
89
150
|
* for (var a in a) {}
|
90
151
|
* for (var a of a) {}
|
91
|
-
*
|
152
|
+
* var C = class { [C]; };
|
153
|
+
* var C = class { static foo = C; };
|
154
|
+
* var C = class { static { foo = C; } };
|
155
|
+
* class C extends C {}
|
156
|
+
* class C extends (class { static foo = C; }) {}
|
157
|
+
* class C { [C]; }
|
92
158
|
* @param {Reference} reference A reference to check.
|
93
|
-
* @returns {boolean} `true` if the reference is
|
159
|
+
* @returns {boolean} `true` if the reference is evaluated during the initialization.
|
94
160
|
*/
|
95
|
-
function
|
96
|
-
if (
|
161
|
+
function isEvaluatedDuringInitialization(reference) {
|
162
|
+
if (isFromSeparateExecutionContext(reference)) {
|
163
|
+
|
164
|
+
/*
|
165
|
+
* Even if the reference appears in the initializer, it isn't evaluated during the initialization.
|
166
|
+
* For example, `const x = () => x;` is valid.
|
167
|
+
*/
|
97
168
|
return false;
|
98
169
|
}
|
99
170
|
|
100
|
-
let node = variable.identifiers[0].parent;
|
101
171
|
const location = reference.identifier.range[1];
|
172
|
+
const definition = reference.resolved.defs[0];
|
173
|
+
|
174
|
+
if (definition.type === "ClassName") {
|
175
|
+
|
176
|
+
// `ClassDeclaration` or `ClassExpression`
|
177
|
+
const classDefinition = definition.node;
|
178
|
+
|
179
|
+
return (
|
180
|
+
isInRange(classDefinition, location) &&
|
181
|
+
|
182
|
+
/*
|
183
|
+
* Class binding is initialized before running static initializers.
|
184
|
+
* For example, `class C { static foo = C; static { bar = C; } }` is valid.
|
185
|
+
*/
|
186
|
+
!isInClassStaticInitializerRange(classDefinition.body, location)
|
187
|
+
);
|
188
|
+
}
|
189
|
+
|
190
|
+
let node = definition.name.parent;
|
102
191
|
|
103
192
|
while (node) {
|
104
193
|
if (node.type === "VariableDeclarator") {
|
@@ -167,65 +256,77 @@ module.exports = {
|
|
167
256
|
const options = parseOptions(context.options[0]);
|
168
257
|
|
169
258
|
/**
|
170
|
-
* Determines whether a given
|
171
|
-
*
|
172
|
-
*
|
173
|
-
*
|
259
|
+
* Determines whether a given reference should be checked.
|
260
|
+
*
|
261
|
+
* Returns `false` if the reference is:
|
262
|
+
* - initialization's (e.g., `let a = 1`).
|
263
|
+
* - referring to an undefined variable (i.e., if it's an unresolved reference).
|
264
|
+
* - referring to a variable that is defined, but not in the given source code
|
265
|
+
* (e.g., global environment variable or `arguments` in functions).
|
266
|
+
* - allowed by options.
|
267
|
+
* @param {eslint-scope.Reference} reference The reference
|
268
|
+
* @returns {boolean} `true` if the reference should be checked
|
174
269
|
*/
|
175
|
-
function
|
176
|
-
if (
|
177
|
-
return
|
270
|
+
function shouldCheck(reference) {
|
271
|
+
if (reference.init) {
|
272
|
+
return false;
|
178
273
|
}
|
179
|
-
|
180
|
-
|
274
|
+
|
275
|
+
const variable = reference.resolved;
|
276
|
+
|
277
|
+
if (!variable || variable.defs.length === 0) {
|
278
|
+
return false;
|
181
279
|
}
|
182
|
-
|
183
|
-
|
280
|
+
|
281
|
+
const definitionType = variable.defs[0].type;
|
282
|
+
|
283
|
+
if (!options.functions && definitionType === "FunctionName") {
|
284
|
+
return false;
|
184
285
|
}
|
286
|
+
|
287
|
+
if (
|
288
|
+
(
|
289
|
+
!options.variables && definitionType === "Variable" ||
|
290
|
+
!options.classes && definitionType === "ClassName"
|
291
|
+
) &&
|
292
|
+
|
293
|
+
// don't skip checking the reference if it's in the same execution context, because of TDZ
|
294
|
+
isFromSeparateExecutionContext(reference)
|
295
|
+
) {
|
296
|
+
return false;
|
297
|
+
}
|
298
|
+
|
185
299
|
return true;
|
186
300
|
}
|
187
301
|
|
188
302
|
/**
|
189
|
-
* Finds and validates all
|
190
|
-
* @param {Scope} scope The scope object.
|
303
|
+
* Finds and validates all references in a given scope and its child scopes.
|
304
|
+
* @param {eslint-scope.Scope} scope The scope object.
|
191
305
|
* @returns {void}
|
192
|
-
* @private
|
193
306
|
*/
|
194
|
-
function
|
195
|
-
scope.references.forEach(reference => {
|
307
|
+
function checkReferencesInScope(scope) {
|
308
|
+
scope.references.filter(shouldCheck).forEach(reference => {
|
196
309
|
const variable = reference.resolved;
|
310
|
+
const definitionIdentifier = variable.defs[0].name;
|
197
311
|
|
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)
|
312
|
+
if (
|
313
|
+
reference.identifier.range[1] < definitionIdentifier.range[1] ||
|
314
|
+
isEvaluatedDuringInitialization(reference)
|
211
315
|
) {
|
212
|
-
|
316
|
+
context.report({
|
317
|
+
node: reference.identifier,
|
318
|
+
messageId: "usedBeforeDefined",
|
319
|
+
data: reference.identifier
|
320
|
+
});
|
213
321
|
}
|
214
|
-
|
215
|
-
// Reports.
|
216
|
-
context.report({
|
217
|
-
node: reference.identifier,
|
218
|
-
messageId: "usedBeforeDefined",
|
219
|
-
data: reference.identifier
|
220
|
-
});
|
221
322
|
});
|
222
323
|
|
223
|
-
scope.childScopes.forEach(
|
324
|
+
scope.childScopes.forEach(checkReferencesInScope);
|
224
325
|
}
|
225
326
|
|
226
327
|
return {
|
227
328
|
Program() {
|
228
|
-
|
329
|
+
checkReferencesInScope(context.getScope());
|
229
330
|
}
|
230
331
|
};
|
231
332
|
}
|