Skip to content

Commit

Permalink
Auto-generate node types
Browse files Browse the repository at this point in the history
  • Loading branch information
lukastaegert committed Apr 30, 2024
1 parent c22ad24 commit 69cb865
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 8 deletions.
1 change: 1 addition & 0 deletions scripts/generate-ast-converters.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,6 @@ import './generate-buffer-parsers.js';
import './generate-buffer-to-ast.js';
import './generate-child-node-keys.js';
import './generate-node-index.js';
import './generate-node-types.js';
import './generate-rust-constants.js';
import './generate-string-constants.js';
21 changes: 21 additions & 0 deletions scripts/generate-node-types.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import { writeFile } from 'node:fs/promises';
import { AST_NODES } from './ast-types.js';
import { generateNotEditFilesComment, lintTsFile } from './helpers.js';

const notEditFilesComment = generateNotEditFilesComment(import.meta.url);

const nodeTypesFile = new URL('../src/ast/nodes/NodeType.ts', import.meta.url);

/** @type string[] */
const astTypes = [
...new Set(Object.entries(AST_NODES).map(([name, node]) => node.astType || name))
].sort();

const nodeIndex = `${notEditFilesComment}
${astTypes.map(astType => `export type t${astType} = '${astType}';`).join('\n')}
${astTypes.map(astType => `export const ${astType}: t${astType} = '${astType}';`).join('\n')}
`;

await writeFile(nodeTypesFile, nodeIndex);
await lintTsFile(nodeTypesFile);
17 changes: 9 additions & 8 deletions src/ast/nodes/NodeType.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
// This file is generated by scripts/generate-node-types.js.
// Do not edit this file directly.

export type tArrayExpression = 'ArrayExpression';
export type tArrayPattern = 'ArrayPattern';
export type tArrowFunctionExpression = 'ArrowFunctionExpression';
Expand All @@ -23,18 +26,17 @@ export type tExportDefaultDeclaration = 'ExportDefaultDeclaration';
export type tExportNamedDeclaration = 'ExportNamedDeclaration';
export type tExportSpecifier = 'ExportSpecifier';
export type tExpressionStatement = 'ExpressionStatement';
export type tForStatement = 'ForStatement';
export type tForInStatement = 'ForInStatement';
export type tForOfStatement = 'ForOfStatement';
export type tForStatement = 'ForStatement';
export type tFunctionDeclaration = 'FunctionDeclaration';
export type tFunctionExpression = 'FunctionExpression';
export type tIdentifier = 'Identifier';
export type tIfStatement = 'IfStatement';
export type tImport = 'Import';
export type tImportAttribute = 'ImportAttribute';
export type tImportDeclaration = 'ImportDeclaration';
export type tImportExpression = 'ImportExpression';
export type tImportDefaultSpecifier = 'ImportDefaultSpecifier';
export type tImportExpression = 'ImportExpression';
export type tImportNamespaceSpecifier = 'ImportNamespaceSpecifier';
export type tImportSpecifier = 'ImportSpecifier';
export type tLabeledStatement = 'LabeledStatement';
Expand Down Expand Up @@ -68,8 +70,8 @@ export type tThrowStatement = 'ThrowStatement';
export type tTryStatement = 'TryStatement';
export type tUnaryExpression = 'UnaryExpression';
export type tUpdateExpression = 'UpdateExpression';
export type tVariableDeclarator = 'VariableDeclarator';
export type tVariableDeclaration = 'VariableDeclaration';
export type tVariableDeclarator = 'VariableDeclarator';
export type tWhileStatement = 'WhileStatement';
export type tYieldExpression = 'YieldExpression';

Expand Down Expand Up @@ -98,18 +100,17 @@ export const ExportDefaultDeclaration: tExportDefaultDeclaration = 'ExportDefaul
export const ExportNamedDeclaration: tExportNamedDeclaration = 'ExportNamedDeclaration';
export const ExportSpecifier: tExportSpecifier = 'ExportSpecifier';
export const ExpressionStatement: tExpressionStatement = 'ExpressionStatement';
export const ForStatement: tForStatement = 'ForStatement';
export const ForInStatement: tForInStatement = 'ForInStatement';
export const ForOfStatement: tForOfStatement = 'ForOfStatement';
export const ForStatement: tForStatement = 'ForStatement';
export const FunctionDeclaration: tFunctionDeclaration = 'FunctionDeclaration';
export const FunctionExpression: tFunctionExpression = 'FunctionExpression';
export const Identifier: tIdentifier = 'Identifier';
export const IfStatement: tIfStatement = 'IfStatement';
export const Import: tImport = 'Import';
export const ImportAttribute: tImportAttribute = 'ImportAttribute';
export const ImportDeclaration: tImportDeclaration = 'ImportDeclaration';
export const ImportExpression: tImportExpression = 'ImportExpression';
export const ImportDefaultSpecifier: tImportDefaultSpecifier = 'ImportDefaultSpecifier';
export const ImportExpression: tImportExpression = 'ImportExpression';
export const ImportNamespaceSpecifier: tImportNamespaceSpecifier = 'ImportNamespaceSpecifier';
export const ImportSpecifier: tImportSpecifier = 'ImportSpecifier';
export const LabeledStatement: tLabeledStatement = 'LabeledStatement';
Expand Down Expand Up @@ -143,7 +144,7 @@ export const ThrowStatement: tThrowStatement = 'ThrowStatement';
export const TryStatement: tTryStatement = 'TryStatement';
export const UnaryExpression: tUnaryExpression = 'UnaryExpression';
export const UpdateExpression: tUpdateExpression = 'UpdateExpression';
export const VariableDeclarator: tVariableDeclarator = 'VariableDeclarator';
export const VariableDeclaration: tVariableDeclaration = 'VariableDeclaration';
export const VariableDeclarator: tVariableDeclarator = 'VariableDeclarator';
export const WhileStatement: tWhileStatement = 'WhileStatement';
export const YieldExpression: tYieldExpression = 'YieldExpression';

0 comments on commit 69cb865

Please sign in to comment.