Skip to content

Commit d179d86

Browse files
committed
🐛 fix for literal property names
1 parent 8a95743 commit d179d86

File tree

2 files changed

+22
-4
lines changed

2 files changed

+22
-4
lines changed

src/get-static-value.js

+18-4
Original file line numberDiff line numberDiff line change
@@ -535,10 +535,24 @@ function getStaticValueR(node, initialScope) {
535535
* @returns {{value:any}|{value:undefined,optional?:true}|null} The static value of the property name of the node, or `null`.
536536
*/
537537
function getStaticPropertyNameValue(node, initialScope) {
538-
const propertyKey = node.type === "Property" ? node.key : node.property
539-
return node.computed
540-
? getStaticValueR(propertyKey, initialScope)
541-
: { value: propertyKey.name }
538+
const nameNode = node.type === "Property" ? node.key : node.property
539+
540+
if (node.computed) {
541+
return getStaticValueR(nameNode, initialScope)
542+
}
543+
544+
if (nameNode.type === "Identifier") {
545+
return { value: nameNode.name }
546+
}
547+
548+
if (nameNode.type === "Literal") {
549+
if (nameNode.bigint) {
550+
return { value: nameNode.bigint }
551+
}
552+
return { value: String(nameNode.value) }
553+
}
554+
555+
return null
542556
}
543557

544558
/**

test/get-static-value.js

+4
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,10 @@ describe("The 'getStaticValue' function", () => {
9191
code: "const obj = {b: 2}; ({a: 1, ...obj})",
9292
expected: { value: { a: 1, b: 2 } },
9393
},
94+
{
95+
code: "({'a': 1, 1e+1: 2, 2n: 3})",
96+
expected: { value: { a: 1, "10": 2, "2": 3 } },
97+
},
9498
{ code: "var obj = {b: 2}; ({a: 1, ...obj})", expected: null },
9599
{ code: "({ get a() {} })", expected: null },
96100
{ code: "({ a })", expected: null },

0 commit comments

Comments
 (0)