Skip to content

Commit 32fd9fa

Browse files
committed
New: add getStaticValue function
1 parent 7c84fdc commit 32fd9fa

File tree

7 files changed

+624
-55
lines changed

7 files changed

+624
-55
lines changed

docs/api/ast-utils.md

Lines changed: 54 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -260,7 +260,7 @@ module.exports = {
260260
create(context) {
261261
return {
262262
MemberExpression(node) {
263-
const name = getPropertyName(node)
263+
const name = getPropertyName(node, context.getScope())
264264
},
265265
}
266266
},
@@ -269,42 +269,81 @@ module.exports = {
269269

270270
----
271271

272-
## getStringIfConstant
272+
## getStaticValue
273273

274274
```js
275-
const str1 = utils.getStringIfConstant(node)
276-
const str2 = utils.getStringIfConstant(node, initialScope)
275+
const ret1 = utils.getStaticValue(node)
276+
const ret2 = utils.getStaticValue(node, initialScope)
277277
```
278278

279-
Get the string value of a given literal node.
279+
Get the value of a given node if it can decide the value statically.
280+
281+
If the 2nd parameter `initialScope` was given, this function tries to resolve identifier references which are in the given node as much as possible.
282+
In the resolving way, it does on the assumption that built-in global objects have not been modified.
283+
For example, it considers `Symbol.iterator`, ``String.raw`hello` ``, and `Object.freeze({a: 1}).a` as static.
284+
285+
For another complex example, this function can evaluate the following cases on AST:
286+
287+
```js{6}
288+
const eventName = "click"
289+
const aMap = Object.freeze({
290+
click: 777
291+
})
292+
293+
;`on${eventName} : ${aMap[eventName]}` // evaluated to "onclick : 777"
294+
```
280295

281296
### Parameters
282297

283298
Name | Type | Description
284299
:-----|:-----|:------------
285-
node | Node | The node to get that string value.
286-
initialScope | Scope or undefined | Optional. The scope object to find variables. If this scope was given and the node is an Identifier node, it finds the variable of the identifier, and if the variable is a `const` variable, it returns the value of the `const` variable.
300+
node | Node | The node to get that the value.
301+
initialScope | Scope or undefined | Optional. The scope object to find variables.
287302

288303
### Return value
289304

290-
The string value of the node.
291-
If the node is not constant then it returns `null`.
305+
The `{ value: any }` shaped object. The `value` property is the static value.
306+
307+
If it couldn't compute the static value of the node, it returns `null`.
292308

293309
### Example
294310

295-
```js{9}
296-
const { getStringIfConstant } = require("eslint-utils")
311+
```js{8}
312+
const { getStaticValue } = require("eslint-utils")
297313
298314
module.exports = {
299315
meta: {},
300316
create(context) {
301317
return {
302-
MemberExpression(node) {
303-
const name = node.computed
304-
? getStringIfConstant(node.property)
305-
: node.property.name
318+
ExpressionStatement(node) {
319+
const evaluated = getStaticValue(node, context.getScope())
320+
if (evaluated) {
321+
const staticValue = evaluated.value
322+
// ...
323+
}
306324
},
307325
}
308326
},
309327
}
310328
```
329+
330+
----
331+
332+
## getStringIfConstant
333+
334+
```js
335+
const str1 = utils.getStringIfConstant(node)
336+
const str2 = utils.getStringIfConstant(node, initialScope)
337+
```
338+
339+
Get the string value of a given node.
340+
341+
This function is a tiny wrapper of the [getStaticValue](#getstaticvalue) function.
342+
I.e., this is the same as below:
343+
344+
```js
345+
function getStringIfConstant(node, initialScope) {
346+
const evaluated = getStaticValue(node, initialScope)
347+
return evaluated && String(evaluated.value)
348+
}
349+
```

0 commit comments

Comments
 (0)