Skip to content

Commit df811d1

Browse files
committed
Improve LiteralMap transform
1 parent b66a302 commit df811d1

File tree

2 files changed

+18
-10
lines changed

2 files changed

+18
-10
lines changed

src/transform-node.ts

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -175,6 +175,10 @@ class Transformer extends Source {
175175

176176
if (node instanceof angular.LiteralMap) {
177177
const { keys, values } = node;
178+
const createChild = <T extends NGNode>(
179+
properties: Partial<T> & { type: T['type'] },
180+
location: angular.AST | RawNGSpan | [number, number] = node,
181+
) => this.#create(properties, location, [node, ...ancestors]);
178182
const tProperties = keys.map((property, index) => {
179183
const { key, quoted, isShorthandInitialized = false } = property;
180184
const { start: valueStart, end: valueEnd } = values[index].sourceSpan;
@@ -186,6 +190,8 @@ class Transformer extends Source {
186190
if (isShorthandInitialized) {
187191
tKey = transformChild<babel.Identifier>(values[index]);
188192
} else {
193+
// No location information
194+
// https://github.com/angular/angular/issues/66175
189195
const keyStart = super.getCharacterIndex(
190196
/\S/,
191197
index === 0
@@ -201,19 +207,17 @@ class Transformer extends Source {
201207
super.getCharacterLastIndex(':', valueStart - 1) - 1,
202208
) + 1;
203209
tKey = quoted
204-
? createNode<babel.StringLiteral>(
210+
? createChild<babel.StringLiteral>(
205211
{ type: 'StringLiteral', value: key },
206212
[keyStart, keyEnd],
207-
[],
208213
)
209-
: createNode<babel.Identifier>(
210-
{ type: 'Identifier', name: key },
211-
[keyStart, keyEnd],
212-
[],
213-
);
214+
: createChild<babel.Identifier>({ type: 'Identifier', name: key }, [
215+
keyStart,
216+
keyEnd,
217+
]);
214218
}
215219

216-
return createNode<babel.ObjectPropertyNonComputed>(
220+
return createChild<babel.ObjectPropertyNonComputed>(
217221
{
218222
type: 'ObjectProperty',
219223
key: tKey,
@@ -224,7 +228,6 @@ class Transformer extends Source {
224228
method: false,
225229
},
226230
[tKey.range[0], valueEnd],
227-
[],
228231
);
229232
});
230233
return createNode<babel.ObjectExpression>({

tests/transform.test.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -218,7 +218,12 @@ const testCases: TestCase[] = [
218218
{
219219
expectedAngularType: 'LiteralMap',
220220
expectedEstreeType: 'ObjectExpression',
221-
text: ' ( { a : 1 } ) ',
221+
text: ' { "a" : ( ( 1 ) ) }',
222+
},
223+
{
224+
expectedAngularType: 'LiteralMap',
225+
expectedEstreeType: 'ObjectExpression',
226+
text: ' ( ( { a : 1 } ) ) ',
222227
},
223228
{
224229
expectedAngularType: 'Call',

0 commit comments

Comments
 (0)