From f698b6243934e358c366d79e00a4899d3398bbe2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C3=ABl=20De=20Boey?= Date: Sun, 20 Jun 2021 18:13:06 +0200 Subject: [PATCH] feat(ast-spec): make `BaseNode` & `BaseToken` more type-safe --- packages/ast-spec/src/base/BaseNode.ts | 21 +++++++------------ packages/ast-spec/src/base/BaseToken.ts | 6 ++++-- packages/ast-spec/src/base/NodeOrTokenData.ts | 17 +++++++++++++++ packages/ast-spec/src/index.ts | 2 +- .../experimental-utils/src/ts-eslint/Rule.ts | 4 ++-- packages/typescript-estree/src/convert.ts | 2 +- 6 files changed, 32 insertions(+), 20 deletions(-) create mode 100644 packages/ast-spec/src/base/NodeOrTokenData.ts diff --git a/packages/ast-spec/src/base/BaseNode.ts b/packages/ast-spec/src/base/BaseNode.ts index 362f156832b4..797b3d351320 100644 --- a/packages/ast-spec/src/base/BaseNode.ts +++ b/packages/ast-spec/src/base/BaseNode.ts @@ -1,22 +1,15 @@ // import type { Node } from '../unions/Node'; -import type { Range } from './Range'; -import type { SourceLocation } from './SourceLocation'; +import type { AST_NODE_TYPES } from '../ast-node-types'; +import type { NodeOrTokenData } from './NodeOrTokenData'; -export interface BaseNode { - /** - * The source location information of the node. - * @see {SourceLocation} - */ - loc: SourceLocation; - /** - * @see {Range} - */ - range: Range; +export interface BaseNode extends NodeOrTokenData { /** * The parent node of the current node + * + * This is added in the @typescript-eslint/types package as ESLint adds it + * while traversing. */ // parent?: Node; - // every node *will* have a type, but let the nodes define their own exact string - // type: string; + type: AST_NODE_TYPES; } diff --git a/packages/ast-spec/src/base/BaseToken.ts b/packages/ast-spec/src/base/BaseToken.ts index cdf0d1286438..8780cfcb9695 100644 --- a/packages/ast-spec/src/base/BaseToken.ts +++ b/packages/ast-spec/src/base/BaseToken.ts @@ -1,8 +1,10 @@ -import type { BaseNode } from './BaseNode'; +import type { AST_TOKEN_TYPES } from '../ast-token-types'; +import type { NodeOrTokenData } from './NodeOrTokenData'; /* * Token and Comment are pseudo-nodes to represent pieces of source code */ -export interface BaseToken extends BaseNode { +export interface BaseToken extends NodeOrTokenData { + type: AST_TOKEN_TYPES; value: string; } diff --git a/packages/ast-spec/src/base/NodeOrTokenData.ts b/packages/ast-spec/src/base/NodeOrTokenData.ts new file mode 100644 index 000000000000..8052278bbf02 --- /dev/null +++ b/packages/ast-spec/src/base/NodeOrTokenData.ts @@ -0,0 +1,17 @@ +import type { Range } from './Range'; +import type { SourceLocation } from './SourceLocation'; + +export interface NodeOrTokenData { + /** + * The source location information of the node. + * @see {SourceLocation} + */ + loc: SourceLocation; + + /** + * @see {Range} + */ + range: Range; + + type: string; +} diff --git a/packages/ast-spec/src/index.ts b/packages/ast-spec/src/index.ts index bd4b6584482d..c0dc09b70b0e 100644 --- a/packages/ast-spec/src/index.ts +++ b/packages/ast-spec/src/index.ts @@ -1,7 +1,7 @@ export * from './base/Accessibility'; export * from './base/BaseNode'; // this is exported so that the `types` package can merge the decl and add the `parent` property -export * from './base/OptionalRangeAndLoc'; export * from './base/LineAndColumnData'; +export * from './base/OptionalRangeAndLoc'; export * from './base/Range'; export * from './base/SourceLocation'; diff --git a/packages/experimental-utils/src/ts-eslint/Rule.ts b/packages/experimental-utils/src/ts-eslint/Rule.ts index 20a7735f08b6..139027372236 100644 --- a/packages/experimental-utils/src/ts-eslint/Rule.ts +++ b/packages/experimental-utils/src/ts-eslint/Rule.ts @@ -277,7 +277,7 @@ interface RuleListener { ClassDeclaration?: RuleFunction; ClassExpression?: RuleFunction; ClassProperty?: RuleFunction; - Comment?: RuleFunction; + // Comment?: RuleFunction; ConditionalExpression?: RuleFunction; ContinueStatement?: RuleFunction; DebuggerStatement?: RuleFunction; @@ -338,7 +338,7 @@ interface RuleListener { TemplateLiteral?: RuleFunction; ThisExpression?: RuleFunction; ThrowStatement?: RuleFunction; - Token?: RuleFunction; + // Token?: RuleFunction; TryStatement?: RuleFunction; TSAbstractClassProperty?: RuleFunction; TSAbstractKeyword?: RuleFunction; diff --git a/packages/typescript-estree/src/convert.ts b/packages/typescript-estree/src/convert.ts index 8ffa10ab9181..e6a8c7a934b0 100644 --- a/packages/typescript-estree/src/convert.ts +++ b/packages/typescript-estree/src/convert.ts @@ -199,7 +199,7 @@ export class Converter { */ private registerTSNodeInNodeMap( node: ts.Node, - result: TSESTree.BaseNode | null, + result: TSESTree.Node | null, ): void { if (result && this.options.shouldPreserveNodeMaps) { if (!this.tsNodeToESTreeNodeMap.has(node)) {