Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit a9eb0b9

Browse files
feat(experimental-utils): move isTypeReadonly from eslint-plugin to experimental-utils (typescript-eslint#3658)
Co-authored-by: Brad Zacher <[email protected]>
1 parent 4bb55a2 commit a9eb0b9

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

49 files changed

+945
-580
lines changed

.github/ISSUE_TEMPLATE.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,7 @@ i.e. eslint --ext ".ts,.js" src --debug
7373
| `@typescript-eslint/parser` | `X.Y.Z` |
7474
| `@typescript-eslint/typescript-estree` | `X.Y.Z` |
7575
| `@typescript-eslint/experimental-utils` | `X.Y.Z` |
76+
| `@typescript-eslint/type-utils` | `X.Y.Z` |
7677
| `TypeScript` | `X.Y.Z` |
7778
| `node` | `X.Y.Z` |
7879
| `npm` | `X.Y.Z` |
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
---
2+
name: '@typescript-eslint/type-utils'
3+
about: Report an issue with the '@typescript-eslint/type-utils' package
4+
title: ''
5+
labels: 'package: type-utils, triage'
6+
assignees: ''
7+
---
8+
9+
<!--
10+
Please don't ignore this template.
11+
12+
If you ignore it, we're just going to respond asking you to fill it out, which wastes everyone's time.
13+
The more relevant information you can include, the faster we can find the issue and fix it without asking you for more info.
14+
-->
15+
16+
<!--
17+
🚨 STOP 🚨 𝗦𝗧𝗢𝗣 🚨 𝑺𝑻𝑶𝑷 🚨
18+
19+
This issue template is only for problems specifically with the `@typescript-eslint/type-utils` package.
20+
21+
If you have a problem with a specific lint rule, please back out and select the `@typescript-eslint/eslint-plugin` template.
22+
If you have a problem with the parser, please back out and select the `@typescript-eslint/parser` template.
23+
-->
24+
25+
- [ ] I have tried restarting my IDE and the issue persists.
26+
- [ ] I have updated to the latest version of the packages.
27+
- [ ] I have [read the FAQ](https://github.com/typescript-eslint/typescript-eslint/blob/master/docs/getting-started/linting/FAQ.md) and my problem is not listed.
28+
29+
**Repro**
30+
31+
<!--
32+
Include a ***minimal*** reproduction case.
33+
The more irrelevant code/config you give, the harder it is for us to investigate.
34+
35+
Please consider creating an isolated reproduction repo to make it easy for the volunteer maintainers debug your issue.
36+
-->
37+
38+
```TS
39+
// your repro code case
40+
```
41+
42+
**Expected Result**
43+
44+
<!--
45+
What did you expect to happen?
46+
Please be specific here - list the exact lines and messages you expect.
47+
-->
48+
49+
**Actual Result**
50+
51+
<!--
52+
What actually happened?
53+
Please be specific here - list the exact lines and messages that caused errors
54+
-->
55+
56+
**Additional Info**
57+
58+
<!--
59+
Did eslint throw an exception?
60+
61+
Please run your lint again with the --debug flag, and dump the output below.
62+
i.e. eslint --ext ".ts,.js" src --debug
63+
-->
64+
65+
**Versions**
66+
67+
| package | version |
68+
| ------------------------------- | ------- |
69+
| `@typescript-eslint/type-utils` | `X.Y.Z` |
70+
| `@typescript-eslint/type-utils` | `X.Y.Z` |
71+
| `TypeScript` | `X.Y.Z` |
72+
| `node` | `X.Y.Z` |

.github/workflows/ci.yml

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,11 @@ jobs:
116116
env:
117117
CI: true
118118

119+
- name: Run unit tests for type-utils
120+
run: npx nx test @typescript-eslint/type-utils
121+
env:
122+
CI: true
123+
119124
- name: Run unit tests for parser
120125
run: npx nx test @typescript-eslint/parser
121126
env:
@@ -283,6 +288,11 @@ jobs:
283288
env:
284289
CI: true
285290

291+
- name: Run unit tests for type-utils
292+
run: npx nx test @typescript-eslint/type-utils
293+
env:
294+
CI: true
295+
286296
- name: Run unit tests for parser
287297
run: npx nx test @typescript-eslint/parser
288298
env:

.vscode/launch.json

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@
2424
"${workspaceFolder}/packages/experimental-utils/dist/index.js",
2525
"${workspaceFolder}/packages/experimental-utils/src/ts-estree.ts",
2626
"${workspaceFolder}/packages/experimental-utils/dist/ts-estree.js",
27+
"${workspaceFolder}/packages/type-utils/src/index.ts",
28+
"${workspaceFolder}/packages/type-utils/dist/index.js",
2729
"${workspaceFolder}/packages/parser/src/index.ts",
2830
"${workspaceFolder}/packages/parser/dist/index.js",
2931
"${workspaceFolder}/packages/typescript-estree/src/index.ts",
@@ -56,6 +58,8 @@
5658
"${workspaceFolder}/packages/experimental-utils/dist/index.js",
5759
"${workspaceFolder}/packages/experimental-utils/src/ts-estree.ts",
5860
"${workspaceFolder}/packages/experimental-utils/dist/ts-estree.js",
61+
"${workspaceFolder}/packages/type-utils/src/index.ts",
62+
"${workspaceFolder}/packages/type-utils/dist/index.js",
5963
"${workspaceFolder}/packages/parser/src/index.ts",
6064
"${workspaceFolder}/packages/parser/dist/index.js",
6165
"${workspaceFolder}/packages/typescript-estree/src/index.ts",
@@ -88,6 +92,8 @@
8892
"${workspaceFolder}/packages/experimental-utils/dist/index.js",
8993
"${workspaceFolder}/packages/experimental-utils/src/ts-estree.ts",
9094
"${workspaceFolder}/packages/experimental-utils/dist/ts-estree.js",
95+
"${workspaceFolder}/packages/type-utils/src/ts-estree.ts",
96+
"${workspaceFolder}/packages/type-utils/dist/ts-estree.js",
9197
"${workspaceFolder}/packages/parser/src/index.ts",
9298
"${workspaceFolder}/packages/parser/dist/index.js",
9399
"${workspaceFolder}/packages/typescript-estree/src/index.ts",
@@ -120,6 +126,8 @@
120126
"${workspaceFolder}/packages/experimental-utils/dist/index.js",
121127
"${workspaceFolder}/packages/experimental-utils/src/ts-estree.ts",
122128
"${workspaceFolder}/packages/experimental-utils/dist/ts-estree.js",
129+
"${workspaceFolder}/packages/type-utils/src/index.ts",
130+
"${workspaceFolder}/packages/type-utils/dist/index.js",
123131
"${workspaceFolder}/packages/parser/src/index.ts",
124132
"${workspaceFolder}/packages/parser/dist/index.js",
125133
"${workspaceFolder}/packages/typescript-estree/src/index.ts",
@@ -152,6 +160,42 @@
152160
"${workspaceFolder}/packages/experimental-utils/dist/index.js",
153161
"${workspaceFolder}/packages/experimental-utils/src/ts-estree.ts",
154162
"${workspaceFolder}/packages/experimental-utils/dist/ts-estree.js",
163+
"${workspaceFolder}/packages/type-utils/src/index.ts",
164+
"${workspaceFolder}/packages/type-utils/dist/index.js",
165+
"${workspaceFolder}/packages/parser/src/index.ts",
166+
"${workspaceFolder}/packages/parser/dist/index.js",
167+
"${workspaceFolder}/packages/typescript-estree/src/index.ts",
168+
"${workspaceFolder}/packages/typescript-estree/dist/index.js",
169+
"${workspaceFolder}/packages/types/src/index.ts",
170+
"${workspaceFolder}/packages/types/dist/index.js",
171+
"${workspaceFolder}/packages/visitor-keys/src/index.ts",
172+
"${workspaceFolder}/packages/visitor-keys/dist/index.js",
173+
"${workspaceFolder}/packages/scope-manager/dist/index.js",
174+
"${workspaceFolder}/packages/scope-manager/dist/index.js",
175+
],
176+
},
177+
{
178+
"type": "node",
179+
"request": "launch",
180+
"name": "Run currently opened type-utils test",
181+
"cwd": "${workspaceFolder}/packages/type-utils/",
182+
"program": "${workspaceFolder}/node_modules/jest/bin/jest.js",
183+
"args": [
184+
"--runInBand",
185+
"--no-cache",
186+
"--no-coverage",
187+
"${fileBasenameNoExtension}"
188+
],
189+
"sourceMaps": true,
190+
"console": "integratedTerminal",
191+
"internalConsoleOptions": "neverOpen",
192+
"skipFiles": [
193+
"${workspaceFolder}/packages/experimental-utils/src/index.ts",
194+
"${workspaceFolder}/packages/experimental-utils/dist/index.js",
195+
"${workspaceFolder}/packages/experimental-utils/src/ts-estree.ts",
196+
"${workspaceFolder}/packages/experimental-utils/dist/ts-estree.js",
197+
"${workspaceFolder}/packages/type-utils/src/index.ts",
198+
"${workspaceFolder}/packages/type-utils/dist/index.js",
155199
"${workspaceFolder}/packages/parser/src/index.ts",
156200
"${workspaceFolder}/packages/parser/dist/index.js",
157201
"${workspaceFolder}/packages/typescript-estree/src/index.ts",
@@ -184,6 +228,8 @@
184228
"${workspaceFolder}/packages/experimental-utils/dist/index.js",
185229
"${workspaceFolder}/packages/experimental-utils/src/ts-estree.ts",
186230
"${workspaceFolder}/packages/experimental-utils/dist/ts-estree.js",
231+
"${workspaceFolder}/packages/type-utils/src/index.ts",
232+
"${workspaceFolder}/packages/type-utils/dist/index.js",
187233
"${workspaceFolder}/packages/parser/src/index.ts",
188234
"${workspaceFolder}/packages/parser/dist/index.js",
189235
"${workspaceFolder}/packages/typescript-estree/src/index.ts",

package.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,6 @@
4444
"start": "nx run website:start",
4545
"test": "nx run-many --target=test --all --parallel",
4646
"test-integration": "yarn jest -c ./tests/integration/jest.config.js",
47-
"test-kill-integration-containers": "docker-compose -f tests/integration/docker-compose.yml down -v --rmi local",
4847
"typecheck": "nx run-many --target=typecheck --all --parallel"
4948
},
5049
"config": {

packages/ast-spec/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ It includes:
1616

1717
**You probably don't want to use it directly.**
1818

19-
If you're building an ESLint plugin, consider using [`@typescript-eslint/experimental-utils`](../experimental-utils).
19+
If you're building an ESLint plugin, consider using [`@typescript-eslint/experimental-utils`](../experimental-utils) and [`@typescript-eslint/type-utils`](../type-utils).
2020
If you're parsing TypeScript code, consider using [`@typescript-eslint/typescript-estree`](../typescript-estree).
2121

2222
## Contributing

packages/eslint-plugin/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@
4646
"dependencies": {
4747
"@typescript-eslint/experimental-utils": "5.8.1",
4848
"@typescript-eslint/scope-manager": "5.8.1",
49+
"@typescript-eslint/type-utils": "5.8.1",
4950
"debug": "^4.3.2",
5051
"functional-red-black-tree": "^1.0.1",
5152
"ignore": "^5.1.8",

packages/eslint-plugin/src/rules/no-unnecessary-boolean-literal-compare.ts

Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -158,7 +158,7 @@ export default util.createRule<Options, MessageIds>({
158158
function deconstructComparison(
159159
node: TSESTree.BinaryExpression,
160160
): BooleanComparison | undefined {
161-
const comparisonType = util.getEqualsKind(node.operator);
161+
const comparisonType = getEqualsKind(node.operator);
162162
if (!comparisonType) {
163163
return undefined;
164164
}
@@ -275,3 +275,39 @@ export default util.createRule<Options, MessageIds>({
275275
};
276276
},
277277
});
278+
279+
interface EqualsKind {
280+
isPositive: boolean;
281+
isStrict: boolean;
282+
}
283+
284+
function getEqualsKind(operator: string): EqualsKind | undefined {
285+
switch (operator) {
286+
case '==':
287+
return {
288+
isPositive: true,
289+
isStrict: false,
290+
};
291+
292+
case '===':
293+
return {
294+
isPositive: true,
295+
isStrict: true,
296+
};
297+
298+
case '!=':
299+
return {
300+
isPositive: false,
301+
isStrict: false,
302+
};
303+
304+
case '!==':
305+
return {
306+
isPositive: false,
307+
isStrict: true,
308+
};
309+
310+
default:
311+
return undefined;
312+
}
313+
}

packages/eslint-plugin/src/rules/no-unnecessary-condition.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,14 +17,14 @@ import {
1717
createRule,
1818
getParserServices,
1919
getConstrainedTypeAtLocation,
20+
getTypeOfPropertyOfName,
2021
isNullableType,
2122
nullThrows,
2223
NullThrowsReasons,
2324
isIdentifier,
2425
isTypeAnyType,
2526
isTypeUnknownType,
2627
getTypeName,
27-
getTypeOfPropertyOfName,
2828
} from '../util';
2929

3030
// Truthiness utilities

packages/eslint-plugin/src/util/collectUnusedVariables.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,10 @@ import {
33
TSESLint,
44
ASTUtils,
55
TSESTree,
6+
ESLintUtils,
67
} from '@typescript-eslint/experimental-utils';
78
import { ImplicitLibVariable } from '@typescript-eslint/scope-manager';
89
import { Visitor } from '@typescript-eslint/scope-manager/dist/referencer/Visitor';
9-
import { nullThrows } from './nullThrows';
1010

1111
class UnusedVarsVisitor<
1212
TMessageIds extends string,
@@ -25,7 +25,7 @@ class UnusedVarsVisitor<
2525
visitChildrenEvenIfSelectorExists: true,
2626
});
2727

28-
this.#scopeManager = nullThrows(
28+
this.#scopeManager = ESLintUtils.nullThrows(
2929
context.getSourceCode().scopeManager,
3030
'Missing required scope manager',
3131
);

packages/eslint-plugin/src/util/explicitReturnTypeUtils.ts

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
import {
22
TSESTree,
33
AST_NODE_TYPES,
4+
ESLintUtils,
45
TSESLint,
56
} from '@typescript-eslint/experimental-utils';
67
import { isTypeAssertion, isConstructor, isSetter } from './astUtils';
78
import { getFunctionHeadLoc } from './getFunctionHeadLoc';
8-
import { nullThrows, NullThrowsReasons } from './nullThrows';
99

1010
type FunctionExpression =
1111
| TSESTree.ArrowFunctionExpression
@@ -187,7 +187,10 @@ function isTypedFunctionExpression(
187187
node: FunctionExpression,
188188
options: Options,
189189
): boolean {
190-
const parent = nullThrows(node.parent, NullThrowsReasons.MissingParent);
190+
const parent = ESLintUtils.nullThrows(
191+
node.parent,
192+
ESLintUtils.NullThrowsReasons.MissingParent,
193+
);
191194

192195
if (!options.allowTypedFunctionExpressions) {
193196
return false;
@@ -215,7 +218,10 @@ function isValidFunctionExpressionReturnType(
215218
return true;
216219
}
217220

218-
const parent = nullThrows(node.parent, NullThrowsReasons.MissingParent);
221+
const parent = ESLintUtils.nullThrows(
222+
node.parent,
223+
ESLintUtils.NullThrowsReasons.MissingParent,
224+
);
219225
if (
220226
options.allowExpressions &&
221227
parent.type !== AST_NODE_TYPES.VariableDeclarator &&

packages/eslint-plugin/src/util/getESLintCoreRule.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1+
import { ESLintUtils } from '@typescript-eslint/experimental-utils';
12
import { version } from 'eslint/package.json';
23
import * as semver from 'semver';
3-
import { nullThrows } from './nullThrows';
44

55
const isESLintV8 = semver.major(version) >= 8;
66

@@ -42,7 +42,7 @@ type RuleId = keyof RuleMap;
4242
export const getESLintCoreRule: <R extends RuleId>(ruleId: R) => RuleMap[R] =
4343
isESLintV8
4444
? <R extends RuleId>(ruleId: R): RuleMap[R] =>
45-
nullThrows(
45+
ESLintUtils.nullThrows(
4646
// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access, @typescript-eslint/no-unsafe-call
4747
require('eslint/use-at-your-own-risk').builtinRules.get(
4848
ruleId,

packages/eslint-plugin/src/util/getWrappingFixer.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ import {
44
ASTUtils,
55
TSESTree,
66
} from '@typescript-eslint/experimental-utils';
7-
import { SourceCode } from '@typescript-eslint/experimental-utils/src/ts-eslint';
87

98
interface WrappingFixerParams {
109
/** Source code. */
@@ -135,7 +134,7 @@ function isWeakPrecedenceParent(node: TSESTree.Node): boolean {
135134
*/
136135
function isMissingSemicolonBefore(
137136
node: TSESTree.Node,
138-
sourceCode: SourceCode,
137+
sourceCode: TSESLint.SourceCode,
139138
): boolean {
140139
for (;;) {
141140
const parent = node.parent!;

0 commit comments

Comments
 (0)