Skip to content

Commit

Permalink
feat!: remove TSParenthesizedType
Browse files Browse the repository at this point in the history
  • Loading branch information
sonallux committed May 3, 2021
1 parent 1c1b572 commit f11b510
Show file tree
Hide file tree
Showing 21 changed files with 169 additions and 293 deletions.
22 changes: 8 additions & 14 deletions packages/eslint-plugin/src/rules/array-type.ts
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,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
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,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
Original file line number Diff line number Diff line change
Expand Up @@ -81,9 +81,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
Original file line number Diff line number Diff line change
Expand Up @@ -293,9 +293,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
Original file line number Diff line number Diff line change
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 @@ -92,6 +89,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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
Expand Up @@ -371,7 +371,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
1 change: 0 additions & 1 deletion packages/types/src/ast-node-types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,6 @@ enum AST_NODE_TYPES {
TSObjectKeyword = 'TSObjectKeyword',
TSOptionalType = 'TSOptionalType',
TSParameterProperty = 'TSParameterProperty',
TSParenthesizedType = 'TSParenthesizedType',
TSPrivateKeyword = 'TSPrivateKeyword',
TSPropertySignature = 'TSPropertySignature',
TSProtectedKeyword = 'TSProtectedKeyword',
Expand Down
7 changes: 0 additions & 7 deletions packages/types/src/ts-estree.ts
Original file line number Diff line number Diff line change
Expand Up @@ -265,7 +265,6 @@ export type Node =
| TSObjectKeyword
| TSOptionalType
| TSParameterProperty
| TSParenthesizedType
| TSPrivateKeyword
| TSPropertySignature
| TSProtectedKeyword
Expand Down Expand Up @@ -556,7 +555,6 @@ export type TypeNode =
| TSNumberKeyword
| TSObjectKeyword
| TSOptionalType
| TSParenthesizedType
| TSRestType
| TSStringKeyword
| TSSymbolKeyword
Expand Down Expand Up @@ -1572,11 +1570,6 @@ export interface TSParameterProperty extends BaseNode {
decorators?: Decorator[];
}

export interface TSParenthesizedType extends BaseNode {
type: AST_NODE_TYPES.TSParenthesizedType;
typeAnnotation: TypeNode;
}

export interface TSPropertySignatureComputedName
extends TSPropertySignatureBase {
key: PropertyNameComputed;
Expand Down
5 changes: 1 addition & 4 deletions packages/typescript-estree/src/convert.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2628,10 +2628,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
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,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
Original file line number Diff line number Diff line change
Expand Up @@ -241,6 +241,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
Original file line number Diff line number Diff line change
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 f11b510

Please sign in to comment.