Skip to content

Commit 825fd9d

Browse files
committed
Minor refactor
1 parent 20ed95f commit 825fd9d

File tree

1 file changed

+44
-22
lines changed

1 file changed

+44
-22
lines changed

src/ast-transform/transform.ts

Lines changed: 44 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,26 @@
1-
import * as angular from '@angular/compiler';
1+
import {
2+
type AST,
3+
type ASTWithSource,
4+
EmptyExpr,
5+
ParenthesizedExpression,
6+
} from '@angular/compiler';
27

38
import { Source } from '../source.ts';
49
import type { NGEmptyExpression, NGNode, RawNGSpan } from '../types.ts';
510
import { transformVisitor } from './visitor.ts';
611

7-
class Transformer extends Source {
8-
node: angular.AST;
9-
ancestors: angular.AST[];
12+
class NodeTransformer extends Source {
13+
node: AST;
14+
ancestors: AST[];
1015

1116
constructor({
1217
node,
1318
text,
1419
ancestors,
1520
}: {
16-
node: angular.AST;
21+
node: AST;
1722
text: string;
18-
ancestors: angular.AST[];
23+
ancestors: AST[];
1924
}) {
2025
super(text);
2126
this.node = node;
@@ -24,12 +29,12 @@ class Transformer extends Source {
2429

2530
create<T extends NGNode>(
2631
properties: Partial<T> & { type: T['type'] },
27-
location: angular.AST | RawNGSpan | [number, number],
28-
ancestors: angular.AST[],
32+
location: AST | RawNGSpan | [number, number],
33+
ancestors: AST[],
2934
) {
3035
const node = super.createNode(properties, location);
3136

32-
if (ancestors[0] instanceof angular.ParenthesizedExpression) {
37+
if (ancestors[0] instanceof ParenthesizedExpression) {
3338
node.extra = {
3439
...node.extra,
3540
parenthesized: true,
@@ -41,27 +46,27 @@ class Transformer extends Source {
4146

4247
createNode<T extends NGNode>(
4348
properties: Partial<T> & { type: T['type'] },
44-
location: angular.AST | RawNGSpan | [number, number] = this.node,
45-
ancestorsToCreate: angular.AST[] = this.ancestors,
49+
location: AST | RawNGSpan | [number, number] = this.node,
50+
ancestorsToCreate: AST[] = this.ancestors,
4651
) {
4752
return this.create(properties, location, ancestorsToCreate);
4853
}
4954

50-
transformChild<T extends NGNode>(child: angular.AST) {
51-
return new Transformer({
55+
transformChild<T extends NGNode>(child: AST) {
56+
return new NodeTransformer({
5257
node: child,
5358
ancestors: [this.node, ...this.ancestors],
5459
text: this.text,
5560
}).transform<T>();
5661
}
5762

58-
transformChildren<T extends NGNode>(children: angular.AST[]) {
63+
transformChildren<T extends NGNode>(children: AST[]) {
5964
return children.map((child) => this.transformChild<T>(child));
6065
}
6166

6267
transform<T extends NGNode = NGNode>() {
6368
const { node } = this;
64-
if (node instanceof angular.EmptyExpr) {
69+
if (node instanceof EmptyExpr) {
6570
return this.createNode<NGEmptyExpression>(
6671
{ type: 'NGEmptyExpression' },
6772
node.sourceSpan,
@@ -82,17 +87,34 @@ class Transformer extends Source {
8287
return estreeNode as T;
8388
}
8489

85-
static transform(node: angular.AST, text: string) {
86-
return new Transformer({ node, text, ancestors: [] }).transform();
90+
static transform(node: AST, text: string) {
91+
return new NodeTransformer({ node, text, ancestors: [] }).transform();
8792
}
8893
}
8994

90-
const transformAstNode = (node: angular.AST, text: string) => {
91-
return Transformer.transform(node, text);
95+
class AstTransformer {
96+
#ast;
97+
98+
constructor(ast: ASTWithSource) {
99+
this.#ast = ast;
100+
}
101+
102+
transform() {
103+
return NodeTransformer.transform(this.#ast, this.#ast.source!);
104+
}
105+
}
106+
107+
const transformAstNode = (node: AST, text: string) => {
108+
return NodeTransformer.transform(node, text);
92109
};
93110

94-
const transformAst = (ast: angular.ASTWithSource) => {
95-
return Transformer.transform(ast, ast.source!);
111+
const transformAst = (ast: ASTWithSource) => {
112+
return new AstTransformer(ast).transform();
96113
};
97114

98-
export { transformAst, transformAstNode, Transformer };
115+
export {
116+
NodeTransformer,
117+
transformAst,
118+
transformAstNode,
119+
NodeTransformer as Transformer,
120+
};

0 commit comments

Comments
 (0)