Skip to content

Commit

Permalink
syntax/types: Derive VisitorKeysMap type from visitorKeys object …
Browse files Browse the repository at this point in the history
…definition
  • Loading branch information
Turbo87 committed Dec 24, 2018
1 parent a018e58 commit ebe2ef1
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 45 deletions.
25 changes: 2 additions & 23 deletions packages/@glimmer/syntax/lib/types/nodes.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { VisitorKeysMap } from './visitor-keys';

export type Option<T> = T | null;

export interface BaseNode {
Expand Down Expand Up @@ -206,29 +208,6 @@ export interface Nodes {
HashPair: HashPair;
}

export interface VisitorKeysMap {
Program: ['body'];
MustacheStatement: ['path', 'params', 'hash'];
BlockStatement: ['path', 'params', 'hash', 'program', 'inverse'];
ElementModifierStatement: ['path', 'params', 'hash'];
PartialStatement: ['name', 'params', 'hash'];
CommentStatement: never[];
MustacheCommentStatement: never[];
ElementNode: ['attributes', 'modifiers', 'children', 'comments'];
AttrNode: ['value'];
TextNode: never[];
ConcatStatement: ['parts'];
SubExpression: ['path', 'params', 'hash'];
PathExpression: never[];
StringLiteral: never[];
BooleanLiteral: never[];
NumberLiteral: never[];
NullLiteral: never[];
UndefinedLiteral: never[];
Hash: ['pairs'];
HashPair: ['value'];
}

export type NodeType = keyof Nodes;
export type Node = Nodes[NodeType];

Expand Down
50 changes: 28 additions & 22 deletions packages/@glimmer/syntax/lib/types/visitor-keys.ts
Original file line number Diff line number Diff line change
@@ -1,31 +1,37 @@
import { VisitorKeysMap } from './nodes';
function tuple(): never[];
function tuple<T extends string[]>(...args: T): T;
function tuple<T>(...args: T[]): T[] {
return args;
}

// ensure stays in sync with typing
// ParentNode and ChildKey types are derived from VisitorKeysMap
const visitorKeys: VisitorKeysMap = {
Program: ['body'],
MustacheStatement: ['path', 'params', 'hash'],
BlockStatement: ['path', 'params', 'hash', 'program', 'inverse'],
ElementModifierStatement: ['path', 'params', 'hash'],
PartialStatement: ['name', 'params', 'hash'],
CommentStatement: [],
MustacheCommentStatement: [],
ElementNode: ['attributes', 'modifiers', 'children', 'comments'],
AttrNode: ['value'],
TextNode: [],
const visitorKeys = {
Program: tuple('body'),
MustacheStatement: tuple('path', 'params', 'hash'),
BlockStatement: tuple('path', 'params', 'hash', 'program', 'inverse'),
ElementModifierStatement: tuple('path', 'params', 'hash'),
PartialStatement: tuple('name', 'params', 'hash'),
CommentStatement: tuple(),
MustacheCommentStatement: tuple(),
ElementNode: tuple('attributes', 'modifiers', 'children', 'comments'),
AttrNode: tuple('value'),
TextNode: tuple(),

ConcatStatement: ['parts'],
SubExpression: ['path', 'params', 'hash'],
PathExpression: [],
ConcatStatement: tuple('parts'),
SubExpression: tuple('path', 'params', 'hash'),
PathExpression: tuple(),

StringLiteral: [],
BooleanLiteral: [],
NumberLiteral: [],
NullLiteral: [],
UndefinedLiteral: [],
StringLiteral: tuple(),
BooleanLiteral: tuple(),
NumberLiteral: tuple(),
NullLiteral: tuple(),
UndefinedLiteral: tuple(),

Hash: ['pairs'],
HashPair: ['value'],
Hash: tuple('pairs'),
HashPair: tuple('value'),
};

export default visitorKeys;

export type VisitorKeysMap = typeof visitorKeys;

0 comments on commit ebe2ef1

Please sign in to comment.