Skip to content

Commit

Permalink
feat(ast-spec): make BaseNode & BaseToken more type-safe
Browse files Browse the repository at this point in the history
  • Loading branch information
MichaelDeBoey committed Jun 22, 2021
1 parent 8cfe933 commit f698b62
Show file tree
Hide file tree
Showing 6 changed files with 32 additions and 20 deletions.
21 changes: 7 additions & 14 deletions 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;
}
6 changes: 4 additions & 2 deletions 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;
}
17 changes: 17 additions & 0 deletions 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;
}
2 changes: 1 addition & 1 deletion 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';

Expand Down
4 changes: 2 additions & 2 deletions packages/experimental-utils/src/ts-eslint/Rule.ts
Expand Up @@ -277,7 +277,7 @@ interface RuleListener {
ClassDeclaration?: RuleFunction<TSESTree.ClassDeclaration>;
ClassExpression?: RuleFunction<TSESTree.ClassExpression>;
ClassProperty?: RuleFunction<TSESTree.ClassProperty>;
Comment?: RuleFunction<TSESTree.Comment>;
// Comment?: RuleFunction<TSESTree.Comment>;
ConditionalExpression?: RuleFunction<TSESTree.ConditionalExpression>;
ContinueStatement?: RuleFunction<TSESTree.ContinueStatement>;
DebuggerStatement?: RuleFunction<TSESTree.DebuggerStatement>;
Expand Down Expand Up @@ -338,7 +338,7 @@ interface RuleListener {
TemplateLiteral?: RuleFunction<TSESTree.TemplateLiteral>;
ThisExpression?: RuleFunction<TSESTree.ThisExpression>;
ThrowStatement?: RuleFunction<TSESTree.ThrowStatement>;
Token?: RuleFunction<TSESTree.Token>;
// Token?: RuleFunction<TSESTree.Token>;
TryStatement?: RuleFunction<TSESTree.TryStatement>;
TSAbstractClassProperty?: RuleFunction<TSESTree.TSAbstractClassProperty>;
TSAbstractKeyword?: RuleFunction<TSESTree.TSAbstractKeyword>;
Expand Down
2 changes: 1 addition & 1 deletion packages/typescript-estree/src/convert.ts
Expand Up @@ -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)) {
Expand Down

0 comments on commit f698b62

Please sign in to comment.