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,198 @@
|
|
1
|
+
'use strict';
|
2
|
+
const {getStaticValue, getPropertyName} = require('@eslint-community/eslint-utils');
|
3
|
+
const {isMethodCall} = require('./ast/index.js');
|
4
|
+
|
5
|
+
const MESSAGE_ID_OBJECT = 'no-thenable-object';
|
6
|
+
const MESSAGE_ID_EXPORT = 'no-thenable-export';
|
7
|
+
const MESSAGE_ID_CLASS = 'no-thenable-class';
|
8
|
+
const messages = {
|
9
|
+
[MESSAGE_ID_OBJECT]: 'Do not add `then` to an object.',
|
10
|
+
[MESSAGE_ID_EXPORT]: 'Do not export `then`.',
|
11
|
+
[MESSAGE_ID_CLASS]: 'Do not add `then` to a class.',
|
12
|
+
};
|
13
|
+
|
14
|
+
const isStringThen = (node, context) =>
|
15
|
+
getStaticValue(node, context.sourceCode.getScope(node))?.value === 'then';
|
16
|
+
const isPropertyThen = (node, context) => {
|
17
|
+
// `getPropertyName` throws on `({[Symbol.prototype]: 0})`
|
18
|
+
// https://github.com/eslint-community/eslint-utils/pull/182
|
19
|
+
try {
|
20
|
+
return getPropertyName(node, context.sourceCode.getScope(node)) === 'then';
|
21
|
+
} catch {}
|
22
|
+
|
23
|
+
return false;
|
24
|
+
};
|
25
|
+
|
26
|
+
const cases = [
|
27
|
+
// `{then() {}}`,
|
28
|
+
// `{get then() {}}`,
|
29
|
+
// `{[computedKey]() {}}`,
|
30
|
+
// `{get [computedKey]() {}}`,
|
31
|
+
{
|
32
|
+
selector: 'ObjectExpression',
|
33
|
+
* getNodes(node, context) {
|
34
|
+
for (const property of node.properties) {
|
35
|
+
if (property.type === 'Property' && isPropertyThen(property, context)) {
|
36
|
+
yield property.key;
|
37
|
+
}
|
38
|
+
}
|
39
|
+
},
|
40
|
+
messageId: MESSAGE_ID_OBJECT,
|
41
|
+
},
|
42
|
+
// `class Foo {then}`,
|
43
|
+
// `class Foo {static then}`,
|
44
|
+
// `class Foo {get then() {}}`,
|
45
|
+
// `class Foo {static get then() {}}`,
|
46
|
+
{
|
47
|
+
selectors: ['PropertyDefinition', 'MethodDefinition'],
|
48
|
+
* getNodes(node, context) {
|
49
|
+
if (getPropertyName(node, context.sourceCode.getScope(node)) === 'then') {
|
50
|
+
yield node.key;
|
51
|
+
}
|
52
|
+
},
|
53
|
+
messageId: MESSAGE_ID_CLASS,
|
54
|
+
},
|
55
|
+
// `foo.then = …`
|
56
|
+
// `foo[computedKey] = …`
|
57
|
+
{
|
58
|
+
selector: 'MemberExpression',
|
59
|
+
* getNodes(node, context) {
|
60
|
+
if (!(node.parent.type === 'AssignmentExpression' && node.parent.left === node)) {
|
61
|
+
return;
|
62
|
+
}
|
63
|
+
|
64
|
+
if (getPropertyName(node, context.sourceCode.getScope(node)) === 'then') {
|
65
|
+
yield node.property;
|
66
|
+
}
|
67
|
+
},
|
68
|
+
messageId: MESSAGE_ID_OBJECT,
|
69
|
+
},
|
70
|
+
// `Object.defineProperty(foo, 'then', …)`
|
71
|
+
// `Reflect.defineProperty(foo, 'then', …)`
|
72
|
+
{
|
73
|
+
selector: 'CallExpression',
|
74
|
+
* getNodes(node, context) {
|
75
|
+
if (!(
|
76
|
+
isMethodCall(node, {
|
77
|
+
objects: ['Object', 'Reflect'],
|
78
|
+
method: 'defineProperty',
|
79
|
+
minimumArguments: 3,
|
80
|
+
optionalCall: false,
|
81
|
+
optionalMember: false,
|
82
|
+
})
|
83
|
+
&& node.arguments[0].type !== 'SpreadElement'
|
84
|
+
)) {
|
85
|
+
return;
|
86
|
+
}
|
87
|
+
|
88
|
+
const [, secondArgument] = node.arguments;
|
89
|
+
if (isStringThen(secondArgument, context)) {
|
90
|
+
yield secondArgument;
|
91
|
+
}
|
92
|
+
},
|
93
|
+
messageId: MESSAGE_ID_OBJECT,
|
94
|
+
},
|
95
|
+
// `Object.fromEntries([['then', …]])`
|
96
|
+
{
|
97
|
+
selector: 'CallExpression',
|
98
|
+
* getNodes(node, context) {
|
99
|
+
if (!(
|
100
|
+
isMethodCall(node, {
|
101
|
+
object: 'Object',
|
102
|
+
method: 'fromEntries',
|
103
|
+
argumentsLength: 1,
|
104
|
+
optionalCall: false,
|
105
|
+
optionalMember: false,
|
106
|
+
})
|
107
|
+
&& node.arguments[0].type === 'ArrayExpression'
|
108
|
+
)) {
|
109
|
+
return;
|
110
|
+
}
|
111
|
+
|
112
|
+
for (const pairs of node.arguments[0].elements) {
|
113
|
+
if (
|
114
|
+
pairs?.type === 'ArrayExpression'
|
115
|
+
&& pairs.elements[0]
|
116
|
+
&& pairs.elements[0].type !== 'SpreadElement'
|
117
|
+
) {
|
118
|
+
const [key] = pairs.elements;
|
119
|
+
|
120
|
+
if (isStringThen(key, context)) {
|
121
|
+
yield key;
|
122
|
+
}
|
123
|
+
}
|
124
|
+
}
|
125
|
+
},
|
126
|
+
messageId: MESSAGE_ID_OBJECT,
|
127
|
+
},
|
128
|
+
// `export {then}`
|
129
|
+
{
|
130
|
+
selector: 'Identifier',
|
131
|
+
* getNodes(node) {
|
132
|
+
if (
|
133
|
+
node.name === 'then'
|
134
|
+
&& node.parent.type === 'ExportSpecifier'
|
135
|
+
&& node.parent.exported === node
|
136
|
+
) {
|
137
|
+
yield node;
|
138
|
+
}
|
139
|
+
},
|
140
|
+
messageId: MESSAGE_ID_EXPORT,
|
141
|
+
},
|
142
|
+
// `export function then() {}`,
|
143
|
+
// `export class then {}`,
|
144
|
+
{
|
145
|
+
selector: 'Identifier',
|
146
|
+
* getNodes(node) {
|
147
|
+
if (
|
148
|
+
node.name === 'then'
|
149
|
+
&& (node.parent.type === 'FunctionDeclaration' || node.parent.type === 'ClassDeclaration')
|
150
|
+
&& node.parent.id === node
|
151
|
+
&& node.parent.parent.type === 'ExportNamedDeclaration'
|
152
|
+
&& node.parent.parent.declaration === node.parent
|
153
|
+
) {
|
154
|
+
yield node;
|
155
|
+
}
|
156
|
+
},
|
157
|
+
messageId: MESSAGE_ID_EXPORT,
|
158
|
+
},
|
159
|
+
// `export const … = …`;
|
160
|
+
{
|
161
|
+
selector: 'VariableDeclaration',
|
162
|
+
* getNodes(node, context) {
|
163
|
+
if (!(node.parent.type === 'ExportNamedDeclaration' && node.parent.declaration === node)) {
|
164
|
+
return;
|
165
|
+
}
|
166
|
+
|
167
|
+
for (const variable of context.sourceCode.getDeclaredVariables(node)) {
|
168
|
+
if (variable.name === 'then') {
|
169
|
+
yield * variable.identifiers;
|
170
|
+
}
|
171
|
+
}
|
172
|
+
},
|
173
|
+
messageId: MESSAGE_ID_EXPORT,
|
174
|
+
},
|
175
|
+
];
|
176
|
+
|
177
|
+
/** @param {import('eslint').Rule.RuleContext} context */
|
178
|
+
const create = context => {
|
179
|
+
for (const {selector, selectors, messageId, getNodes} of cases) {
|
180
|
+
context.on(selector ?? selectors, function * (node) {
|
181
|
+
for (const problematicNode of getNodes(node, context)) {
|
182
|
+
yield {node: problematicNode, messageId};
|
183
|
+
}
|
184
|
+
});
|
185
|
+
}
|
186
|
+
};
|
187
|
+
|
188
|
+
/** @type {import('eslint').Rule.RuleModule} */
|
189
|
+
module.exports = {
|
190
|
+
create,
|
191
|
+
meta: {
|
192
|
+
type: 'problem',
|
193
|
+
docs: {
|
194
|
+
description: 'Disallow `then` property.',
|
195
|
+
},
|
196
|
+
messages,
|
197
|
+
},
|
198
|
+
};
|
@@ -0,0 +1,38 @@
|
|
1
|
+
'use strict';
|
2
|
+
const MESSAGE_ID = 'no-this-assignment';
|
3
|
+
const messages = {
|
4
|
+
[MESSAGE_ID]: 'Do not assign `this` to `{{name}}`.',
|
5
|
+
};
|
6
|
+
|
7
|
+
function getProblem(variableNode, valueNode) {
|
8
|
+
if (
|
9
|
+
variableNode.type !== 'Identifier'
|
10
|
+
|| valueNode?.type !== 'ThisExpression'
|
11
|
+
) {
|
12
|
+
return;
|
13
|
+
}
|
14
|
+
|
15
|
+
return {
|
16
|
+
node: valueNode.parent,
|
17
|
+
data: {name: variableNode.name},
|
18
|
+
messageId: MESSAGE_ID,
|
19
|
+
};
|
20
|
+
}
|
21
|
+
|
22
|
+
/** @param {import('eslint').Rule.RuleContext} context */
|
23
|
+
const create = context => {
|
24
|
+
context.on('VariableDeclarator', node => getProblem(node.id, node.init));
|
25
|
+
context.on('AssignmentExpression', node => getProblem(node.left, node.right));
|
26
|
+
};
|
27
|
+
|
28
|
+
/** @type {import('eslint').Rule.RuleModule} */
|
29
|
+
module.exports = {
|
30
|
+
create,
|
31
|
+
meta: {
|
32
|
+
type: 'suggestion',
|
33
|
+
docs: {
|
34
|
+
description: 'Disallow assigning `this` to a variable.',
|
35
|
+
},
|
36
|
+
messages,
|
37
|
+
},
|
38
|
+
};
|
@@ -0,0 +1,143 @@
|
|
1
|
+
'use strict';
|
2
|
+
const {isLiteral} = require('./ast/index.js');
|
3
|
+
const {
|
4
|
+
addParenthesizesToReturnOrThrowExpression,
|
5
|
+
removeSpacesAfter,
|
6
|
+
} = require('./fix/index.js');
|
7
|
+
const {
|
8
|
+
needsSemicolon,
|
9
|
+
isParenthesized,
|
10
|
+
isOnSameLine,
|
11
|
+
isShadowed,
|
12
|
+
} = require('./utils/index.js');
|
13
|
+
|
14
|
+
const MESSAGE_ID_ERROR = 'no-typeof-undefined/error';
|
15
|
+
const MESSAGE_ID_SUGGESTION = 'no-typeof-undefined/suggestion';
|
16
|
+
const messages = {
|
17
|
+
[MESSAGE_ID_ERROR]: 'Compare with `undefined` directly instead of using `typeof`.',
|
18
|
+
[MESSAGE_ID_SUGGESTION]: 'Switch to `… {{operator}} undefined`.',
|
19
|
+
};
|
20
|
+
|
21
|
+
/** @param {import('eslint').Rule.RuleContext} context */
|
22
|
+
const create = context => {
|
23
|
+
const {
|
24
|
+
checkGlobalVariables,
|
25
|
+
} = {
|
26
|
+
checkGlobalVariables: false,
|
27
|
+
...context.options[0],
|
28
|
+
};
|
29
|
+
const {sourceCode} = context;
|
30
|
+
|
31
|
+
return {
|
32
|
+
BinaryExpression(binaryExpression) {
|
33
|
+
if (!(
|
34
|
+
(
|
35
|
+
binaryExpression.operator === '==='
|
36
|
+
|| binaryExpression.operator === '!=='
|
37
|
+
|| binaryExpression.operator === '=='
|
38
|
+
|| binaryExpression.operator === '!='
|
39
|
+
)
|
40
|
+
&& binaryExpression.left.type === 'UnaryExpression'
|
41
|
+
&& binaryExpression.left.operator === 'typeof'
|
42
|
+
&& binaryExpression.left.prefix
|
43
|
+
&& isLiteral(binaryExpression.right, 'undefined')
|
44
|
+
)) {
|
45
|
+
return;
|
46
|
+
}
|
47
|
+
|
48
|
+
const {left: typeofNode, right: undefinedString, operator} = binaryExpression;
|
49
|
+
|
50
|
+
const valueNode = typeofNode.argument;
|
51
|
+
const isGlobalVariable = valueNode.type === 'Identifier'
|
52
|
+
&& !isShadowed(sourceCode.getScope(valueNode), valueNode);
|
53
|
+
|
54
|
+
if (!checkGlobalVariables && isGlobalVariable) {
|
55
|
+
return;
|
56
|
+
}
|
57
|
+
|
58
|
+
const [typeofToken, secondToken] = sourceCode.getFirstTokens(typeofNode, 2);
|
59
|
+
|
60
|
+
const fix = function * (fixer) {
|
61
|
+
// Change `==`/`!=` to `===`/`!==`
|
62
|
+
if (operator === '==' || operator === '!=') {
|
63
|
+
const operatorToken = sourceCode.getTokenAfter(
|
64
|
+
typeofNode,
|
65
|
+
token => token.type === 'Punctuator' && token.value === operator,
|
66
|
+
);
|
67
|
+
|
68
|
+
yield fixer.insertTextAfter(operatorToken, '=');
|
69
|
+
}
|
70
|
+
|
71
|
+
yield fixer.replaceText(undefinedString, 'undefined');
|
72
|
+
|
73
|
+
yield fixer.remove(typeofToken);
|
74
|
+
yield removeSpacesAfter(typeofToken, sourceCode, fixer);
|
75
|
+
|
76
|
+
const {parent} = binaryExpression;
|
77
|
+
if (
|
78
|
+
(parent.type === 'ReturnStatement' || parent.type === 'ThrowStatement')
|
79
|
+
&& parent.argument === binaryExpression
|
80
|
+
&& !isOnSameLine(typeofToken, secondToken)
|
81
|
+
&& !isParenthesized(binaryExpression, sourceCode)
|
82
|
+
&& !isParenthesized(typeofNode, sourceCode)
|
83
|
+
) {
|
84
|
+
yield * addParenthesizesToReturnOrThrowExpression(fixer, parent, sourceCode);
|
85
|
+
return;
|
86
|
+
}
|
87
|
+
|
88
|
+
const tokenBefore = sourceCode.getTokenBefore(binaryExpression);
|
89
|
+
if (needsSemicolon(tokenBefore, sourceCode, secondToken.value)) {
|
90
|
+
yield fixer.insertTextBefore(binaryExpression, ';');
|
91
|
+
}
|
92
|
+
};
|
93
|
+
|
94
|
+
const problem = {
|
95
|
+
node: binaryExpression,
|
96
|
+
loc: typeofToken.loc,
|
97
|
+
messageId: MESSAGE_ID_ERROR,
|
98
|
+
};
|
99
|
+
|
100
|
+
if (isGlobalVariable) {
|
101
|
+
problem.suggest = [
|
102
|
+
{
|
103
|
+
messageId: MESSAGE_ID_SUGGESTION,
|
104
|
+
data: {operator: operator.startsWith('!') ? '!==' : '==='},
|
105
|
+
fix,
|
106
|
+
},
|
107
|
+
];
|
108
|
+
} else {
|
109
|
+
problem.fix = fix;
|
110
|
+
}
|
111
|
+
|
112
|
+
return problem;
|
113
|
+
},
|
114
|
+
};
|
115
|
+
};
|
116
|
+
|
117
|
+
const schema = [
|
118
|
+
{
|
119
|
+
type: 'object',
|
120
|
+
additionalProperties: false,
|
121
|
+
properties: {
|
122
|
+
checkGlobalVariables: {
|
123
|
+
type: 'boolean',
|
124
|
+
default: false,
|
125
|
+
},
|
126
|
+
},
|
127
|
+
},
|
128
|
+
];
|
129
|
+
|
130
|
+
/** @type {import('eslint').Rule.RuleModule} */
|
131
|
+
module.exports = {
|
132
|
+
create,
|
133
|
+
meta: {
|
134
|
+
type: 'suggestion',
|
135
|
+
docs: {
|
136
|
+
description: 'Disallow comparing `undefined` using `typeof`.',
|
137
|
+
},
|
138
|
+
fixable: 'code',
|
139
|
+
hasSuggestions: true,
|
140
|
+
schema,
|
141
|
+
messages,
|
142
|
+
},
|
143
|
+
};
|
@@ -0,0 +1,107 @@
|
|
1
|
+
'use strict';
|
2
|
+
const {
|
3
|
+
addParenthesizesToReturnOrThrowExpression,
|
4
|
+
removeSpacesAfter,
|
5
|
+
} = require('./fix/index.js');
|
6
|
+
const {isParenthesized} = require('./utils/parentheses.js');
|
7
|
+
const needsSemicolon = require('./utils/needs-semicolon.js');
|
8
|
+
const isOnSameLine = require('./utils/is-on-same-line.js');
|
9
|
+
|
10
|
+
const MESSAGE_ID = 'no-unnecessary-await';
|
11
|
+
const messages = {
|
12
|
+
[MESSAGE_ID]: 'Do not `await` non-promise value.',
|
13
|
+
};
|
14
|
+
|
15
|
+
function notPromise(node) {
|
16
|
+
switch (node.type) {
|
17
|
+
case 'ArrayExpression':
|
18
|
+
case 'ArrowFunctionExpression':
|
19
|
+
case 'AwaitExpression':
|
20
|
+
case 'BinaryExpression':
|
21
|
+
case 'ClassExpression':
|
22
|
+
case 'FunctionExpression':
|
23
|
+
case 'JSXElement':
|
24
|
+
case 'JSXFragment':
|
25
|
+
case 'Literal':
|
26
|
+
case 'TemplateLiteral':
|
27
|
+
case 'UnaryExpression':
|
28
|
+
case 'UpdateExpression': {
|
29
|
+
return true;
|
30
|
+
}
|
31
|
+
|
32
|
+
case 'SequenceExpression': {
|
33
|
+
return notPromise(node.expressions.at(-1));
|
34
|
+
}
|
35
|
+
|
36
|
+
// No default
|
37
|
+
}
|
38
|
+
|
39
|
+
return false;
|
40
|
+
}
|
41
|
+
|
42
|
+
/** @param {import('eslint').Rule.RuleContext} context */
|
43
|
+
const create = context => ({
|
44
|
+
AwaitExpression(node) {
|
45
|
+
if (!notPromise(node.argument)) {
|
46
|
+
return;
|
47
|
+
}
|
48
|
+
|
49
|
+
const {sourceCode} = context;
|
50
|
+
const awaitToken = sourceCode.getFirstToken(node);
|
51
|
+
const problem = {
|
52
|
+
node,
|
53
|
+
loc: awaitToken.loc,
|
54
|
+
messageId: MESSAGE_ID,
|
55
|
+
};
|
56
|
+
|
57
|
+
const valueNode = node.argument;
|
58
|
+
if (
|
59
|
+
// Removing `await` may change them to a declaration, if there is no `id` will cause SyntaxError
|
60
|
+
valueNode.type === 'FunctionExpression'
|
61
|
+
|| valueNode.type === 'ClassExpression'
|
62
|
+
// `+await +1` -> `++1`
|
63
|
+
|| (
|
64
|
+
node.parent.type === 'UnaryExpression'
|
65
|
+
&& valueNode.type === 'UnaryExpression'
|
66
|
+
&& node.parent.operator === valueNode.operator
|
67
|
+
)
|
68
|
+
) {
|
69
|
+
return problem;
|
70
|
+
}
|
71
|
+
|
72
|
+
return Object.assign(problem, {
|
73
|
+
/** @param {import('eslint').Rule.RuleFixer} fixer */
|
74
|
+
* fix(fixer) {
|
75
|
+
if (
|
76
|
+
!isOnSameLine(awaitToken, valueNode)
|
77
|
+
&& !isParenthesized(node, sourceCode)
|
78
|
+
) {
|
79
|
+
yield * addParenthesizesToReturnOrThrowExpression(fixer, node.parent, sourceCode);
|
80
|
+
}
|
81
|
+
|
82
|
+
yield fixer.remove(awaitToken);
|
83
|
+
yield removeSpacesAfter(awaitToken, sourceCode, fixer);
|
84
|
+
|
85
|
+
const nextToken = sourceCode.getTokenAfter(awaitToken);
|
86
|
+
const tokenBefore = sourceCode.getTokenBefore(awaitToken);
|
87
|
+
if (needsSemicolon(tokenBefore, sourceCode, nextToken.value)) {
|
88
|
+
yield fixer.insertTextBefore(nextToken, ';');
|
89
|
+
}
|
90
|
+
},
|
91
|
+
});
|
92
|
+
},
|
93
|
+
});
|
94
|
+
|
95
|
+
/** @type {import('eslint').Rule.RuleModule} */
|
96
|
+
module.exports = {
|
97
|
+
create,
|
98
|
+
meta: {
|
99
|
+
type: 'suggestion',
|
100
|
+
docs: {
|
101
|
+
description: 'Disallow awaiting non-promise values.',
|
102
|
+
},
|
103
|
+
fixable: 'code',
|
104
|
+
|
105
|
+
messages,
|
106
|
+
},
|
107
|
+
};
|
@@ -0,0 +1,176 @@
|
|
1
|
+
'use strict';
|
2
|
+
const path = require('node:path');
|
3
|
+
const readPkgUp = require('read-pkg-up');
|
4
|
+
const coreJsCompat = require('core-js-compat');
|
5
|
+
const {camelCase} = require('./utils/lodash.js');
|
6
|
+
const isStaticRequire = require('./ast/is-static-require.js');
|
7
|
+
|
8
|
+
const {data: compatData, entries: coreJsEntries} = coreJsCompat;
|
9
|
+
|
10
|
+
const MESSAGE_ID_POLYFILL = 'unnecessaryPolyfill';
|
11
|
+
const MESSAGE_ID_CORE_JS = 'unnecessaryCoreJsModule';
|
12
|
+
const messages = {
|
13
|
+
[MESSAGE_ID_POLYFILL]: 'Use built-in instead.',
|
14
|
+
[MESSAGE_ID_CORE_JS]:
|
15
|
+
'All polyfilled features imported from `{{coreJsModule}}` are available as built-ins. Use the built-ins instead.',
|
16
|
+
};
|
17
|
+
|
18
|
+
const additionalPolyfillPatterns = {
|
19
|
+
'es.promise.finally': '|(p-finally)',
|
20
|
+
'es.object.set-prototype-of': '|(setprototypeof)',
|
21
|
+
'es.string.code-point-at': '|(code-point-at)',
|
22
|
+
};
|
23
|
+
|
24
|
+
const prefixes = '(mdn-polyfills/|polyfill-)';
|
25
|
+
const suffixes = '(-polyfill)';
|
26
|
+
const delimiter = '(\\.|-|\\.prototype\\.|/)?';
|
27
|
+
|
28
|
+
const polyfills = Object.keys(compatData).map(feature => {
|
29
|
+
let [ecmaVersion, constructorName, methodName = ''] = feature.split('.');
|
30
|
+
|
31
|
+
if (ecmaVersion === 'es') {
|
32
|
+
ecmaVersion = '(es\\d*)';
|
33
|
+
}
|
34
|
+
|
35
|
+
constructorName = `(${constructorName}|${camelCase(constructorName)})`;
|
36
|
+
methodName &&= `(${methodName}|${camelCase(methodName)})`;
|
37
|
+
|
38
|
+
const methodOrConstructor = methodName || constructorName;
|
39
|
+
|
40
|
+
const patterns = [
|
41
|
+
`^((${prefixes}?(`,
|
42
|
+
methodName && `(${ecmaVersion}${delimiter}${constructorName}${delimiter}${methodName})|`, // Ex: es6-array-copy-within
|
43
|
+
methodName && `(${constructorName}${delimiter}${methodName})|`, // Ex: array-copy-within
|
44
|
+
`(${ecmaVersion}${delimiter}${constructorName}))`, // Ex: es6-array
|
45
|
+
`${suffixes}?)|`,
|
46
|
+
`(${prefixes}${methodOrConstructor}|${methodOrConstructor}${suffixes})`, // Ex: polyfill-copy-within / polyfill-promise
|
47
|
+
`${additionalPolyfillPatterns[feature] || ''})$`,
|
48
|
+
];
|
49
|
+
|
50
|
+
return {
|
51
|
+
feature,
|
52
|
+
pattern: new RegExp(patterns.join(''), 'i'),
|
53
|
+
};
|
54
|
+
});
|
55
|
+
|
56
|
+
function getTargets(options, dirname) {
|
57
|
+
if (options?.targets) {
|
58
|
+
return options.targets;
|
59
|
+
}
|
60
|
+
|
61
|
+
/** @type {readPkgUp.ReadResult | undefined} */
|
62
|
+
let packageResult;
|
63
|
+
try {
|
64
|
+
// It can fail if, for example, the package.json file has comments.
|
65
|
+
packageResult = readPkgUp.sync({normalize: false, cwd: dirname});
|
66
|
+
} catch {}
|
67
|
+
|
68
|
+
if (!packageResult) {
|
69
|
+
return;
|
70
|
+
}
|
71
|
+
|
72
|
+
const {browserlist, engines} = packageResult.packageJson;
|
73
|
+
return browserlist ?? engines;
|
74
|
+
}
|
75
|
+
|
76
|
+
function create(context) {
|
77
|
+
const targets = getTargets(context.options[0], path.dirname(context.filename));
|
78
|
+
if (!targets) {
|
79
|
+
return {};
|
80
|
+
}
|
81
|
+
|
82
|
+
let unavailableFeatures;
|
83
|
+
try {
|
84
|
+
unavailableFeatures = coreJsCompat({targets}).list;
|
85
|
+
} catch {
|
86
|
+
// This can happen if the targets are invalid or use unsupported syntax like `{node:'*'}`.
|
87
|
+
return {};
|
88
|
+
}
|
89
|
+
|
90
|
+
const checkFeatures = features => !features.every(feature => unavailableFeatures.includes(feature));
|
91
|
+
|
92
|
+
return {
|
93
|
+
Literal(node) {
|
94
|
+
if (
|
95
|
+
!(
|
96
|
+
(['ImportDeclaration', 'ImportExpression'].includes(node.parent.type) && node.parent.source === node)
|
97
|
+
|| (isStaticRequire(node.parent) && node.parent.arguments[0] === node)
|
98
|
+
)
|
99
|
+
) {
|
100
|
+
return;
|
101
|
+
}
|
102
|
+
|
103
|
+
const importedModule = node.value;
|
104
|
+
if (typeof importedModule !== 'string' || ['.', '/'].includes(importedModule[0])) {
|
105
|
+
return;
|
106
|
+
}
|
107
|
+
|
108
|
+
const coreJsModuleFeatures = coreJsEntries[importedModule.replace('core-js-pure', 'core-js')];
|
109
|
+
|
110
|
+
if (coreJsModuleFeatures) {
|
111
|
+
if (coreJsModuleFeatures.length > 1) {
|
112
|
+
if (checkFeatures(coreJsModuleFeatures)) {
|
113
|
+
return {
|
114
|
+
node,
|
115
|
+
messageId: MESSAGE_ID_CORE_JS,
|
116
|
+
data: {
|
117
|
+
coreJsModule: importedModule,
|
118
|
+
},
|
119
|
+
};
|
120
|
+
}
|
121
|
+
} else if (!unavailableFeatures.includes(coreJsModuleFeatures[0])) {
|
122
|
+
return {node, messageId: MESSAGE_ID_POLYFILL};
|
123
|
+
}
|
124
|
+
|
125
|
+
return;
|
126
|
+
}
|
127
|
+
|
128
|
+
const polyfill = polyfills.find(({pattern}) => pattern.test(importedModule));
|
129
|
+
if (polyfill) {
|
130
|
+
const [, namespace, method = ''] = polyfill.feature.split('.');
|
131
|
+
const [, features] = Object.entries(coreJsEntries).find(
|
132
|
+
entry => entry[0] === `core-js/full/${namespace}${method && '/'}${method}`,
|
133
|
+
);
|
134
|
+
if (checkFeatures(features)) {
|
135
|
+
return {node, messageId: MESSAGE_ID_POLYFILL};
|
136
|
+
}
|
137
|
+
}
|
138
|
+
},
|
139
|
+
};
|
140
|
+
}
|
141
|
+
|
142
|
+
const schema = [
|
143
|
+
{
|
144
|
+
type: 'object',
|
145
|
+
additionalProperties: false,
|
146
|
+
required: ['targets'],
|
147
|
+
properties: {
|
148
|
+
targets: {
|
149
|
+
oneOf: [
|
150
|
+
{
|
151
|
+
type: 'string',
|
152
|
+
},
|
153
|
+
{
|
154
|
+
type: 'array',
|
155
|
+
},
|
156
|
+
{
|
157
|
+
type: 'object',
|
158
|
+
},
|
159
|
+
],
|
160
|
+
},
|
161
|
+
},
|
162
|
+
},
|
163
|
+
];
|
164
|
+
|
165
|
+
/** @type {import('eslint').Rule.RuleModule} */
|
166
|
+
module.exports = {
|
167
|
+
create,
|
168
|
+
meta: {
|
169
|
+
type: 'suggestion',
|
170
|
+
docs: {
|
171
|
+
description: 'Enforce the use of built-in methods instead of unnecessary polyfills.',
|
172
|
+
},
|
173
|
+
schema,
|
174
|
+
messages,
|
175
|
+
},
|
176
|
+
};
|