eslint-plugin-unicorn-ts 0.0.1-security → 50.0.1
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.
Potentially problematic release.
This version of eslint-plugin-unicorn-ts might be problematic. Click here for more details.
- package/configs/all.js +6 -0
- package/configs/flat-config-base.js +10 -0
- package/configs/legacy-config-base.js +10 -0
- package/configs/recommended.js +117 -0
- package/index.js +91 -0
- package/license +9 -0
- package/package.json +186 -4
- package/readme.md +356 -0
- package/rules/ast/call-or-new-expression.js +127 -0
- package/rules/ast/function-types.js +5 -0
- package/rules/ast/index.js +39 -0
- package/rules/ast/is-arrow-function-body.js +7 -0
- package/rules/ast/is-empty-node.js +20 -0
- package/rules/ast/is-expression-statement.js +11 -0
- package/rules/ast/is-function.js +8 -0
- package/rules/ast/is-member-expression.js +101 -0
- package/rules/ast/is-method-call.js +65 -0
- package/rules/ast/is-reference-identifier.js +156 -0
- package/rules/ast/is-static-require.js +14 -0
- package/rules/ast/is-undefined.js +7 -0
- package/rules/ast/literal.js +29 -0
- package/rules/better-regex.js +144 -0
- package/rules/catch-error-name.js +136 -0
- package/rules/consistent-destructuring.js +168 -0
- package/rules/consistent-function-scoping.js +223 -0
- package/rules/custom-error-definition.js +215 -0
- package/rules/empty-brace-spaces.js +72 -0
- package/rules/error-message.js +104 -0
- package/rules/escape-case.js +63 -0
- package/rules/expiring-todo-comments.js +580 -0
- package/rules/explicit-length-check.js +229 -0
- package/rules/filename-case.js +258 -0
- package/rules/fix/add-parenthesizes-to-return-or-throw-expression.js +21 -0
- package/rules/fix/append-argument.js +20 -0
- package/rules/fix/extend-fix-range.js +15 -0
- package/rules/fix/fix-space-around-keywords.js +35 -0
- package/rules/fix/index.js +23 -0
- package/rules/fix/remove-argument.js +32 -0
- package/rules/fix/remove-member-expression-property.js +11 -0
- package/rules/fix/remove-method-call.js +20 -0
- package/rules/fix/remove-parentheses.js +11 -0
- package/rules/fix/remove-spaces-after.js +14 -0
- package/rules/fix/rename-variable.js +9 -0
- package/rules/fix/replace-argument.js +8 -0
- package/rules/fix/replace-node-or-token-and-spaces-before.js +21 -0
- package/rules/fix/replace-reference-identifier.js +35 -0
- package/rules/fix/replace-string-literal.js +11 -0
- package/rules/fix/replace-string-raw.js +14 -0
- package/rules/fix/replace-template-element.js +11 -0
- package/rules/fix/switch-call-expression-to-new-expression.js +18 -0
- package/rules/fix/switch-new-expression-to-call-expression.js +34 -0
- package/rules/import-style.js +364 -0
- package/rules/new-for-builtins.js +85 -0
- package/rules/no-abusive-eslint-disable.js +48 -0
- package/rules/no-array-callback-reference.js +256 -0
- package/rules/no-array-for-each.js +473 -0
- package/rules/no-array-method-this-argument.js +188 -0
- package/rules/no-array-push-push.js +144 -0
- package/rules/no-array-reduce.js +126 -0
- package/rules/no-await-expression-member.js +90 -0
- package/rules/no-console-spaces.js +86 -0
- package/rules/no-document-cookie.js +25 -0
- package/rules/no-empty-file.js +57 -0
- package/rules/no-for-loop.js +427 -0
- package/rules/no-hex-escape.js +46 -0
- package/rules/no-instanceof-array.js +65 -0
- package/rules/no-invalid-remove-event-listener.js +60 -0
- package/rules/no-keyword-prefix.js +199 -0
- package/rules/no-lonely-if.js +151 -0
- package/rules/no-negated-condition.js +144 -0
- package/rules/no-nested-ternary.js +58 -0
- package/rules/no-new-array.js +104 -0
- package/rules/no-new-buffer.js +98 -0
- package/rules/no-null.js +153 -0
- package/rules/no-object-as-default-parameter.js +50 -0
- package/rules/no-process-exit.js +104 -0
- package/rules/no-static-only-class.js +224 -0
- package/rules/no-thenable.js +198 -0
- package/rules/no-this-assignment.js +38 -0
- package/rules/no-typeof-undefined.js +143 -0
- package/rules/no-unnecessary-await.js +107 -0
- package/rules/no-unnecessary-polyfills.js +176 -0
- package/rules/no-unreadable-array-destructuring.js +83 -0
- package/rules/no-unreadable-iife.js +45 -0
- package/rules/no-unused-properties.js +238 -0
- package/rules/no-useless-fallback-in-spread.js +68 -0
- package/rules/no-useless-length-check.js +152 -0
- package/rules/no-useless-promise-resolve-reject.js +212 -0
- package/rules/no-useless-spread.js +381 -0
- package/rules/no-useless-switch-case.js +71 -0
- package/rules/no-useless-undefined.js +301 -0
- package/rules/no-zero-fractions.js +79 -0
- package/rules/number-literal-case.js +52 -0
- package/rules/numeric-separators-style.js +181 -0
- package/rules/prefer-add-event-listener.js +188 -0
- package/rules/prefer-array-find.js +423 -0
- package/rules/prefer-array-flat-map.js +82 -0
- package/rules/prefer-array-flat.js +279 -0
- package/rules/prefer-array-index-of.js +32 -0
- package/rules/prefer-array-some.js +157 -0
- package/rules/prefer-at.js +374 -0
- package/rules/prefer-blob-reading-methods.js +45 -0
- package/rules/prefer-code-point.js +67 -0
- package/rules/prefer-date-now.js +135 -0
- package/rules/prefer-default-parameters.js +219 -0
- package/rules/prefer-dom-node-append.js +48 -0
- package/rules/prefer-dom-node-dataset.js +120 -0
- package/rules/prefer-dom-node-remove.js +122 -0
- package/rules/prefer-dom-node-text-content.js +75 -0
- package/rules/prefer-event-target.js +117 -0
- package/rules/prefer-export-from.js +413 -0
- package/rules/prefer-includes.js +98 -0
- package/rules/prefer-json-parse-buffer.js +159 -0
- package/rules/prefer-keyboard-event-key.js +186 -0
- package/rules/prefer-logical-operator-over-ternary.js +159 -0
- package/rules/prefer-math-trunc.js +109 -0
- package/rules/prefer-modern-dom-apis.js +141 -0
- package/rules/prefer-modern-math-apis.js +212 -0
- package/rules/prefer-module.js +349 -0
- package/rules/prefer-native-coercion-functions.js +185 -0
- package/rules/prefer-negative-index.js +213 -0
- package/rules/prefer-node-protocol.js +61 -0
- package/rules/prefer-number-properties.js +126 -0
- package/rules/prefer-object-from-entries.js +252 -0
- package/rules/prefer-optional-catch-binding.js +75 -0
- package/rules/prefer-prototype-methods.js +88 -0
- package/rules/prefer-query-selector.js +135 -0
- package/rules/prefer-reflect-apply.js +97 -0
- package/rules/prefer-regexp-test.js +156 -0
- package/rules/prefer-set-has.js +186 -0
- package/rules/prefer-set-size.js +103 -0
- package/rules/prefer-spread.js +529 -0
- package/rules/prefer-string-replace-all.js +145 -0
- package/rules/prefer-string-slice.js +182 -0
- package/rules/prefer-string-starts-ends-with.js +199 -0
- package/rules/prefer-string-trim-start-end.js +44 -0
- package/rules/prefer-switch.js +344 -0
- package/rules/prefer-ternary.js +282 -0
- package/rules/prefer-top-level-await.js +152 -0
- package/rules/prefer-type-error.js +151 -0
- package/rules/prevent-abbreviations.js +645 -0
- package/rules/relative-url-style.js +168 -0
- package/rules/require-array-join-separator.js +63 -0
- package/rules/require-number-to-fixed-digits-argument.js +54 -0
- package/rules/require-post-message-target-origin.js +71 -0
- package/rules/shared/abbreviations.js +262 -0
- package/rules/shared/dom-events.js +275 -0
- package/rules/shared/event-keys.js +52 -0
- package/rules/shared/negative-index.js +46 -0
- package/rules/shared/simple-array-search-rule.js +128 -0
- package/rules/shared/typed-array.js +16 -0
- package/rules/string-content.js +187 -0
- package/rules/switch-case-braces.js +109 -0
- package/rules/template-indent.js +219 -0
- package/rules/text-encoding-identifier-case.js +108 -0
- package/rules/throw-new-error.js +53 -0
- package/rules/utils/array-or-object-prototype-property.js +63 -0
- package/rules/utils/assert-token.js +32 -0
- package/rules/utils/avoid-capture.js +146 -0
- package/rules/utils/boolean.js +92 -0
- package/rules/utils/builtins.js +36 -0
- package/rules/utils/cartesian-product-samples.js +24 -0
- package/rules/utils/create-deprecated-rules.js +25 -0
- package/rules/utils/escape-string.js +26 -0
- package/rules/utils/escape-template-element-raw.js +6 -0
- package/rules/utils/get-ancestor.js +20 -0
- package/rules/utils/get-builtin-rule.js +7 -0
- package/rules/utils/get-call-expression-arguments-text.js +21 -0
- package/rules/utils/get-class-head-location.js +22 -0
- package/rules/utils/get-documentation-url.js +10 -0
- package/rules/utils/get-indent-string.js +11 -0
- package/rules/utils/get-previous-node.js +24 -0
- package/rules/utils/get-references.js +9 -0
- package/rules/utils/get-scopes.js +14 -0
- package/rules/utils/get-switch-case-head-location.js +21 -0
- package/rules/utils/get-variable-identifiers.js +7 -0
- package/rules/utils/global-reference-tracker.js +72 -0
- package/rules/utils/has-optional-chain-element.js +21 -0
- package/rules/utils/has-same-range.js +7 -0
- package/rules/utils/index.js +53 -0
- package/rules/utils/is-function-self-used-inside.js +43 -0
- package/rules/utils/is-left-hand-side.js +22 -0
- package/rules/utils/is-logical-expression.js +16 -0
- package/rules/utils/is-method-named.js +9 -0
- package/rules/utils/is-new-expression-with-parentheses.js +26 -0
- package/rules/utils/is-node-matches.js +53 -0
- package/rules/utils/is-node-value-not-dom-node.js +21 -0
- package/rules/utils/is-node-value-not-function.js +42 -0
- package/rules/utils/is-number.js +224 -0
- package/rules/utils/is-object-method.js +11 -0
- package/rules/utils/is-on-same-line.js +7 -0
- package/rules/utils/is-same-identifier.js +8 -0
- package/rules/utils/is-same-reference.js +173 -0
- package/rules/utils/is-shadowed.js +33 -0
- package/rules/utils/is-shorthand-export-local.js +9 -0
- package/rules/utils/is-shorthand-import-local.js +9 -0
- package/rules/utils/is-shorthand-property-assignment-pattern-left.js +10 -0
- package/rules/utils/is-shorthand-property-value.js +8 -0
- package/rules/utils/is-value-not-usable.js +5 -0
- package/rules/utils/lodash.js +1589 -0
- package/rules/utils/needs-semicolon.js +114 -0
- package/rules/utils/numeric.js +53 -0
- package/rules/utils/parentheses.js +73 -0
- package/rules/utils/resolve-variable-name.js +20 -0
- package/rules/utils/rule.js +190 -0
- package/rules/utils/should-add-parentheses-to-conditional-expression-child.js +17 -0
- package/rules/utils/should-add-parentheses-to-expression-statement-expression.js +26 -0
- package/rules/utils/should-add-parentheses-to-logical-expression-child.js +47 -0
- package/rules/utils/should-add-parentheses-to-member-expression-object.js +47 -0
- package/rules/utils/should-add-parentheses-to-new-expression-callee.js +32 -0
- package/rules/utils/should-add-parentheses-to-spread-element-argument.js +22 -0
- package/rules/utils/singular.js +18 -0
- package/rules/utils/to-location.js +21 -0
- package/README.md +0 -5
@@ -0,0 +1,212 @@
|
|
1
|
+
'use strict';
|
2
|
+
const {getParenthesizedRange} = require('./utils/index.js');
|
3
|
+
const {isFunction, isMethodCall} = require('./ast/index.js');
|
4
|
+
|
5
|
+
const MESSAGE_ID_RESOLVE = 'resolve';
|
6
|
+
const MESSAGE_ID_REJECT = 'reject';
|
7
|
+
const messages = {
|
8
|
+
[MESSAGE_ID_RESOLVE]: 'Prefer `{{type}} value` over `{{type}} Promise.resolve(value)`.',
|
9
|
+
[MESSAGE_ID_REJECT]: 'Prefer `throw error` over `{{type}} Promise.reject(error)`.',
|
10
|
+
};
|
11
|
+
|
12
|
+
function getFunctionNode(node) {
|
13
|
+
let isInTryStatement = false;
|
14
|
+
let functionNode;
|
15
|
+
for (; node; node = node.parent) {
|
16
|
+
if (isFunction(node)) {
|
17
|
+
functionNode = node;
|
18
|
+
break;
|
19
|
+
}
|
20
|
+
|
21
|
+
if (node.type === 'TryStatement') {
|
22
|
+
isInTryStatement = true;
|
23
|
+
}
|
24
|
+
}
|
25
|
+
|
26
|
+
return {
|
27
|
+
functionNode,
|
28
|
+
isInTryStatement,
|
29
|
+
};
|
30
|
+
}
|
31
|
+
|
32
|
+
function isPromiseCallback(node) {
|
33
|
+
if (
|
34
|
+
node.parent.type === 'CallExpression'
|
35
|
+
&& node.parent.callee.type === 'MemberExpression'
|
36
|
+
&& !node.parent.callee.computed
|
37
|
+
&& node.parent.callee.property.type === 'Identifier'
|
38
|
+
) {
|
39
|
+
const {callee: {property}, arguments: arguments_} = node.parent;
|
40
|
+
|
41
|
+
if (
|
42
|
+
arguments_.length === 1
|
43
|
+
&& (
|
44
|
+
property.name === 'then'
|
45
|
+
|| property.name === 'catch'
|
46
|
+
|| property.name === 'finally'
|
47
|
+
)
|
48
|
+
&& arguments_[0] === node
|
49
|
+
) {
|
50
|
+
return true;
|
51
|
+
}
|
52
|
+
|
53
|
+
if (
|
54
|
+
arguments_.length === 2
|
55
|
+
&& property.name === 'then'
|
56
|
+
&& (
|
57
|
+
arguments_[0] === node
|
58
|
+
|| (arguments_[0].type !== 'SpreadElement' && arguments_[1] === node)
|
59
|
+
)
|
60
|
+
) {
|
61
|
+
return true;
|
62
|
+
}
|
63
|
+
}
|
64
|
+
|
65
|
+
return false;
|
66
|
+
}
|
67
|
+
|
68
|
+
function createProblem(callExpression, fix) {
|
69
|
+
const {callee, parent} = callExpression;
|
70
|
+
const method = callee.property.name;
|
71
|
+
const type = parent.type === 'YieldExpression' ? 'yield' : 'return';
|
72
|
+
|
73
|
+
return {
|
74
|
+
node: callee,
|
75
|
+
messageId: method,
|
76
|
+
data: {type},
|
77
|
+
fix,
|
78
|
+
};
|
79
|
+
}
|
80
|
+
|
81
|
+
function fix(callExpression, isInTryStatement, sourceCode) {
|
82
|
+
if (callExpression.arguments.length > 1) {
|
83
|
+
return;
|
84
|
+
}
|
85
|
+
|
86
|
+
const {callee, parent, arguments: [errorOrValue]} = callExpression;
|
87
|
+
if (errorOrValue?.type === 'SpreadElement') {
|
88
|
+
return;
|
89
|
+
}
|
90
|
+
|
91
|
+
const isReject = callee.property.name === 'reject';
|
92
|
+
const isYieldExpression = parent.type === 'YieldExpression';
|
93
|
+
if (
|
94
|
+
isReject
|
95
|
+
&& (
|
96
|
+
isInTryStatement
|
97
|
+
|| (isYieldExpression && parent.parent.type !== 'ExpressionStatement')
|
98
|
+
)
|
99
|
+
) {
|
100
|
+
return;
|
101
|
+
}
|
102
|
+
|
103
|
+
return function (fixer) {
|
104
|
+
const isArrowFunctionBody = parent.type === 'ArrowFunctionExpression';
|
105
|
+
|
106
|
+
let text = errorOrValue ? sourceCode.getText(errorOrValue) : '';
|
107
|
+
|
108
|
+
if (errorOrValue?.type === 'SequenceExpression') {
|
109
|
+
text = `(${text})`;
|
110
|
+
}
|
111
|
+
|
112
|
+
if (isReject) {
|
113
|
+
// `return Promise.reject()` -> `throw undefined`
|
114
|
+
text = text || 'undefined';
|
115
|
+
text = `throw ${text}`;
|
116
|
+
|
117
|
+
if (isYieldExpression) {
|
118
|
+
return fixer.replaceTextRange(
|
119
|
+
getParenthesizedRange(parent, sourceCode),
|
120
|
+
text,
|
121
|
+
);
|
122
|
+
}
|
123
|
+
|
124
|
+
text += ';';
|
125
|
+
|
126
|
+
// `=> Promise.reject(error)` -> `=> { throw error; }`
|
127
|
+
if (isArrowFunctionBody) {
|
128
|
+
text = `{ ${text} }`;
|
129
|
+
return fixer.replaceTextRange(
|
130
|
+
getParenthesizedRange(callExpression, sourceCode),
|
131
|
+
text,
|
132
|
+
);
|
133
|
+
}
|
134
|
+
} else {
|
135
|
+
// eslint-disable-next-line no-lonely-if
|
136
|
+
if (isYieldExpression) {
|
137
|
+
text = `yield${text ? ' ' : ''}${text}`;
|
138
|
+
} else if (parent.type === 'ReturnStatement') {
|
139
|
+
text = `return${text ? ' ' : ''}${text};`;
|
140
|
+
} else {
|
141
|
+
if (errorOrValue?.type === 'ObjectExpression') {
|
142
|
+
text = `(${text})`;
|
143
|
+
}
|
144
|
+
|
145
|
+
// `=> Promise.resolve()` -> `=> {}`
|
146
|
+
text = text || '{}';
|
147
|
+
}
|
148
|
+
}
|
149
|
+
|
150
|
+
return fixer.replaceText(
|
151
|
+
isArrowFunctionBody ? callExpression : parent,
|
152
|
+
text,
|
153
|
+
);
|
154
|
+
};
|
155
|
+
}
|
156
|
+
|
157
|
+
/** @param {import('eslint').Rule.RuleContext} context */
|
158
|
+
const create = context => {
|
159
|
+
const {sourceCode} = context;
|
160
|
+
|
161
|
+
return {
|
162
|
+
CallExpression(callExpression) {
|
163
|
+
if (!(
|
164
|
+
isMethodCall(callExpression, {
|
165
|
+
object: 'Promise',
|
166
|
+
methods: ['resolve', 'reject'],
|
167
|
+
optionalCall: false,
|
168
|
+
optionalMember: false,
|
169
|
+
})
|
170
|
+
&& (
|
171
|
+
(
|
172
|
+
callExpression.parent.type === 'ArrowFunctionExpression'
|
173
|
+
&& callExpression.parent.body === callExpression
|
174
|
+
)
|
175
|
+
|| (
|
176
|
+
callExpression.parent.type === 'ReturnStatement'
|
177
|
+
&& callExpression.parent.argument === callExpression
|
178
|
+
)
|
179
|
+
|| (
|
180
|
+
callExpression.parent.type === 'YieldExpression'
|
181
|
+
&& !callExpression.parent.delegate && callExpression.parent.argument === callExpression
|
182
|
+
)
|
183
|
+
)
|
184
|
+
)) {
|
185
|
+
return;
|
186
|
+
}
|
187
|
+
|
188
|
+
const {functionNode, isInTryStatement} = getFunctionNode(callExpression);
|
189
|
+
if (!functionNode || !(functionNode.async || isPromiseCallback(functionNode))) {
|
190
|
+
return;
|
191
|
+
}
|
192
|
+
|
193
|
+
return createProblem(
|
194
|
+
callExpression,
|
195
|
+
fix(callExpression, isInTryStatement, sourceCode),
|
196
|
+
);
|
197
|
+
},
|
198
|
+
};
|
199
|
+
};
|
200
|
+
|
201
|
+
/** @type {import('eslint').Rule.RuleModule} */
|
202
|
+
module.exports = {
|
203
|
+
create,
|
204
|
+
meta: {
|
205
|
+
type: 'suggestion',
|
206
|
+
docs: {
|
207
|
+
description: 'Disallow returning/yielding `Promise.resolve/reject()` in async functions or promise callbacks',
|
208
|
+
},
|
209
|
+
fixable: 'code',
|
210
|
+
messages,
|
211
|
+
},
|
212
|
+
};
|
@@ -0,0 +1,381 @@
|
|
1
|
+
'use strict';
|
2
|
+
const {isCommaToken} = require('@eslint-community/eslint-utils');
|
3
|
+
const typedArray = require('./shared/typed-array.js');
|
4
|
+
const {
|
5
|
+
removeParentheses,
|
6
|
+
fixSpaceAroundKeyword,
|
7
|
+
addParenthesizesToReturnOrThrowExpression,
|
8
|
+
} = require('./fix/index.js');
|
9
|
+
const {
|
10
|
+
isParenthesized,
|
11
|
+
isOnSameLine,
|
12
|
+
} = require('./utils/index.js');
|
13
|
+
const {isNewExpression, isMethodCall, isCallOrNewExpression} = require('./ast/index.js');
|
14
|
+
|
15
|
+
const SPREAD_IN_LIST = 'spread-in-list';
|
16
|
+
const ITERABLE_TO_ARRAY = 'iterable-to-array';
|
17
|
+
const ITERABLE_TO_ARRAY_IN_FOR_OF = 'iterable-to-array-in-for-of';
|
18
|
+
const ITERABLE_TO_ARRAY_IN_YIELD_STAR = 'iterable-to-array-in-yield-star';
|
19
|
+
const CLONE_ARRAY = 'clone-array';
|
20
|
+
const messages = {
|
21
|
+
[SPREAD_IN_LIST]: 'Spread an {{argumentType}} literal in {{parentDescription}} is unnecessary.',
|
22
|
+
[ITERABLE_TO_ARRAY]: '`{{parentDescription}}` accepts iterable as argument, it\'s unnecessary to convert to an array.',
|
23
|
+
[ITERABLE_TO_ARRAY_IN_FOR_OF]: '`for…of` can iterate over iterable, it\'s unnecessary to convert to an array.',
|
24
|
+
[ITERABLE_TO_ARRAY_IN_YIELD_STAR]: '`yield*` can delegate iterable, it\'s unnecessary to convert to an array.',
|
25
|
+
[CLONE_ARRAY]: 'Unnecessarily cloning an array.',
|
26
|
+
};
|
27
|
+
|
28
|
+
const isSingleArraySpread = node =>
|
29
|
+
node.type === 'ArrayExpression'
|
30
|
+
&& node.elements.length === 1
|
31
|
+
&& node.elements[0]?.type === 'SpreadElement';
|
32
|
+
|
33
|
+
const parentDescriptions = {
|
34
|
+
ArrayExpression: 'array literal',
|
35
|
+
ObjectExpression: 'object literal',
|
36
|
+
CallExpression: 'arguments',
|
37
|
+
NewExpression: 'arguments',
|
38
|
+
};
|
39
|
+
|
40
|
+
function getCommaTokens(arrayExpression, sourceCode) {
|
41
|
+
let startToken = sourceCode.getFirstToken(arrayExpression);
|
42
|
+
|
43
|
+
return arrayExpression.elements.map((element, index, elements) => {
|
44
|
+
if (index === elements.length - 1) {
|
45
|
+
const penultimateToken = sourceCode.getLastToken(arrayExpression, {skip: 1});
|
46
|
+
if (isCommaToken(penultimateToken)) {
|
47
|
+
return penultimateToken;
|
48
|
+
}
|
49
|
+
|
50
|
+
return;
|
51
|
+
}
|
52
|
+
|
53
|
+
const commaToken = sourceCode.getTokenAfter(element || startToken, isCommaToken);
|
54
|
+
startToken = commaToken;
|
55
|
+
return commaToken;
|
56
|
+
});
|
57
|
+
}
|
58
|
+
|
59
|
+
function * unwrapSingleArraySpread(fixer, arrayExpression, sourceCode) {
|
60
|
+
const [
|
61
|
+
openingBracketToken,
|
62
|
+
spreadToken,
|
63
|
+
thirdToken,
|
64
|
+
] = sourceCode.getFirstTokens(arrayExpression, 3);
|
65
|
+
|
66
|
+
// `[...value]`
|
67
|
+
// ^
|
68
|
+
yield fixer.remove(openingBracketToken);
|
69
|
+
|
70
|
+
// `[...value]`
|
71
|
+
// ^^^
|
72
|
+
yield fixer.remove(spreadToken);
|
73
|
+
|
74
|
+
const [
|
75
|
+
commaToken,
|
76
|
+
closingBracketToken,
|
77
|
+
] = sourceCode.getLastTokens(arrayExpression, 2);
|
78
|
+
|
79
|
+
// `[...value]`
|
80
|
+
// ^
|
81
|
+
yield fixer.remove(closingBracketToken);
|
82
|
+
|
83
|
+
// `[...value,]`
|
84
|
+
// ^
|
85
|
+
if (isCommaToken(commaToken)) {
|
86
|
+
yield fixer.remove(commaToken);
|
87
|
+
}
|
88
|
+
|
89
|
+
/*
|
90
|
+
```js
|
91
|
+
function foo() {
|
92
|
+
return [
|
93
|
+
...value,
|
94
|
+
];
|
95
|
+
}
|
96
|
+
```
|
97
|
+
*/
|
98
|
+
const {parent} = arrayExpression;
|
99
|
+
if (
|
100
|
+
(parent.type === 'ReturnStatement' || parent.type === 'ThrowStatement')
|
101
|
+
&& parent.argument === arrayExpression
|
102
|
+
&& !isOnSameLine(openingBracketToken, thirdToken)
|
103
|
+
&& !isParenthesized(arrayExpression, sourceCode)
|
104
|
+
) {
|
105
|
+
yield * addParenthesizesToReturnOrThrowExpression(fixer, parent, sourceCode);
|
106
|
+
return;
|
107
|
+
}
|
108
|
+
|
109
|
+
yield * fixSpaceAroundKeyword(fixer, arrayExpression, sourceCode);
|
110
|
+
}
|
111
|
+
|
112
|
+
/** @param {import('eslint').Rule.RuleContext} context */
|
113
|
+
const create = context => {
|
114
|
+
const {sourceCode} = context;
|
115
|
+
|
116
|
+
// Useless spread in list
|
117
|
+
context.on(['ArrayExpression', 'ObjectExpression'], node => {
|
118
|
+
if (!(
|
119
|
+
node.parent.type === 'SpreadElement'
|
120
|
+
&& node.parent.argument === node
|
121
|
+
&& (
|
122
|
+
(
|
123
|
+
node.type === 'ObjectExpression'
|
124
|
+
&& node.parent.parent.type === 'ObjectExpression'
|
125
|
+
&& node.parent.parent.properties.includes(node.parent)
|
126
|
+
)
|
127
|
+
|| (
|
128
|
+
node.type === 'ArrayExpression'
|
129
|
+
&& (
|
130
|
+
(
|
131
|
+
node.parent.parent.type === 'ArrayExpression'
|
132
|
+
&& node.parent.parent.elements.includes(node.parent)
|
133
|
+
)
|
134
|
+
|| (
|
135
|
+
isCallOrNewExpression(node.parent.parent)
|
136
|
+
&& node.parent.parent.arguments.includes(node.parent)
|
137
|
+
)
|
138
|
+
)
|
139
|
+
)
|
140
|
+
)
|
141
|
+
)) {
|
142
|
+
return;
|
143
|
+
}
|
144
|
+
|
145
|
+
const spreadObject = node;
|
146
|
+
const spreadElement = spreadObject.parent;
|
147
|
+
const spreadToken = sourceCode.getFirstToken(spreadElement);
|
148
|
+
const parentType = spreadElement.parent.type;
|
149
|
+
|
150
|
+
return {
|
151
|
+
node: spreadToken,
|
152
|
+
messageId: SPREAD_IN_LIST,
|
153
|
+
data: {
|
154
|
+
argumentType: spreadObject.type === 'ArrayExpression' ? 'array' : 'object',
|
155
|
+
parentDescription: parentDescriptions[parentType],
|
156
|
+
},
|
157
|
+
/** @param {import('eslint').Rule.RuleFixer} fixer */
|
158
|
+
* fix(fixer) {
|
159
|
+
// `[...[foo]]`
|
160
|
+
// ^^^
|
161
|
+
yield fixer.remove(spreadToken);
|
162
|
+
|
163
|
+
// `[...(( [foo] ))]`
|
164
|
+
// ^^ ^^
|
165
|
+
yield * removeParentheses(spreadObject, fixer, sourceCode);
|
166
|
+
|
167
|
+
// `[...[foo]]`
|
168
|
+
// ^
|
169
|
+
const firstToken = sourceCode.getFirstToken(spreadObject);
|
170
|
+
yield fixer.remove(firstToken);
|
171
|
+
|
172
|
+
const [
|
173
|
+
penultimateToken,
|
174
|
+
lastToken,
|
175
|
+
] = sourceCode.getLastTokens(spreadObject, 2);
|
176
|
+
|
177
|
+
// `[...[foo]]`
|
178
|
+
// ^
|
179
|
+
yield fixer.remove(lastToken);
|
180
|
+
|
181
|
+
// `[...[foo,]]`
|
182
|
+
// ^
|
183
|
+
if (isCommaToken(penultimateToken)) {
|
184
|
+
yield fixer.remove(penultimateToken);
|
185
|
+
}
|
186
|
+
|
187
|
+
if (parentType !== 'CallExpression' && parentType !== 'NewExpression') {
|
188
|
+
return;
|
189
|
+
}
|
190
|
+
|
191
|
+
const commaTokens = getCommaTokens(spreadObject, sourceCode);
|
192
|
+
for (const [index, commaToken] of commaTokens.entries()) {
|
193
|
+
if (spreadObject.elements[index]) {
|
194
|
+
continue;
|
195
|
+
}
|
196
|
+
|
197
|
+
// `call(...[foo, , bar])`
|
198
|
+
// ^ Replace holes with `undefined`
|
199
|
+
yield fixer.insertTextBefore(commaToken, 'undefined');
|
200
|
+
}
|
201
|
+
},
|
202
|
+
};
|
203
|
+
});
|
204
|
+
|
205
|
+
// Useless iterable to array
|
206
|
+
context.on('ArrayExpression', arrayExpression => {
|
207
|
+
if (!isSingleArraySpread(arrayExpression)) {
|
208
|
+
return;
|
209
|
+
}
|
210
|
+
|
211
|
+
const {parent} = arrayExpression;
|
212
|
+
if (!(
|
213
|
+
(parent.type === 'ForOfStatement' && parent.right === arrayExpression)
|
214
|
+
|| (parent.type === 'YieldExpression' && parent.delegate && parent.argument === arrayExpression)
|
215
|
+
|| (
|
216
|
+
(
|
217
|
+
isNewExpression(parent, {names: ['Map', 'WeakMap', 'Set', 'WeakSet'], argumentsLength: 1})
|
218
|
+
|| isNewExpression(parent, {names: typedArray, minimumArguments: 1})
|
219
|
+
|| isMethodCall(parent, {
|
220
|
+
object: 'Promise',
|
221
|
+
methods: ['all', 'allSettled', 'any', 'race'],
|
222
|
+
argumentsLength: 1,
|
223
|
+
optionalCall: false,
|
224
|
+
optionalMember: false,
|
225
|
+
})
|
226
|
+
|| isMethodCall(parent, {
|
227
|
+
objects: ['Array', ...typedArray],
|
228
|
+
method: 'from',
|
229
|
+
argumentsLength: 1,
|
230
|
+
optionalCall: false,
|
231
|
+
optionalMember: false,
|
232
|
+
})
|
233
|
+
|| isMethodCall(parent, {
|
234
|
+
object: 'Object',
|
235
|
+
method: 'fromEntries',
|
236
|
+
argumentsLength: 1,
|
237
|
+
optionalCall: false,
|
238
|
+
optionalMember: false,
|
239
|
+
})
|
240
|
+
)
|
241
|
+
&& parent.arguments[0] === arrayExpression
|
242
|
+
)
|
243
|
+
)) {
|
244
|
+
return;
|
245
|
+
}
|
246
|
+
|
247
|
+
let parentDescription = '';
|
248
|
+
let messageId = ITERABLE_TO_ARRAY;
|
249
|
+
switch (parent.type) {
|
250
|
+
case 'ForOfStatement': {
|
251
|
+
messageId = ITERABLE_TO_ARRAY_IN_FOR_OF;
|
252
|
+
break;
|
253
|
+
}
|
254
|
+
|
255
|
+
case 'YieldExpression': {
|
256
|
+
messageId = ITERABLE_TO_ARRAY_IN_YIELD_STAR;
|
257
|
+
break;
|
258
|
+
}
|
259
|
+
|
260
|
+
case 'NewExpression': {
|
261
|
+
parentDescription = `new ${parent.callee.name}(…)`;
|
262
|
+
break;
|
263
|
+
}
|
264
|
+
|
265
|
+
case 'CallExpression': {
|
266
|
+
parentDescription = `${parent.callee.object.name}.${parent.callee.property.name}(…)`;
|
267
|
+
break;
|
268
|
+
}
|
269
|
+
// No default
|
270
|
+
}
|
271
|
+
|
272
|
+
return {
|
273
|
+
node: arrayExpression,
|
274
|
+
messageId,
|
275
|
+
data: {parentDescription},
|
276
|
+
fix: fixer => unwrapSingleArraySpread(fixer, arrayExpression, sourceCode),
|
277
|
+
};
|
278
|
+
});
|
279
|
+
|
280
|
+
// Useless array clone
|
281
|
+
context.on('ArrayExpression', arrayExpression => {
|
282
|
+
if (!isSingleArraySpread(arrayExpression)) {
|
283
|
+
return;
|
284
|
+
}
|
285
|
+
|
286
|
+
const node = arrayExpression.elements[0].argument;
|
287
|
+
if (!(
|
288
|
+
// Array methods returns a new array
|
289
|
+
isMethodCall(node, {
|
290
|
+
methods: [
|
291
|
+
'concat',
|
292
|
+
'copyWithin',
|
293
|
+
'filter',
|
294
|
+
'flat',
|
295
|
+
'flatMap',
|
296
|
+
'map',
|
297
|
+
'slice',
|
298
|
+
'splice',
|
299
|
+
'toReversed',
|
300
|
+
'toSorted',
|
301
|
+
'toSpliced',
|
302
|
+
'with',
|
303
|
+
],
|
304
|
+
optionalCall: false,
|
305
|
+
optionalMember: false,
|
306
|
+
})
|
307
|
+
// `String#split()`
|
308
|
+
|| isMethodCall(node, {
|
309
|
+
method: 'split',
|
310
|
+
optionalCall: false,
|
311
|
+
optionalMember: false,
|
312
|
+
})
|
313
|
+
// `Object.keys()` and `Object.values()`
|
314
|
+
|| isMethodCall(node, {
|
315
|
+
object: 'Object',
|
316
|
+
methods: ['keys', 'values'],
|
317
|
+
argumentsLength: 1,
|
318
|
+
optionalCall: false,
|
319
|
+
optionalMember: false,
|
320
|
+
})
|
321
|
+
// `await Promise.all()` and `await Promise.allSettled`
|
322
|
+
|| (
|
323
|
+
node.type === 'AwaitExpression'
|
324
|
+
&& isMethodCall(node.argument, {
|
325
|
+
object: 'Promise',
|
326
|
+
methods: ['all', 'allSettled'],
|
327
|
+
argumentsLength: 1,
|
328
|
+
optionalCall: false,
|
329
|
+
optionalMember: false,
|
330
|
+
})
|
331
|
+
)
|
332
|
+
// `Array.from()`, `Array.of()`
|
333
|
+
|| isMethodCall(node, {
|
334
|
+
object: 'Array',
|
335
|
+
methods: ['from', 'of'],
|
336
|
+
optionalCall: false,
|
337
|
+
optionalMember: false,
|
338
|
+
})
|
339
|
+
// `new Array()`
|
340
|
+
|| isNewExpression(node, {name: 'Array'})
|
341
|
+
)) {
|
342
|
+
return;
|
343
|
+
}
|
344
|
+
|
345
|
+
const problem = {
|
346
|
+
node: arrayExpression,
|
347
|
+
messageId: CLONE_ARRAY,
|
348
|
+
};
|
349
|
+
|
350
|
+
if (
|
351
|
+
// `[...new Array(1)]` -> `new Array(1)` is not safe to fix since there are holes
|
352
|
+
isNewExpression(node, {name: 'Array'})
|
353
|
+
// `[...foo.slice(1)]` -> `foo.slice(1)` is not safe to fix since `foo` can be a string
|
354
|
+
|| (
|
355
|
+
node.type === 'CallExpression'
|
356
|
+
&& node.callee.type === 'MemberExpression'
|
357
|
+
&& node.callee.property.type === 'Identifier'
|
358
|
+
&& node.callee.property.name === 'slice'
|
359
|
+
)
|
360
|
+
) {
|
361
|
+
return problem;
|
362
|
+
}
|
363
|
+
|
364
|
+
return Object.assign(problem, {
|
365
|
+
fix: fixer => unwrapSingleArraySpread(fixer, arrayExpression, sourceCode),
|
366
|
+
});
|
367
|
+
});
|
368
|
+
};
|
369
|
+
|
370
|
+
/** @type {import('eslint').Rule.RuleModule} */
|
371
|
+
module.exports = {
|
372
|
+
create,
|
373
|
+
meta: {
|
374
|
+
type: 'suggestion',
|
375
|
+
docs: {
|
376
|
+
description: 'Disallow unnecessary spread.',
|
377
|
+
},
|
378
|
+
fixable: 'code',
|
379
|
+
messages,
|
380
|
+
},
|
381
|
+
};
|
@@ -0,0 +1,71 @@
|
|
1
|
+
'use strict';
|
2
|
+
const {isEmptyNode} = require('./ast/index.js');
|
3
|
+
const getSwitchCaseHeadLocation = require('./utils/get-switch-case-head-location.js');
|
4
|
+
|
5
|
+
const MESSAGE_ID_ERROR = 'no-useless-switch-case/error';
|
6
|
+
const MESSAGE_ID_SUGGESTION = 'no-useless-switch-case/suggestion';
|
7
|
+
const messages = {
|
8
|
+
[MESSAGE_ID_ERROR]: 'Useless case in switch statement.',
|
9
|
+
[MESSAGE_ID_SUGGESTION]: 'Remove this case.',
|
10
|
+
};
|
11
|
+
|
12
|
+
const isEmptySwitchCase = node => node.consequent.every(node => isEmptyNode(node));
|
13
|
+
|
14
|
+
/** @param {import('eslint').Rule.RuleContext} context */
|
15
|
+
const create = context => ({
|
16
|
+
* SwitchStatement(switchStatement) {
|
17
|
+
const {cases} = switchStatement;
|
18
|
+
|
19
|
+
// TypeScript allows multiple `default` cases
|
20
|
+
const defaultCases = cases.filter(switchCase => switchCase.test === null);
|
21
|
+
if (defaultCases.length !== 1) {
|
22
|
+
return;
|
23
|
+
}
|
24
|
+
|
25
|
+
const [defaultCase] = defaultCases;
|
26
|
+
|
27
|
+
// We only check cases where the last case is the `default` case
|
28
|
+
if (defaultCase !== cases.at(-1)) {
|
29
|
+
return;
|
30
|
+
}
|
31
|
+
|
32
|
+
const uselessCases = [];
|
33
|
+
|
34
|
+
for (let index = cases.length - 2; index >= 0; index--) {
|
35
|
+
const node = cases[index];
|
36
|
+
if (isEmptySwitchCase(node)) {
|
37
|
+
uselessCases.unshift(node);
|
38
|
+
} else {
|
39
|
+
break;
|
40
|
+
}
|
41
|
+
}
|
42
|
+
|
43
|
+
for (const node of uselessCases) {
|
44
|
+
yield {
|
45
|
+
node,
|
46
|
+
loc: getSwitchCaseHeadLocation(node, context.sourceCode),
|
47
|
+
messageId: MESSAGE_ID_ERROR,
|
48
|
+
suggest: [
|
49
|
+
{
|
50
|
+
messageId: MESSAGE_ID_SUGGESTION,
|
51
|
+
/** @param {import('eslint').Rule.RuleFixer} fixer */
|
52
|
+
fix: fixer => fixer.remove(node),
|
53
|
+
},
|
54
|
+
],
|
55
|
+
};
|
56
|
+
}
|
57
|
+
},
|
58
|
+
});
|
59
|
+
|
60
|
+
/** @type {import('eslint').Rule.RuleModule} */
|
61
|
+
module.exports = {
|
62
|
+
create,
|
63
|
+
meta: {
|
64
|
+
type: 'suggestion',
|
65
|
+
docs: {
|
66
|
+
description: 'Disallow useless case in switch statements.',
|
67
|
+
},
|
68
|
+
hasSuggestions: true,
|
69
|
+
messages,
|
70
|
+
},
|
71
|
+
};
|