yuku-ast 0.6.7 → 0.6.8

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "yuku-ast",
3
- "version": "0.6.7",
3
+ "version": "0.6.8",
4
4
  "description": "Typed AST toolkit for JavaScript and TypeScript ESTree trees: walker, builders, guards, and syntactic utilities",
5
5
  "license": "MIT",
6
6
  "repository": {
@@ -11,15 +11,11 @@
11
11
  "main": "./dist/index.js",
12
12
  "types": "./dist/index.d.ts",
13
13
  "exports": {
14
- ".": {
15
- "types": "./dist/index.d.ts",
16
- "bun": "./src/index.ts",
17
- "default": "./dist/index.js"
18
- }
14
+ ".": "./dist/index.js",
15
+ "./package.json": "./package.json"
19
16
  },
20
17
  "files": [
21
- "dist",
22
- "src"
18
+ "dist"
23
19
  ],
24
20
  "scripts": {
25
21
  "build": "tsdown"
@@ -40,5 +36,6 @@
40
36
  "typescript",
41
37
  "javascript",
42
38
  "yuku"
43
- ]
39
+ ],
40
+ "module": "./dist/index.js"
44
41
  }
package/src/aliases.ts DELETED
@@ -1,190 +0,0 @@
1
- import type {
2
- ClassType,
3
- Declaration,
4
- Expression,
5
- FunctionType,
6
- MethodDefinitionType,
7
- ModuleDeclaration,
8
- NodeOfType,
9
- NodeType,
10
- Statement,
11
- TSType,
12
- } from "@yuku-toolchain/types";
13
-
14
- // every group member must be a real node type, and the union-backed
15
- // groups must match their @yuku-toolchain/types union exactly, both
16
- // directions checked below
17
- export const ALIAS_GROUPS = {
18
- Expression: [
19
- "Identifier",
20
- "Literal",
21
- "ThisExpression",
22
- "Super",
23
- "ArrayExpression",
24
- "ObjectExpression",
25
- "FunctionExpression",
26
- "ArrowFunctionExpression",
27
- "ClassExpression",
28
- "TaggedTemplateExpression",
29
- "TemplateLiteral",
30
- "MemberExpression",
31
- "CallExpression",
32
- "NewExpression",
33
- "ChainExpression",
34
- "SequenceExpression",
35
- "ParenthesizedExpression",
36
- "BinaryExpression",
37
- "LogicalExpression",
38
- "ConditionalExpression",
39
- "UnaryExpression",
40
- "UpdateExpression",
41
- "AssignmentExpression",
42
- "YieldExpression",
43
- "AwaitExpression",
44
- "ImportExpression",
45
- "MetaProperty",
46
- "TSAsExpression",
47
- "TSSatisfiesExpression",
48
- "TSTypeAssertion",
49
- "TSNonNullExpression",
50
- "TSInstantiationExpression",
51
- "JSXElement",
52
- "JSXFragment",
53
- ],
54
- Statement: [
55
- "ExpressionStatement",
56
- "BlockStatement",
57
- "EmptyStatement",
58
- "DebuggerStatement",
59
- "ReturnStatement",
60
- "LabeledStatement",
61
- "BreakStatement",
62
- "ContinueStatement",
63
- "IfStatement",
64
- "SwitchStatement",
65
- "ThrowStatement",
66
- "TryStatement",
67
- "WhileStatement",
68
- "DoWhileStatement",
69
- "ForStatement",
70
- "ForInStatement",
71
- "ForOfStatement",
72
- "WithStatement",
73
- "FunctionDeclaration",
74
- "ClassDeclaration",
75
- "VariableDeclaration",
76
- "TSDeclareFunction",
77
- "TSTypeAliasDeclaration",
78
- "TSInterfaceDeclaration",
79
- "TSEnumDeclaration",
80
- "TSModuleDeclaration",
81
- "TSImportEqualsDeclaration",
82
- ],
83
- Declaration: [
84
- "FunctionDeclaration",
85
- "ClassDeclaration",
86
- "VariableDeclaration",
87
- "TSDeclareFunction",
88
- "TSTypeAliasDeclaration",
89
- "TSInterfaceDeclaration",
90
- "TSEnumDeclaration",
91
- "TSModuleDeclaration",
92
- "TSImportEqualsDeclaration",
93
- ],
94
- ModuleDeclaration: [
95
- "ImportDeclaration",
96
- "ExportNamedDeclaration",
97
- "ExportDefaultDeclaration",
98
- "ExportAllDeclaration",
99
- "TSExportAssignment",
100
- "TSNamespaceExportDeclaration",
101
- ],
102
- Function: [
103
- "FunctionDeclaration",
104
- "FunctionExpression",
105
- "TSDeclareFunction",
106
- "TSEmptyBodyFunctionExpression",
107
- "ArrowFunctionExpression",
108
- ],
109
- Class: ["ClassDeclaration", "ClassExpression"],
110
- Method: ["MethodDefinition", "TSAbstractMethodDefinition"],
111
- Loop: ["DoWhileStatement", "ForInStatement", "ForOfStatement", "ForStatement", "WhileStatement"],
112
- Pattern: ["ArrayPattern", "ObjectPattern", "AssignmentPattern", "RestElement"],
113
- JSX: [
114
- "JSXAttribute",
115
- "JSXClosingElement",
116
- "JSXClosingFragment",
117
- "JSXElement",
118
- "JSXEmptyExpression",
119
- "JSXExpressionContainer",
120
- "JSXFragment",
121
- "JSXIdentifier",
122
- "JSXMemberExpression",
123
- "JSXNamespacedName",
124
- "JSXOpeningElement",
125
- "JSXOpeningFragment",
126
- "JSXSpreadAttribute",
127
- "JSXSpreadChild",
128
- "JSXText",
129
- ],
130
- TSType: [
131
- "TSAnyKeyword",
132
- "TSUnknownKeyword",
133
- "TSNeverKeyword",
134
- "TSVoidKeyword",
135
- "TSNullKeyword",
136
- "TSUndefinedKeyword",
137
- "TSStringKeyword",
138
- "TSNumberKeyword",
139
- "TSBigIntKeyword",
140
- "TSBooleanKeyword",
141
- "TSSymbolKeyword",
142
- "TSObjectKeyword",
143
- "TSIntrinsicKeyword",
144
- "TSThisType",
145
- "TSTypeReference",
146
- "TSTypeQuery",
147
- "TSImportType",
148
- "TSLiteralType",
149
- "TSTemplateLiteralType",
150
- "TSArrayType",
151
- "TSIndexedAccessType",
152
- "TSTupleType",
153
- "TSNamedTupleMember",
154
- "TSJSDocNullableType",
155
- "TSJSDocNonNullableType",
156
- "TSJSDocUnknownType",
157
- "TSUnionType",
158
- "TSIntersectionType",
159
- "TSConditionalType",
160
- "TSInferType",
161
- "TSTypeOperator",
162
- "TSParenthesizedType",
163
- "TSFunctionType",
164
- "TSConstructorType",
165
- "TSTypePredicate",
166
- "TSTypeLiteral",
167
- "TSMappedType",
168
- ],
169
- } as const satisfies Record<string, readonly NodeType[]>;
170
-
171
- export type AliasName = keyof typeof ALIAS_GROUPS;
172
-
173
- type GroupType<A extends AliasName> = (typeof ALIAS_GROUPS)[A][number];
174
-
175
- /** The node union each alias covers, e.g. `AliasMap["Function"]`. */
176
- export type AliasMap = { [A in AliasName]: NodeOfType<GroupType<A>> };
177
-
178
- export const ALIAS_NAMES = Object.keys(ALIAS_GROUPS) as readonly AliasName[];
179
-
180
- // exact two-way agreement with the @yuku-toolchain/types unions
181
- type Same<A, B> = [A] extends [B] ? ([B] extends [A] ? true : false) : false;
182
- type Expect<T extends true> = T;
183
- type _expression = Expect<Same<GroupType<"Expression">, Expression["type"]>>;
184
- type _statement = Expect<Same<GroupType<"Statement">, Statement["type"]>>;
185
- type _declaration = Expect<Same<GroupType<"Declaration">, Declaration["type"]>>;
186
- type _module = Expect<Same<GroupType<"ModuleDeclaration">, ModuleDeclaration["type"]>>;
187
- type _function = Expect<Same<GroupType<"Function">, FunctionType | "ArrowFunctionExpression">>;
188
- type _class = Expect<Same<GroupType<"Class">, ClassType>>;
189
- type _method = Expect<Same<GroupType<"Method">, MethodDefinitionType>>;
190
- type _tstype = Expect<Same<GroupType<"TSType">, TSType["type"]>>;
package/src/builders.ts DELETED
@@ -1,27 +0,0 @@
1
- import type { Node, NodeOfType, NodeType } from "@yuku-toolchain/types";
2
- import { NODE_TYPES } from "./generated.ts";
3
-
4
- // distributes over unions, so b.Literal accepts each literal shape
5
- type BuilderInput<T> = T extends Node
6
- ? Omit<T, "type" | "start" | "end"> & { start?: number; end?: number }
7
- : never;
8
-
9
- type Builders = {
10
- [K in NodeType]: (fields: BuilderInput<NodeOfType<K>>) => NodeOfType<K>;
11
- };
12
-
13
- /**
14
- * One constructor per node type, its fields derived from the node type
15
- * itself. `start` and `end` default to 0, which `ctx.replace` treats
16
- * as span-less and fills from the replaced node.
17
- *
18
- * @example
19
- * b.Identifier({ name: "x" })
20
- * b.ExpressionStatement({ expression: b.Identifier({ name: "x" }) })
21
- */
22
- export const b = Object.fromEntries(
23
- NODE_TYPES.map((type) => [
24
- type,
25
- (fields: Record<string, unknown>) => ({ type, start: 0, end: 0, ...fields }),
26
- ]),
27
- ) as unknown as Builders;
package/src/context.ts DELETED
@@ -1,110 +0,0 @@
1
- import type { Node } from "@yuku-toolchain/types";
2
-
3
- interface Frame {
4
- i: number;
5
- }
6
-
7
- /**
8
- * The walk context: one reused object exposing the current position and
9
- * the tree mutation operations. Valid only during the visit that
10
- * receives it, do not store it. Underscore members are engine state.
11
- */
12
- export class WalkContext<T extends Node = Node, S = unknown> {
13
- _ancestors: Node[] = [];
14
- _node: Node | null = null;
15
- _key: string | null = null;
16
- _list: Node[] | null = null;
17
- _frame: Frame | null = null;
18
- _skip = false;
19
- _stopped = false;
20
- _removed = false;
21
- _replacement: Node | null = null;
22
- /** State threaded through the walk, the third `walk` argument. */
23
- state!: S;
24
-
25
- /** The node being visited. */
26
- get node(): T {
27
- return this._node as T;
28
- }
29
-
30
- /** The node holding {@link node}, or null at the walk root. */
31
- get parent(): Node | null {
32
- const a = this._ancestors;
33
- return a.length === 0 ? null : a[a.length - 1]!;
34
- }
35
-
36
- /** The field on {@link parent} holding {@link node}, or null at the root. */
37
- get key(): string | null {
38
- return this._key;
39
- }
40
-
41
- /** Index within an array field, or null in a plain field. */
42
- get index(): number | null {
43
- return this._list === null ? null : this._frame!.i;
44
- }
45
-
46
- /** Ancestors from the walk root down to {@link parent}. */
47
- ancestors(): Node[] {
48
- return [...this._ancestors];
49
- }
50
-
51
- /** Do not descend into the current node's children. */
52
- skip(): void {
53
- this._skip = true;
54
- }
55
-
56
- /** Stop the walk entirely. */
57
- stop(): void {
58
- this._stopped = true;
59
- }
60
-
61
- /**
62
- * Replace the current node. The walk continues into the replacement's
63
- * children and `leave` fires for the replacement's type. A synthetic
64
- * node with `start === 0 && end === 0` inherits the original span,
65
- * for source maps. Throws at the walk root.
66
- */
67
- replace(node: Node): void {
68
- if (node === null || typeof node !== "object" || typeof node.type !== "string") {
69
- throw new TypeError("replace: expected an AST node");
70
- }
71
- if (this.parent === null) {
72
- throw new TypeError("replace: cannot replace the walk root");
73
- }
74
- if (node.start === 0 && node.end === 0) {
75
- node.start = this.node.start;
76
- node.end = this.node.end;
77
- }
78
- this._replacement = node;
79
- }
80
-
81
- /**
82
- * Remove the current node from its parent: spliced from array fields,
83
- * nulled in plain fields. Children are not walked and `leave` does
84
- * not fire. Throws at the walk root.
85
- */
86
- remove(): void {
87
- if (this.parent === null) {
88
- throw new TypeError("remove: cannot remove the walk root");
89
- }
90
- this._removed = true;
91
- }
92
-
93
- /** Insert a sibling before the current node, not visited. Array fields only. */
94
- insertBefore(node: Node): void {
95
- this.#insert(node, 0);
96
- this._frame!.i++;
97
- }
98
-
99
- /** Insert a sibling after the current node, visited by the walk. Array fields only. */
100
- insertAfter(node: Node): void {
101
- this.#insert(node, 1);
102
- }
103
-
104
- #insert(node: Node, offset: number): void {
105
- if (this._list === null) {
106
- throw new TypeError("insertBefore/insertAfter require a node in an array field");
107
- }
108
- this._list.splice(this._frame!.i + offset, 0, node);
109
- }
110
- }