10000 feat!: remove `TSParenthesizedType` · sonallux/typescript-eslint@f66458b · GitHub
[go: up one dir, main page]

Skip to content

Commit f66458b

Browse files
committed
feat!: remove TSParenthesizedType
fixes typescript-eslint#3136
1 parent 299d86f commit f66458b

24 files changed

+169
-299
lines changed

packages/ast-spec/src/ast-node-types.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,6 @@ export enum AST_NODE_TYPES {
134134
TSObjectKeyword = 'TSObjectKeyword',
135135
TSOptionalType = 'TSOptionalType',
136136
TSParameterProperty = 'TSParameterProperty',
137-
TSParenthesizedType = 'TSParenthesizedType',
138137
TSPrivateKeyword = 'TSPrivateKeyword',
139138
TSPropertySignature = 'TSPropertySignature',
140139
TSProtectedKeyword = 'TSProtectedKeyword',

packages/ast-spec/src/type/TSParenthesizedType/spec.ts

Lines changed: 0 additions & 8 deletions
This file was deleted.

packages/ast-spec/src/type/spec.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@ export * from './TSNullKeyword/spec';
1717
export * from './TSNumberKeyword/spec';
1818
export * from './TSObjectKeyword/spec';
1919
export * from './TSOptionalType/spec';
20-
export * from './TSParenthesizedType/spec';
2120
export * from './TSQualifiedName/spec';
2221
export * from './TSRestType/spec';
2322
export * from './TSStringKeyword/spec';

packages/ast-spec/src/unions/Node.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,6 @@ import type { TSNullKeyword } from '../type/TSNullKeyword/spec';
141141
import type { TSNumberKeyword } from '../type/TSNumberKeyword/spec';
142142
import type { TSObjectKeyword } from '../type/TSObjectKeyword/spec';
143143
import type { TSOptionalType } from '../type/TSOptionalType/spec';
144-
import type { TSParenthesizedType } from '../type/TSParenthesizedType/spec';
145144
import type { TSQualifiedName } from '../type/TSQualifiedName/spec';
146145
import type { TSRestType } from '../type/TSRestType/spec';
147146
import type { TSStringKeyword } from '../type/TSStringKeyword/spec';
@@ -291,7 +290,6 @@ export type Node =
291290
| TSObjectKeyword
292291
| TSOptionalType
293292
| TSParameterProperty
294-
| TSParenthesizedType
295293
| TSPrivateKeyword
296294
| TSPropertySignature
297295
| TSProtectedKeyword

packages/ast-spec/src/unions/TypeNode.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ import type { TSNullKeyword } from '../type/TSNullKeyword/spec';
1818
import type { TSNumberKeyword } from '../type/TSNumberKeyword/spec';
1919
import type { TSObjectKeyword } from '../type/TSObjectKeyword/spec';
2020
import type { TSOptionalType } from '../type/TSOptionalType/spec';
21-
import type { TSParenthesizedType } from '../type/TSParenthesizedType/spec';
2221
import type { TSRestType } from '../type/TSRestType/spec';
2322
import type { TSStringKeyword } from '../type/TSStringKeyword/spec';
2423
import type { TSSymbolKeyword } from '../type/TSSymbolKeyword/spec';
@@ -56,7 +55,6 @@ export type TypeNode =
5655
| TSNumberKeyword
5756
| TSObjectKeyword
5857
| TSOptionalType
59-
| TSParenthesizedType
6058
| TSRestType
6159
| TSStringKeyword
6260
| TSSymbolKeyword

packages/eslint-plugin/src/rules/array-type.ts

Lines changed: 8 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -132,13 +132,8 @@ export default util.createRule<Options, MessageIds>({
132132
* @param node the node to be evaluated.
133133
*/
134134
function getMessageType(node: TSESTree.Node): string {
135-
if (node) {
136-
if (node.type === AST_NODE_TYPES.TSParenthesizedType) {
137-
return getMessageType(node.typeAnnotation);
138-
}
139-
if (isSimpleType(node)) {
140-
return sourceCode.getText(node);
141-
}
135+
if (node && isSimpleType(node)) {
136+
return sourceCode.getText(node);
142137
}
143138
return 'T';
144139
}
@@ -172,11 +167,7 @@ export default util.createRule<Options, MessageIds>({
172167
type: getMessageType(node.elementType),
173168
},
174169
fix(fixer) {
175-
const typeNode =
176-
node.elementType.type === AST_NODE_TYPES.TSParenthesizedType
177-
? node.elementType.typeAnnotation
178-
: node.elementType;
179-
170+
const typeNode = node.elementType;
180171
const arrayType = isReadonly ? 'ReadonlyArray' : 'Array';
181172

182173
return [
@@ -244,9 +235,12 @@ export default util.createRule<Options, MessageIds>({
244235
}
245236

246237
const type = typeParams[0];
247-
const typeParens = typeNeedsParentheses(type);
238+
const typeParens =
239+
!util.isParenthesized(type, sourceCode) && typeNeedsParentheses(type);
248240
const parentParens =
249-
readonlyPrefix && node.parent?.type === AST_NODE_TYPES.TSArrayType;
241+
readonlyPrefix &&
242+
node.parent?.type === AST_NODE_TYPES.TSArrayType &&
243+
!util.isParenthesized(node.parent.elementType, sourceCode);
250244

251245
const start = `${parentParens ? '(' : ''}${readonlyPrefix}${
252246
typeParens ? '(' : ''

packages/eslint-plugin/src/rules/indent-new-do-not-use/index.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -162,7 +162,6 @@ const KNOWN_NODES = new Set([
162162
AST_NODE_TYPES.TSModuleDeclaration,
163163
AST_NODE_TYPES.TSNonNullExpression,
164164
AST_NODE_TYPES.TSParameterProperty,
165-
AST_NODE_TYPES.TSParenthesizedType,
166165
'TSPlusToken',
167166
AST_NODE_TYPES.TSPropertySignature,
168167
AST_NODE_TYPES.TSQualifiedName,

packages/eslint-plugin/src/rules/indent.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,6 @@ const KNOWN_NODES = new Set([
6565
AST_NODE_TYPES.TSModuleDeclaration,
6666
AST_NODE_TYPES.TSNonNullExpression,
6767
AST_NODE_TYPES.TSParameterProperty,
68-
AST_NODE_TYPES.TSParenthesizedType,
6968
'TSPlusToken',
7069
AST_NODE_TYPES.TSPropertySignature,
7170
AST_NODE_TYPES.TSQualifiedName,

packages/eslint-plugin/src/rules/no-extra-parens.ts

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -81,9 +81,7 @@ export default util.createRule<Options, MessageIds>({
8181
if (
8282
node.arguments.length === 1 &&
8383
node.typeParameters?.params.some(
84-
param =>
85-
param.type === AST_NODE_TYPES.TSParenthesizedType ||
86-
param.type === AST_NODE_TYPES.TSImportType,
84+
param => param.type === AST_NODE_TYPES.TSImportType,
8785
)
8886
) {
8987
retur F438 n rule({

packages/eslint-plugin/src/rules/no-type-alias.ts

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -293,9 +293,6 @@ export default util.createRule<Options, MessageIds>({
293293
return acc;
294294
}, []);
295295
}
296-
if (node.type === AST_NODE_TYPES.TSParenthesizedType) {
297-
return getTypes(node.typeAnnotation, compositionType);
298-
}
299296
return [{ node, compositionType }];
300297
}
301298

packages/eslint-plugin/src/rules/sort-type-union-intersection-members.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,6 @@ enum Group {
2323

2424
function getGroup(node: TSESTree.TypeNode): Group {
2525
switch (node.type) {
26-
case AST_NODE_TYPES.TSParenthesizedType:
27-
return getGroup(node.typeAnnotation);
28-
2926
case AST_NODE_TYPES.TSConditionalType:
3027
return Group.conditional;
3128

@@ -91,6 +88,10 @@ function getGroup(node: TSESTree.TypeNode): Group {
9188
}
9289
}
9390

91+
function requiresParentheses(node: TSESTree.TypeNode): boolean {
92+
return node.type === AST_NODE_TYPES.TSFunctionType;
93+
}
94+
9495
export type Options = [
9596
{
9697
checkIntersections?: boolean;
@@ -211,7 +212,7 @@ export default util.createRule<Options, MessageIds>({
211212

212213
const fix: TSESLint.ReportFixFunction = fixer => {
213214
const sorted = expectedOrder
214-
.map(t => t.text)
215+
.map(t => (requiresParentheses(t.node) ? `(${t.text})` : t.text))
215216
.join(
216217
node.type === AST_NODE_TYPES.TSIntersectionType ? ' & ' : ' | ',
217218
);

packages/eslint-plugin/tests/rules/indent/indent.test.ts

Lines changed: 0 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -451,29 +451,6 @@ class Foo {
451451
`,
452452
],
453453
},
454-
{
455-
node: AST_NODE_TYPES.TSParenthesizedType,
456-
code: [
457-
`
458-
const x: Array<(
459-
| {
460-
__typename: "Foo",
461-
}
462-
| {
463-
__typename: "Baz",
464-
}
465-
| (
466-
| {
467-
__typename: "Baz",
468-
}
469-
| {
470-
__typename: "Buzz",
471-
}
472-
)
473-
)>;
474-
`,
475-
],
476-
},
477454
// TSPlusToken - tested in TSMappedType
478455
{
479456
node: AST_NODE_TYPES.TSPropertySignature,

packages/eslint-plugin/tests/rules/no-extra-parens.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ for (a in b, c);
2828
for (a in b);
2929
a<import('')>(1);
3030
new a<import('')>(1);
31-
a<(A)>(1);
31+
a<A>(1);
3232
`,
3333
}),
3434
...batchedSingleLineTests({

packages/eslint-plugin/tests/rules/sort-type-union-intersection-members.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,7 @@ const invalid = (
127127
},
128128
{
129129
code: noFormat`type T = (B) ${operator} (A);`,
130-
output: noFormat`type T = (A) ${operator} (B);`,
130+
output: noFormat`type T = A ${operator} B;`,
131131
errors: [
132132
{
133133
messageId: 'notSortedNamed',

packages/experimental-utils/src/ts-eslint/Rule.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -379,7 +379,6 @@ interface RuleListener {
379379
TSObjectKeyword?: RuleFunction<TSESTree.TSObjectKeyword>;
380380
TSOptionalType?: RuleFunction<TSESTree.TSOptionalType>;
381381
TSParameterProperty?: RuleFunction<TSESTree.TSParameterProperty>;
382-
TSParenthesizedType?: RuleFunction<TSESTree.TSParenthesizedType>;
383382
TSPrivateKeyword?: RuleFunction<TSESTree.TSPrivateKeyword>;
384383
TSPropertySignature?: RuleFunction<TSESTree.TSPropertySignature>;
385384
TSProtectedKeyword?: RuleFunction<TSESTree.TSProtectedKeyword>;

packages/typescript-estree/src/convert.ts

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2628,10 +2628,7 @@ export class Converter {
26282628

26292629
// TypeScript specific types
26302630
case SyntaxKind.ParenthesizedType: {
2631-
return this.createNode<TSESTree.TSParenthesizedType>(node, {
2632-
type: AST_NODE_TYPES.TSParenthesizedType,
2633-
typeAnnotation: this.convertType(node.type),
2634-
});
2631+
return this.convertType(node.type);
26352632
}
26362633
case SyntaxKind.UnionType: {
26372634
return this.createNode<TSESTree.TSUnionType>(node, {

packages/typescript-estree/src/ts-estree/estree-to-ts-node-types.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -188,7 +188,6 @@ export interface EstreeToTsNodeTypes {
188188
[AST_NODE_TYPES.TSNonNullExpression]: ts.NonNullExpression;
189189
[AST_NODE_TYPES.TSOptionalType]: ts.OptionalTypeNode;
190190
[AST_NODE_TYPES.TSParameterProperty]: ts.ParameterDeclaration;
191-
[AST_NODE_TYPES.TSParenthesizedType]: ts.ParenthesizedTypeNode;
192191
[AST_NODE_TYPES.TSPropertySignature]: ts.PropertySignature;
193192
[AST_NODE_TYPES.TSQualifiedName]: ts.QualifiedName;
194193
[AST_NODE_TYPES.TSRestType]:

packages/typescript-estree/tests/ast-alignment/utils.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -241,6 +241,16 @@ export function preprocessBabylonAST(ast: BabelTypes.File): any {
241241
}
242242
}
243243
},
244+
/**
245+
* Remove TSParenthesizedType from babel AST. Babel 8 will stop generating the TSParenthesizedType.
246+
* Once we use babel 8, this can be removed.
247+
* @see https://github.com/babel/babel/pull/12608
248+
*/
249+
TSParenthesizedType(node: any) {
250+
const { typeAnnotation } = node;
251+
Object.keys(node).forEach(key => delete node[key]);
252+
Object.assign(node, typeAnnotation);
253+
},
244254
},
245255
);
246256
}

packages/typescript-estree/tests/snapshots/typescript/types/conditional-infer-nested.src.ts.shot

Lines changed: 17 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -78,38 +78,33 @@ Object {
7878
"elementType": Object {
7979
"loc": Object {
8080
"end": Object {
81-
"column": 21,
81+
"column": 20,
8282
"line": 2,
8383
},
8484
"start": Object {
85-
"column": 12,
85+
"column": 13,
8686
"line": 2,
8787
},
8888
},
8989
"range": Array [
90-
31,
91-
40,
90+
32,
91+
39,
9292
],
93-
"type": "TSParenthesizedType",
94-
"typeAnnotation": Object {
93+
"type": "TSInferType",
94+
"typeParameter": Object {
95+
"constraint": undefined,
96+
"default": undefined,
9597
"loc": Object {
9698
"end": Object {
9799
"column": 20,
98100
"line": 2,
99101
},
100102
"start": Object {
101-
"column": 13,
103+
"column": 19,
102104
"line": 2,
103105
},
104106
},
105-
"range": Array [
106-
32,
107-
39,
108-
],
109-
"type": "TSInferType",
110-
"typeParameter": Object {
111-
"constraint": undefined,
112-
"default": undefined,
107+
"name": Object {
113108
"loc": Object {
114109
"end": Object {
115110
"column": 20,
@@ -120,30 +115,18 @@ Object {
120115
"line": 2,
121116
},
122117
},
123-
"name": Object {
124-
"loc": Object {
125-
"end": Object {
126-
"column": 20,
127-
"line": 2,
128-
},
129-
"start": Object {
130-
"column": 19,
131-
"line": 2,
132-
},
133-
},
134-
"name": "U",
135-
"range": Array [
136-
38,
137-
39,
138-
],
139-
"type": "Identifier",
140-
},
118+
"name": "U",
141119
"range": Array [
142120
38,
143121
39,
144122
],
145-
"type": "TSTypeParameter",
123+
"type": "Identifier",
146124
},
125+
"range": Array [
126+
38,
127+
39,
128+
],
129+
"type": "TSTypeParameter",
147130
},
148131
},
149132
"loc": Object {

0 commit comments

Comments
 (0)
0