eslint 7.14.0 → 7.15.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 +8 -0
- package/lib/rules/index.js +1 -0
- package/lib/rules/no-unsafe-optional-chaining.js +205 -0
- package/lib/rules/one-var.js +5 -3
- package/package.json +4 -4
package/CHANGELOG.md
CHANGED
@@ -1,3 +1,11 @@
|
|
1
|
+
v7.15.0 - December 5, 2020
|
2
|
+
|
3
|
+
* [`5c11aab`](https://github.com/eslint/eslint/commit/5c11aabbe8249aeb8cad29bc6a33fc20c8c683ef) Upgrade: @eslint/esintrc and espree for bug fixes (refs #13878) (#13908) (Brandon Mills)
|
4
|
+
* [`0eb7957`](https://github.com/eslint/eslint/commit/0eb7957e27fd521317bd5c8479ce7abc1399169c) Upgrade: file-entry-cache@6.0.0 (#13877) (Rouven Weßling)
|
5
|
+
* [`683ad00`](https://github.com/eslint/eslint/commit/683ad00c41e1ae4d889deff82b2a94318e8c2129) New: no-unsafe-optional-chaining rule (fixes #13431) (#13859) (YeonJuan)
|
6
|
+
* [`cbc57fb`](https://github.com/eslint/eslint/commit/cbc57fb7d07c00663ed5781f5e6bc8f534cc2d76) Fix: one-var autofixing for export (fixes #13834) (#13891) (Anix)
|
7
|
+
* [`110cf96`](https://github.com/eslint/eslint/commit/110cf962d05625a8a1bf7b5f4ec2194db150eb32) Docs: Fix a broken link in working-with-rules.md (#13875) (Anton Niklasson)
|
8
|
+
|
1
9
|
v7.14.0 - November 20, 2020
|
2
10
|
|
3
11
|
* [`5f09073`](https://github.com/eslint/eslint/commit/5f0907399a9666dec78c74384c8969c01483c30e) Update: fix 'skip' options in no-irregular-whitespace (fixes #13852) (#13853) (Milos Djermanovic)
|
package/lib/rules/index.js
CHANGED
@@ -218,6 +218,7 @@ module.exports = new LazyLoadingRuleMap(Object.entries({
|
|
218
218
|
"no-unreachable-loop": () => require("./no-unreachable-loop"),
|
219
219
|
"no-unsafe-finally": () => require("./no-unsafe-finally"),
|
220
220
|
"no-unsafe-negation": () => require("./no-unsafe-negation"),
|
221
|
+
"no-unsafe-optional-chaining": () => require("./no-unsafe-optional-chaining"),
|
221
222
|
"no-unused-expressions": () => require("./no-unused-expressions"),
|
222
223
|
"no-unused-labels": () => require("./no-unused-labels"),
|
223
224
|
"no-unused-vars": () => require("./no-unused-vars"),
|
@@ -0,0 +1,205 @@
|
|
1
|
+
/**
|
2
|
+
* @fileoverview Rule to disallow unsafe optional chaining
|
3
|
+
* @author Yeon JuAn
|
4
|
+
*/
|
5
|
+
|
6
|
+
"use strict";
|
7
|
+
|
8
|
+
const UNSAFE_ARITHMETIC_OPERATORS = new Set(["+", "-", "/", "*", "%", "**"]);
|
9
|
+
const UNSAFE_ASSIGNMENT_OPERATORS = new Set(["+=", "-=", "/=", "*=", "%=", "**="]);
|
10
|
+
const UNSAFE_RELATIONAL_OPERATORS = new Set(["in", "instanceof"]);
|
11
|
+
|
12
|
+
/**
|
13
|
+
* Checks whether a node is a destructuring pattern or not
|
14
|
+
* @param {ASTNode} node node to check
|
15
|
+
* @returns {boolean} `true` if a node is a destructuring pattern, otherwise `false`
|
16
|
+
*/
|
17
|
+
function isDestructuringPattern(node) {
|
18
|
+
return node.type === "ObjectPattern" || node.type === "ArrayPattern";
|
19
|
+
}
|
20
|
+
|
21
|
+
module.exports = {
|
22
|
+
meta: {
|
23
|
+
type: "problem",
|
24
|
+
|
25
|
+
docs: {
|
26
|
+
description: "disallow use of optional chaining in contexts where the `undefined` value is not allowed",
|
27
|
+
category: "Possible Errors",
|
28
|
+
recommended: false,
|
29
|
+
url: "https://eslint.org/docs/rules/no-unsafe-optional-chaining"
|
30
|
+
},
|
31
|
+
schema: [{
|
32
|
+
type: "object",
|
33
|
+
properties: {
|
34
|
+
disallowArithmeticOperators: {
|
35
|
+
type: "boolean",
|
36
|
+
default: false
|
37
|
+
}
|
38
|
+
},
|
39
|
+
additionalProperties: false
|
40
|
+
}],
|
41
|
+
fixable: null,
|
42
|
+
messages: {
|
43
|
+
unsafeOptionalChain: "Unsafe usage of optional chaining. If it short-circuits with 'undefined' the evaluation will throw TypeError.",
|
44
|
+
unsafeArithmetic: "Unsafe arithmetic operation on optional chaining. It can result in NaN."
|
45
|
+
}
|
46
|
+
},
|
47
|
+
|
48
|
+
create(context) {
|
49
|
+
const options = context.options[0] || {};
|
50
|
+
const disallowArithmeticOperators = (options.disallowArithmeticOperators) || false;
|
51
|
+
|
52
|
+
/**
|
53
|
+
* Reports unsafe usage of optional chaining
|
54
|
+
* @param {ASTNode} node node to report
|
55
|
+
* @returns {void}
|
56
|
+
*/
|
57
|
+
function reportUnsafeUsage(node) {
|
58
|
+
context.report({
|
59
|
+
messageId: "unsafeOptionalChain",
|
60
|
+
node
|
61
|
+
});
|
62
|
+
}
|
63
|
+
|
64
|
+
/**
|
65
|
+
* Reports unsafe arithmetic operation on optional chaining
|
66
|
+
* @param {ASTNode} node node to report
|
67
|
+
* @returns {void}
|
68
|
+
*/
|
69
|
+
function reportUnsafeArithmetic(node) {
|
70
|
+
context.report({
|
71
|
+
messageId: "unsafeArithmetic",
|
72
|
+
node
|
73
|
+
});
|
74
|
+
}
|
75
|
+
|
76
|
+
/**
|
77
|
+
* Checks and reports if a node can short-circuit with `undefined` by optional chaining.
|
78
|
+
* @param {ASTNode} [node] node to check
|
79
|
+
* @param {Function} reportFunc report function
|
80
|
+
* @returns {void}
|
81
|
+
*/
|
82
|
+
function checkUndefinedShortCircuit(node, reportFunc) {
|
83
|
+
if (!node) {
|
84
|
+
return;
|
85
|
+
}
|
86
|
+
switch (node.type) {
|
87
|
+
case "LogicalExpression":
|
88
|
+
if (node.operator === "||" || node.operator === "??") {
|
89
|
+
checkUndefinedShortCircuit(node.right, reportFunc);
|
90
|
+
} else if (node.operator === "&&") {
|
91
|
+
checkUndefinedShortCircuit(node.left, reportFunc);
|
92
|
+
checkUndefinedShortCircuit(node.right, reportFunc);
|
93
|
+
}
|
94
|
+
break;
|
95
|
+
case "SequenceExpression":
|
96
|
+
checkUndefinedShortCircuit(
|
97
|
+
node.expressions[node.expressions.length - 1],
|
98
|
+
reportFunc
|
99
|
+
);
|
100
|
+
break;
|
101
|
+
case "ConditionalExpression":
|
102
|
+
checkUndefinedShortCircuit(node.consequent, reportFunc);
|
103
|
+
checkUndefinedShortCircuit(node.alternate, reportFunc);
|
104
|
+
break;
|
105
|
+
case "AwaitExpression":
|
106
|
+
checkUndefinedShortCircuit(node.argument, reportFunc);
|
107
|
+
break;
|
108
|
+
case "ChainExpression":
|
109
|
+
reportFunc(node);
|
110
|
+
break;
|
111
|
+
default:
|
112
|
+
break;
|
113
|
+
}
|
114
|
+
}
|
115
|
+
|
116
|
+
/**
|
117
|
+
* Checks unsafe usage of optional chaining
|
118
|
+
* @param {ASTNode} node node to check
|
119
|
+
* @returns {void}
|
120
|
+
*/
|
121
|
+
function checkUnsafeUsage(node) {
|
122
|
+
checkUndefinedShortCircuit(node, reportUnsafeUsage);
|
123
|
+
}
|
124
|
+
|
125
|
+
/**
|
126
|
+
* Checks unsafe arithmetic operations on optional chaining
|
127
|
+
* @param {ASTNode} node node to check
|
128
|
+
* @returns {void}
|
129
|
+
*/
|
130
|
+
function checkUnsafeArithmetic(node) {
|
131
|
+
checkUndefinedShortCircuit(node, reportUnsafeArithmetic);
|
132
|
+
}
|
133
|
+
|
134
|
+
return {
|
135
|
+
"AssignmentExpression, AssignmentPattern"(node) {
|
136
|
+
if (isDestructuringPattern(node.left)) {
|
137
|
+
checkUnsafeUsage(node.right);
|
138
|
+
}
|
139
|
+
},
|
140
|
+
"ClassDeclaration, ClassExpression"(node) {
|
141
|
+
checkUnsafeUsage(node.superClass);
|
142
|
+
},
|
143
|
+
CallExpression(node) {
|
144
|
+
if (!node.optional) {
|
145
|
+
checkUnsafeUsage(node.callee);
|
146
|
+
}
|
147
|
+
},
|
148
|
+
NewExpression(node) {
|
149
|
+
checkUnsafeUsage(node.callee);
|
150
|
+
},
|
151
|
+
VariableDeclarator(node) {
|
152
|
+
if (isDestructuringPattern(node.id)) {
|
153
|
+
checkUnsafeUsage(node.init);
|
154
|
+
}
|
155
|
+
},
|
156
|
+
MemberExpression(node) {
|
157
|
+
if (!node.optional) {
|
158
|
+
checkUnsafeUsage(node.object);
|
159
|
+
}
|
160
|
+
},
|
161
|
+
TaggedTemplateExpression(node) {
|
162
|
+
checkUnsafeUsage(node.tag);
|
163
|
+
},
|
164
|
+
ForOfStatement(node) {
|
165
|
+
checkUnsafeUsage(node.right);
|
166
|
+
},
|
167
|
+
SpreadElement(node) {
|
168
|
+
if (node.parent && node.parent.type !== "ObjectExpression") {
|
169
|
+
checkUnsafeUsage(node.argument);
|
170
|
+
}
|
171
|
+
},
|
172
|
+
BinaryExpression(node) {
|
173
|
+
if (UNSAFE_RELATIONAL_OPERATORS.has(node.operator)) {
|
174
|
+
checkUnsafeUsage(node.right);
|
175
|
+
}
|
176
|
+
if (
|
177
|
+
disallowArithmeticOperators &&
|
178
|
+
UNSAFE_ARITHMETIC_OPERATORS.has(node.operator)
|
179
|
+
) {
|
180
|
+
checkUnsafeArithmetic(node.right);
|
181
|
+
checkUnsafeArithmetic(node.left);
|
182
|
+
}
|
183
|
+
},
|
184
|
+
WithStatement(node) {
|
185
|
+
checkUnsafeUsage(node.object);
|
186
|
+
},
|
187
|
+
UnaryExpression(node) {
|
188
|
+
if (
|
189
|
+
disallowArithmeticOperators &&
|
190
|
+
UNSAFE_ARITHMETIC_OPERATORS.has(node.operator)
|
191
|
+
) {
|
192
|
+
checkUnsafeArithmetic(node.argument);
|
193
|
+
}
|
194
|
+
},
|
195
|
+
AssignmentExpression(node) {
|
196
|
+
if (
|
197
|
+
disallowArithmeticOperators &&
|
198
|
+
UNSAFE_ASSIGNMENT_OPERATORS.has(node.operator)
|
199
|
+
) {
|
200
|
+
checkUnsafeArithmetic(node.right);
|
201
|
+
}
|
202
|
+
}
|
203
|
+
};
|
204
|
+
}
|
205
|
+
};
|
package/lib/rules/one-var.js
CHANGED
@@ -314,12 +314,14 @@ module.exports = {
|
|
314
314
|
return null;
|
315
315
|
}
|
316
316
|
|
317
|
+
const exportPlacement = declaration.parent.type === "ExportNamedDeclaration" ? "export " : "";
|
318
|
+
|
317
319
|
/*
|
318
320
|
* `var x,y`
|
319
321
|
* tokenAfterDeclarator ^^ afterComma
|
320
322
|
*/
|
321
323
|
if (afterComma.range[0] === tokenAfterDeclarator.range[1]) {
|
322
|
-
return fixer.replaceText(tokenAfterDeclarator, `; ${declaration.kind} `);
|
324
|
+
return fixer.replaceText(tokenAfterDeclarator, `; ${exportPlacement}${declaration.kind} `);
|
323
325
|
}
|
324
326
|
|
325
327
|
/*
|
@@ -341,11 +343,11 @@ module.exports = {
|
|
341
343
|
|
342
344
|
return fixer.replaceTextRange(
|
343
345
|
[tokenAfterDeclarator.range[0], lastComment.range[0]],
|
344
|
-
`;${sourceCode.text.slice(tokenAfterDeclarator.range[1], lastComment.range[0])}${declaration.kind} `
|
346
|
+
`;${sourceCode.text.slice(tokenAfterDeclarator.range[1], lastComment.range[0])}${exportPlacement}${declaration.kind} `
|
345
347
|
);
|
346
348
|
}
|
347
349
|
|
348
|
-
return fixer.replaceText(tokenAfterDeclarator, `; ${declaration.kind}`);
|
350
|
+
return fixer.replaceText(tokenAfterDeclarator, `; ${exportPlacement}${declaration.kind}`);
|
349
351
|
}).filter(x => x);
|
350
352
|
}
|
351
353
|
|
package/package.json
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
{
|
2
2
|
"name": "eslint",
|
3
|
-
"version": "7.
|
3
|
+
"version": "7.15.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
|
"bugs": "https://github.com/eslint/eslint/issues/",
|
48
48
|
"dependencies": {
|
49
49
|
"@babel/code-frame": "^7.0.0",
|
50
|
-
"@eslint/eslintrc": "^0.2.
|
50
|
+
"@eslint/eslintrc": "^0.2.2",
|
51
51
|
"ajv": "^6.10.0",
|
52
52
|
"chalk": "^4.0.0",
|
53
53
|
"cross-spawn": "^7.0.2",
|
@@ -57,10 +57,10 @@
|
|
57
57
|
"eslint-scope": "^5.1.1",
|
58
58
|
"eslint-utils": "^2.1.0",
|
59
59
|
"eslint-visitor-keys": "^2.0.0",
|
60
|
-
"espree": "^7.3.
|
60
|
+
"espree": "^7.3.1",
|
61
61
|
"esquery": "^1.2.0",
|
62
62
|
"esutils": "^2.0.2",
|
63
|
-
"file-entry-cache": "^
|
63
|
+
"file-entry-cache": "^6.0.0",
|
64
64
|
"functional-red-black-tree": "^1.0.1",
|
65
65
|
"glob-parent": "^5.0.0",
|
66
66
|
"globals": "^12.1.0",
|