Skip to content

Commit

Permalink
Fix SyntaxError when computed keys have invalid javascript identifier
Browse files Browse the repository at this point in the history
  • Loading branch information
icy0307 committed Apr 4, 2023
1 parent 7ec95e0 commit 102cff8
Show file tree
Hide file tree
Showing 6 changed files with 8 additions and 7 deletions.
4 changes: 2 additions & 2 deletions src/transformers/chunk/literal-computed-keys.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,11 +43,11 @@ export default class LiteralComputedKeys extends ChunkTransform implements Trans
if (isProperty(property) && property.computed && property.key.type === 'Literal') {
const [propertyStart]: Range = property.range as Range;
const [valueStart]: Range = property.value.range as Range;

const keyValue = property.key.raw ?? property.key.value;
source.overwrite(
propertyStart,
valueStart,
`${property.key.value}${property.value.type !== 'FunctionExpression' ? ':' : ''}`,
`${keyValue}${property.value.type !== 'FunctionExpression' ? ':' : ''}`,
);
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1 +1 @@
console.log({0:"value",1:"value",2:"value2",3:"value3",4(a){console.log(a)},5(a){console.log(a)}})
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 number Diff line number Diff line change
@@ -1 +1 @@
console.log({0:"value",1:"value",2:"value2",3:"value3",4(a){console.log(a)},5(a){console.log(a)}})
console.log({0:"value",1:"value",2:"value2",3:"value3",4(a){console.log(a)},5(a){console.log(a)},"6#":"value6"})
2 changes: 1 addition & 1 deletion test/literal-computed-keys/fixtures/mixed-keys.esm.es5.js
Original file line number Diff line number Diff line change
@@ -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))
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))
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@ console.log({0:"value", 1:"value", 2:"value2", 3:"value3", 4(a) {
console.log(a);
}, 5(a) {
console.log(a);
}})
}, "6#":"value6"})
3 changes: 2 additions & 1 deletion test/literal-computed-keys/fixtures/mixed-keys.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,6 @@ console.log({
},
5(value) {
console.log(bar(value));
}
},
['6#']: 'value6'
});

0 comments on commit 102cff8

Please sign in to comment.