@typescript-eslint/eslint-plugin 8.0.0-alpha.6 → 8.0.0-alpha.61
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/configs/all.js +7 -6
- package/dist/configs/all.js.map +1 -1
- package/dist/configs/disable-type-checked.js +3 -3
- package/dist/configs/disable-type-checked.js.map +1 -1
- package/dist/configs/recommended-type-checked-only.js +6 -0
- package/dist/configs/recommended-type-checked-only.js.map +1 -1
- package/dist/configs/recommended-type-checked.js +13 -2
- package/dist/configs/recommended-type-checked.js.map +1 -1
- package/dist/configs/recommended.js +7 -2
- package/dist/configs/recommended.js.map +1 -1
- package/dist/configs/strict-type-checked-only.js +8 -2
- package/dist/configs/strict-type-checked-only.js.map +1 -1
- package/dist/configs/strict-type-checked.js +15 -5
- package/dist/configs/strict-type-checked.js.map +1 -1
- package/dist/configs/strict.js +7 -3
- package/dist/configs/strict.js.map +1 -1
- package/dist/configs/stylistic-type-checked-only.js +3 -0
- package/dist/configs/stylistic-type-checked-only.js.map +1 -1
- package/dist/configs/stylistic-type-checked.js +3 -2
- package/dist/configs/stylistic-type-checked.js.map +1 -1
- package/dist/configs/stylistic.js +0 -2
- package/dist/configs/stylistic.js.map +1 -1
- package/dist/rules/adjacent-overload-signatures.js +2 -6
- package/dist/rules/adjacent-overload-signatures.js.map +1 -1
- package/dist/rules/array-type.js +14 -5
- package/dist/rules/array-type.js.map +1 -1
- package/dist/rules/class-literal-property-style.js +16 -16
- package/dist/rules/class-literal-property-style.js.map +1 -1
- package/dist/rules/class-methods-use-this.js.map +1 -1
- package/dist/rules/consistent-type-assertions.js +5 -19
- package/dist/rules/consistent-type-assertions.js.map +1 -1
- package/dist/rules/consistent-type-definitions.js +1 -1
- package/dist/rules/consistent-type-definitions.js.map +1 -1
- package/dist/rules/consistent-type-imports.js +13 -13
- package/dist/rules/consistent-type-imports.js.map +1 -1
- package/dist/rules/default-param-last.js +6 -1
- package/dist/rules/default-param-last.js.map +1 -1
- package/dist/rules/dot-notation.js +8 -1
- package/dist/rules/dot-notation.js.map +1 -1
- package/dist/rules/enum-utils/shared.js +3 -4
- package/dist/rules/enum-utils/shared.js.map +1 -1
- package/dist/rules/explicit-member-accessibility.js +49 -37
- package/dist/rules/explicit-member-accessibility.js.map +1 -1
- package/dist/rules/index.js +14 -6
- package/dist/rules/index.js.map +1 -1
- package/dist/rules/init-declarations.js +57 -1
- package/dist/rules/init-declarations.js.map +1 -1
- package/dist/rules/member-ordering.js +8 -2
- package/dist/rules/member-ordering.js.map +1 -1
- package/dist/rules/method-signature-style.js +1 -3
- package/dist/rules/method-signature-style.js.map +1 -1
- package/dist/rules/naming-convention-utils/parse-options.js +2 -5
- package/dist/rules/naming-convention-utils/parse-options.js.map +1 -1
- package/dist/rules/naming-convention-utils/shared.js +3 -4
- package/dist/rules/naming-convention-utils/shared.js.map +1 -1
- package/dist/rules/naming-convention-utils/validator.js +1 -2
- package/dist/rules/naming-convention-utils/validator.js.map +1 -1
- package/dist/rules/naming-convention.js +3 -1
- package/dist/rules/naming-convention.js.map +1 -1
- package/dist/rules/no-array-delete.js +1 -1
- package/dist/rules/no-array-delete.js.map +1 -1
- package/dist/rules/no-base-to-string.js +1 -1
- package/dist/rules/no-base-to-string.js.map +1 -1
- package/dist/rules/no-duplicate-type-constituents.js +29 -3
- package/dist/rules/no-duplicate-type-constituents.js.map +1 -1
- package/dist/rules/no-dynamic-delete.js +8 -40
- package/dist/rules/no-dynamic-delete.js.map +1 -1
- package/dist/rules/no-empty-interface.js +2 -1
- package/dist/rules/no-empty-interface.js.map +1 -1
- package/dist/rules/no-empty-object-type.js +141 -0
- package/dist/rules/no-empty-object-type.js.map +1 -0
- package/dist/rules/no-extraneous-class.js +11 -3
- package/dist/rules/no-extraneous-class.js.map +1 -1
- package/dist/rules/no-floating-promises.js +91 -55
- package/dist/rules/no-floating-promises.js.map +1 -1
- package/dist/rules/no-implied-eval.js +6 -24
- package/dist/rules/no-implied-eval.js.map +1 -1
- package/dist/rules/no-inferrable-types.js +1 -2
- package/dist/rules/no-inferrable-types.js.map +1 -1
- package/dist/rules/no-invalid-this.js +6 -0
- package/dist/rules/no-invalid-this.js.map +1 -1
- package/dist/rules/no-magic-numbers.js +32 -1
- package/dist/rules/no-magic-numbers.js.map +1 -1
- package/dist/rules/no-misused-new.js +4 -3
- package/dist/rules/no-misused-new.js.map +1 -1
- package/dist/rules/no-misused-promises.js +76 -1
- package/dist/rules/no-misused-promises.js.map +1 -1
- package/dist/rules/no-mixed-enums.js +6 -9
- package/dist/rules/no-mixed-enums.js.map +1 -1
- package/dist/rules/no-non-null-assertion.js +12 -14
- package/dist/rules/no-non-null-assertion.js.map +1 -1
- package/dist/rules/no-require-imports.js +13 -3
- package/dist/rules/no-require-imports.js.map +1 -1
- package/dist/rules/no-restricted-imports.js +5 -6
- package/dist/rules/no-restricted-imports.js.map +1 -1
- package/dist/rules/{ban-types.js → no-restricted-types.js} +17 -91
- package/dist/rules/no-restricted-types.js.map +1 -0
- package/dist/rules/no-shadow.js +4 -9
- package/dist/rules/no-shadow.js.map +1 -1
- package/dist/rules/no-unnecessary-condition.js +8 -6
- package/dist/rules/no-unnecessary-condition.js.map +1 -1
- package/dist/rules/no-unnecessary-parameter-property-assignment.js +151 -0
- package/dist/rules/no-unnecessary-parameter-property-assignment.js.map +1 -0
- package/dist/rules/no-unnecessary-qualifier.js.map +1 -1
- package/dist/rules/no-unnecessary-template-expression.js +223 -0
- package/dist/rules/no-unnecessary-template-expression.js.map +1 -0
- package/dist/rules/no-unnecessary-type-assertion.js +16 -13
- package/dist/rules/no-unnecessary-type-assertion.js.map +1 -1
- package/dist/rules/no-unnecessary-type-parameters.js +309 -0
- package/dist/rules/no-unnecessary-type-parameters.js.map +1 -0
- package/dist/rules/no-unsafe-argument.js +4 -2
- package/dist/rules/no-unsafe-argument.js.map +1 -1
- package/dist/rules/no-unsafe-assignment.js +24 -9
- package/dist/rules/no-unsafe-assignment.js.map +1 -1
- package/dist/rules/no-unsafe-call.js +5 -1
- package/dist/rules/no-unsafe-call.js.map +1 -1
- package/dist/rules/no-unsafe-enum-comparison.js +1 -5
- package/dist/rules/no-unsafe-enum-comparison.js.map +1 -1
- package/dist/rules/no-unsafe-function-type.js +47 -0
- package/dist/rules/no-unsafe-function-type.js.map +1 -0
- package/dist/rules/no-unsafe-member-access.js +10 -4
- package/dist/rules/no-unsafe-member-access.js.map +1 -1
- package/dist/rules/no-unsafe-return.js +9 -3
- package/dist/rules/no-unsafe-return.js.map +1 -1
- package/dist/rules/no-unsafe-unary-minus.js +1 -0
- package/dist/rules/no-unsafe-unary-minus.js.map +1 -1
- package/dist/rules/no-unused-expressions.js +1 -0
- package/dist/rules/no-unused-expressions.js.map +1 -1
- package/dist/rules/no-unused-vars.js +198 -79
- package/dist/rules/no-unused-vars.js.map +1 -1
- package/dist/rules/no-use-before-define.js +2 -16
- package/dist/rules/no-use-before-define.js.map +1 -1
- package/dist/rules/no-useless-constructor.js +1 -3
- package/dist/rules/no-useless-constructor.js.map +1 -1
- package/dist/rules/no-var-requires.js +2 -1
- package/dist/rules/no-var-requires.js.map +1 -1
- package/dist/rules/no-wrapper-object-types.js +61 -0
- package/dist/rules/no-wrapper-object-types.js.map +1 -0
- package/dist/rules/only-throw-error.js +1 -1
- package/dist/rules/only-throw-error.js.map +1 -1
- package/dist/rules/prefer-as-const.js +1 -1
- package/dist/rules/prefer-as-const.js.map +1 -1
- package/dist/rules/prefer-destructuring.js +3 -1
- package/dist/rules/prefer-destructuring.js.map +1 -1
- package/dist/rules/prefer-find.js +2 -1
- package/dist/rules/prefer-find.js.map +1 -1
- package/dist/rules/prefer-includes.js +1 -1
- package/dist/rules/prefer-includes.js.map +1 -1
- package/dist/rules/prefer-literal-enum-member.js +43 -2
- package/dist/rules/prefer-literal-enum-member.js.map +1 -1
- package/dist/rules/prefer-namespace-keyword.js +1 -1
- package/dist/rules/prefer-namespace-keyword.js.map +1 -1
- package/dist/rules/prefer-nullish-coalescing.js +8 -6
- package/dist/rules/prefer-nullish-coalescing.js.map +1 -1
- package/dist/rules/prefer-optional-chain-utils/analyzeChain.js +32 -37
- package/dist/rules/prefer-optional-chain-utils/analyzeChain.js.map +1 -1
- package/dist/rules/prefer-optional-chain-utils/checkNullishAndReport.js +1 -2
- package/dist/rules/prefer-optional-chain-utils/checkNullishAndReport.js.map +1 -1
- package/dist/rules/prefer-optional-chain-utils/compareNodes.js +1 -2
- package/dist/rules/prefer-optional-chain-utils/compareNodes.js.map +1 -1
- package/dist/rules/prefer-optional-chain-utils/gatherLogicalOperands.js +6 -11
- package/dist/rules/prefer-optional-chain-utils/gatherLogicalOperands.js.map +1 -1
- package/dist/rules/prefer-promise-reject-errors.js +1 -1
- package/dist/rules/prefer-promise-reject-errors.js.map +1 -1
- package/dist/rules/prefer-readonly.js +24 -12
- package/dist/rules/prefer-readonly.js.map +1 -1
- package/dist/rules/prefer-regexp-exec.js +15 -6
- package/dist/rules/prefer-regexp-exec.js.map +1 -1
- package/dist/rules/prefer-string-starts-ends-with.js.map +1 -1
- package/dist/rules/prefer-ts-expect-error.js +2 -1
- package/dist/rules/prefer-ts-expect-error.js.map +1 -1
- package/dist/rules/require-await.js +1 -1
- package/dist/rules/require-await.js.map +1 -1
- package/dist/rules/restrict-template-expressions.js +3 -3
- package/dist/rules/restrict-template-expressions.js.map +1 -1
- package/dist/rules/return-await.js +167 -86
- package/dist/rules/return-await.js.map +1 -1
- package/dist/rules/sort-type-constituents.js +23 -1
- package/dist/rules/sort-type-constituents.js.map +1 -1
- package/dist/rules/strict-boolean-expressions.js +112 -5
- package/dist/rules/strict-boolean-expressions.js.map +1 -1
- package/dist/rules/switch-exhaustiveness-check.js +7 -9
- package/dist/rules/switch-exhaustiveness-check.js.map +1 -1
- package/dist/rules/unbound-method.js +96 -27
- package/dist/rules/unbound-method.js.map +1 -1
- package/dist/rules/unified-signatures.js +3 -1
- package/dist/rules/unified-signatures.js.map +1 -1
- package/dist/util/astUtils.js +2 -3
- package/dist/util/astUtils.js.map +1 -1
- package/dist/util/collectUnusedVariables.js +110 -73
- package/dist/util/collectUnusedVariables.js.map +1 -1
- package/dist/util/createRule.js.map +1 -1
- package/dist/util/escapeRegExp.js +1 -2
- package/dist/util/escapeRegExp.js.map +1 -1
- package/dist/util/explicitReturnTypeUtils.js +11 -7
- package/dist/util/explicitReturnTypeUtils.js.map +1 -1
- package/dist/util/getESLintCoreRule.js +2 -2
- package/dist/util/getESLintCoreRule.js.map +1 -1
- package/dist/util/getForStatementHeadLoc.js +1 -2
- package/dist/util/getForStatementHeadLoc.js.map +1 -1
- package/dist/util/getFunctionHeadLoc.js +3 -4
- package/dist/util/getFunctionHeadLoc.js.map +1 -1
- package/dist/util/getMemberHeadLoc.js +80 -0
- package/dist/util/getMemberHeadLoc.js.map +1 -0
- package/dist/util/getOperatorPrecedence.js +5 -5
- package/dist/util/getOperatorPrecedence.js.map +1 -1
- package/dist/util/getStaticStringValue.js +1 -2
- package/dist/util/getStaticStringValue.js.map +1 -1
- package/dist/util/getStringLength.js +1 -2
- package/dist/util/getStringLength.js.map +1 -1
- package/dist/util/getTextWithParentheses.js +17 -0
- package/dist/util/getTextWithParentheses.js.map +1 -0
- package/dist/util/getThisExpression.js +1 -2
- package/dist/util/getThisExpression.js.map +1 -1
- package/dist/util/getWrappedCode.js +1 -2
- package/dist/util/getWrappedCode.js.map +1 -1
- package/dist/util/getWrappingFixer.js +2 -3
- package/dist/util/getWrappingFixer.js.map +1 -1
- package/dist/util/index.js +2 -0
- package/dist/util/index.js.map +1 -1
- package/dist/util/isAssignee.js +1 -2
- package/dist/util/isAssignee.js.map +1 -1
- package/dist/util/isNodeEqual.js +1 -2
- package/dist/util/isNodeEqual.js.map +1 -1
- package/dist/util/isNullLiteral.js +1 -2
- package/dist/util/isNullLiteral.js.map +1 -1
- package/dist/util/isTypeImport.js +22 -0
- package/dist/util/isTypeImport.js.map +1 -0
- package/dist/util/isUndefinedIdentifier.js +1 -2
- package/dist/util/isUndefinedIdentifier.js.map +1 -1
- package/dist/util/misc.js +15 -15
- package/dist/util/misc.js.map +1 -1
- package/dist/util/objectIterators.js +3 -4
- package/dist/util/objectIterators.js.map +1 -1
- package/dist/util/referenceContainsTypeQuery.js +20 -0
- package/dist/util/referenceContainsTypeQuery.js.map +1 -0
- package/dist/util/scopeUtils.js +11 -0
- package/dist/util/scopeUtils.js.map +1 -0
- package/docs/rules/array-type.mdx +4 -1
- package/docs/rules/ban-types.md +22 -0
- package/docs/rules/class-methods-use-this.mdx +1 -1
- package/docs/rules/consistent-indexed-object-style.mdx +1 -1
- package/docs/rules/consistent-type-imports.mdx +1 -1
- package/docs/rules/naming-convention.mdx +16 -0
- package/docs/rules/no-base-to-string.mdx +1 -1
- package/docs/rules/no-duplicate-imports.mdx +1 -1
- package/docs/rules/no-duplicate-type-constituents.mdx +6 -2
- package/docs/rules/no-dynamic-delete.mdx +12 -7
- package/docs/rules/no-empty-interface.mdx +10 -0
- package/docs/rules/no-empty-object-type.mdx +145 -0
- package/docs/rules/no-extraneous-class.mdx +6 -0
- package/docs/rules/no-floating-promises.mdx +148 -4
- package/docs/rules/no-import-type-side-effects.mdx +1 -1
- package/docs/rules/no-misused-promises.mdx +18 -20
- package/docs/rules/no-redundant-type-constituents.mdx +4 -0
- package/docs/rules/no-require-imports.mdx +28 -1
- package/docs/rules/no-restricted-types.mdx +71 -0
- package/docs/rules/no-type-alias.mdx +1 -1
- package/docs/rules/no-unnecessary-boolean-literal-compare.mdx +12 -12
- package/docs/rules/no-unnecessary-parameter-property-assignment.mdx +42 -0
- package/docs/rules/no-unnecessary-template-expression.mdx +85 -0
- package/docs/rules/no-unnecessary-type-parameters.mdx +109 -0
- package/docs/rules/no-unsafe-function-type.mdx +63 -0
- package/docs/rules/no-unused-expressions.mdx +41 -1
- package/docs/rules/no-unused-vars.mdx +5 -0
- package/docs/rules/no-useless-template-literals.mdx +3 -59
- package/docs/rules/no-var-requires.mdx +6 -0
- package/docs/rules/no-wrapper-object-types.mdx +75 -0
- package/docs/rules/only-throw-error.mdx +7 -0
- package/docs/rules/prefer-readonly-parameter-types.mdx +10 -4
- package/docs/rules/prefer-ts-expect-error.mdx +10 -0
- package/docs/rules/require-await.mdx +2 -2
- package/docs/rules/restrict-template-expressions.mdx +1 -1
- package/docs/rules/return-await.mdx +119 -23
- package/docs/rules/sort-type-constituents.mdx +29 -9
- package/docs/rules/sort-type-union-intersection-members.mdx +12 -0
- package/docs/rules/strict-boolean-expressions.mdx +6 -0
- package/docs/rules/unbound-method.mdx +3 -2
- package/package.json +14 -20
- package/rules.d.ts +34 -3
- package/dist/rules/ban-types.js.map +0 -1
- package/dist/rules/no-throw-literal.js +0 -97
- package/dist/rules/no-throw-literal.js.map +0 -1
- package/dist/rules/no-useless-template-literals.js +0 -153
- package/dist/rules/no-useless-template-literals.js.map +0 -1
- package/docs/rules/ban-types.mdx +0 -138
- package/docs/rules/no-throw-literal.mdx +0 -25
@@ -17,13 +17,14 @@ Valid ways of handling a Promise-valued statement include:
|
|
17
17
|
|
18
18
|
- `await`ing it
|
19
19
|
- `return`ing it
|
20
|
+
- `void`ing it
|
20
21
|
- Calling its `.then()` with two arguments
|
21
22
|
- Calling its `.catch()` with one argument
|
22
23
|
|
23
24
|
This rule also reports when an Array containing Promises is created and not properly handled. The main way to resolve this is by using one of the Promise concurrency methods to create a single Promise, then handling that according to the procedure above. These methods include:
|
24
25
|
|
25
|
-
- `Promise.all()
|
26
|
-
- `Promise.allSettled()
|
26
|
+
- `Promise.all()`
|
27
|
+
- `Promise.allSettled()`
|
27
28
|
- `Promise.any()`
|
28
29
|
- `Promise.race()`
|
29
30
|
|
@@ -63,6 +64,9 @@ await promise;
|
|
63
64
|
async function returnsPromise() {
|
64
65
|
return 'value';
|
65
66
|
}
|
67
|
+
|
68
|
+
void returnsPromise();
|
69
|
+
|
66
70
|
returnsPromise().then(
|
67
71
|
() => {},
|
68
72
|
() => {},
|
@@ -80,9 +84,54 @@ await Promise.all([1, 2, 3].map(async x => x + 1));
|
|
80
84
|
|
81
85
|
## Options
|
82
86
|
|
87
|
+
### `checkThenables`
|
88
|
+
|
89
|
+
A ["Thenable"](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise#thenables) value is an object which has a `then` method, such as a `Promise`.
|
90
|
+
Other Thenables include TypeScript's built-in `PromiseLike` interface and any custom object that happens to have a `.then()`.
|
91
|
+
|
92
|
+
The `checkThenables` option triggers `no-floating-promises` to also consider all values that satisfy the Thenable shape (a `.then()` method that takes two callback parameters), not just Promises.
|
93
|
+
This can be useful if your code works with older `Promise` polyfills instead of the native `Promise` class.
|
94
|
+
|
95
|
+
<Tabs>
|
96
|
+
<TabItem value="❌ Incorrect">
|
97
|
+
|
98
|
+
```ts option='{"checkThenables": true}'
|
99
|
+
declare function createPromiseLike(): PromiseLike<string>;
|
100
|
+
|
101
|
+
createPromiseLike();
|
102
|
+
|
103
|
+
interface MyThenable {
|
104
|
+
then(onFulfilled: () => void, onRejected: () => void): MyThenable;
|
105
|
+
}
|
106
|
+
|
107
|
+
declare function createMyThenable(): MyThenable;
|
108
|
+
|
109
|
+
createMyThenable();
|
110
|
+
```
|
111
|
+
|
112
|
+
</TabItem>
|
113
|
+
<TabItem value="✅ Correct">
|
114
|
+
|
115
|
+
```ts option='{"checkThenables": true}'
|
116
|
+
declare function createPromiseLike(): PromiseLike<string>;
|
117
|
+
|
118
|
+
await createPromiseLike();
|
119
|
+
|
120
|
+
interface MyThenable {
|
121
|
+
then(onFulfilled: () => void, onRejected: () => void): MyThenable;
|
122
|
+
}
|
123
|
+
|
124
|
+
declare function createMyThenable(): MyThenable;
|
125
|
+
|
126
|
+
await createMyThenable();
|
127
|
+
```
|
128
|
+
|
129
|
+
</TabItem>
|
130
|
+
</Tabs>
|
131
|
+
|
83
132
|
### `ignoreVoid`
|
84
133
|
|
85
|
-
This allows you to stop the rule reporting promises consumed with void operator.
|
134
|
+
This option, which is `true` by default, allows you to stop the rule reporting promises consumed with void operator.
|
86
135
|
This can be a good way to explicitly mark a promise as intentionally not awaited.
|
87
136
|
|
88
137
|
Examples of **correct** code for this rule with `{ ignoreVoid: true }`:
|
@@ -100,7 +149,7 @@ With this option set to `true`, and if you are using `no-void`, you should turn
|
|
100
149
|
|
101
150
|
### `ignoreIIFE`
|
102
151
|
|
103
|
-
This allows you to skip checking of async IIFEs (Immediately Invoked
|
152
|
+
This allows you to skip checking of async IIFEs (Immediately Invoked Function Expressions).
|
104
153
|
|
105
154
|
Examples of **correct** code for this rule with `{ ignoreIIFE: true }`:
|
106
155
|
|
@@ -115,6 +164,101 @@ await (async function () {
|
|
115
164
|
})();
|
116
165
|
```
|
117
166
|
|
167
|
+
### `allowForKnownSafePromises`
|
168
|
+
|
169
|
+
This option allows marking specific types as "safe" to be floating. For example, you may need to do this in the case of libraries whose APIs return Promises whose rejections are safely handled by the library.
|
170
|
+
|
171
|
+
This option takes an array of type specifiers to consider safe.
|
172
|
+
Each item in the array must have one of the following forms:
|
173
|
+
|
174
|
+
- A type defined in a file (`{ from: "file", name: "Foo", path: "src/foo-file.ts" }` with `path` being an optional path relative to the project root directory)
|
175
|
+
- A type from the default library (`{ from: "lib", name: "PromiseLike" }`)
|
176
|
+
- A type from a package (`{ from: "package", name: "Foo", package: "foo-lib" }`, this also works for types defined in a typings package).
|
177
|
+
|
178
|
+
Examples of code for this rule with:
|
179
|
+
|
180
|
+
```json
|
181
|
+
{
|
182
|
+
"allowForKnownSafePromises": [
|
183
|
+
{ "from": "file", "name": "SafePromise" },
|
184
|
+
{ "from": "lib", "name": "PromiseLike" },
|
185
|
+
{ "from": "package", "name": "Bar", "package": "bar-lib" }
|
186
|
+
]
|
187
|
+
}
|
188
|
+
```
|
189
|
+
|
190
|
+
<Tabs>
|
191
|
+
<TabItem value="❌ Incorrect">
|
192
|
+
|
193
|
+
```ts option='{"allowForKnownSafePromises":[{"from":"file","name":"SafePromise"},{"from":"lib","name":"PromiseLike"},{"from":"package","name":"Bar","package":"bar-lib"}]}'
|
194
|
+
let promise: Promise<number> = Promise.resolve(2);
|
195
|
+
promise;
|
196
|
+
|
197
|
+
function returnsPromise(): Promise<number> {
|
198
|
+
return Promise.resolve(42);
|
199
|
+
}
|
200
|
+
|
201
|
+
returnsPromise();
|
202
|
+
```
|
203
|
+
|
204
|
+
</TabItem>
|
205
|
+
<TabItem value="✅ Correct">
|
206
|
+
|
207
|
+
```ts option='{"allowForKnownSafePromises":[{"from":"file","name":"SafePromise"},{"from":"lib","name":"PromiseLike"},{"from":"package","name":"Bar","package":"bar-lib"}]}'
|
208
|
+
// promises can be marked as safe by using branded types
|
209
|
+
type SafePromise = Promise<number> & { __linterBrands?: string };
|
210
|
+
|
211
|
+
let promise: SafePromise = Promise.resolve(2);
|
212
|
+
promise;
|
213
|
+
|
214
|
+
function returnsSafePromise(): SafePromise {
|
215
|
+
return Promise.resolve(42);
|
216
|
+
}
|
217
|
+
|
218
|
+
returnsSafePromise();
|
219
|
+
```
|
220
|
+
|
221
|
+
</TabItem>
|
222
|
+
</Tabs>
|
223
|
+
|
224
|
+
### `allowForKnownSafeCalls`
|
225
|
+
|
226
|
+
This option allows marking specific functions as "safe" to be called to create floating Promises.
|
227
|
+
For example, you may need to do this in the case of libraries whose APIs may be called without handling the resultant Promises.
|
228
|
+
|
229
|
+
This option takes the same array format as [`allowForKnownSafePromises`](#allowForKnownSafePromises).
|
230
|
+
|
231
|
+
Examples of code for this rule with:
|
232
|
+
|
233
|
+
```json
|
234
|
+
{
|
235
|
+
"allowForKnownSafeCalls": [
|
236
|
+
{ "from": "file", "name": "safe", "path": "input.ts" }
|
237
|
+
]
|
238
|
+
}
|
239
|
+
```
|
240
|
+
|
241
|
+
<Tabs>
|
242
|
+
<TabItem value="❌ Incorrect">
|
243
|
+
|
244
|
+
```ts option='{"allowForKnownSafeCalls":[{"from":"file","name":"safe","path":"input.ts"}]}'
|
245
|
+
declare function unsafe(...args: unknown[]): Promise<void>;
|
246
|
+
|
247
|
+
unsafe('...', () => {});
|
248
|
+
```
|
249
|
+
|
250
|
+
</TabItem>
|
251
|
+
<TabItem value="✅ Correct">
|
252
|
+
|
253
|
+
```ts option='{"allowForKnownSafeCalls":[{"from":"file","name":"safe","path":"input.ts"}]}' skipValidation
|
254
|
+
declare function safe(...args: unknown[]): Promise<void>;
|
255
|
+
|
256
|
+
safe('...', () => {});
|
257
|
+
```
|
258
|
+
|
259
|
+
</TabItem>
|
260
|
+
</Tabs>
|
261
|
+
|
118
262
|
## When Not To Use It
|
119
263
|
|
120
264
|
This rule can be difficult to enable on large existing projects that set up many floating Promises.
|
@@ -76,5 +76,5 @@ If you're not using TypeScript 5.0's `verbatimModuleSyntax` option and your proj
|
|
76
76
|
## Related To
|
77
77
|
|
78
78
|
- [`consistent-type-imports`](./consistent-type-imports.mdx)
|
79
|
-
- [`import/consistent-type-specifier-style`](https://github.com/import-js/eslint-plugin-import/blob/main/docs/rules/consistent-type-specifier-style.
|
79
|
+
- [`import/consistent-type-specifier-style`](https://github.com/import-js/eslint-plugin-import/blob/main/docs/rules/consistent-type-specifier-style.md)
|
80
80
|
- [`import/no-duplicates` with `{"prefer-inline": true}`](https://github.com/import-js/eslint-plugin-import/blob/main/docs/rules/no-duplicates.md#inline-type-imports)
|
@@ -127,19 +127,18 @@ Examples of code for this rule with `checksVoidReturn: true`:
|
|
127
127
|
|
128
128
|
```ts option='{ "checksVoidReturn": true }'
|
129
129
|
[1, 2, 3].forEach(async value => {
|
130
|
-
await
|
130
|
+
await fetch(`/${value}`);
|
131
131
|
});
|
132
132
|
|
133
|
-
new Promise(async (resolve, reject) => {
|
134
|
-
await
|
133
|
+
new Promise<void>(async (resolve, reject) => {
|
134
|
+
await fetch('/');
|
135
135
|
resolve();
|
136
136
|
});
|
137
137
|
|
138
|
-
|
139
|
-
|
140
|
-
|
141
|
-
|
142
|
-
otherSynchronousCall();
|
138
|
+
document.addEventListener('click', async () => {
|
139
|
+
console.log('synchronous call');
|
140
|
+
await fetch('/');
|
141
|
+
console.log('synchronous call');
|
143
142
|
});
|
144
143
|
```
|
145
144
|
|
@@ -169,8 +168,7 @@ new Promise((resolve, reject) => {
|
|
169
168
|
});
|
170
169
|
|
171
170
|
// Name the async wrapper to call it later
|
172
|
-
|
173
|
-
eventEmitter.on('some-event', () => {
|
171
|
+
document.addEventListener('click', () => {
|
174
172
|
const handler = async () => {
|
175
173
|
await doSomething();
|
176
174
|
otherSynchronousCall();
|
@@ -210,30 +208,30 @@ Examples of code for this rule with `checksSpreads: true`:
|
|
210
208
|
<TabItem value="❌ Incorrect">
|
211
209
|
|
212
210
|
```ts option='{ "checksSpreads": true }'
|
213
|
-
const getData = () =>
|
211
|
+
const getData = () => fetch('/');
|
214
212
|
|
215
|
-
|
213
|
+
console.log({ foo: 42, ...getData() });
|
216
214
|
|
217
|
-
const
|
218
|
-
await
|
215
|
+
const awaitData = async () => {
|
216
|
+
await fetch('/');
|
219
217
|
};
|
220
218
|
|
221
|
-
|
219
|
+
console.log({ foo: 42, ...awaitData() });
|
222
220
|
```
|
223
221
|
|
224
222
|
</TabItem>
|
225
223
|
<TabItem value="✅ Correct">
|
226
224
|
|
227
225
|
```ts option='{ "checksSpreads": true }'
|
228
|
-
const getData = () =>
|
226
|
+
const getData = () => fetch('/');
|
229
227
|
|
230
|
-
|
228
|
+
console.log({ foo: 42, ...(await getData()) });
|
231
229
|
|
232
|
-
const
|
233
|
-
await
|
230
|
+
const awaitData = async () => {
|
231
|
+
await fetch('/');
|
234
232
|
};
|
235
233
|
|
236
|
-
|
234
|
+
console.log({ foo: 42, ...(await awaitData()) });
|
237
235
|
```
|
238
236
|
|
239
237
|
</TabItem>
|
@@ -96,3 +96,7 @@ If you strongly feel a preference for these unnecessary type constituents, this
|
|
96
96
|
- [Intersection Types](https://www.typescriptlang.org/docs/handbook/2/objects.html#intersection-types)
|
97
97
|
- [Bottom Types](https://en.wikipedia.org/wiki/Bottom_type)
|
98
98
|
- [Top Types](https://en.wikipedia.org/wiki/Top_type)
|
99
|
+
|
100
|
+
## Related To
|
101
|
+
|
102
|
+
- [no-duplicate-type-constituents](./no-duplicate-type-constituents.mdx)
|
@@ -38,7 +38,7 @@ import * as lib3 from 'lib3';
|
|
38
38
|
|
39
39
|
### `allow`
|
40
40
|
|
41
|
-
|
41
|
+
An array of strings. These strings will be compiled into regular expressions with the `u` flag and be used to test against the imported path. A common use case is to allow importing `package.json`. This is because `package.json` commonly lives outside of the TS root directory, so statically importing it would lead to root directory conflicts, especially with `resolveJsonModule` enabled. You can also use it to allow importing any JSON if your environment doesn't support JSON modules, or use it for other cases where `import` statements cannot work.
|
42
42
|
|
43
43
|
With `{allow: ['/package\\.json$']}`:
|
44
44
|
|
@@ -59,6 +59,33 @@ console.log(require('../package.json').version);
|
|
59
59
|
</TabItem>
|
60
60
|
</Tabs>
|
61
61
|
|
62
|
+
### `allowAsImport`
|
63
|
+
|
64
|
+
When set to `true`, the `import x = require(...)` declaration won't be reported.
|
65
|
+
This is useful if you use certain module options that require strict CommonJS interop semantics.
|
66
|
+
|
67
|
+
With `{allowAsImport: true}`:
|
68
|
+
|
69
|
+
<Tabs>
|
70
|
+
<TabItem value="❌ Incorrect">
|
71
|
+
|
72
|
+
```ts option='{ "allowAsImport": true }'
|
73
|
+
var foo = require('foo');
|
74
|
+
const foo = require('foo');
|
75
|
+
let foo = require('foo');
|
76
|
+
```
|
77
|
+
|
78
|
+
</TabItem>
|
79
|
+
<TabItem value="✅ Correct">
|
80
|
+
|
81
|
+
```ts option='{ "allowAsImport": true }'
|
82
|
+
import foo = require('foo');
|
83
|
+
import foo from 'foo';
|
84
|
+
```
|
85
|
+
|
86
|
+
</TabItem>
|
87
|
+
</Tabs>
|
88
|
+
|
62
89
|
## When Not To Use It
|
63
90
|
|
64
91
|
If your project frequently uses older CommonJS `require`s, then this rule might not be applicable to you.
|
@@ -0,0 +1,71 @@
|
|
1
|
+
---
|
2
|
+
description: 'Disallow certain types.'
|
3
|
+
---
|
4
|
+
|
5
|
+
import Tabs from '@theme/Tabs';
|
6
|
+
import TabItem from '@theme/TabItem';
|
7
|
+
|
8
|
+
> 🛑 This file is source code, not the primary documentation location! 🛑
|
9
|
+
>
|
10
|
+
> See **https://typescript-eslint.io/rules/no-restricted-types** for documentation.
|
11
|
+
|
12
|
+
It can sometimes be useful to ban specific types from being used in type annotations.
|
13
|
+
For example, a project might be migrating from using one type to another, and want to ban references to the old type.
|
14
|
+
|
15
|
+
This rule can be configured to ban a list of specific types and can suggest alternatives.
|
16
|
+
Note that it does not ban the corresponding runtime objects from being used.
|
17
|
+
|
18
|
+
## Options
|
19
|
+
|
20
|
+
### `types`
|
21
|
+
|
22
|
+
An object whose keys are the types you want to ban, and the values are error messages.
|
23
|
+
|
24
|
+
The type can either be a type name literal (`OldType`) or a a type name with generic parameter instantiation(s) (`OldType<MyArgument>`).
|
25
|
+
|
26
|
+
The values can be:
|
27
|
+
|
28
|
+
- A string, which is the error message to be reported; or
|
29
|
+
- `false` to specifically un-ban this type (useful when you are using `extendDefaults`); or
|
30
|
+
- An object with the following properties:
|
31
|
+
- `message: string`: the message to display when the type is matched.
|
32
|
+
- `fixWith?: string`: a string to replace the banned type with when the fixer is run. If this is omitted, no fix will be done.
|
33
|
+
- `suggest?: string[]`: a list of suggested replacements for the banned type.
|
34
|
+
|
35
|
+
Example configuration:
|
36
|
+
|
37
|
+
```jsonc
|
38
|
+
{
|
39
|
+
"@typescript-eslint/no-restricted-types": [
|
40
|
+
"error",
|
41
|
+
{
|
42
|
+
"types": {
|
43
|
+
// add a custom message to help explain why not to use it
|
44
|
+
"OldType": "Don't use OldType because it is unsafe",
|
45
|
+
|
46
|
+
// add a custom message, and tell the plugin how to fix it
|
47
|
+
"OldAPI": {
|
48
|
+
"message": "Use NewAPI instead",
|
49
|
+
"fixWith": "NewAPI",
|
50
|
+
},
|
51
|
+
|
52
|
+
// add a custom message, and tell the plugin how to suggest a fix
|
53
|
+
"SoonToBeOldAPI": {
|
54
|
+
"message": "Use NewAPI instead",
|
55
|
+
"suggest": ["NewAPIOne", "NewAPITwo"],
|
56
|
+
},
|
57
|
+
},
|
58
|
+
},
|
59
|
+
],
|
60
|
+
}
|
61
|
+
```
|
62
|
+
|
63
|
+
## When Not To Use It
|
64
|
+
|
65
|
+
If you have no need to ban specific types from being used in type annotations, you don't need this rule.
|
66
|
+
|
67
|
+
## Related To
|
68
|
+
|
69
|
+
- [`no-empty-object-type`](./no-empty-object-type.mdx)
|
70
|
+
- [`no-unsafe-function-type`](./no-unsafe-function-type.mdx)
|
71
|
+
- [`no-wrapper-object-types`](./no-wrapper-object-types.mdx)
|
@@ -19,7 +19,7 @@ TypeScript type aliases are a commonly necessary language feature; banning it al
|
|
19
19
|
:::note
|
20
20
|
|
21
21
|
If you want to ban certain classifications of type aliases, consider using [`no-restricted-syntax`](https://eslint.org/docs/latest/rules/no-restricted-syntax).
|
22
|
-
See [Troubleshooting & FAQs](/troubleshooting#how-can-i-ban-specific-language-feature).
|
22
|
+
See [Troubleshooting & FAQs](/troubleshooting/faqs/general#how-can-i-ban-specific-language-feature).
|
23
23
|
|
24
24
|
:::
|
25
25
|
|
@@ -114,11 +114,11 @@ if (someNullCondition !== false) {
|
|
114
114
|
|
115
115
|
```ts option='{ "allowComparingNullableBooleansToFalse": false }'
|
116
116
|
declare const someUndefinedCondition: boolean | undefined;
|
117
|
-
if (someUndefinedCondition ?? true) {
|
117
|
+
if (!(someUndefinedCondition ?? true)) {
|
118
118
|
}
|
119
119
|
|
120
120
|
declare const someNullCondition: boolean | null;
|
121
|
-
if (
|
121
|
+
if (someNullCondition ?? true) {
|
122
122
|
}
|
123
123
|
```
|
124
124
|
|
@@ -127,16 +127,16 @@ if (!(someNullCondition ?? true)) {
|
|
127
127
|
|
128
128
|
## Fixer
|
129
129
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
130
|
+
| Comparison | Fixer Output | Notes |
|
131
|
+
| :----------------------------: | ------------------------------- | ----------------------------------------------------------------------------------- |
|
132
|
+
| `booleanVar === true` | `booleanVar` | |
|
133
|
+
| `booleanVar !== true` | `!booleanVar` | |
|
134
|
+
| `booleanVar === false` | `!booleanVar` | |
|
135
|
+
| `booleanVar !== false` | `booleanVar` | |
|
136
|
+
| `nullableBooleanVar === true` | `nullableBooleanVar` | Only checked/fixed if the `allowComparingNullableBooleansToTrue` option is `false` |
|
137
|
+
| `nullableBooleanVar !== true` | `!nullableBooleanVar` | Only checked/fixed if the `allowComparingNullableBooleansToTrue` option is `false` |
|
138
|
+
| `nullableBooleanVar === false` | `!(nullableBooleanVar ?? true)` | Only checked/fixed if the `allowComparingNullableBooleansToFalse` option is `false` |
|
139
|
+
| `nullableBooleanVar !== false` | `nullableBooleanVar ?? true` | Only checked/fixed if the `allowComparingNullableBooleansToFalse` option is `false` |
|
140
140
|
|
141
141
|
## When Not To Use It
|
142
142
|
|
@@ -0,0 +1,42 @@
|
|
1
|
+
---
|
2
|
+
description: 'Disallow unnecessary assignment of constructor property parameter.'
|
3
|
+
---
|
4
|
+
|
5
|
+
import Tabs from '@theme/Tabs';
|
6
|
+
import TabItem from '@theme/TabItem';
|
7
|
+
|
8
|
+
> 🛑 This file is source code, not the primary documentation location! 🛑
|
9
|
+
>
|
10
|
+
> See **https://typescript-eslint.io/rules/no-unnecessary-parameter-property-assignment** for documentation.
|
11
|
+
|
12
|
+
[TypeScript's parameter properties](https://www.typescriptlang.org/docs/handbook/2/classes.html#parameter-properties) allow creating and initializing a member in one place.
|
13
|
+
Therefore, in most cases, it is not necessary to assign parameter properties of the same name to members within a constructor.
|
14
|
+
|
15
|
+
## Examples
|
16
|
+
|
17
|
+
<Tabs>
|
18
|
+
<TabItem value="❌ Incorrect">
|
19
|
+
|
20
|
+
```ts
|
21
|
+
class Foo {
|
22
|
+
constructor(public bar: string) {
|
23
|
+
this.bar = bar;
|
24
|
+
}
|
25
|
+
}
|
26
|
+
```
|
27
|
+
|
28
|
+
</TabItem>
|
29
|
+
<TabItem value="✅ Correct">
|
30
|
+
|
31
|
+
```ts
|
32
|
+
class Foo {
|
33
|
+
constructor(public bar: string) {}
|
34
|
+
}
|
35
|
+
```
|
36
|
+
|
37
|
+
</TabItem>
|
38
|
+
</Tabs>
|
39
|
+
|
40
|
+
## When Not To Use It
|
41
|
+
|
42
|
+
If you don't use parameter properties, you can ignore this rule.
|
@@ -0,0 +1,85 @@
|
|
1
|
+
---
|
2
|
+
description: 'Disallow unnecessary template expressions.'
|
3
|
+
---
|
4
|
+
|
5
|
+
import Tabs from '@theme/Tabs';
|
6
|
+
import TabItem from '@theme/TabItem';
|
7
|
+
|
8
|
+
> 🛑 This file is source code, not the primary documentation location! 🛑
|
9
|
+
>
|
10
|
+
> See **https://typescript-eslint.io/rules/no-unnecessary-template-expression** for documentation.
|
11
|
+
|
12
|
+
This rule reports template literals that contain substitution expressions (also variously referred to as embedded expressions or string interpolations) that are unnecessary and can be simplified.
|
13
|
+
|
14
|
+
:::info[Migration from `no-useless-template-literals`]
|
15
|
+
|
16
|
+
This rule was formerly known as [`no-useless-template-literals`](./no-useless-template-literals.mdx).
|
17
|
+
The new name is a drop-in replacement with identical functionality.
|
18
|
+
|
19
|
+
:::
|
20
|
+
|
21
|
+
## Examples
|
22
|
+
|
23
|
+
<Tabs>
|
24
|
+
<TabItem value="❌ Incorrect">
|
25
|
+
|
26
|
+
```ts
|
27
|
+
// Static values can be incorporated into the surrounding template.
|
28
|
+
|
29
|
+
const ab1 = `${'a'}${'b'}`;
|
30
|
+
const ab2 = `a${'b'}`;
|
31
|
+
|
32
|
+
const stringWithNumber = `${'1 + 1 = '}${2}`;
|
33
|
+
|
34
|
+
const stringWithBoolean = `${'true is '}${true}`;
|
35
|
+
|
36
|
+
// Some simple expressions that are already strings
|
37
|
+
// can be rewritten without a template at all.
|
38
|
+
|
39
|
+
const text = 'a';
|
40
|
+
const wrappedText = `${text}`;
|
41
|
+
|
42
|
+
declare const intersectionWithString: string & { _brand: 'test-brand' };
|
43
|
+
const wrappedIntersection = `${intersectionWithString}`;
|
44
|
+
```
|
45
|
+
|
46
|
+
</TabItem>
|
47
|
+
<TabItem value="✅ Correct">
|
48
|
+
|
49
|
+
```ts
|
50
|
+
// Static values can be incorporated into the surrounding template.
|
51
|
+
|
52
|
+
const ab1 = `ab`;
|
53
|
+
const ab2 = `ab`;
|
54
|
+
|
55
|
+
const stringWithNumber = `1 + 1 = 2`;
|
56
|
+
|
57
|
+
const stringWithBoolean = `true is true`;
|
58
|
+
|
59
|
+
// Some simple expressions that are already strings
|
60
|
+
// can be rewritten without a template at all.
|
61
|
+
|
62
|
+
const text = 'a';
|
63
|
+
const wrappedText = text;
|
64
|
+
|
65
|
+
declare const intersectionWithString: string & { _brand: 'test-brand' };
|
66
|
+
const wrappedIntersection = intersectionWithString;
|
67
|
+
```
|
68
|
+
|
69
|
+
</TabItem>
|
70
|
+
</Tabs>
|
71
|
+
|
72
|
+
:::info
|
73
|
+
This rule does not aim to flag template literals without substitution expressions that could have been written as an ordinary string.
|
74
|
+
That is to say, this rule will not help you turn `` `this` `` into `"this"`.
|
75
|
+
If you are looking for such a rule, you can configure the [`@stylistic/ts/quotes`](https://eslint.style/rules/ts/quotes) rule to do this.
|
76
|
+
:::
|
77
|
+
|
78
|
+
## When Not To Use It
|
79
|
+
|
80
|
+
When you want to allow string expressions inside template literals.
|
81
|
+
|
82
|
+
## Related To
|
83
|
+
|
84
|
+
- [`restrict-template-expressions`](./restrict-template-expressions.mdx)
|
85
|
+
- [`@stylistic/ts/quotes`](https://eslint.style/rules/ts/quotes)
|