From 4528b8d38bf671d580a105640dcc2fcf71260cea Mon Sep 17 00:00:00 2001 From: Brad Zacher Date: Wed, 25 Jan 2023 13:57:37 +1030 Subject: [PATCH] chore(typescript-estree): remove unnecessary this.inTypeMode field (#6375) --- packages/typescript-estree/src/convert.ts | 94 +++++++++-------------- 1 file changed, 36 insertions(+), 58 deletions(-) diff --git a/packages/typescript-estree/src/convert.ts b/packages/typescript-estree/src/convert.ts index 4af9163c34e..b102ca325b0 100644 --- a/packages/typescript-estree/src/convert.ts +++ b/packages/typescript-estree/src/convert.ts @@ -68,7 +68,6 @@ export class Converter { private readonly tsNodeToESTreeNodeMap = new WeakMap(); private allowPattern = false; - private inTypeMode = false; /** * Converts a TypeScript node into an ESTree node @@ -96,14 +95,12 @@ export class Converter { * Converts a TypeScript node into an ESTree node. * @param node the child ts.Node * @param parent parentNode - * @param inTypeMode flag to determine if we are in typeMode * @param allowPattern flag to determine if patterns are allowed * @returns the converted ESTree node */ private converter( node?: ts.Node, parent?: ts.Node, - inTypeMode?: boolean, allowPattern?: boolean, ): any { /** @@ -113,11 +110,7 @@ export class Converter { return null; } - const typeMode = this.inTypeMode; const pattern = this.allowPattern; - if (inTypeMode !== undefined) { - this.inTypeMode = inTypeMode; - } if (allowPattern !== undefined) { this.allowPattern = allowPattern; } @@ -129,7 +122,6 @@ export class Converter { this.registerTSNodeInNodeMap(node, result); - this.inTypeMode = typeMode; this.allowPattern = pattern; return result; } @@ -225,7 +217,7 @@ export class Converter { * @returns the converted ESTree node */ private convertPattern(child?: ts.Node, parent?: ts.Node): any | null { - return this.converter(child, parent, this.inTypeMode, true); + return this.converter(child, parent, true); } /** @@ -235,17 +227,7 @@ export class Converter { * @returns the converted ESTree node */ private convertChild(child?: ts.Node, parent?: ts.Node): any | null { - return this.converter(child, parent, this.inTypeMode, false); - } - - /** - * Converts a TypeScript node into an ESTree node. - * @param child the child ts.Node - * @param parent parentNode - * @returns the converted ESTree node - */ - private convertType(child?: ts.Node, parent?: ts.Node): any | null { - return this.converter(child, parent, true, false); + return this.converter(child, parent, false); } private createNode( @@ -306,7 +288,7 @@ export class Converter { type: AST_NODE_TYPES.TSTypeAnnotation, loc, range: [annotationStartCol, child.end], - typeAnnotation: this.convertType(child), + typeAnnotation: this.convertChild(child), } as TSESTree.TSTypeAnnotation; } @@ -365,7 +347,9 @@ export class Converter { return this.createNode(node, { type: AST_NODE_TYPES.TSTypeParameterInstantiation, range: [typeArguments.pos - 1, greaterThanToken.end], - params: typeArguments.map(typeArgument => this.convertType(typeArgument)), + params: typeArguments.map(typeArgument => + this.convertChild(typeArgument), + ), }); } @@ -384,7 +368,7 @@ export class Converter { range: [typeParameters.pos - 1, greaterThanToken.end], loc: getLocFor(typeParameters.pos - 1, greaterThanToken.end, this.ast), params: typeParameters.map(typeParameter => - this.convertType(typeParameter), + this.convertChild(typeParameter), ), } as TSESTree.TSTypeParameterDeclaration; } @@ -1062,12 +1046,7 @@ export class Converter { return this.createNode(node, { type: AST_NODE_TYPES.Property, key: this.convertChild(node.name), - value: this.converter( - node.initializer, - node, - this.inTypeMode, - this.allowPattern, - ), + value: this.converter(node.initializer, node, this.allowPattern), computed: isComputedProperty(node.name), method: false, shorthand: false, @@ -1962,7 +1941,6 @@ export class Converter { left: this.converter( node.left, node, - this.inTypeMode, type === AST_NODE_TYPES.AssignmentExpression, ), right: this.convertChild(node.right), @@ -2320,7 +2298,7 @@ export class Converter { case SyntaxKind.TypeReference: { return this.createNode(node, { type: AST_NODE_TYPES.TSTypeReference, - typeName: this.convertType(node.typeName), + typeName: this.convertChild(node.typeName), typeParameters: node.typeArguments ? this.convertTypeArgumentsToTypeParameters( node.typeArguments, @@ -2333,11 +2311,11 @@ export class Converter { case SyntaxKind.TypeParameter: { return this.createNode(node, { type: AST_NODE_TYPES.TSTypeParameter, - name: this.convertType(node.name), + name: this.convertChild(node.name), constraint: node.constraint - ? this.convertType(node.constraint) + ? this.convertChild(node.constraint) : undefined, - default: node.default ? this.convertType(node.default) : undefined, + default: node.default ? this.convertChild(node.default) : undefined, in: hasModifier(SyntaxKind.InKeyword, node), out: hasModifier(SyntaxKind.OutKeyword, node), }); @@ -2384,32 +2362,32 @@ export class Converter { case SyntaxKind.ArrayType: { return this.createNode(node, { type: AST_NODE_TYPES.TSArrayType, - elementType: this.convertType(node.elementType), + elementType: this.convertChild(node.elementType), }); } case SyntaxKind.IndexedAccessType: { return this.createNode(node, { type: AST_NODE_TYPES.TSIndexedAccessType, - objectType: this.convertType(node.objectType), - indexType: this.convertType(node.indexType), + objectType: this.convertChild(node.objectType), + indexType: this.convertChild(node.indexType), }); } case SyntaxKind.ConditionalType: { return this.createNode(node, { type: AST_NODE_TYPES.TSConditionalType, - checkType: this.convertType(node.checkType), - extendsType: this.convertType(node.extendsType), - trueType: this.convertType(node.trueType), - falseType: this.convertType(node.falseType), + checkType: this.convertChild(node.checkType), + extendsType: this.convertChild(node.extendsType), + trueType: this.convertChild(node.trueType), + falseType: this.convertChild(node.falseType), }); } case SyntaxKind.TypeQuery: { return this.createNode(node, { type: AST_NODE_TYPES.TSTypeQuery, - exprName: this.convertType(node.exprName), + exprName: this.convertChild(node.exprName), typeParameters: node.typeArguments && this.convertTypeArgumentsToTypeParameters(node.typeArguments, node), @@ -2419,8 +2397,8 @@ export class Converter { case SyntaxKind.MappedType: { const result = this.createNode(node, { type: AST_NODE_TYPES.TSMappedType, - typeParameter: this.convertType(node.typeParameter), - nameType: this.convertType(node.nameType) ?? null, + typeParameter: this.convertChild(node.typeParameter), + nameType: this.convertChild(node.nameType) ?? null, }); if (node.readonlyToken) { @@ -2440,7 +2418,7 @@ export class Converter { } if (node.type) { - result.typeAnnotation = this.convertType(node.type); + result.typeAnnotation = this.convertChild(node.type); } return result; } @@ -2452,7 +2430,7 @@ export class Converter { const result = this.createNode(node, { type: AST_NODE_TYPES.TSTypeAliasDeclaration, id: this.convertChild(node.name), - typeAnnotation: this.convertType(node.type), + typeAnnotation: this.convertChild(node.type), }); if (hasModifier(SyntaxKind.DeclareKeyword, node)) { @@ -2748,31 +2726,31 @@ export class Converter { // TypeScript specific types case SyntaxKind.ParenthesizedType: { - return this.convertType(node.type); + return this.convertChild(node.type); } case SyntaxKind.UnionType: { return this.createNode(node, { type: AST_NODE_TYPES.TSUnionType, - types: node.types.map(el => this.convertType(el)), + types: node.types.map(el => this.convertChild(el)), }); } case SyntaxKind.IntersectionType: { return this.createNode(node, { type: AST_NODE_TYPES.TSIntersectionType, - types: node.types.map(el => this.convertType(el)), + types: node.types.map(el => this.convertChild(el)), }); } case SyntaxKind.AsExpression: { return this.createNode(node, { type: AST_NODE_TYPES.TSAsExpression, expression: this.convertChild(node.expression), - typeAnnotation: this.convertType(node.type), + typeAnnotation: this.convertChild(node.type), }); } case SyntaxKind.InferType: { return this.createNode(node, { type: AST_NODE_TYPES.TSInferType, - typeParameter: this.convertType(node.typeParameter), + typeParameter: this.convertChild(node.typeParameter), }); } case SyntaxKind.LiteralType: { @@ -2788,14 +2766,14 @@ export class Converter { } else { return this.createNode(node, { type: AST_NODE_TYPES.TSLiteralType, - literal: this.convertType(node.literal), + literal: this.convertChild(node.literal), }); } } case SyntaxKind.TypeAssertionExpression: { return this.createNode(node, { type: AST_NODE_TYPES.TSTypeAssertion, - typeAnnotation: this.convertType(node.type), + typeAnnotation: this.convertChild(node.type), expression: this.convertChild(node.expression), }); } @@ -2834,9 +2812,9 @@ export class Converter { const elementTypes = 'elementTypes' in node ? (node as any).elementTypes.map((el: ts.Node) => - this.convertType(el), + this.convertChild(el), ) - : node.elements.map(el => this.convertType(el)); + : node.elements.map(el => this.convertChild(el)); return this.createNode(node, { type: AST_NODE_TYPES.TSTupleType, @@ -2846,7 +2824,7 @@ export class Converter { case SyntaxKind.NamedTupleMember: { const member = this.createNode(node, { type: AST_NODE_TYPES.TSNamedTupleMember, - elementType: this.convertType(node.type, node), + elementType: this.convertChild(node.type, node), label: this.convertChild(node.name, node), optional: node.questionToken != null, }); @@ -2866,13 +2844,13 @@ export class Converter { case SyntaxKind.OptionalType: { return this.createNode(node, { type: AST_NODE_TYPES.TSOptionalType, - typeAnnotation: this.convertType(node.type), + typeAnnotation: this.convertChild(node.type), }); } case SyntaxKind.RestType: { return this.createNode(node, { type: AST_NODE_TYPES.TSRestType, - typeAnnotation: this.convertType(node.type), + typeAnnotation: this.convertChild(node.type), }); }