Skip to content

Commit

Permalink
🐛 fix for literal property names
Browse files Browse the repository at this point in the history
  • Loading branch information
mysticatea committed May 14, 2021
1 parent 8a95743 commit d179d86
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 4 deletions.
22 changes: 18 additions & 4 deletions src/get-static-value.js
Original file line number Diff line number Diff line change
Expand Up @@ -535,10 +535,24 @@ function getStaticValueR(node, initialScope) {
* @returns {{value:any}|{value:undefined,optional?:true}|null} The static value of the property name of the node, or `null`.
*/
function getStaticPropertyNameValue(node, initialScope) {
const propertyKey = node.type === "Property" ? node.key : node.property
return node.computed
? getStaticValueR(propertyKey, initialScope)
: { value: propertyKey.name }
const nameNode = node.type === "Property" ? node.key : node.property

if (node.computed) {
return getStaticValueR(nameNode, initialScope)
}

if (nameNode.type === "Identifier") {
return { value: nameNode.name }
}

if (nameNode.type === "Literal") {
if (nameNode.bigint) {
return { value: nameNode.bigint }
}
return { value: String(nameNode.value) }
}

return null
}

/**
Expand Down
4 changes: 4 additions & 0 deletions test/get-static-value.js
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,10 @@ describe("The 'getStaticValue' function", () => {
code: "const obj = {b: 2}; ({a: 1, ...obj})",
expected: { value: { a: 1, b: 2 } },
},
{
code: "({'a': 1, 1e+1: 2, 2n: 3})",
expected: { value: { a: 1, "10": 2, "2": 3 } },
},
{ code: "var obj = {b: 2}; ({a: 1, ...obj})", expected: null },
{ code: "({ get a() {} })", expected: null },
{ code: "({ a })", expected: null },
Expand Down

0 comments on commit d179d86

Please sign in to comment.