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,279 @@
|
|
1
|
+
'use strict';
|
2
|
+
const {
|
3
|
+
getParenthesizedText,
|
4
|
+
isArrayPrototypeProperty,
|
5
|
+
isNodeMatches,
|
6
|
+
isNodeMatchesNameOrPath,
|
7
|
+
isParenthesized,
|
8
|
+
isSameIdentifier,
|
9
|
+
needsSemicolon,
|
10
|
+
shouldAddParenthesesToMemberExpressionObject,
|
11
|
+
} = require('./utils/index.js');
|
12
|
+
const {fixSpaceAroundKeyword} = require('./fix/index.js');
|
13
|
+
const {
|
14
|
+
isMethodCall,
|
15
|
+
isCallExpression,
|
16
|
+
} = require('./ast/index.js');
|
17
|
+
|
18
|
+
const MESSAGE_ID = 'prefer-array-flat';
|
19
|
+
const messages = {
|
20
|
+
[MESSAGE_ID]: 'Prefer `Array#flat()` over `{{description}}` to flatten an array.',
|
21
|
+
};
|
22
|
+
|
23
|
+
const isEmptyArrayExpression = node =>
|
24
|
+
node.type === 'ArrayExpression'
|
25
|
+
&& node.elements.length === 0;
|
26
|
+
|
27
|
+
// `array.flatMap(x => x)`
|
28
|
+
const arrayFlatMap = {
|
29
|
+
testFunction(node) {
|
30
|
+
if (!isMethodCall(node, {
|
31
|
+
method: 'flatMap',
|
32
|
+
argumentsLength: 1,
|
33
|
+
optionalCall: false,
|
34
|
+
optionalMember: false,
|
35
|
+
})) {
|
36
|
+
return false;
|
37
|
+
}
|
38
|
+
|
39
|
+
const [firstArgument] = node.arguments;
|
40
|
+
return (
|
41
|
+
firstArgument.type === 'ArrowFunctionExpression'
|
42
|
+
&& !firstArgument.async
|
43
|
+
&& firstArgument.params.length === 1
|
44
|
+
&& isSameIdentifier(firstArgument.params[0], firstArgument.body)
|
45
|
+
);
|
46
|
+
},
|
47
|
+
getArrayNode: node => node.callee.object,
|
48
|
+
description: 'Array#flatMap()',
|
49
|
+
};
|
50
|
+
|
51
|
+
// `array.reduce((a, b) => a.concat(b), [])`
|
52
|
+
// `array.reduce((a, b) => [...a, ...b], [])`
|
53
|
+
const arrayReduce = {
|
54
|
+
testFunction(node) {
|
55
|
+
if (!isMethodCall(node, {
|
56
|
+
method: 'reduce',
|
57
|
+
argumentsLength: 2,
|
58
|
+
optionalCall: false,
|
59
|
+
optionalMember: false,
|
60
|
+
})) {
|
61
|
+
return false;
|
62
|
+
}
|
63
|
+
|
64
|
+
const [firstArgument, secondArgument] = node.arguments;
|
65
|
+
if (!(
|
66
|
+
firstArgument.type === 'ArrowFunctionExpression'
|
67
|
+
&& !firstArgument.async
|
68
|
+
&& firstArgument.params.length === 2
|
69
|
+
&& isEmptyArrayExpression(secondArgument)
|
70
|
+
)) {
|
71
|
+
return false;
|
72
|
+
}
|
73
|
+
|
74
|
+
const firstArgumentBody = firstArgument.body;
|
75
|
+
const [firstParameter, secondParameter] = firstArgument.params;
|
76
|
+
return (
|
77
|
+
// `(a, b) => a.concat(b)`
|
78
|
+
(
|
79
|
+
isMethodCall(firstArgumentBody, {
|
80
|
+
method: 'concat',
|
81
|
+
argumentsLength: 1,
|
82
|
+
optionalCall: false,
|
83
|
+
optionalMember: false,
|
84
|
+
})
|
85
|
+
&& isSameIdentifier(firstParameter, firstArgumentBody.callee.object)
|
86
|
+
&& isSameIdentifier(secondParameter, firstArgumentBody.arguments[0])
|
87
|
+
)
|
88
|
+
// `(a, b) => [...a, ...b]`
|
89
|
+
|| (
|
90
|
+
firstArgumentBody.type === 'ArrayExpression'
|
91
|
+
&& firstArgumentBody.elements.length === 2
|
92
|
+
&& firstArgumentBody.elements.every((node, index) =>
|
93
|
+
node?.type === 'SpreadElement'
|
94
|
+
&& node.argument.type === 'Identifier'
|
95
|
+
&& isSameIdentifier(firstArgument.params[index], node.argument),
|
96
|
+
)
|
97
|
+
)
|
98
|
+
);
|
99
|
+
},
|
100
|
+
getArrayNode: node => node.callee.object,
|
101
|
+
description: 'Array#reduce()',
|
102
|
+
};
|
103
|
+
|
104
|
+
// `[].concat(maybeArray)`
|
105
|
+
// `[].concat(...array)`
|
106
|
+
const emptyArrayConcat = {
|
107
|
+
testFunction(node) {
|
108
|
+
return isMethodCall(node, {
|
109
|
+
method: 'concat',
|
110
|
+
argumentsLength: 1,
|
111
|
+
allowSpreadElement: true,
|
112
|
+
optionalCall: false,
|
113
|
+
optionalMember: false,
|
114
|
+
})
|
115
|
+
&& isEmptyArrayExpression(node.callee.object);
|
116
|
+
},
|
117
|
+
getArrayNode(node) {
|
118
|
+
const argumentNode = node.arguments[0];
|
119
|
+
return argumentNode.type === 'SpreadElement' ? argumentNode.argument : argumentNode;
|
120
|
+
},
|
121
|
+
description: '[].concat()',
|
122
|
+
shouldSwitchToArray: node => node.arguments[0].type !== 'SpreadElement',
|
123
|
+
};
|
124
|
+
|
125
|
+
// - `[].concat.apply([], array)` and `Array.prototype.concat.apply([], array)`
|
126
|
+
// - `[].concat.call([], maybeArray)` and `Array.prototype.concat.call([], maybeArray)`
|
127
|
+
// - `[].concat.call([], ...array)` and `Array.prototype.concat.call([], ...array)`
|
128
|
+
const arrayPrototypeConcat = {
|
129
|
+
testFunction(node) {
|
130
|
+
if (!(
|
131
|
+
isMethodCall(node, {
|
132
|
+
methods: ['apply', 'call'],
|
133
|
+
argumentsLength: 2,
|
134
|
+
allowSpreadElement: true,
|
135
|
+
optionalCall: false,
|
136
|
+
optionalMember: false,
|
137
|
+
})
|
138
|
+
&& isArrayPrototypeProperty(node.callee.object, {
|
139
|
+
property: 'concat',
|
140
|
+
})
|
141
|
+
)) {
|
142
|
+
return false;
|
143
|
+
}
|
144
|
+
|
145
|
+
const [firstArgument, secondArgument] = node.arguments;
|
146
|
+
return isEmptyArrayExpression(firstArgument)
|
147
|
+
&& (
|
148
|
+
node.callee.property.name === 'call'
|
149
|
+
|| secondArgument.type !== 'SpreadElement'
|
150
|
+
);
|
151
|
+
},
|
152
|
+
getArrayNode(node) {
|
153
|
+
const argumentNode = node.arguments[1];
|
154
|
+
return argumentNode.type === 'SpreadElement' ? argumentNode.argument : argumentNode;
|
155
|
+
},
|
156
|
+
description: 'Array.prototype.concat()',
|
157
|
+
shouldSwitchToArray: node => node.arguments[1].type !== 'SpreadElement' && node.callee.property.name === 'call',
|
158
|
+
};
|
159
|
+
|
160
|
+
const lodashFlattenFunctions = [
|
161
|
+
'_.flatten',
|
162
|
+
'lodash.flatten',
|
163
|
+
'underscore.flatten',
|
164
|
+
];
|
165
|
+
|
166
|
+
function fix(node, array, sourceCode, shouldSwitchToArray) {
|
167
|
+
if (typeof shouldSwitchToArray === 'function') {
|
168
|
+
shouldSwitchToArray = shouldSwitchToArray(node);
|
169
|
+
}
|
170
|
+
|
171
|
+
return function * (fixer) {
|
172
|
+
let fixed = getParenthesizedText(array, sourceCode);
|
173
|
+
if (shouldSwitchToArray) {
|
174
|
+
// `array` is an argument, when it changes to `array[]`, we don't need add extra parentheses
|
175
|
+
fixed = `[${fixed}]`;
|
176
|
+
// And we don't need to add parentheses to the new array to call `.flat()`
|
177
|
+
} else if (
|
178
|
+
!isParenthesized(array, sourceCode)
|
179
|
+
&& shouldAddParenthesesToMemberExpressionObject(array, sourceCode)
|
180
|
+
) {
|
181
|
+
fixed = `(${fixed})`;
|
182
|
+
}
|
183
|
+
|
184
|
+
fixed = `${fixed}.flat()`;
|
185
|
+
|
186
|
+
const tokenBefore = sourceCode.getTokenBefore(node);
|
187
|
+
if (needsSemicolon(tokenBefore, sourceCode, fixed)) {
|
188
|
+
fixed = `;${fixed}`;
|
189
|
+
}
|
190
|
+
|
191
|
+
yield fixer.replaceText(node, fixed);
|
192
|
+
|
193
|
+
yield * fixSpaceAroundKeyword(fixer, node, sourceCode);
|
194
|
+
};
|
195
|
+
}
|
196
|
+
|
197
|
+
function create(context) {
|
198
|
+
const {functions: configFunctions} = {
|
199
|
+
functions: [],
|
200
|
+
...context.options[0],
|
201
|
+
};
|
202
|
+
const functions = [...configFunctions, ...lodashFlattenFunctions];
|
203
|
+
|
204
|
+
const cases = [
|
205
|
+
arrayFlatMap,
|
206
|
+
arrayReduce,
|
207
|
+
emptyArrayConcat,
|
208
|
+
arrayPrototypeConcat,
|
209
|
+
{
|
210
|
+
testFunction: node => isCallExpression(node, {
|
211
|
+
argumentsLength: 1,
|
212
|
+
optional: false,
|
213
|
+
}) && isNodeMatches(node.callee, functions),
|
214
|
+
getArrayNode: node => node.arguments[0],
|
215
|
+
description: node => `${functions.find(nameOrPath => isNodeMatchesNameOrPath(node.callee, nameOrPath)).trim()}()`,
|
216
|
+
},
|
217
|
+
];
|
218
|
+
|
219
|
+
return {
|
220
|
+
* CallExpression(node) {
|
221
|
+
for (const {testFunction, description, getArrayNode, shouldSwitchToArray} of cases) {
|
222
|
+
if (!testFunction(node)) {
|
223
|
+
continue;
|
224
|
+
}
|
225
|
+
|
226
|
+
const array = getArrayNode(node);
|
227
|
+
|
228
|
+
const data = {
|
229
|
+
description: typeof description === 'string' ? description : description(node),
|
230
|
+
};
|
231
|
+
|
232
|
+
const problem = {
|
233
|
+
node,
|
234
|
+
messageId: MESSAGE_ID,
|
235
|
+
data,
|
236
|
+
};
|
237
|
+
|
238
|
+
const {sourceCode} = context;
|
239
|
+
|
240
|
+
// Don't fix if it has comments.
|
241
|
+
if (
|
242
|
+
sourceCode.getCommentsInside(node).length
|
243
|
+
=== sourceCode.getCommentsInside(array).length
|
244
|
+
) {
|
245
|
+
problem.fix = fix(node, array, sourceCode, shouldSwitchToArray);
|
246
|
+
}
|
247
|
+
|
248
|
+
yield problem;
|
249
|
+
}
|
250
|
+
},
|
251
|
+
};
|
252
|
+
}
|
253
|
+
|
254
|
+
const schema = [
|
255
|
+
{
|
256
|
+
type: 'object',
|
257
|
+
additionalProperties: false,
|
258
|
+
properties: {
|
259
|
+
functions: {
|
260
|
+
type: 'array',
|
261
|
+
uniqueItems: true,
|
262
|
+
},
|
263
|
+
},
|
264
|
+
},
|
265
|
+
];
|
266
|
+
|
267
|
+
/** @type {import('eslint').Rule.RuleModule} */
|
268
|
+
module.exports = {
|
269
|
+
create,
|
270
|
+
meta: {
|
271
|
+
type: 'suggestion',
|
272
|
+
docs: {
|
273
|
+
description: 'Prefer `Array#flat()` over legacy techniques to flatten arrays.',
|
274
|
+
},
|
275
|
+
fixable: 'code',
|
276
|
+
schema,
|
277
|
+
messages,
|
278
|
+
},
|
279
|
+
};
|
@@ -0,0 +1,32 @@
|
|
1
|
+
'use strict';
|
2
|
+
const simpleArraySearchRule = require('./shared/simple-array-search-rule.js');
|
3
|
+
|
4
|
+
const indexOfOverFindIndexRule = simpleArraySearchRule({
|
5
|
+
method: 'findIndex',
|
6
|
+
replacement: 'indexOf',
|
7
|
+
});
|
8
|
+
|
9
|
+
const lastIndexOfOverFindLastIndexRule = simpleArraySearchRule({
|
10
|
+
method: 'findLastIndex',
|
11
|
+
replacement: 'lastIndexOf',
|
12
|
+
});
|
13
|
+
|
14
|
+
/** @type {import('eslint').Rule.RuleModule} */
|
15
|
+
module.exports = {
|
16
|
+
create(context) {
|
17
|
+
indexOfOverFindIndexRule.listen(context);
|
18
|
+
lastIndexOfOverFindLastIndexRule.listen(context);
|
19
|
+
},
|
20
|
+
meta: {
|
21
|
+
type: 'suggestion',
|
22
|
+
docs: {
|
23
|
+
description: 'Prefer `Array#{indexOf,lastIndexOf}()` over `Array#{findIndex,findLastIndex}()` when looking for the index of an item.',
|
24
|
+
},
|
25
|
+
fixable: 'code',
|
26
|
+
hasSuggestions: true,
|
27
|
+
messages: {
|
28
|
+
...indexOfOverFindIndexRule.messages,
|
29
|
+
...lastIndexOfOverFindLastIndexRule.messages,
|
30
|
+
},
|
31
|
+
},
|
32
|
+
};
|
@@ -0,0 +1,157 @@
|
|
1
|
+
'use strict';
|
2
|
+
const {checkVueTemplate} = require('./utils/rule.js');
|
3
|
+
const {
|
4
|
+
isBooleanNode,
|
5
|
+
getParenthesizedRange,
|
6
|
+
isNodeValueNotFunction,
|
7
|
+
} = require('./utils/index.js');
|
8
|
+
const {removeMemberExpressionProperty} = require('./fix/index.js');
|
9
|
+
const {isLiteral, isUndefined, isMethodCall, isMemberExpression} = require('./ast/index.js');
|
10
|
+
|
11
|
+
const ERROR_ID_ARRAY_SOME = 'some';
|
12
|
+
const SUGGESTION_ID_ARRAY_SOME = 'some-suggestion';
|
13
|
+
const ERROR_ID_ARRAY_FILTER = 'filter';
|
14
|
+
const messages = {
|
15
|
+
[ERROR_ID_ARRAY_SOME]: 'Prefer `.some(…)` over `.{{method}}(…)`.',
|
16
|
+
[SUGGESTION_ID_ARRAY_SOME]: 'Replace `.{{method}}(…)` with `.some(…)`.',
|
17
|
+
[ERROR_ID_ARRAY_FILTER]: 'Prefer `.some(…)` over non-zero length check from `.filter(…)`.',
|
18
|
+
};
|
19
|
+
|
20
|
+
const isCheckingUndefined = node =>
|
21
|
+
node.parent.type === 'BinaryExpression'
|
22
|
+
// Not checking yoda expression `null != foo.find()` and `undefined !== foo.find()
|
23
|
+
&& node.parent.left === node
|
24
|
+
&& (
|
25
|
+
(
|
26
|
+
(
|
27
|
+
node.parent.operator === '!='
|
28
|
+
|| node.parent.operator === '=='
|
29
|
+
|| node.parent.operator === '==='
|
30
|
+
|| node.parent.operator === '!=='
|
31
|
+
)
|
32
|
+
&& isUndefined(node.parent.right)
|
33
|
+
)
|
34
|
+
|| (
|
35
|
+
(
|
36
|
+
node.parent.operator === '!='
|
37
|
+
|| node.parent.operator === '=='
|
38
|
+
)
|
39
|
+
// eslint-disable-next-line unicorn/no-null
|
40
|
+
&& isLiteral(node.parent.right, null)
|
41
|
+
)
|
42
|
+
);
|
43
|
+
|
44
|
+
/** @param {import('eslint').Rule.RuleContext} context */
|
45
|
+
const create = context => ({
|
46
|
+
CallExpression(callExpression) {
|
47
|
+
if (!isMethodCall(callExpression, {
|
48
|
+
methods: ['find', 'findLast'],
|
49
|
+
minimumArguments: 1,
|
50
|
+
maximumArguments: 2,
|
51
|
+
optionalCall: false,
|
52
|
+
optionalMember: false,
|
53
|
+
})) {
|
54
|
+
return;
|
55
|
+
}
|
56
|
+
|
57
|
+
const isCompare = isCheckingUndefined(callExpression);
|
58
|
+
if (!isCompare && !isBooleanNode(callExpression)) {
|
59
|
+
return;
|
60
|
+
}
|
61
|
+
|
62
|
+
const methodNode = callExpression.callee.property;
|
63
|
+
return {
|
64
|
+
node: methodNode,
|
65
|
+
messageId: ERROR_ID_ARRAY_SOME,
|
66
|
+
data: {method: methodNode.name},
|
67
|
+
suggest: [
|
68
|
+
{
|
69
|
+
messageId: SUGGESTION_ID_ARRAY_SOME,
|
70
|
+
* fix(fixer) {
|
71
|
+
yield fixer.replaceText(methodNode, 'some');
|
72
|
+
|
73
|
+
if (!isCompare) {
|
74
|
+
return;
|
75
|
+
}
|
76
|
+
|
77
|
+
const parenthesizedRange = getParenthesizedRange(callExpression, context.sourceCode);
|
78
|
+
yield fixer.replaceTextRange([parenthesizedRange[1], callExpression.parent.range[1]], '');
|
79
|
+
|
80
|
+
if (callExpression.parent.operator === '!=' || callExpression.parent.operator === '!==') {
|
81
|
+
return;
|
82
|
+
}
|
83
|
+
|
84
|
+
yield fixer.insertTextBeforeRange(parenthesizedRange, '!');
|
85
|
+
},
|
86
|
+
},
|
87
|
+
],
|
88
|
+
};
|
89
|
+
},
|
90
|
+
BinaryExpression(binaryExpression) {
|
91
|
+
if (!(
|
92
|
+
// We assume the user already follows `unicorn/explicit-length-check`. These are allowed in that rule.
|
93
|
+
(binaryExpression.operator === '>' || binaryExpression.operator === '!==')
|
94
|
+
&& binaryExpression.right.type === 'Literal'
|
95
|
+
&& binaryExpression.right.raw === '0'
|
96
|
+
&& isMemberExpression(binaryExpression.left, {property: 'length', optional: false})
|
97
|
+
&& isMethodCall(binaryExpression.left.object, {
|
98
|
+
method: 'filter',
|
99
|
+
optionalCall: false,
|
100
|
+
optionalMember: false,
|
101
|
+
})
|
102
|
+
)) {
|
103
|
+
return;
|
104
|
+
}
|
105
|
+
|
106
|
+
const filterCall = binaryExpression.left.object;
|
107
|
+
const [firstArgument] = filterCall.arguments;
|
108
|
+
if (!firstArgument || isNodeValueNotFunction(firstArgument)) {
|
109
|
+
return;
|
110
|
+
}
|
111
|
+
|
112
|
+
const filterProperty = filterCall.callee.property;
|
113
|
+
return {
|
114
|
+
node: filterProperty,
|
115
|
+
messageId: ERROR_ID_ARRAY_FILTER,
|
116
|
+
* fix(fixer) {
|
117
|
+
// `.filter` to `.some`
|
118
|
+
yield fixer.replaceText(filterProperty, 'some');
|
119
|
+
|
120
|
+
const {sourceCode} = context;
|
121
|
+
const lengthNode = binaryExpression.left;
|
122
|
+
/*
|
123
|
+
Remove `.length`
|
124
|
+
`(( (( array.filter() )).length )) > (( 0 ))`
|
125
|
+
------------------------^^^^^^^
|
126
|
+
*/
|
127
|
+
yield removeMemberExpressionProperty(fixer, lengthNode, sourceCode);
|
128
|
+
|
129
|
+
/*
|
130
|
+
Remove `> 0`
|
131
|
+
`(( (( array.filter() )).length )) > (( 0 ))`
|
132
|
+
----------------------------------^^^^^^^^^^
|
133
|
+
*/
|
134
|
+
yield fixer.removeRange([
|
135
|
+
getParenthesizedRange(lengthNode, sourceCode)[1],
|
136
|
+
binaryExpression.range[1],
|
137
|
+
]);
|
138
|
+
|
139
|
+
// The `BinaryExpression` always ends with a number or `)`, no need check for ASI
|
140
|
+
},
|
141
|
+
};
|
142
|
+
},
|
143
|
+
});
|
144
|
+
|
145
|
+
/** @type {import('eslint').Rule.RuleModule} */
|
146
|
+
module.exports = {
|
147
|
+
create: checkVueTemplate(create),
|
148
|
+
meta: {
|
149
|
+
type: 'suggestion',
|
150
|
+
docs: {
|
151
|
+
description: 'Prefer `.some(…)` over `.filter(…).length` check and `.{find,findLast}(…)`.',
|
152
|
+
},
|
153
|
+
fixable: 'code',
|
154
|
+
messages,
|
155
|
+
hasSuggestions: true,
|
156
|
+
},
|
157
|
+
};
|