Skip to content

Commit 49f94a7

Browse files
committed
transformChildren
1 parent 2b006ae commit 49f94a7

File tree

1 file changed

+32
-48
lines changed

1 file changed

+32
-48
lines changed

src/transform-node.ts

Lines changed: 32 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -65,9 +65,13 @@ class Transformer extends Source {
6565
const ancestors = options.ancestors;
6666
const transformChild = <T extends NGNode>(child: angular.AST) =>
6767
this.transform<T>(child, { ancestors: [node, ...ancestors] });
68+
const transformChildren = <T extends NGNode>(children: angular.AST[]) =>
69+
children.map((child) => transformChild<T>(child));
6870
const createNode = <T extends NGNode>(
6971
properties: Partial<T> & { type: T['type'] },
70-
) => this.#create(properties, node, ancestors);
72+
location: angular.AST | RawNGSpan | [number, number] = node,
73+
ancestorsToCreate: angular.AST[] = ancestors,
74+
) => this.#create(properties, location, ancestorsToCreate);
7175

7276
if (node instanceof angular.Interpolation) {
7377
const { expressions } = node;
@@ -91,9 +95,10 @@ class Transformer extends Source {
9195

9296
if (node instanceof angular.Binary) {
9397
const { operation: operator } = node;
94-
const [left, right] = [node.left, node.right].map((node) =>
95-
transformChild<babel.Expression>(node),
96-
);
98+
const [left, right] = transformChildren<babel.Expression>([
99+
node.left,
100+
node.right,
101+
]);
97102

98103
if (operator === '&&' || operator === '||' || operator === '??') {
99104
return createNode<babel.LogicalExpression>({
@@ -129,14 +134,11 @@ class Transformer extends Source {
129134
/\S/,
130135
super.getCharacterIndex('|', leftEnd) + 1,
131136
);
132-
const right = this.#create<babel.Identifier>(
133-
{ type: 'Identifier', name },
134-
[rightStart, rightStart + name.length],
135-
ancestors,
136-
);
137-
const arguments_ = node.args.map<babel.Expression>((node) =>
138-
transformChild(node),
139-
);
137+
const right = createNode<babel.Identifier>({ type: 'Identifier', name }, [
138+
rightStart,
139+
rightStart + name.length,
140+
]);
141+
const arguments_ = transformChildren<babel.Expression>(node.args);
140142
return createNode<NGPipeExpression>({
141143
type: 'NGPipeExpression',
142144
left,
@@ -148,18 +150,14 @@ class Transformer extends Source {
148150
if (node instanceof angular.Chain) {
149151
return createNode<NGChainedExpression>({
150152
type: 'NGChainedExpression',
151-
expressions: node.expressions.map<babel.Expression>((node) =>
152-
transformChild(node),
153-
),
153+
expressions: transformChildren<babel.Expression>(node.expressions),
154154
});
155155
}
156156

157157
if (node instanceof angular.Conditional) {
158-
const [test, consequent, alternate] = [
159-
node.condition,
160-
node.trueExp,
161-
node.falseExp,
162-
].map((node) => transformChild<babel.Expression>(node));
158+
const [test, consequent, alternate] = transformChildren<babel.Expression>(
159+
[node.condition, node.trueExp, node.falseExp],
160+
);
163161

164162
return createNode<babel.ConditionalExpression>({
165163
type: 'ConditionalExpression',
@@ -180,9 +178,7 @@ class Transformer extends Source {
180178
if (node instanceof angular.LiteralArray) {
181179
return createNode<babel.ArrayExpression>({
182180
type: 'ArrayExpression',
183-
elements: node.expressions.map<babel.Expression>((node) =>
184-
transformChild(node),
185-
),
181+
elements: transformChildren<babel.Expression>(node.expressions),
186182
});
187183
}
188184

@@ -207,15 +203,15 @@ class Transformer extends Source {
207203
super.getCharacterLastIndex(':', valueStart - 1) - 1,
208204
) + 1;
209205
const tKey = quoted
210-
? this.#create<babel.StringLiteral>(
206+
? createNode<babel.StringLiteral>(
211207
{
212208
type: 'StringLiteral',
213209
value: key,
214210
},
215211
[keyStart, keyEnd],
216212
[],
217213
)
218-
: this.#create<babel.Identifier>(
214+
: createNode<babel.Identifier>(
219215
{
220216
type: 'Identifier',
221217
name: key,
@@ -226,7 +222,7 @@ class Transformer extends Source {
226222
const shorthand = tKey.end < tKey.start || keyStart === valueStart;
227223
const value = transformChild<babel.Expression>(values[index]);
228224

229-
return this.#create<babel.ObjectProperty>(
225+
return createNode<babel.ObjectProperty>(
230226
{
231227
type: 'ObjectProperty',
232228
key: tKey,
@@ -286,9 +282,7 @@ class Transformer extends Source {
286282
}
287283

288284
if (node instanceof angular.Call || node instanceof angular.SafeCall) {
289-
const arguments_ = node.args.map<babel.Expression>((node) =>
290-
transformChild(node),
291-
);
285+
const arguments_ = transformChildren<babel.Expression>(node.args);
292286
const callee = transformChild<babel.Expression>(node.receiver);
293287
const isOptionalReceiver = isOptionalObjectOrCallee(callee);
294288
const isOptional = node instanceof angular.SafeCall;
@@ -350,17 +344,16 @@ class Transformer extends Source {
350344
start = index;
351345
}
352346

353-
const expression = this.transform<babel.Expression>(node.expression);
347+
const expression = transformChild<babel.Expression>(node.expression);
354348

355-
return this.#create<babel.UnaryExpression>(
349+
return createNode<babel.UnaryExpression>(
356350
{
357351
type: 'UnaryExpression',
358352
prefix: true,
359353
operator,
360354
argument: expression,
361355
},
362356
[start, node.sourceSpan.end],
363-
ancestors,
364357
);
365358
}
366359

@@ -389,11 +382,8 @@ class Transformer extends Source {
389382
const { name, nameSpan } = node;
390383

391384
isImplicitThis = node.sourceSpan.start === nameSpan.start;
392-
property = this.#create<babel.Identifier>(
393-
{
394-
type: 'Identifier',
395-
name,
396-
},
385+
property = createNode<babel.Identifier>(
386+
{ type: 'Identifier', name },
397387
node.nameSpan,
398388
isImplicitThis ? ancestors : [],
399389
);
@@ -403,7 +393,7 @@ class Transformer extends Source {
403393
return property;
404394
}
405395

406-
const object = this.transform<babel.Expression>(receiver);
396+
const object = transformChild<babel.Expression>(receiver);
407397
const isOptionalObject = isOptionalObjectOrCallee(object);
408398

409399
if (isOptional || isOptionalObject) {
@@ -444,10 +434,8 @@ class Transformer extends Source {
444434
if (node instanceof angular.TemplateLiteral) {
445435
return createNode<babel.TemplateLiteral>({
446436
type: 'TemplateLiteral',
447-
quasis: node.elements.map((element) => transformChild(element)),
448-
expressions: node.expressions.map((expression) =>
449-
transformChild(expression),
450-
),
437+
quasis: transformChildren(node.elements),
438+
expressions: transformChildren(node.expressions),
451439
});
452440
}
453441

@@ -462,17 +450,13 @@ class Transformer extends Source {
462450
const start = node.sourceSpan.start + (isFirst ? 1 : 0);
463451
const raw = this.text.slice(start, end);
464452

465-
return this.#create<babel.TemplateElement>(
453+
return createNode<babel.TemplateElement>(
466454
{
467455
type: 'TemplateElement',
468-
value: {
469-
cooked: node.text,
470-
raw,
471-
},
456+
value: { cooked: node.text, raw },
472457
tail: isLast,
473458
},
474459
[start, end],
475-
ancestors,
476460
);
477461
}
478462

0 commit comments

Comments
 (0)