Skip to content

Commit b66a302

Browse files
committed
Simplify BindingPipe transform
1 parent fa27333 commit b66a302

File tree

3 files changed

+150
-15
lines changed

3 files changed

+150
-15
lines changed

src/transform-node.ts

Lines changed: 7 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -127,23 +127,14 @@ class Transformer extends Source {
127127
}
128128

129129
if (node instanceof angular.BindingPipe) {
130-
const { name } = node;
131-
const left = transformChild<babel.Expression>(node.exp);
132-
const leftEnd = node.exp.sourceSpan.end;
133-
const rightStart = super.getCharacterIndex(
134-
/\S/,
135-
super.getCharacterIndex('|', leftEnd) + 1,
136-
);
137-
const right = createNode<babel.Identifier>({ type: 'Identifier', name }, [
138-
rightStart,
139-
rightStart + name.length,
140-
]);
141-
const arguments_ = transformChildren<babel.Expression>(node.args);
142130
return createNode<NGPipeExpression>({
143131
type: 'NGPipeExpression',
144-
left,
145-
right,
146-
arguments: arguments_,
132+
left: transformChild<babel.Expression>(node.exp),
133+
right: createNode<babel.Identifier>(
134+
{ type: 'Identifier', name: node.name },
135+
node.nameSpan,
136+
),
137+
arguments: transformChildren<babel.Expression>(node.args),
147138
});
148139
}
149140

@@ -331,6 +322,7 @@ class Transformer extends Source {
331322

332323
let { start } = node.sourceSpan;
333324

325+
// https://github.com/angular/angular/issues/66174
334326
if (operator === 'typeof' || operator === 'void') {
335327
const index = this.text.lastIndexOf(operator, start);
336328

tests/__snapshots__/transform.test.ts.snap

Lines changed: 136 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,74 @@ Identifier {
3232
| ^
3333
`;
3434

35+
exports[`('BindingPipe' -> 'NGPipeExpression') > parseBinding(" a | b : ( ( c ) ) ") 1`] = `
36+
NGPipeExpression {
37+
"left": "Identifier",
38+
"right": "Identifier",
39+
"arguments": [
40+
"Identifier"
41+
],
42+
"comments": []
43+
}
44+
> 1 | a | b : ( ( c ) )
45+
| ^^^^^^^^^^^^^^^^^
46+
--------------------------------------------------------------------------------
47+
Identifier {
48+
"name": "a"
49+
}
50+
> 1 | a | b : ( ( c ) )
51+
| ^
52+
--------------------------------------------------------------------------------
53+
Identifier {
54+
"name": "b"
55+
}
56+
> 1 | a | b : ( ( c ) )
57+
| ^
58+
--------------------------------------------------------------------------------
59+
Identifier {
60+
"name": "c",
61+
"extra": {
62+
"parenthesized": true
63+
}
64+
}
65+
> 1 | a | b : ( ( c ) )
66+
| ^
67+
`;
68+
69+
exports[`('BindingPipe' -> 'NGPipeExpression') > parseBinding(" a | b : (c) ") 1`] = `
70+
NGPipeExpression {
71+
"left": "Identifier",
72+
"right": "Identifier",
73+
"arguments": [
74+
"Identifier"
75+
],
76+
"comments": []
77+
}
78+
> 1 | a | b : (c)
79+
| ^^^^^^^^^^^
80+
--------------------------------------------------------------------------------
81+
Identifier {
82+
"name": "a"
83+
}
84+
> 1 | a | b : (c)
85+
| ^
86+
--------------------------------------------------------------------------------
87+
Identifier {
88+
"name": "b"
89+
}
90+
> 1 | a | b : (c)
91+
| ^
92+
--------------------------------------------------------------------------------
93+
Identifier {
94+
"name": "c",
95+
"extra": {
96+
"parenthesized": true
97+
}
98+
}
99+
> 1 | a | b : (c)
100+
| ^
101+
`;
102+
35103
exports[`('BindingPipe' -> 'NGPipeExpression') > parseBinding(" a | b : c ") 1`] = `
36104
NGPipeExpression {
37105
"left": "Identifier",
@@ -95,6 +163,74 @@ Identifier {
95163
| ^
96164
`;
97165

166+
exports[`('BindingPipe' -> 'NGPipeExpression') > parseInterpolationExpression(" a | b : ( ( c ) ) ") 1`] = `
167+
NGPipeExpression {
168+
"left": "Identifier",
169+
"right": "Identifier",
170+
"arguments": [
171+
"Identifier"
172+
],
173+
"comments": []
174+
}
175+
> 1 | a | b : ( ( c ) )
176+
| ^^^^^^^^^^^^^^^^^
177+
--------------------------------------------------------------------------------
178+
Identifier {
179+
"name": "a"
180+
}
181+
> 1 | a | b : ( ( c ) )
182+
| ^
183+
--------------------------------------------------------------------------------
184+
Identifier {
185+
"name": "b"
186+
}
187+
> 1 | a | b : ( ( c ) )
188+
| ^
189+
--------------------------------------------------------------------------------
190+
Identifier {
191+
"name": "c",
192+
"extra": {
193+
"parenthesized": true
194+
}
195+
}
196+
> 1 | a | b : ( ( c ) )
197+
| ^
198+
`;
199+
200+
exports[`('BindingPipe' -> 'NGPipeExpression') > parseInterpolationExpression(" a | b : (c) ") 1`] = `
201+
NGPipeExpression {
202+
"left": "Identifier",
203+
"right": "Identifier",
204+
"arguments": [
205+
"Identifier"
206+
],
207+
"comments": []
208+
}
209+
> 1 | a | b : (c)
210+
| ^^^^^^^^^^^
211+
--------------------------------------------------------------------------------
212+
Identifier {
213+
"name": "a"
214+
}
215+
> 1 | a | b : (c)
216+
| ^
217+
--------------------------------------------------------------------------------
218+
Identifier {
219+
"name": "b"
220+
}
221+
> 1 | a | b : (c)
222+
| ^
223+
--------------------------------------------------------------------------------
224+
Identifier {
225+
"name": "c",
226+
"extra": {
227+
"parenthesized": true
228+
}
229+
}
230+
> 1 | a | b : (c)
231+
| ^
232+
`;
233+
98234
exports[`('BindingPipe' -> 'NGPipeExpression') > parseInterpolationExpression(" a | b : c ") 1`] = `
99235
NGPipeExpression {
100236
"left": "Identifier",

tests/transform.test.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,13 @@ const testCases: TestCase[] = [
110110
parseAction: false,
111111
parseSimpleBinding: false,
112112
},
113+
{
114+
expectedAngularType: 'BindingPipe',
115+
expectedEstreeType: 'NGPipeExpression',
116+
text: ' a | b : ( ( c ) ) ',
117+
parseAction: false,
118+
parseSimpleBinding: false,
119+
},
113120
{
114121
expectedAngularType: 'Chain',
115122
expectedEstreeType: 'NGChainedExpression',

0 commit comments

Comments
 (0)