Skip to content

Commit

Permalink
feat: remove TSParenthesizedType (#3340)
Browse files Browse the repository at this point in the history
fixes #3136

Co-authored-by: Brad Zacher <brad.zacher@gmail.com>
  • Loading branch information
sonallux and bradzacher committed Oct 11, 2021
1 parent 3034181 commit c8ee432
Show file tree
Hide file tree
Showing 24 changed files with 169 additions and 299 deletions.
1 change: 0 additions & 1 deletion packages/ast-spec/src/ast-node-types.ts
Expand Up @@ -135,7 +135,6 @@ export enum AST_NODE_TYPES {
TSObjectKeyword = 'TSObjectKeyword',
TSOptionalType = 'TSOptionalType',
TSParameterProperty = 'TSParameterProperty',
TSParenthesizedType = 'TSParenthesizedType',
TSPrivateKeyword = 'TSPrivateKeyword',
TSPropertySignature = 'TSPropertySignature',
TSProtectedKeyword = 'TSProtectedKeyword',
Expand Down
8 changes: 0 additions & 8 deletions packages/ast-spec/src/type/TSParenthesizedType/spec.ts

This file was deleted.

1 change: 0 additions & 1 deletion packages/ast-spec/src/type/spec.ts
Expand Up @@ -17,7 +17,6 @@ export * from './TSNullKeyword/spec';
export * from './TSNumberKeyword/spec';
export * from './TSObjectKeyword/spec';
export * from './TSOptionalType/spec';
export * from './TSParenthesizedType/spec';
export * from './TSQualifiedName/spec';
export * from './TSRestType/spec';
export * from './TSStringKeyword/spec';
Expand Down
2 changes: 0 additions & 2 deletions packages/ast-spec/src/unions/Node.ts
Expand Up @@ -142,7 +142,6 @@ import type { TSNullKeyword } from '../type/TSNullKeyword/spec';
import type { TSNumberKeyword } from '../type/TSNumberKeyword/spec';
import type { TSObjectKeyword } from '../type/TSObjectKeyword/spec';
import type { TSOptionalType } from '../type/TSOptionalType/spec';
import type { TSParenthesizedType } from '../type/TSParenthesizedType/spec';
import type { TSQualifiedName } from '../type/TSQualifiedName/spec';
import type { TSRestType } from '../type/TSRestType/spec';
import type { TSStringKeyword } from '../type/TSStringKeyword/spec';
Expand Down Expand Up @@ -293,7 +292,6 @@ export type Node =
| TSObjectKeyword
| TSOptionalType
| TSParameterProperty
| TSParenthesizedType
| TSPrivateKeyword
| TSPropertySignature
| TSProtectedKeyword
Expand Down
2 changes: 0 additions & 2 deletions packages/ast-spec/src/unions/TypeNode.ts
Expand Up @@ -18,7 +18,6 @@ import type { TSNullKeyword } from '../type/TSNullKeyword/spec';
import type { TSNumberKeyword } from '../type/TSNumberKeyword/spec';
import type { TSObjectKeyword } from '../type/TSObjectKeyword/spec';
import type { TSOptionalType } from '../type/TSOptionalType/spec';
import type { TSParenthesizedType } from '../type/TSParenthesizedType/spec';
import type { TSRestType } from '../type/TSRestType/spec';
import type { TSStringKeyword } from '../type/TSStringKeyword/spec';
import type { TSSymbolKeyword } from '../type/TSSymbolKeyword/spec';
Expand Down Expand Up @@ -56,7 +55,6 @@ export type TypeNode =
| TSNumberKeyword
| TSObjectKeyword
| TSOptionalType
| TSParenthesizedType
| TSRestType
| TSStringKeyword
| TSSymbolKeyword
Expand Down
22 changes: 8 additions & 14 deletions packages/eslint-plugin/src/rules/array-type.ts
Expand Up @@ -132,13 +132,8 @@ export default util.createRule<Options, MessageIds>({
* @param node the node to be evaluated.
*/
function getMessageType(node: TSESTree.Node): string {
if (node) {
if (node.type === AST_NODE_TYPES.TSParenthesizedType) {
return getMessageType(node.typeAnnotation);
}
if (isSimpleType(node)) {
return sourceCode.getText(node);
}
if (node && isSimpleType(node)) {
return sourceCode.getText(node);
}
return 'T';
}
Expand Down Expand Up @@ -172,11 +167,7 @@ export default util.createRule<Options, MessageIds>({
type: getMessageType(node.elementType),
},
fix(fixer) {
const typeNode =
node.elementType.type === AST_NODE_TYPES.TSParenthesizedType
? node.elementType.typeAnnotation
: node.elementType;

const typeNode = node.elementType;
const arrayType = isReadonly ? 'ReadonlyArray' : 'Array';

return [
Expand Down Expand Up @@ -244,9 +235,12 @@ export default util.createRule<Options, MessageIds>({
}

const type = typeParams[0];
const typeParens = typeNeedsParentheses(type);
const typeParens =
!util.isParenthesized(type, sourceCode) && typeNeedsParentheses(type);
const parentParens =
readonlyPrefix && node.parent?.type === AST_NODE_TYPES.TSArrayType;
readonlyPrefix &&
node.parent?.type === AST_NODE_TYPES.TSArrayType &&
!util.isParenthesized(node.parent.elementType, sourceCode);

const start = `${parentParens ? '(' : ''}${readonlyPrefix}${
typeParens ? '(' : ''
Expand Down
Expand Up @@ -161,7 +161,6 @@ const KNOWN_NODES = new Set([
AST_NODE_TYPES.TSModuleDeclaration,
AST_NODE_TYPES.TSNonNullExpression,
AST_NODE_TYPES.TSParameterProperty,
AST_NODE_TYPES.TSParenthesizedType,
'TSPlusToken',
AST_NODE_TYPES.TSPropertySignature,
AST_NODE_TYPES.TSQualifiedName,
Expand Down
1 change: 0 additions & 1 deletion packages/eslint-plugin/src/rules/indent.ts
Expand Up @@ -67,7 +67,6 @@ const KNOWN_NODES = new Set([
AST_NODE_TYPES.TSModuleDeclaration,
AST_NODE_TYPES.TSNonNullExpression,
AST_NODE_TYPES.TSParameterProperty,
AST_NODE_TYPES.TSParenthesizedType,
'TSPlusToken',
AST_NODE_TYPES.TSPropertySignature,
AST_NODE_TYPES.TSQualifiedName,
Expand Down
4 changes: 1 addition & 3 deletions packages/eslint-plugin/src/rules/no-extra-parens.ts
Expand Up @@ -84,9 +84,7 @@ export default util.createRule<Options, MessageIds>({
if (
node.arguments.length === 1 &&
node.typeParameters?.params.some(
param =>
param.type === AST_NODE_TYPES.TSParenthesizedType ||
param.type === AST_NODE_TYPES.TSImportType,
param => param.type === AST_NODE_TYPES.TSImportType,
)
) {
return rule({
Expand Down
3 changes: 0 additions & 3 deletions packages/eslint-plugin/src/rules/no-type-alias.ts
Expand Up @@ -311,9 +311,6 @@ export default util.createRule<Options, MessageIds>({
return acc;
}, []);
}
if (node.type === AST_NODE_TYPES.TSParenthesizedType) {
return getTypes(node.typeAnnotation, compositionType);
}
return [{ node, compositionType }];
}

Expand Down
Expand Up @@ -23,9 +23,6 @@ enum Group {

function getGroup(node: TSESTree.TypeNode): Group {
switch (node.type) {
case AST_NODE_TYPES.TSParenthesizedType:
return getGroup(node.typeAnnotation);

case AST_NODE_TYPES.TSConditionalType:
return Group.conditional;

Expand Down Expand Up @@ -91,6 +88,10 @@ function getGroup(node: TSESTree.TypeNode): Group {
}
}

function requiresParentheses(node: TSESTree.TypeNode): boolean {
return node.type === AST_NODE_TYPES.TSFunctionType;
}

export type Options = [
{
checkIntersections?: boolean;
Expand Down Expand Up @@ -212,7 +213,7 @@ export default util.createRule<Options, MessageIds>({

const fix: TSESLint.ReportFixFunction = fixer => {
const sorted = expectedOrder
.map(t => t.text)
.map(t => (requiresParentheses(t.node) ? `(${t.text})` : t.text))
.join(
node.type === AST_NODE_TYPES.TSIntersectionType ? ' & ' : ' | ',
);
Expand Down
23 changes: 0 additions & 23 deletions packages/eslint-plugin/tests/rules/indent/indent.test.ts
Expand Up @@ -451,29 +451,6 @@ class Foo {
`,
],
},
{
node: AST_NODE_TYPES.TSParenthesizedType,
code: [
`
const x: Array<(
| {
__typename: "Foo",
}
| {
__typename: "Baz",
}
| (
| {
__typename: "Baz",
}
| {
__typename: "Buzz",
}
)
)>;
`,
],
},
// TSPlusToken - tested in TSMappedType
{
node: AST_NODE_TYPES.TSPropertySignature,
Expand Down
2 changes: 1 addition & 1 deletion packages/eslint-plugin/tests/rules/no-extra-parens.test.ts
Expand Up @@ -28,7 +28,7 @@ for (a in b, c);
for (a in b);
a<import('')>(1);
new a<import('')>(1);
a<(A)>(1);
a<A>(1);
`,
}),
...batchedSingleLineTests({
Expand Down
Expand Up @@ -127,7 +127,7 @@ const invalid = (
},
{
code: noFormat`type T = (B) ${operator} (A);`,
output: noFormat`type T = (A) ${operator} (B);`,
output: noFormat`type T = A ${operator} B;`,
errors: [
{
messageId: 'notSortedNamed',
Expand Down
1 change: 0 additions & 1 deletion packages/experimental-utils/src/ts-eslint/Rule.ts
Expand Up @@ -399,7 +399,6 @@ interface RuleListener {
TSObjectKeyword?: RuleFunction<TSESTree.TSObjectKeyword>;
TSOptionalType?: RuleFunction<TSESTree.TSOptionalType>;
TSParameterProperty?: RuleFunction<TSESTree.TSParameterProperty>;
TSParenthesizedType?: RuleFunction<TSESTree.TSParenthesizedType>;
TSPrivateKeyword?: RuleFunction<TSESTree.TSPrivateKeyword>;
TSPropertySignature?: RuleFunction<TSESTree.TSPropertySignature>;
TSProtectedKeyword?: RuleFunction<TSESTree.TSProtectedKeyword>;
Expand Down
5 changes: 1 addition & 4 deletions packages/typescript-estree/src/convert.ts
Expand Up @@ -2698,10 +2698,7 @@ export class Converter {

// TypeScript specific types
case SyntaxKind.ParenthesizedType: {
return this.createNode<TSESTree.TSParenthesizedType>(node, {
type: AST_NODE_TYPES.TSParenthesizedType,
typeAnnotation: this.convertType(node.type),
});
return this.convertType(node.type);
}
case SyntaxKind.UnionType: {
return this.createNode<TSESTree.TSUnionType>(node, {
Expand Down
Expand Up @@ -192,7 +192,6 @@ export interface EstreeToTsNodeTypes {
[AST_NODE_TYPES.TSNonNullExpression]: ts.NonNullExpression;
[AST_NODE_TYPES.TSOptionalType]: ts.OptionalTypeNode;
[AST_NODE_TYPES.TSParameterProperty]: ts.ParameterDeclaration;
[AST_NODE_TYPES.TSParenthesizedType]: ts.ParenthesizedTypeNode;
[AST_NODE_TYPES.TSPropertySignature]: ts.PropertySignature;
[AST_NODE_TYPES.TSQualifiedName]: ts.QualifiedName;
[AST_NODE_TYPES.TSRestType]:
Expand Down
10 changes: 10 additions & 0 deletions packages/typescript-estree/tests/ast-alignment/utils.ts
Expand Up @@ -250,6 +250,16 @@ export function preprocessBabylonAST(ast: BabelTypes.File): any {
}
}
},
/**
* Remove TSParenthesizedType from babel AST. Babel 8 will stop generating the TSParenthesizedType.
* Once we use babel 8, this can be removed.
* @see https://github.com/babel/babel/pull/12608
*/
TSParenthesizedType(node: any) {
const { typeAnnotation } = node;
Object.keys(node).forEach(key => delete node[key]);
Object.assign(node, typeAnnotation);
},
},
);
}
Expand Down
Expand Up @@ -78,38 +78,33 @@ Object {
"elementType": Object {
"loc": Object {
"end": Object {
"column": 21,
"column": 20,
"line": 2,
},
"start": Object {
"column": 12,
"column": 13,
"line": 2,
},
},
"range": Array [
31,
40,
32,
39,
],
"type": "TSParenthesizedType",
"typeAnnotation": Object {
"type": "TSInferType",
"typeParameter": Object {
"constraint": undefined,
"default": undefined,
"loc": Object {
"end": Object {
"column": 20,
"line": 2,
},
"start": Object {
"column": 13,
"column": 19,
"line": 2,
},
},
"range": Array [
32,
39,
],
"type": "TSInferType",
"typeParameter": Object {
"constraint": undefined,
"default": undefined,
"name": Object {
"loc": Object {
"end": Object {
"column": 20,
Expand All @@ -120,30 +115,18 @@ Object {
"line": 2,
},
},
"name": Object {
"loc": Object {
"end": Object {
"column": 20,
"line": 2,
},
"start": Object {
"column": 19,
"line": 2,
},
},
"name": "U",
"range": Array [
38,
39,
],
"type": "Identifier",
},
"name": "U",
"range": Array [
38,
39,
],
"type": "TSTypeParameter",
"type": "Identifier",
},
"range": Array [
38,
39,
],
"type": "TSTypeParameter",
},
},
"loc": Object {
Expand Down

0 comments on commit c8ee432

Please sign in to comment.