yuku-ast 0.8.6 → 0.9.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -106,6 +106,7 @@ import {
106
106
  nameOf, // Identifier name or string Literal value
107
107
  literalValue, // string | number | boolean | bigint | RegExp | null
108
108
  unwrap, // strips parens and erased TS assertion wrappers
109
+ isWrapper, // true for the wrappers unwrap strips
109
110
  isCallOf, // isCallOf(node, "require")
110
111
  bindingIdentifiers, // every binding Identifier a pattern introduces
111
112
  findAll, // findAll(program, "CallExpression")
@@ -0,0 +1,22 @@
1
+ import type { NodeOfType } from "@yuku-toolchain/types";
2
+ export declare const ALIAS_GROUPS: {
3
+ readonly Expression: readonly ["Identifier", "Literal", "ThisExpression", "Super", "ArrayExpression", "ObjectExpression", "FunctionExpression", "ArrowFunctionExpression", "ClassExpression", "TaggedTemplateExpression", "TemplateLiteral", "MemberExpression", "CallExpression", "NewExpression", "ChainExpression", "SequenceExpression", "ParenthesizedExpression", "BinaryExpression", "LogicalExpression", "ConditionalExpression", "UnaryExpression", "UpdateExpression", "AssignmentExpression", "YieldExpression", "AwaitExpression", "ImportExpression", "MetaProperty", "TSAsExpression", "TSSatisfiesExpression", "TSTypeAssertion", "TSNonNullExpression", "TSInstantiationExpression", "JSXElement", "JSXFragment"];
4
+ readonly Statement: readonly ["ExpressionStatement", "BlockStatement", "EmptyStatement", "DebuggerStatement", "ReturnStatement", "LabeledStatement", "BreakStatement", "ContinueStatement", "IfStatement", "SwitchStatement", "ThrowStatement", "TryStatement", "WhileStatement", "DoWhileStatement", "ForStatement", "ForInStatement", "ForOfStatement", "WithStatement", "FunctionDeclaration", "ClassDeclaration", "VariableDeclaration", "TSDeclareFunction", "TSTypeAliasDeclaration", "TSInterfaceDeclaration", "TSEnumDeclaration", "TSModuleDeclaration", "TSImportEqualsDeclaration"];
5
+ readonly Declaration: readonly ["FunctionDeclaration", "ClassDeclaration", "VariableDeclaration", "TSDeclareFunction", "TSTypeAliasDeclaration", "TSInterfaceDeclaration", "TSEnumDeclaration", "TSModuleDeclaration", "TSImportEqualsDeclaration"];
6
+ readonly ModuleDeclaration: readonly ["ImportDeclaration", "ExportNamedDeclaration", "ExportDefaultDeclaration", "ExportAllDeclaration", "TSExportAssignment", "TSNamespaceExportDeclaration"];
7
+ readonly Function: readonly ["FunctionDeclaration", "FunctionExpression", "TSDeclareFunction", "TSEmptyBodyFunctionExpression", "ArrowFunctionExpression"];
8
+ readonly Class: readonly ["ClassDeclaration", "ClassExpression"];
9
+ readonly Method: readonly ["MethodDefinition", "TSAbstractMethodDefinition"];
10
+ readonly Loop: readonly ["DoWhileStatement", "ForInStatement", "ForOfStatement", "ForStatement", "WhileStatement"];
11
+ readonly Pattern: readonly ["ArrayPattern", "ObjectPattern", "AssignmentPattern", "RestElement"];
12
+ readonly JSX: readonly ["JSXAttribute", "JSXClosingElement", "JSXClosingFragment", "JSXElement", "JSXEmptyExpression", "JSXExpressionContainer", "JSXFragment", "JSXIdentifier", "JSXMemberExpression", "JSXNamespacedName", "JSXOpeningElement", "JSXOpeningFragment", "JSXSpreadAttribute", "JSXSpreadChild", "JSXText"];
13
+ readonly TSType: readonly ["TSAnyKeyword", "TSUnknownKeyword", "TSNeverKeyword", "TSVoidKeyword", "TSNullKeyword", "TSUndefinedKeyword", "TSStringKeyword", "TSNumberKeyword", "TSBigIntKeyword", "TSBooleanKeyword", "TSSymbolKeyword", "TSObjectKeyword", "TSIntrinsicKeyword", "TSThisType", "TSTypeReference", "TSTypeQuery", "TSImportType", "TSLiteralType", "TSTemplateLiteralType", "TSArrayType", "TSIndexedAccessType", "TSTupleType", "TSNamedTupleMember", "TSJSDocNullableType", "TSJSDocNonNullableType", "TSJSDocUnknownType", "TSUnionType", "TSIntersectionType", "TSConditionalType", "TSInferType", "TSTypeOperator", "TSParenthesizedType", "TSFunctionType", "TSConstructorType", "TSTypePredicate", "TSTypeLiteral", "TSMappedType"];
14
+ };
15
+ export type AliasName = keyof typeof ALIAS_GROUPS;
16
+ type GroupType<A extends AliasName> = (typeof ALIAS_GROUPS)[A][number];
17
+ /** The node union each alias covers, e.g. `AliasMap["Function"]`. */
18
+ export type AliasMap = {
19
+ [A in AliasName]: NodeOfType<GroupType<A>>;
20
+ };
21
+ export declare const ALIAS_NAMES: readonly AliasName[];
22
+ export {};
@@ -0,0 +1,157 @@
1
+ // every group member must be a real node type, and the union-backed
2
+ // groups must match their @yuku-toolchain/types union exactly, both
3
+ // directions checked below
4
+ export const ALIAS_GROUPS = {
5
+ Expression: [
6
+ "Identifier",
7
+ "Literal",
8
+ "ThisExpression",
9
+ "Super",
10
+ "ArrayExpression",
11
+ "ObjectExpression",
12
+ "FunctionExpression",
13
+ "ArrowFunctionExpression",
14
+ "ClassExpression",
15
+ "TaggedTemplateExpression",
16
+ "TemplateLiteral",
17
+ "MemberExpression",
18
+ "CallExpression",
19
+ "NewExpression",
20
+ "ChainExpression",
21
+ "SequenceExpression",
22
+ "ParenthesizedExpression",
23
+ "BinaryExpression",
24
+ "LogicalExpression",
25
+ "ConditionalExpression",
26
+ "UnaryExpression",
27
+ "UpdateExpression",
28
+ "AssignmentExpression",
29
+ "YieldExpression",
30
+ "AwaitExpression",
31
+ "ImportExpression",
32
+ "MetaProperty",
33
+ "TSAsExpression",
34
+ "TSSatisfiesExpression",
35
+ "TSTypeAssertion",
36
+ "TSNonNullExpression",
37
+ "TSInstantiationExpression",
38
+ "JSXElement",
39
+ "JSXFragment",
40
+ ],
41
+ Statement: [
42
+ "ExpressionStatement",
43
+ "BlockStatement",
44
+ "EmptyStatement",
45
+ "DebuggerStatement",
46
+ "ReturnStatement",
47
+ "LabeledStatement",
48
+ "BreakStatement",
49
+ "ContinueStatement",
50
+ "IfStatement",
51
+ "SwitchStatement",
52
+ "ThrowStatement",
53
+ "TryStatement",
54
+ "WhileStatement",
55
+ "DoWhileStatement",
56
+ "ForStatement",
57
+ "ForInStatement",
58
+ "ForOfStatement",
59
+ "WithStatement",
60
+ "FunctionDeclaration",
61
+ "ClassDeclaration",
62
+ "VariableDeclaration",
63
+ "TSDeclareFunction",
64
+ "TSTypeAliasDeclaration",
65
+ "TSInterfaceDeclaration",
66
+ "TSEnumDeclaration",
67
+ "TSModuleDeclaration",
68
+ "TSImportEqualsDeclaration",
69
+ ],
70
+ Declaration: [
71
+ "FunctionDeclaration",
72
+ "ClassDeclaration",
73
+ "VariableDeclaration",
74
+ "TSDeclareFunction",
75
+ "TSTypeAliasDeclaration",
76
+ "TSInterfaceDeclaration",
77
+ "TSEnumDeclaration",
78
+ "TSModuleDeclaration",
79
+ "TSImportEqualsDeclaration",
80
+ ],
81
+ ModuleDeclaration: [
82
+ "ImportDeclaration",
83
+ "ExportNamedDeclaration",
84
+ "ExportDefaultDeclaration",
85
+ "ExportAllDeclaration",
86
+ "TSExportAssignment",
87
+ "TSNamespaceExportDeclaration",
88
+ ],
89
+ Function: [
90
+ "FunctionDeclaration",
91
+ "FunctionExpression",
92
+ "TSDeclareFunction",
93
+ "TSEmptyBodyFunctionExpression",
94
+ "ArrowFunctionExpression",
95
+ ],
96
+ Class: ["ClassDeclaration", "ClassExpression"],
97
+ Method: ["MethodDefinition", "TSAbstractMethodDefinition"],
98
+ Loop: ["DoWhileStatement", "ForInStatement", "ForOfStatement", "ForStatement", "WhileStatement"],
99
+ Pattern: ["ArrayPattern", "ObjectPattern", "AssignmentPattern", "RestElement"],
100
+ JSX: [
101
+ "JSXAttribute",
102
+ "JSXClosingElement",
103
+ "JSXClosingFragment",
104
+ "JSXElement",
105
+ "JSXEmptyExpression",
106
+ "JSXExpressionContainer",
107
+ "JSXFragment",
108
+ "JSXIdentifier",
109
+ "JSXMemberExpression",
110
+ "JSXNamespacedName",
111
+ "JSXOpeningElement",
112
+ "JSXOpeningFragment",
113
+ "JSXSpreadAttribute",
114
+ "JSXSpreadChild",
115
+ "JSXText",
116
+ ],
117
+ TSType: [
118
+ "TSAnyKeyword",
119
+ "TSUnknownKeyword",
120
+ "TSNeverKeyword",
121
+ "TSVoidKeyword",
122
+ "TSNullKeyword",
123
+ "TSUndefinedKeyword",
124
+ "TSStringKeyword",
125
+ "TSNumberKeyword",
126
+ "TSBigIntKeyword",
127
+ "TSBooleanKeyword",
128
+ "TSSymbolKeyword",
129
+ "TSObjectKeyword",
130
+ "TSIntrinsicKeyword",
131
+ "TSThisType",
132
+ "TSTypeReference",
133
+ "TSTypeQuery",
134
+ "TSImportType",
135
+ "TSLiteralType",
136
+ "TSTemplateLiteralType",
137
+ "TSArrayType",
138
+ "TSIndexedAccessType",
139
+ "TSTupleType",
140
+ "TSNamedTupleMember",
141
+ "TSJSDocNullableType",
142
+ "TSJSDocNonNullableType",
143
+ "TSJSDocUnknownType",
144
+ "TSUnionType",
145
+ "TSIntersectionType",
146
+ "TSConditionalType",
147
+ "TSInferType",
148
+ "TSTypeOperator",
149
+ "TSParenthesizedType",
150
+ "TSFunctionType",
151
+ "TSConstructorType",
152
+ "TSTypePredicate",
153
+ "TSTypeLiteral",
154
+ "TSMappedType",
155
+ ],
156
+ };
157
+ export const ALIAS_NAMES = Object.keys(ALIAS_GROUPS);
@@ -0,0 +1,19 @@
1
+ import type { Node, NodeOfType, NodeType } from "@yuku-toolchain/types";
2
+ type BuilderInput<T> = T extends Node ? Omit<T, "type" | "start" | "end"> & {
3
+ start?: number;
4
+ end?: number;
5
+ } : never;
6
+ type Builders = {
7
+ [K in NodeType]: (fields: BuilderInput<NodeOfType<K>>) => NodeOfType<K>;
8
+ };
9
+ /**
10
+ * One constructor per node type, its fields derived from the node type
11
+ * itself. `start` and `end` default to 0, which `ctx.replace` treats
12
+ * as span-less and fills from the replaced node.
13
+ *
14
+ * @example
15
+ * b.Identifier({ name: "x" })
16
+ * b.ExpressionStatement({ expression: b.Identifier({ name: "x" }) })
17
+ */
18
+ export declare const b: Builders;
19
+ export {};
@@ -0,0 +1,14 @@
1
+ import { NODE_TYPES } from "./generated.js";
2
+ /**
3
+ * One constructor per node type, its fields derived from the node type
4
+ * itself. `start` and `end` default to 0, which `ctx.replace` treats
5
+ * as span-less and fills from the replaced node.
6
+ *
7
+ * @example
8
+ * b.Identifier({ name: "x" })
9
+ * b.ExpressionStatement({ expression: b.Identifier({ name: "x" }) })
10
+ */
11
+ export const b = Object.fromEntries(NODE_TYPES.map((type) => [
12
+ type,
13
+ (fields) => ({ type, start: 0, end: 0, ...fields }),
14
+ ]));
@@ -0,0 +1,55 @@
1
+ import type { Node } from "@yuku-toolchain/types";
2
+ interface Frame {
3
+ i: number;
4
+ }
5
+ /**
6
+ * The walk context: one reused object exposing the current position and
7
+ * the tree mutation operations. Valid only during the visit that
8
+ * receives it, do not store it. Underscore members are engine state.
9
+ */
10
+ export declare class WalkContext<T extends Node = Node, S = unknown> {
11
+ #private;
12
+ _ancestors: Node[];
13
+ _node: Node | null;
14
+ _key: string | null;
15
+ _list: Node[] | null;
16
+ _frame: Frame | null;
17
+ _skip: boolean;
18
+ _stopped: boolean;
19
+ _removed: boolean;
20
+ _replacement: Node | null;
21
+ /** State threaded through the walk, the third `walk` argument. */
22
+ state: S;
23
+ /** The node being visited. */
24
+ get node(): T;
25
+ /** The node holding {@link node}, or null at the walk root. */
26
+ get parent(): Node | null;
27
+ /** The field on {@link parent} holding {@link node}, or null at the root. */
28
+ get key(): string | null;
29
+ /** Index within an array field, or null in a plain field. */
30
+ get index(): number | null;
31
+ /** Ancestors from the walk root down to {@link parent}. */
32
+ ancestors(): Node[];
33
+ /** Do not descend into the current node's children. */
34
+ skip(): void;
35
+ /** Stop the walk entirely. */
36
+ stop(): void;
37
+ /**
38
+ * Replace the current node. The walk continues into the replacement's
39
+ * children and `leave` fires for the replacement's type. A synthetic
40
+ * node with `start === 0 && end === 0` inherits the original span,
41
+ * for source maps. Throws at the walk root.
42
+ */
43
+ replace(node: Node): void;
44
+ /**
45
+ * Remove the current node from its parent: spliced from array fields,
46
+ * nulled in plain fields. Children are not walked and `leave` does
47
+ * not fire. Throws at the walk root.
48
+ */
49
+ remove(): void;
50
+ /** Insert a sibling before the current node, not visited. Array fields only. */
51
+ insertBefore(node: Node): void;
52
+ /** Insert a sibling after the current node, visited by the walk. Array fields only. */
53
+ insertAfter(node: Node): void;
54
+ }
55
+ export {};
@@ -0,0 +1,92 @@
1
+ /**
2
+ * The walk context: one reused object exposing the current position and
3
+ * the tree mutation operations. Valid only during the visit that
4
+ * receives it, do not store it. Underscore members are engine state.
5
+ */
6
+ export class WalkContext {
7
+ _ancestors = [];
8
+ _node = null;
9
+ _key = null;
10
+ _list = null;
11
+ _frame = null;
12
+ _skip = false;
13
+ _stopped = false;
14
+ _removed = false;
15
+ _replacement = null;
16
+ /** State threaded through the walk, the third `walk` argument. */
17
+ state;
18
+ /** The node being visited. */
19
+ get node() {
20
+ return this._node;
21
+ }
22
+ /** The node holding {@link node}, or null at the walk root. */
23
+ get parent() {
24
+ const a = this._ancestors;
25
+ return a.length === 0 ? null : a[a.length - 1];
26
+ }
27
+ /** The field on {@link parent} holding {@link node}, or null at the root. */
28
+ get key() {
29
+ return this._key;
30
+ }
31
+ /** Index within an array field, or null in a plain field. */
32
+ get index() {
33
+ return this._list === null ? null : this._frame.i;
34
+ }
35
+ /** Ancestors from the walk root down to {@link parent}. */
36
+ ancestors() {
37
+ return [...this._ancestors];
38
+ }
39
+ /** Do not descend into the current node's children. */
40
+ skip() {
41
+ this._skip = true;
42
+ }
43
+ /** Stop the walk entirely. */
44
+ stop() {
45
+ this._stopped = true;
46
+ }
47
+ /**
48
+ * Replace the current node. The walk continues into the replacement's
49
+ * children and `leave` fires for the replacement's type. A synthetic
50
+ * node with `start === 0 && end === 0` inherits the original span,
51
+ * for source maps. Throws at the walk root.
52
+ */
53
+ replace(node) {
54
+ if (node === null || typeof node !== "object" || typeof node.type !== "string") {
55
+ throw new TypeError("replace: expected an AST node");
56
+ }
57
+ if (this.parent === null) {
58
+ throw new TypeError("replace: cannot replace the walk root");
59
+ }
60
+ if (node.start === 0 && node.end === 0) {
61
+ node.start = this.node.start;
62
+ node.end = this.node.end;
63
+ }
64
+ this._replacement = node;
65
+ }
66
+ /**
67
+ * Remove the current node from its parent: spliced from array fields,
68
+ * nulled in plain fields. Children are not walked and `leave` does
69
+ * not fire. Throws at the walk root.
70
+ */
71
+ remove() {
72
+ if (this.parent === null) {
73
+ throw new TypeError("remove: cannot remove the walk root");
74
+ }
75
+ this._removed = true;
76
+ }
77
+ /** Insert a sibling before the current node, not visited. Array fields only. */
78
+ insertBefore(node) {
79
+ this.#insert(node, 0);
80
+ this._frame.i++;
81
+ }
82
+ /** Insert a sibling after the current node, visited by the walk. Array fields only. */
83
+ insertAfter(node) {
84
+ this.#insert(node, 1);
85
+ }
86
+ #insert(node, offset) {
87
+ if (this._list === null) {
88
+ throw new TypeError("insertBefore/insertAfter require a node in an array field");
89
+ }
90
+ this._list.splice(this._frame.i + offset, 0, node);
91
+ }
92
+ }
@@ -0,0 +1,5 @@
1
+ export declare const CHILD_KEYS: Readonly<Record<string, readonly string[]>>;
2
+ declare const TYPES: readonly ["SequenceExpression", "ParenthesizedExpression", "ArrowFunctionExpression", "FunctionDeclaration", "FunctionExpression", "TSDeclareFunction", "TSEmptyBodyFunctionExpression", "BlockStatement", "BlockStatement", "BinaryExpression", "LogicalExpression", "ConditionalExpression", "UnaryExpression", "UpdateExpression", "AssignmentExpression", "ArrayExpression", "ObjectExpression", "SpreadElement", "Property", "MemberExpression", "CallExpression", "ChainExpression", "TaggedTemplateExpression", "NewExpression", "AwaitExpression", "YieldExpression", "MetaProperty", "Decorator", "ClassDeclaration", "ClassExpression", "ClassBody", "MethodDefinition", "TSAbstractMethodDefinition", "PropertyDefinition", "AccessorProperty", "TSAbstractPropertyDefinition", "TSAbstractAccessorProperty", "StaticBlock", "Super", "Literal", "Literal", "Literal", "Literal", "Literal", "ThisExpression", "Literal", "TemplateLiteral", "TemplateElement", "Identifier", "PrivateIdentifier", "Identifier", "Identifier", "Identifier", "ExpressionStatement", "IfStatement", "SwitchStatement", "SwitchCase", "ForStatement", "ForInStatement", "ForOfStatement", "WhileStatement", "DoWhileStatement", "BreakStatement", "ContinueStatement", "LabeledStatement", "WithStatement", "ReturnStatement", "ThrowStatement", "TryStatement", "CatchClause", "DebuggerStatement", "EmptyStatement", "VariableDeclaration", "VariableDeclarator", "ExpressionStatement", "AssignmentPattern", "RestElement", "ArrayPattern", "ObjectPattern", "Property", "Program", "ImportExpression", "ImportDeclaration", "ImportSpecifier", "ImportDefaultSpecifier", "ImportNamespaceSpecifier", "ImportAttribute", "ExportNamedDeclaration", "ExportDefaultDeclaration", "ExportAllDeclaration", "ExportSpecifier", "TSTypeAnnotation", "TSAnyKeyword", "TSUnknownKeyword", "TSNeverKeyword", "TSVoidKeyword", "TSNullKeyword", "TSUndefinedKeyword", "TSStringKeyword", "TSNumberKeyword", "TSBigIntKeyword", "TSBooleanKeyword", "TSSymbolKeyword", "TSObjectKeyword", "TSIntrinsicKeyword", "TSThisType", "TSTypeReference", "TSQualifiedName", "TSTypeQuery", "TSImportType", "TSTypeParameter", "TSTypeParameterDeclaration", "TSTypeParameterInstantiation", "TSLiteralType", "TSTemplateLiteralType", "TSArrayType", "TSIndexedAccessType", "TSTupleType", "TSNamedTupleMember", "TSOptionalType", "TSRestType", "TSJSDocNullableType", "TSJSDocNonNullableType", "TSJSDocUnknownType", "TSUnionType", "TSIntersectionType", "TSConditionalType", "TSInferType", "TSTypeOperator", "TSParenthesizedType", "TSFunctionType", "TSConstructorType", "TSTypePredicate", "TSTypeLiteral", "TSMappedType", "TSPropertySignature", "TSMethodSignature", "TSCallSignatureDeclaration", "TSConstructSignatureDeclaration", "TSIndexSignature", "TSTypeAliasDeclaration", "TSInterfaceDeclaration", "TSInterfaceBody", "TSInterfaceHeritage", "TSClassImplements", "TSEnumDeclaration", "TSEnumBody", "TSEnumMember", "TSModuleDeclaration", "TSModuleBlock", "TSModuleDeclaration", "TSParameterProperty", "Identifier", "TSAsExpression", "TSSatisfiesExpression", "TSTypeAssertion", "TSNonNullExpression", "TSInstantiationExpression", "TSExportAssignment", "TSNamespaceExportDeclaration", "TSImportEqualsDeclaration", "TSExternalModuleReference", "JSXElement", "JSXOpeningElement", "JSXClosingElement", "JSXFragment", "JSXOpeningFragment", "JSXClosingFragment", "JSXIdentifier", "JSXNamespacedName", "JSXMemberExpression", "JSXAttribute", "JSXSpreadAttribute", "JSXExpressionContainer", "JSXEmptyExpression", "JSXText", "JSXSpreadChild", "Hashbang"];
3
+ export type GeneratedNodeType = (typeof TYPES)[number];
4
+ export declare const NODE_TYPES: readonly GeneratedNodeType[];
5
+ export {};