Skip to content

Commit fa27333

Browse files
committed
Simplify LiteralMap transform
1 parent e68cbc8 commit fa27333

File tree

1 file changed

+36
-30
lines changed

1 file changed

+36
-30
lines changed

src/transform-node.ts

Lines changed: 36 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -185,48 +185,54 @@ class Transformer extends Source {
185185
if (node instanceof angular.LiteralMap) {
186186
const { keys, values } = node;
187187
const tProperties = keys.map((property, index) => {
188-
const { key, quoted } = property;
188+
const { key, quoted, isShorthandInitialized = false } = property;
189189
const { start: valueStart, end: valueEnd } = values[index].sourceSpan;
190-
191-
const keyStart = super.getCharacterIndex(
192-
/\S/,
193-
index === 0
194-
? node.sourceSpan.start + 1 // {
195-
: super.getCharacterIndex(',', values[index - 1].sourceSpan.end) +
196-
1,
197-
);
198-
const keyEnd =
199-
valueStart === keyStart
200-
? valueEnd
201-
: super.getCharacterLastIndex(
202-
/\S/,
203-
super.getCharacterLastIndex(':', valueStart - 1) - 1,
204-
) + 1;
205-
const tKey = quoted
206-
? createNode<babel.StringLiteral>(
207-
{ type: 'StringLiteral', value: key },
208-
[keyStart, keyEnd],
209-
[],
210-
)
211-
: createNode<babel.Identifier>(
212-
{ type: 'Identifier', name: key },
213-
[keyStart, keyEnd],
214-
[],
215-
);
216-
const shorthand = tKey.end < tKey.start || keyStart === valueStart;
217190
const value = transformChild<babel.Expression>(values[index]);
218191

192+
let tKey: (babel.StringLiteral | babel.Identifier) &
193+
LocationInformation;
194+
195+
if (isShorthandInitialized) {
196+
tKey = transformChild<babel.Identifier>(values[index]);
197+
} else {
198+
const keyStart = super.getCharacterIndex(
199+
/\S/,
200+
index === 0
201+
? node.sourceSpan.start + 1 // {
202+
: super.getCharacterIndex(',', values[index - 1].sourceSpan.end) +
203+
1,
204+
);
205+
const keyEnd =
206+
valueStart === keyStart
207+
? valueEnd
208+
: super.getCharacterLastIndex(
209+
/\S/,
210+
super.getCharacterLastIndex(':', valueStart - 1) - 1,
211+
) + 1;
212+
tKey = quoted
213+
? createNode<babel.StringLiteral>(
214+
{ type: 'StringLiteral', value: key },
215+
[keyStart, keyEnd],
216+
[],
217+
)
218+
: createNode<babel.Identifier>(
219+
{ type: 'Identifier', name: key },
220+
[keyStart, keyEnd],
221+
[],
222+
);
223+
}
224+
219225
return createNode<babel.ObjectPropertyNonComputed>(
220226
{
221227
type: 'ObjectProperty',
222228
key: tKey,
223229
value,
224-
shorthand,
230+
shorthand: isShorthandInitialized,
225231
computed: false,
226232
// @ts-expect-error -- Missed in types
227233
method: false,
228234
},
229-
[tKey.start, valueEnd],
235+
[tKey.range[0], valueEnd],
230236
[],
231237
);
232238
});

0 commit comments

Comments
 (0)