Skip to content

Commit 904e48f

Browse files
committed
Update: add 2nd parameter to getPropertyName
1 parent ccaba15 commit 904e48f

File tree

2 files changed

+8
-3
lines changed

2 files changed

+8
-3
lines changed

docs/api/ast-utils.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -231,15 +231,19 @@ module.exports = {
231231

232232
```js
233233
const name = utils.getPropertyName(node)
234+
const name = utils.getPropertyName(node, initialScope)
234235
```
235236

236237
Get the property name of a given property node.
237238

239+
If the node is a computed property, this tries to compute the property name by the [getStringIfConstant](#getstringifconstant) function.
240+
238241
### Parameters
239242

240243
Name | Type | Description
241244
:-----|:-----|:------------
242245
node | Node | The node to get that name. This shuld be any of `MemberExpression`, `Property`, and `MethodDefinition` node.
246+
initialScope | Scope or undefined | Optional. The scope object to find variables.
243247

244248
### Return value
245249

src/get-property-name.js

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,20 +3,21 @@ import { getStringIfConstant } from "./get-string-if-constant"
33
/**
44
* Get the property name from a MemberExpression node or a Property node.
55
* @param {Node} node The node to get.
6+
* @param {Scope} [initialScope] The scope to start finding variable. Optional. If the node is a computed property node and this scope was given, this checks the computed property name by the `getStringIfConstant` function with the scope, and returns the value of it.
67
* @returns {string|null} The property name of the node.
78
*/
8-
export function getPropertyName(node) {
9+
export function getPropertyName(node, initialScope) {
910
switch (node.type) {
1011
case "MemberExpression":
1112
if (node.computed) {
12-
return getStringIfConstant(node.property)
13+
return getStringIfConstant(node.property, initialScope)
1314
}
1415
return node.property.name
1516

1617
case "Property":
1718
case "MethodDefinition":
1819
if (node.computed) {
19-
return getStringIfConstant(node.key)
20+
return getStringIfConstant(node.key, initialScope)
2021
}
2122
if (node.key.type === "Literal") {
2223
return String(node.key.value)

0 commit comments

Comments
 (0)