Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor(typescript-estree): add type guards for typescript-estree #3078

Closed
wants to merge 17 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
8 changes: 2 additions & 6 deletions packages/typescript-estree/src/convert.ts
Expand Up @@ -244,11 +244,7 @@ export class Converter {
): T {
const result = data;
if (!result.range) {
result.range = getRange(
// this is completely valid, but TS hates it
node as never,
this.ast,
);
result.range = getRange(node, this.ast);
}
if (!result.loc) {
result.loc = getLocFor(result.range[0], result.range[1], this.ast);
Expand Down Expand Up @@ -469,7 +465,7 @@ export class Converter {
throw new Error(`Unknown AST_NODE_TYPE: "${customType}"`);
}

const result = this.createNode<any>(node, {
const result = this.createNode<any>(node as any, {
type: customType,
});

Expand Down
@@ -1,6 +1,6 @@
import { AST_NODE_TYPES, TSESTree } from '@typescript-eslint/types';
import * as ts from 'typescript';
import { TSNode } from './ts-nodes';
import { TSNode, TSNodeUnsupported, TSNodeSkipped } from './ts-nodes';

export interface EstreeToTsNodeTypes {
[AST_NODE_TYPES.ArrayExpression]: ts.ArrayLiteralExpression;
Expand Down Expand Up @@ -279,7 +279,8 @@ export interface EstreeToTsNodeTypes {
* This mapping is based on the internal logic of the parser.
*/
export type TSESTreeToTSNode<T extends TSESTree.Node = TSESTree.Node> = Extract<
TSNode | ts.Token<ts.SyntaxKind.NewKeyword | ts.SyntaxKind.ImportKeyword>,
| Exclude<TSNode, TSNodeUnsupported | TSNodeSkipped>
| ts.Token<ts.SyntaxKind.NewKeyword | ts.SyntaxKind.ImportKeyword>,
// if this errors, it means that one of the AST_NODE_TYPES is not defined in the above interface
EstreeToTsNodeTypes[T['type']]
>;
103 changes: 58 additions & 45 deletions packages/typescript-estree/src/ts-estree/ts-nodes.ts
Expand Up @@ -11,11 +11,67 @@ declare module 'typescript' {

export type TSToken = ts.Token<ts.SyntaxKind>;

export type TSNodeSkipped =
armano2 marked this conversation as resolved.
Show resolved Hide resolved
| ts.OmittedExpression
| ts.ComputedPropertyName // we are skipping this node
| ts.CaseBlock
| ts.HeritageClause
| ts.NamedImports
| ts.NamedExports
| ts.TemplateSpan;

export type TSNodeUnsupported =
| ts.PartiallyEmittedExpression
| ts.SyntheticExpression
| ts.ParenthesizedExpression
| ts.NotEmittedStatement
| ts.CommaListExpression
armano2 marked this conversation as resolved.
Show resolved Hide resolved
| ts.MissingDeclaration
| ts.Bundle
| ts.InputFiles
| ts.UnparsedNode
| ts.UnparsedSource
| ts.SemicolonClassElement

// Modifiers
| ts.ConstKeyword
| ts.DefaultKeyword

// JSON: Unsupported
| ts.JsonMinusNumericLiteral

// JSDoc: Unsupported
| ts.JSDoc
| ts.JSDocTypeExpression
| ts.JSDocUnknownTag
| ts.JSDocAugmentsTag
| ts.JSDocClassTag
| ts.JSDocEnumTag
| ts.JSDocThisTag
| ts.JSDocTemplateTag
| ts.JSDocReturnTag
| ts.JSDocTypeTag
| ts.JSDocTypedefTag
| ts.JSDocCallbackTag
| ts.JSDocSignature
| ts.JSDocPropertyTag
| ts.JSDocParameterTag
| ts.JSDocTypeLiteral
| ts.JSDocFunctionType
| ts.JSDocAllType
| ts.JSDocUnknownType
| ts.JSDocNullableType
| ts.JSDocNonNullableType
| ts.JSDocOptionalType
| ts.JSDocVariadicType
| ts.JSDocAuthorTag;

export type TSNode =
| TSNodeUnsupported
| TSNodeSkipped
| ts.Modifier
| ts.Identifier
| ts.QualifiedName
| ts.ComputedPropertyName
| ts.Decorator
| ts.TypeParameterDeclaration
// | ts.SignatureDeclarationBase -> CallSignatureDeclaration, ConstructSignatureDeclaration
Expand All @@ -36,7 +92,6 @@ export type TSNode =
| ts.MethodSignature
| ts.MethodDeclaration
| ts.ConstructorDeclaration
| ts.SemicolonClassElement
| ts.GetAccessorDeclaration
| ts.SetAccessorDeclaration
| ts.IndexSignatureDeclaration
Expand Down Expand Up @@ -65,8 +120,6 @@ export type TSNode =
| ts.MappedTypeNode
| ts.LiteralTypeNode
| ts.StringLiteral
| ts.OmittedExpression
| ts.PartiallyEmittedExpression
| ts.PrefixUnaryExpression
| ts.PostfixUnaryExpression
| ts.NullLiteral
Expand All @@ -79,7 +132,6 @@ export type TSNode =
| ts.VoidExpression
| ts.AwaitExpression
| ts.YieldExpression
| ts.SyntheticExpression
| ts.BinaryExpression
| ts.ConditionalExpression
| ts.FunctionExpression
Expand All @@ -92,8 +144,6 @@ export type TSNode =
| ts.TemplateMiddle
| ts.TemplateTail
| ts.TemplateExpression
| ts.TemplateSpan
| ts.ParenthesizedExpression
| ts.ArrayLiteralExpression
| ts.SpreadElement
| ts.ObjectLiteralExpression
Expand All @@ -118,11 +168,8 @@ export type TSNode =
| ts.JsxClosingElement
| ts.JsxExpression
| ts.JsxText
| ts.NotEmittedStatement
| ts.CommaListExpression
| ts.EmptyStatement
| ts.DebuggerStatement
| ts.MissingDeclaration
| ts.Block
| ts.VariableStatement
| ts.ExpressionStatement
Expand All @@ -137,7 +184,6 @@ export type TSNode =
| ts.ReturnStatement
| ts.WithStatement
| ts.SwitchStatement
| ts.CaseBlock
| ts.CaseClause
| ts.DefaultClause
| ts.LabeledStatement
Expand All @@ -148,7 +194,6 @@ export type TSNode =
| ts.ClassDeclaration
| ts.ClassExpression
| ts.InterfaceDeclaration
| ts.HeritageClause
| ts.TypeAliasDeclaration
| ts.EnumMember
| ts.EnumDeclaration
Expand All @@ -161,40 +206,8 @@ export type TSNode =
| ts.NamespaceImport
| ts.NamespaceExportDeclaration
| ts.ExportDeclaration
| ts.NamedImports
| ts.NamedExports
| ts.ImportSpecifier
| ts.ExportSpecifier
| ts.ExportAssignment
| ts.SourceFile
| ts.Bundle
| ts.InputFiles
| ts.UnparsedSource
| ts.JsonMinusNumericLiteral
| ts.TemplateLiteralTypeNode

// JSDoc: Unsupported
| ts.JSDoc
| ts.JSDocTypeExpression
| ts.JSDocUnknownTag
| ts.JSDocAugmentsTag
| ts.JSDocClassTag
| ts.JSDocEnumTag
| ts.JSDocThisTag
| ts.JSDocTemplateTag
| ts.JSDocReturnTag
| ts.JSDocTypeTag
| ts.JSDocTypedefTag
| ts.JSDocCallbackTag
| ts.JSDocSignature
| ts.JSDocPropertyTag
| ts.JSDocParameterTag
| ts.JSDocTypeLiteral
| ts.JSDocFunctionType
| ts.JSDocAllType
| ts.JSDocUnknownType
| ts.JSDocNullableType
| ts.JSDocNonNullableType
| ts.JSDocOptionalType
| ts.JSDocVariadicType
| ts.JSDocAuthorTag;
| ts.TemplateLiteralTypeNode;