Skip to content

Commit 102cff8

Browse files
author
icy0307
committed
Fix SyntaxError when computed keys have invalid javascript identifier
close ampproject#465
1 parent 7ec95e0 commit 102cff8

File tree

6 files changed

+8
-7
lines changed

6 files changed

+8
-7
lines changed

src/transformers/chunk/literal-computed-keys.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -43,11 +43,11 @@ export default class LiteralComputedKeys extends ChunkTransform implements Trans
4343
if (isProperty(property) && property.computed && property.key.type === 'Literal') {
4444
const [propertyStart]: Range = property.range as Range;
4545
const [valueStart]: Range = property.value.range as Range;
46-
46+
const keyValue = property.key.raw ?? property.key.value;
4747
source.overwrite(
4848
propertyStart,
4949
valueStart,
50-
`${property.key.value}${property.value.type !== 'FunctionExpression' ? ':' : ''}`,
50+
`${keyValue}${property.value.type !== 'FunctionExpression' ? ':' : ''}`,
5151
);
5252
}
5353
}
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
console.log({0:"value",1:"value",2:"value2",3:"value3",4(a){console.log(a)},5(a){console.log(a)}})
1+
console.log({0:"value",1:"value",2:"value2",3:"value3",4(a){console.log(a)},5(a){console.log(a)},"6#":"value6"})
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
console.log({0:"value",1:"value",2:"value2",3:"value3",4(a){console.log(a)},5(a){console.log(a)}})
1+
console.log({0:"value",1:"value",2:"value2",3:"value3",4(a){console.log(a)},5(a){console.log(a)},"6#":"value6"})
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
var a={};console.log((a["0"]="value",a[1]="value",a[2]="value2",a[3]="value3",a[4]=function(b){console.log(b)},a[5]=function(b){console.log(b)},a))
1+
var a={};console.log((a["0"]="value",a[1]="value",a[2]="value2",a[3]="value3",a[4]=function(b){console.log(b)},a[5]=function(b){console.log(b)},a["6#"]="value6",a))

test/literal-computed-keys/fixtures/mixed-keys.esm.pretty.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,4 @@ console.log({0:"value", 1:"value", 2:"value2", 3:"value3", 4(a) {
33
console.log(a);
44
}, 5(a) {
55
console.log(a);
6-
}})
6+
}, "6#":"value6"})

test/literal-computed-keys/fixtures/mixed-keys.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -12,5 +12,6 @@ console.log({
1212
},
1313
5(value) {
1414
console.log(bar(value));
15-
}
15+
},
16+
['6#']: 'value6'
1617
});

0 commit comments

Comments
 (0)