@typescript-eslint/eslint-plugin 8.21.1-alpha.1 → 8.21.1-alpha.11
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/rules/no-duplicate-type-constituents.js +97 -71
- package/dist/rules/no-duplicate-type-constituents.js.map +1 -1
- package/dist/rules/no-extraneous-class.js +4 -3
- package/dist/rules/no-extraneous-class.js.map +1 -1
- package/dist/rules/no-shadow.js +2 -3
- package/dist/rules/no-shadow.js.map +1 -1
- package/dist/rules/no-unnecessary-condition.js +7 -43
- package/dist/rules/no-unnecessary-condition.js.map +1 -1
- package/dist/rules/no-unnecessary-template-expression.js +257 -173
- package/dist/rules/no-unnecessary-template-expression.js.map +1 -1
- package/dist/rules/prefer-nullish-coalescing.js +106 -56
- package/dist/rules/prefer-nullish-coalescing.js.map +1 -1
- package/dist/rules/prefer-readonly.js +58 -1
- package/dist/rules/prefer-readonly.js.map +1 -1
- package/dist/util/getValueOfLiteralType.js +17 -0
- package/dist/util/getValueOfLiteralType.js.map +1 -0
- package/dist/util/index.js +2 -0
- package/dist/util/index.js.map +1 -1
- package/dist/util/truthinessAndNullishUtils.js +68 -0
- package/dist/util/truthinessAndNullishUtils.js.map +1 -0
- package/docs/rules/consistent-type-imports.mdx +2 -2
- package/docs/rules/no-namespace.mdx +12 -4
- package/docs/rules/no-require-imports.mdx +20 -6
- package/docs/rules/no-unnecessary-template-expression.mdx +19 -0
- package/docs/rules/no-unused-vars.mdx +68 -1
- package/docs/rules/prefer-nullish-coalescing.mdx +5 -0
- package/package.json +7 -7
@@ -9,7 +9,9 @@ import TabItem from '@theme/TabItem';
|
|
9
9
|
>
|
10
10
|
> See **https://typescript-eslint.io/rules/no-require-imports** for documentation.
|
11
11
|
|
12
|
-
|
12
|
+
Depending on your TSConfig settings and whether you're authoring ES Modules or CommonJS, TS may allow both `import` and `require()` to be used, even within a single file.
|
13
|
+
|
14
|
+
This rule enforces that you use the newer ES Module `import` syntax over CommonJS `require()`.
|
13
15
|
|
14
16
|
## Examples
|
15
17
|
|
@@ -42,7 +44,7 @@ import * as lib3 from 'lib3';
|
|
42
44
|
|
43
45
|
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.
|
44
46
|
|
45
|
-
With `{allow: ['/package\\.json$']}`:
|
47
|
+
With `{ allow: ['/package\\.json$'] }`:
|
46
48
|
|
47
49
|
<Tabs>
|
48
50
|
<TabItem value="❌ Incorrect">
|
@@ -66,9 +68,9 @@ console.log(require('../package.json').version);
|
|
66
68
|
{/* insert option description */}
|
67
69
|
|
68
70
|
When set to `true`, `import ... = require(...)` declarations won't be reported.
|
69
|
-
This is
|
71
|
+
This is beneficial if you use certain module options that require strict CommonJS interop semantics, such as [verbatimModuleSyntax](https://www.typescriptlang.org/tsconfig/#verbatimModuleSyntax).
|
70
72
|
|
71
|
-
With `{allowAsImport: true}`:
|
73
|
+
With `{ allowAsImport: true }`:
|
72
74
|
|
73
75
|
<Tabs>
|
74
76
|
<TabItem value="❌ Incorrect">
|
@@ -90,10 +92,22 @@ import foo from 'foo';
|
|
90
92
|
</TabItem>
|
91
93
|
</Tabs>
|
92
94
|
|
95
|
+
## Usage with CommonJS
|
96
|
+
|
97
|
+
While this rule is primarily intended to promote ES Module syntax, it still makes sense to enable this rule when authoring CommonJS modules.
|
98
|
+
|
99
|
+
If you prefer to use TypeScript's built-in `import ... from ...` ES Module syntax, which is transformed to `require()` calls during transpilation when outputting CommonJS, you can use the rule's default behavior.
|
100
|
+
|
101
|
+
If, instead, you prefer to use `require()` syntax, we recommend you use this rule with [`allowAsImport`](#allowAsImport) enabled.
|
102
|
+
That way, you still enforce usage of `import ... = require(...)` rather than bare `require()` calls, which are not statically analyzed by TypeScript.
|
103
|
+
We don't directly a way to _prohibit_ ES Module syntax from being used; consider instead using TypeScript's [`verbatimModuleSyntax`](https://www.typescriptlang.org/tsconfig/#verbatimModuleSyntax) option if you find yourself in a situation where you would want this.
|
104
|
+
|
93
105
|
## When Not To Use It
|
94
106
|
|
95
|
-
If your project frequently uses
|
96
|
-
|
107
|
+
If you are authoring CommonJS modules _and_ your project frequently uses dynamic `require`s, then this rule might not be applicable to you.
|
108
|
+
Otherwise the `allowAsImport` option probably suits your needs.
|
109
|
+
|
110
|
+
If only a subset of your project uses dynamic `require`s then you might consider using [ESLint disable comments](https://eslint.org/docs/latest/use/configure/rules#using-configuration-comments-1) for those specific situations instead of completely disabling this rule.
|
97
111
|
|
98
112
|
## Related To
|
99
113
|
|
@@ -28,6 +28,8 @@ The new name is a drop-in replacement with identical functionality.
|
|
28
28
|
|
29
29
|
const ab1 = `${'a'}${'b'}`;
|
30
30
|
const ab2 = `a${'b'}`;
|
31
|
+
type AB1 = `${'A'}${'B'}`;
|
32
|
+
type AB2 = `A${'B'}`;
|
31
33
|
|
32
34
|
const stringWithNumber = `${'1 + 1 = '}${2}`;
|
33
35
|
|
@@ -38,9 +40,13 @@ const stringWithBoolean = `${'true is '}${true}`;
|
|
38
40
|
|
39
41
|
const text = 'a';
|
40
42
|
const wrappedText = `${text}`;
|
43
|
+
type Text = 'A';
|
44
|
+
type WrappedText = `${Text}`;
|
41
45
|
|
42
46
|
declare const intersectionWithString: string & { _brand: 'test-brand' };
|
43
47
|
const wrappedIntersection = `${intersectionWithString}`;
|
48
|
+
type IntersectionWithString = string & { _brand: 'test-brand' };
|
49
|
+
type WrappedIntersection = `${IntersectionWithString}`;
|
44
50
|
```
|
45
51
|
|
46
52
|
</TabItem>
|
@@ -51,6 +57,15 @@ const wrappedIntersection = `${intersectionWithString}`;
|
|
51
57
|
|
52
58
|
const ab1 = `ab`;
|
53
59
|
const ab2 = `ab`;
|
60
|
+
type AB = `AB`;
|
61
|
+
|
62
|
+
// Transforming enum members into string unions using template literals is allowed.
|
63
|
+
enum ABC {
|
64
|
+
A = 'A',
|
65
|
+
B = 'B',
|
66
|
+
C = 'C',
|
67
|
+
}
|
68
|
+
type ABCUnion = `${ABC}`;
|
54
69
|
|
55
70
|
const stringWithNumber = `1 + 1 = 2`;
|
56
71
|
|
@@ -61,9 +76,13 @@ const stringWithBoolean = `true is true`;
|
|
61
76
|
|
62
77
|
const text = 'a';
|
63
78
|
const wrappedText = text;
|
79
|
+
type Text = 'A';
|
80
|
+
type WrappedText = Text;
|
64
81
|
|
65
82
|
declare const intersectionWithString: string & { _brand: 'test-brand' };
|
66
83
|
const wrappedIntersection = intersectionWithString;
|
84
|
+
type IntersectionWithString = string & { _brand: 'test-brand' };
|
85
|
+
type WrappedIntersection = IntersectionWithString;
|
67
86
|
```
|
68
87
|
|
69
88
|
</TabItem>
|
@@ -12,7 +12,11 @@ import TabItem from '@theme/TabItem';
|
|
12
12
|
This rule extends the base [`eslint/no-unused-vars`](https://eslint.org/docs/rules/no-unused-vars) rule.
|
13
13
|
It adds support for TypeScript features, such as types.
|
14
14
|
|
15
|
-
##
|
15
|
+
## Options
|
16
|
+
|
17
|
+
## FAQs
|
18
|
+
|
19
|
+
### What benefits does this rule have over TypeScript?
|
16
20
|
|
17
21
|
TypeScript provides [`noUnusedLocals`](https://www.typescriptlang.org/tsconfig#noUnusedLocals) and [`noUnusedParameters`](https://www.typescriptlang.org/tsconfig#noUnusedParameters) compiler options that can report errors on unused local variables or parameters, respectively.
|
18
22
|
Those compiler options can be convenient to use if you don't want to set up ESLint and typescript-eslint.
|
@@ -52,3 +56,66 @@ Also see similar rules provided by ESLint:
|
|
52
56
|
|
53
57
|
- [`no-unused-private-class-members`](https://eslint.org/docs/latest/rules/no-unused-private-class-members)
|
54
58
|
- [`no-unused-labels`](https://eslint.org/docs/latest/rules/no-unused-labels)
|
59
|
+
|
60
|
+
### Why does this rule report variables used only for types?
|
61
|
+
|
62
|
+
This rule does not count type-only uses when determining whether a variable is used.
|
63
|
+
Declaring variables only to use them for types adds code and runtime complexity.
|
64
|
+
The variables are never actually used at runtime.
|
65
|
+
They can be misleading to readers of the code.
|
66
|
+
|
67
|
+
<Tabs>
|
68
|
+
|
69
|
+
<TabItem value="typeof Variables">
|
70
|
+
|
71
|
+
For example, if a variable is only used for `typeof`, this rule will report:
|
72
|
+
|
73
|
+
```ts
|
74
|
+
const box = {
|
75
|
+
// ~~~
|
76
|
+
// 'box' is assigned a value but only used as a type.
|
77
|
+
value: 123,
|
78
|
+
};
|
79
|
+
|
80
|
+
export type Box = typeof box;
|
81
|
+
```
|
82
|
+
|
83
|
+
Instead, it's often cleaner and less code to write out the types directly:
|
84
|
+
|
85
|
+
```ts
|
86
|
+
export interface Box {
|
87
|
+
value: number;
|
88
|
+
}
|
89
|
+
```
|
90
|
+
|
91
|
+
</TabItem>
|
92
|
+
|
93
|
+
<TabItem value="Zod Schemas">
|
94
|
+
|
95
|
+
For example, if a Zod schema variable is only used for `typeof`, this rule will report:
|
96
|
+
|
97
|
+
```ts
|
98
|
+
import { z } from 'zod';
|
99
|
+
|
100
|
+
const schema = z.object({
|
101
|
+
// ~~~~~~
|
102
|
+
// 'schema' is assigned a value but only used as a type.
|
103
|
+
value: z.number(),
|
104
|
+
});
|
105
|
+
|
106
|
+
export type Box = z.infer<typeof schema>;
|
107
|
+
```
|
108
|
+
|
109
|
+
Instead, it's often cleaner and less code to write out the types directly:
|
110
|
+
|
111
|
+
```ts
|
112
|
+
export interface Box {
|
113
|
+
value: number;
|
114
|
+
}
|
115
|
+
```
|
116
|
+
|
117
|
+
</TabItem>
|
118
|
+
|
119
|
+
</Tabs>
|
120
|
+
|
121
|
+
If you find yourself writing runtime values only for types, consider refactoring your code to declare types directly.
|
@@ -16,6 +16,7 @@ This rule reports when you may consider replacing:
|
|
16
16
|
|
17
17
|
- An `||` operator with `??`
|
18
18
|
- An `||=` operator with `??=`
|
19
|
+
- Ternary expressions (`?:`) that are equivalent to `||` or `??` with `??`
|
19
20
|
|
20
21
|
:::caution
|
21
22
|
This rule will not work as expected if [`strictNullChecks`](https://www.typescriptlang.org/tsconfig#strictNullChecks) is not enabled.
|
@@ -42,7 +43,9 @@ foo === undefined ? 'a string' : foo;
|
|
42
43
|
|
43
44
|
const foo: string | null = 'bar';
|
44
45
|
foo !== null ? foo : 'a string';
|
46
|
+
foo ? foo : 'a string';
|
45
47
|
foo === null ? 'a string' : foo;
|
48
|
+
!foo ? 'a string' : foo;
|
46
49
|
```
|
47
50
|
|
48
51
|
Correct code for `ignoreTernaryTests: false`:
|
@@ -61,6 +64,8 @@ foo ?? 'a string';
|
|
61
64
|
const foo: string | null = 'bar';
|
62
65
|
foo ?? 'a string';
|
63
66
|
foo ?? 'a string';
|
67
|
+
foo ?? 'a string';
|
68
|
+
foo ?? 'a string';
|
64
69
|
```
|
65
70
|
|
66
71
|
### `ignoreConditionalTests`
|
package/package.json
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
{
|
2
2
|
"name": "@typescript-eslint/eslint-plugin",
|
3
|
-
"version": "8.21.1-alpha.
|
3
|
+
"version": "8.21.1-alpha.11",
|
4
4
|
"description": "TypeScript plugin for ESLint",
|
5
5
|
"files": [
|
6
6
|
"dist",
|
@@ -61,10 +61,10 @@
|
|
61
61
|
},
|
62
62
|
"dependencies": {
|
63
63
|
"@eslint-community/regexpp": "^4.10.0",
|
64
|
-
"@typescript-eslint/scope-manager": "8.21.1-alpha.
|
65
|
-
"@typescript-eslint/type-utils": "8.21.1-alpha.
|
66
|
-
"@typescript-eslint/utils": "8.21.1-alpha.
|
67
|
-
"@typescript-eslint/visitor-keys": "8.21.1-alpha.
|
64
|
+
"@typescript-eslint/scope-manager": "8.21.1-alpha.11",
|
65
|
+
"@typescript-eslint/type-utils": "8.21.1-alpha.11",
|
66
|
+
"@typescript-eslint/utils": "8.21.1-alpha.11",
|
67
|
+
"@typescript-eslint/visitor-keys": "8.21.1-alpha.11",
|
68
68
|
"graphemer": "^1.4.0",
|
69
69
|
"ignore": "^5.3.1",
|
70
70
|
"natural-compare": "^1.4.0",
|
@@ -75,8 +75,8 @@
|
|
75
75
|
"@types/marked": "^5.0.2",
|
76
76
|
"@types/mdast": "^4.0.3",
|
77
77
|
"@types/natural-compare": "*",
|
78
|
-
"@typescript-eslint/rule-schema-to-typescript-types": "8.21.1-alpha.
|
79
|
-
"@typescript-eslint/rule-tester": "8.21.1-alpha.
|
78
|
+
"@typescript-eslint/rule-schema-to-typescript-types": "8.21.1-alpha.11",
|
79
|
+
"@typescript-eslint/rule-tester": "8.21.1-alpha.11",
|
80
80
|
"ajv": "^6.12.6",
|
81
81
|
"cross-env": "^7.0.3",
|
82
82
|
"cross-fetch": "*",
|