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,144 @@
|
|
1
|
+
'use strict';
|
2
|
+
const {hasSideEffect, isCommaToken, isSemicolonToken} = require('@eslint-community/eslint-utils');
|
3
|
+
const getCallExpressionArgumentsText = require('./utils/get-call-expression-arguments-text.js');
|
4
|
+
const isSameReference = require('./utils/is-same-reference.js');
|
5
|
+
const {isNodeMatches} = require('./utils/is-node-matches.js');
|
6
|
+
const getPreviousNode = require('./utils/get-previous-node.js');
|
7
|
+
const {isMethodCall} = require('./ast/index.js');
|
8
|
+
|
9
|
+
const ERROR = 'error';
|
10
|
+
const SUGGESTION = 'suggestion';
|
11
|
+
const messages = {
|
12
|
+
[ERROR]: 'Do not call `Array#push()` multiple times.',
|
13
|
+
[SUGGESTION]: 'Merge with previous one.',
|
14
|
+
};
|
15
|
+
|
16
|
+
const isArrayPushCall = node =>
|
17
|
+
node
|
18
|
+
&& node.parent.type === 'ExpressionStatement'
|
19
|
+
&& node.parent.expression === node
|
20
|
+
&& isMethodCall(node, {
|
21
|
+
method: 'push',
|
22
|
+
optionalCall: false,
|
23
|
+
optionalMember: false,
|
24
|
+
});
|
25
|
+
|
26
|
+
function getFirstArrayPushCall(secondCall, sourceCode) {
|
27
|
+
const firstCall = getPreviousNode(secondCall.parent, sourceCode)?.expression;
|
28
|
+
if (isArrayPushCall(firstCall)) {
|
29
|
+
return firstCall;
|
30
|
+
}
|
31
|
+
}
|
32
|
+
|
33
|
+
function create(context) {
|
34
|
+
const {ignore} = {
|
35
|
+
ignore: [],
|
36
|
+
...context.options[0],
|
37
|
+
};
|
38
|
+
const ignoredObjects = [
|
39
|
+
'stream',
|
40
|
+
'this',
|
41
|
+
'this.stream',
|
42
|
+
'process.stdin',
|
43
|
+
'process.stdout',
|
44
|
+
'process.stderr',
|
45
|
+
...ignore,
|
46
|
+
];
|
47
|
+
const {sourceCode} = context;
|
48
|
+
|
49
|
+
return {
|
50
|
+
CallExpression(secondCall) {
|
51
|
+
if (!isArrayPushCall(secondCall)) {
|
52
|
+
return;
|
53
|
+
}
|
54
|
+
|
55
|
+
const secondCallArray = secondCall.callee.object;
|
56
|
+
|
57
|
+
if (isNodeMatches(secondCallArray, ignoredObjects)) {
|
58
|
+
return;
|
59
|
+
}
|
60
|
+
|
61
|
+
const firstCall = getFirstArrayPushCall(secondCall, sourceCode);
|
62
|
+
if (!firstCall) {
|
63
|
+
return;
|
64
|
+
}
|
65
|
+
|
66
|
+
const firstCallArray = firstCall.callee.object;
|
67
|
+
|
68
|
+
// Not same array
|
69
|
+
if (!isSameReference(firstCallArray, secondCallArray)) {
|
70
|
+
return;
|
71
|
+
}
|
72
|
+
|
73
|
+
const secondCallArguments = secondCall.arguments;
|
74
|
+
const problem = {
|
75
|
+
node: secondCall.callee.property,
|
76
|
+
messageId: ERROR,
|
77
|
+
};
|
78
|
+
|
79
|
+
const fix = function * (fixer) {
|
80
|
+
if (secondCallArguments.length > 0) {
|
81
|
+
const text = getCallExpressionArgumentsText(secondCall, sourceCode);
|
82
|
+
|
83
|
+
const [penultimateToken, lastToken] = sourceCode.getLastTokens(firstCall, 2);
|
84
|
+
yield (
|
85
|
+
isCommaToken(penultimateToken)
|
86
|
+
? fixer.insertTextAfter(penultimateToken, ` ${text}`)
|
87
|
+
: fixer.insertTextBefore(lastToken, firstCall.arguments.length > 0 ? `, ${text}` : text)
|
88
|
+
);
|
89
|
+
}
|
90
|
+
|
91
|
+
const firstExpression = firstCall.parent;
|
92
|
+
const secondExpression = secondCall.parent;
|
93
|
+
const shouldKeepSemicolon = !isSemicolonToken(sourceCode.getLastToken(firstExpression))
|
94
|
+
&& isSemicolonToken(sourceCode.getLastToken(secondExpression));
|
95
|
+
|
96
|
+
yield fixer.replaceTextRange(
|
97
|
+
[firstExpression.range[1], secondExpression.range[1]],
|
98
|
+
shouldKeepSemicolon ? ';' : '',
|
99
|
+
);
|
100
|
+
};
|
101
|
+
|
102
|
+
if (secondCallArguments.some(element => hasSideEffect(element, sourceCode))) {
|
103
|
+
problem.suggest = [
|
104
|
+
{
|
105
|
+
messageId: SUGGESTION,
|
106
|
+
fix,
|
107
|
+
},
|
108
|
+
];
|
109
|
+
} else {
|
110
|
+
problem.fix = fix;
|
111
|
+
}
|
112
|
+
|
113
|
+
return problem;
|
114
|
+
},
|
115
|
+
};
|
116
|
+
}
|
117
|
+
|
118
|
+
const schema = [
|
119
|
+
{
|
120
|
+
type: 'object',
|
121
|
+
additionalProperties: false,
|
122
|
+
properties: {
|
123
|
+
ignore: {
|
124
|
+
type: 'array',
|
125
|
+
uniqueItems: true,
|
126
|
+
},
|
127
|
+
},
|
128
|
+
},
|
129
|
+
];
|
130
|
+
|
131
|
+
/** @type {import('eslint').Rule.RuleModule} */
|
132
|
+
module.exports = {
|
133
|
+
create,
|
134
|
+
meta: {
|
135
|
+
type: 'suggestion',
|
136
|
+
docs: {
|
137
|
+
description: 'Enforce combining multiple `Array#push()` into one call.',
|
138
|
+
},
|
139
|
+
fixable: 'code',
|
140
|
+
hasSuggestions: true,
|
141
|
+
schema,
|
142
|
+
messages,
|
143
|
+
},
|
144
|
+
};
|
@@ -0,0 +1,126 @@
|
|
1
|
+
'use strict';
|
2
|
+
const {isMethodCall} = require('./ast/index.js');
|
3
|
+
const {isNodeValueNotFunction, isArrayPrototypeProperty} = require('./utils/index.js');
|
4
|
+
|
5
|
+
const MESSAGE_ID = 'no-reduce';
|
6
|
+
const messages = {
|
7
|
+
[MESSAGE_ID]: '`Array#{{method}}()` is not allowed',
|
8
|
+
};
|
9
|
+
|
10
|
+
const cases = [
|
11
|
+
// `array.{reduce,reduceRight}()`
|
12
|
+
{
|
13
|
+
test: callExpression =>
|
14
|
+
isMethodCall(callExpression, {
|
15
|
+
methods: ['reduce', 'reduceRight'],
|
16
|
+
minimumArguments: 1,
|
17
|
+
maximumArguments: 2,
|
18
|
+
optionalCall: false,
|
19
|
+
optionalMember: false,
|
20
|
+
})
|
21
|
+
&& !isNodeValueNotFunction(callExpression.arguments[0]),
|
22
|
+
getMethodNode: callExpression => callExpression.callee.property,
|
23
|
+
isSimpleOperation(callExpression) {
|
24
|
+
const [callback] = callExpression.arguments;
|
25
|
+
|
26
|
+
return (
|
27
|
+
callback
|
28
|
+
&& (
|
29
|
+
// `array.reduce((accumulator, element) => accumulator + element)`
|
30
|
+
(callback.type === 'ArrowFunctionExpression' && callback.body.type === 'BinaryExpression')
|
31
|
+
// `array.reduce((accumulator, element) => {return accumulator + element;})`
|
32
|
+
// `array.reduce(function (accumulator, element){return accumulator + element;})`
|
33
|
+
|| (
|
34
|
+
(callback.type === 'ArrowFunctionExpression' || callback.type === 'FunctionExpression')
|
35
|
+
&& callback.body.type === 'BlockStatement'
|
36
|
+
&& callback.body.body.length === 1
|
37
|
+
&& callback.body.body[0].type === 'ReturnStatement'
|
38
|
+
&& callback.body.body[0].argument.type === 'BinaryExpression'
|
39
|
+
)
|
40
|
+
)
|
41
|
+
);
|
42
|
+
},
|
43
|
+
},
|
44
|
+
// `[].{reduce,reduceRight}.call()` and `Array.{reduce,reduceRight}.call()`
|
45
|
+
{
|
46
|
+
test: callExpression =>
|
47
|
+
isMethodCall(callExpression, {
|
48
|
+
method: 'call',
|
49
|
+
optionalCall: false,
|
50
|
+
optionalMember: false,
|
51
|
+
})
|
52
|
+
&& isArrayPrototypeProperty(callExpression.callee.object, {
|
53
|
+
properties: ['reduce', 'reduceRight'],
|
54
|
+
})
|
55
|
+
&& (
|
56
|
+
!callExpression.arguments[1]
|
57
|
+
|| !isNodeValueNotFunction(callExpression.arguments[1])
|
58
|
+
),
|
59
|
+
getMethodNode: callExpression => callExpression.callee.object.property,
|
60
|
+
},
|
61
|
+
// `[].{reduce,reduceRight}.apply()` and `Array.{reduce,reduceRight}.apply()`
|
62
|
+
{
|
63
|
+
test: callExpression =>
|
64
|
+
isMethodCall(callExpression, {
|
65
|
+
method: 'apply',
|
66
|
+
optionalCall: false,
|
67
|
+
optionalMember: false,
|
68
|
+
})
|
69
|
+
&& isArrayPrototypeProperty(callExpression.callee.object, {
|
70
|
+
properties: ['reduce', 'reduceRight'],
|
71
|
+
}),
|
72
|
+
getMethodNode: callExpression => callExpression.callee.object.property,
|
73
|
+
},
|
74
|
+
];
|
75
|
+
|
76
|
+
const schema = [
|
77
|
+
{
|
78
|
+
type: 'object',
|
79
|
+
additionalProperties: false,
|
80
|
+
properties: {
|
81
|
+
allowSimpleOperations: {
|
82
|
+
type: 'boolean',
|
83
|
+
default: true,
|
84
|
+
},
|
85
|
+
},
|
86
|
+
},
|
87
|
+
];
|
88
|
+
|
89
|
+
/** @param {import('eslint').Rule.RuleContext} context */
|
90
|
+
const create = context => {
|
91
|
+
const {allowSimpleOperations} = {allowSimpleOperations: true, ...context.options[0]};
|
92
|
+
|
93
|
+
return {
|
94
|
+
* CallExpression(callExpression) {
|
95
|
+
for (const {test, getMethodNode, isSimpleOperation} of cases) {
|
96
|
+
if (!test(callExpression)) {
|
97
|
+
continue;
|
98
|
+
}
|
99
|
+
|
100
|
+
if (allowSimpleOperations && isSimpleOperation?.(callExpression)) {
|
101
|
+
continue;
|
102
|
+
}
|
103
|
+
|
104
|
+
const methodNode = getMethodNode(callExpression);
|
105
|
+
yield {
|
106
|
+
node: methodNode,
|
107
|
+
messageId: MESSAGE_ID,
|
108
|
+
data: {method: methodNode.name},
|
109
|
+
};
|
110
|
+
}
|
111
|
+
},
|
112
|
+
};
|
113
|
+
};
|
114
|
+
|
115
|
+
/** @type {import('eslint').Rule.RuleModule} */
|
116
|
+
module.exports = {
|
117
|
+
create,
|
118
|
+
meta: {
|
119
|
+
type: 'suggestion',
|
120
|
+
docs: {
|
121
|
+
description: 'Disallow `Array#reduce()` and `Array#reduceRight()`.',
|
122
|
+
},
|
123
|
+
schema,
|
124
|
+
messages,
|
125
|
+
},
|
126
|
+
};
|
@@ -0,0 +1,90 @@
|
|
1
|
+
'use strict';
|
2
|
+
const {
|
3
|
+
removeParentheses,
|
4
|
+
removeMemberExpressionProperty,
|
5
|
+
} = require('./fix/index.js');
|
6
|
+
const {isLiteral} = require('./ast/index.js');
|
7
|
+
|
8
|
+
const MESSAGE_ID = 'no-await-expression-member';
|
9
|
+
const messages = {
|
10
|
+
[MESSAGE_ID]: 'Do not access a member directly from an await expression.',
|
11
|
+
};
|
12
|
+
|
13
|
+
/** @param {import('eslint').Rule.RuleContext} context */
|
14
|
+
const create = context => {
|
15
|
+
const {sourceCode} = context;
|
16
|
+
|
17
|
+
return {
|
18
|
+
MemberExpression(memberExpression) {
|
19
|
+
if (memberExpression.object.type !== 'AwaitExpression') {
|
20
|
+
return;
|
21
|
+
}
|
22
|
+
|
23
|
+
const {property} = memberExpression;
|
24
|
+
const problem = {
|
25
|
+
node: property,
|
26
|
+
messageId: MESSAGE_ID,
|
27
|
+
};
|
28
|
+
|
29
|
+
// `const foo = (await bar)[0]`
|
30
|
+
if (
|
31
|
+
memberExpression.computed
|
32
|
+
&& !memberExpression.optional
|
33
|
+
&& (isLiteral(property, 0) || isLiteral(property, 1))
|
34
|
+
&& memberExpression.parent.type === 'VariableDeclarator'
|
35
|
+
&& memberExpression.parent.init === memberExpression
|
36
|
+
&& memberExpression.parent.id.type === 'Identifier'
|
37
|
+
&& !memberExpression.parent.id.typeAnnotation
|
38
|
+
) {
|
39
|
+
problem.fix = function * (fixer) {
|
40
|
+
const variable = memberExpression.parent.id;
|
41
|
+
yield fixer.insertTextBefore(variable, property.value === 0 ? '[' : '[, ');
|
42
|
+
yield fixer.insertTextAfter(variable, ']');
|
43
|
+
|
44
|
+
yield removeMemberExpressionProperty(fixer, memberExpression, sourceCode);
|
45
|
+
yield * removeParentheses(memberExpression.object, fixer, sourceCode);
|
46
|
+
};
|
47
|
+
|
48
|
+
return problem;
|
49
|
+
}
|
50
|
+
|
51
|
+
// `const foo = (await bar).foo`
|
52
|
+
if (
|
53
|
+
!memberExpression.computed
|
54
|
+
&& !memberExpression.optional
|
55
|
+
&& property.type === 'Identifier'
|
56
|
+
&& memberExpression.parent.type === 'VariableDeclarator'
|
57
|
+
&& memberExpression.parent.init === memberExpression
|
58
|
+
&& memberExpression.parent.id.type === 'Identifier'
|
59
|
+
&& memberExpression.parent.id.name === property.name
|
60
|
+
&& !memberExpression.parent.id.typeAnnotation
|
61
|
+
) {
|
62
|
+
problem.fix = function * (fixer) {
|
63
|
+
const variable = memberExpression.parent.id;
|
64
|
+
yield fixer.insertTextBefore(variable, '{');
|
65
|
+
yield fixer.insertTextAfter(variable, '}');
|
66
|
+
|
67
|
+
yield removeMemberExpressionProperty(fixer, memberExpression, sourceCode);
|
68
|
+
yield * removeParentheses(memberExpression.object, fixer, sourceCode);
|
69
|
+
};
|
70
|
+
|
71
|
+
return problem;
|
72
|
+
}
|
73
|
+
|
74
|
+
return problem;
|
75
|
+
},
|
76
|
+
};
|
77
|
+
};
|
78
|
+
|
79
|
+
/** @type {import('eslint').Rule.RuleModule} */
|
80
|
+
module.exports = {
|
81
|
+
create,
|
82
|
+
meta: {
|
83
|
+
type: 'suggestion',
|
84
|
+
docs: {
|
85
|
+
description: 'Disallow member access from await expression.',
|
86
|
+
},
|
87
|
+
fixable: 'code',
|
88
|
+
messages,
|
89
|
+
},
|
90
|
+
};
|
@@ -0,0 +1,86 @@
|
|
1
|
+
'use strict';
|
2
|
+
const toLocation = require('./utils/to-location.js');
|
3
|
+
const {isStringLiteral, isMethodCall} = require('./ast/index.js');
|
4
|
+
|
5
|
+
const MESSAGE_ID = 'no-console-spaces';
|
6
|
+
const messages = {
|
7
|
+
[MESSAGE_ID]: 'Do not use {{position}} space between `console.{{method}}` parameters.',
|
8
|
+
};
|
9
|
+
|
10
|
+
// Find exactly one leading space, allow exactly one space
|
11
|
+
const hasLeadingSpace = value => value.length > 1 && value.charAt(0) === ' ' && value.charAt(1) !== ' ';
|
12
|
+
|
13
|
+
// Find exactly one trailing space, allow exactly one space
|
14
|
+
const hasTrailingSpace = value => value.length > 1 && value.at(-1) === ' ' && value.at(-2) !== ' ';
|
15
|
+
|
16
|
+
/** @param {import('eslint').Rule.RuleContext} context */
|
17
|
+
const create = context => {
|
18
|
+
const {sourceCode} = context;
|
19
|
+
const getProblem = (node, method, position) => {
|
20
|
+
const index = position === 'leading'
|
21
|
+
? node.range[0] + 1
|
22
|
+
: node.range[1] - 2;
|
23
|
+
const range = [index, index + 1];
|
24
|
+
|
25
|
+
return {
|
26
|
+
loc: toLocation(range, sourceCode),
|
27
|
+
messageId: MESSAGE_ID,
|
28
|
+
data: {method, position},
|
29
|
+
fix: fixer => fixer.removeRange(range),
|
30
|
+
};
|
31
|
+
};
|
32
|
+
|
33
|
+
return {
|
34
|
+
* CallExpression(node) {
|
35
|
+
if (
|
36
|
+
!isMethodCall(node, {
|
37
|
+
object: 'console',
|
38
|
+
methods: [
|
39
|
+
'log',
|
40
|
+
'debug',
|
41
|
+
'info',
|
42
|
+
'warn',
|
43
|
+
'error',
|
44
|
+
],
|
45
|
+
minimumArguments: 1,
|
46
|
+
optionalCall: false,
|
47
|
+
optionalMember: false,
|
48
|
+
})
|
49
|
+
) {
|
50
|
+
return;
|
51
|
+
}
|
52
|
+
|
53
|
+
const method = node.callee.property.name;
|
54
|
+
const {arguments: messages} = node;
|
55
|
+
const {length} = messages;
|
56
|
+
for (const [index, node] of messages.entries()) {
|
57
|
+
if (!isStringLiteral(node) && node.type !== 'TemplateLiteral') {
|
58
|
+
continue;
|
59
|
+
}
|
60
|
+
|
61
|
+
const raw = sourceCode.getText(node).slice(1, -1);
|
62
|
+
|
63
|
+
if (index !== 0 && hasLeadingSpace(raw)) {
|
64
|
+
yield getProblem(node, method, 'leading');
|
65
|
+
}
|
66
|
+
|
67
|
+
if (index !== length - 1 && hasTrailingSpace(raw)) {
|
68
|
+
yield getProblem(node, method, 'trailing');
|
69
|
+
}
|
70
|
+
}
|
71
|
+
},
|
72
|
+
};
|
73
|
+
};
|
74
|
+
|
75
|
+
/** @type {import('eslint').Rule.RuleModule} */
|
76
|
+
module.exports = {
|
77
|
+
create,
|
78
|
+
meta: {
|
79
|
+
type: 'suggestion',
|
80
|
+
docs: {
|
81
|
+
description: 'Do not use leading/trailing space between `console.log` parameters.',
|
82
|
+
},
|
83
|
+
fixable: 'code',
|
84
|
+
messages,
|
85
|
+
},
|
86
|
+
};
|
@@ -0,0 +1,25 @@
|
|
1
|
+
'use strict';
|
2
|
+
const {GlobalReferenceTracker} = require('./utils/global-reference-tracker.js');
|
3
|
+
|
4
|
+
const MESSAGE_ID = 'no-document-cookie';
|
5
|
+
const messages = {
|
6
|
+
[MESSAGE_ID]: 'Do not use `document.cookie` directly.',
|
7
|
+
};
|
8
|
+
|
9
|
+
const tracker = new GlobalReferenceTracker({
|
10
|
+
object: 'document.cookie',
|
11
|
+
filter: ({node}) => node.parent.type === 'AssignmentExpression' && node.parent.left === node,
|
12
|
+
handle: ({node}) => ({node, messageId: MESSAGE_ID}),
|
13
|
+
});
|
14
|
+
|
15
|
+
/** @type {import('eslint').Rule.RuleModule} */
|
16
|
+
module.exports = {
|
17
|
+
create: context => tracker.createListeners(context),
|
18
|
+
meta: {
|
19
|
+
type: 'problem',
|
20
|
+
docs: {
|
21
|
+
description: 'Do not use `document.cookie` directly.',
|
22
|
+
},
|
23
|
+
messages,
|
24
|
+
},
|
25
|
+
};
|
@@ -0,0 +1,57 @@
|
|
1
|
+
'use strict';
|
2
|
+
const {isEmptyNode} = require('./ast/index.js');
|
3
|
+
|
4
|
+
const MESSAGE_ID = 'no-empty-file';
|
5
|
+
const messages = {
|
6
|
+
[MESSAGE_ID]: 'Empty files are not allowed.',
|
7
|
+
};
|
8
|
+
|
9
|
+
const isDirective = node => node.type === 'ExpressionStatement' && typeof node.directive === 'string';
|
10
|
+
const isEmpty = node => isEmptyNode(node, isDirective);
|
11
|
+
|
12
|
+
const isTripleSlashDirective = node =>
|
13
|
+
node.type === 'Line' && node.value.startsWith('/');
|
14
|
+
|
15
|
+
const hasTripeSlashDirectives = comments =>
|
16
|
+
comments.some(currentNode => isTripleSlashDirective(currentNode));
|
17
|
+
|
18
|
+
/** @param {import('eslint').Rule.RuleContext} context */
|
19
|
+
const create = context => {
|
20
|
+
const filename = context.physicalFilename;
|
21
|
+
|
22
|
+
if (!/\.(?:js|mjs|cjs|jsx|ts|mts|cts|tsx)$/i.test(filename)) {
|
23
|
+
return;
|
24
|
+
}
|
25
|
+
|
26
|
+
return {
|
27
|
+
Program(node) {
|
28
|
+
if (node.body.some(node => !isEmpty(node))) {
|
29
|
+
return;
|
30
|
+
}
|
31
|
+
|
32
|
+
const {sourceCode} = context;
|
33
|
+
const comments = sourceCode.getAllComments();
|
34
|
+
|
35
|
+
if (hasTripeSlashDirectives(comments)) {
|
36
|
+
return;
|
37
|
+
}
|
38
|
+
|
39
|
+
return {
|
40
|
+
node,
|
41
|
+
messageId: MESSAGE_ID,
|
42
|
+
};
|
43
|
+
},
|
44
|
+
};
|
45
|
+
};
|
46
|
+
|
47
|
+
/** @type {import('eslint').Rule.RuleModule} */
|
48
|
+
module.exports = {
|
49
|
+
create,
|
50
|
+
meta: {
|
51
|
+
type: 'suggestion',
|
52
|
+
docs: {
|
53
|
+
description: 'Disallow empty files.',
|
54
|
+
},
|
55
|
+
messages,
|
56
|
+
},
|
57
|
+
};
|