UNPKG

2.73 kBMarkdownView Raw
1# eslint-visitor-keys
2
3[![npm version](https://img.shields.io/npm/v/eslint-visitor-keys.svg)](https://www.npmjs.com/package/eslint-visitor-keys)
4[![Downloads/month](https://img.shields.io/npm/dm/eslint-visitor-keys.svg)](http://www.npmtrends.com/eslint-visitor-keys)
5[![Build Status](https://github.com/eslint/eslint-visitor-keys/workflows/CI/badge.svg)](https://github.com/eslint/eslint-visitor-keys/actions)
6
7Constants and utilities about visitor keys to traverse AST.
8
9## 💿 Installation
10
11Use [npm] to install.
12
13```bash
14$ npm install eslint-visitor-keys
15```
16
17### Requirements
18
19- [Node.js] `^18.18.0`, `^20.9.0`, or `>=21.1.0`
20
21
22## 📖 Usage
23
24To use in an ESM file:
25
26```js
27import * as evk from "eslint-visitor-keys"
28```
29
30To use in a CommonJS file:
31
32```js
33const evk = require("eslint-visitor-keys")
34```
35
36### evk.KEYS
37
38> type: `{ [type: string]: string[] | undefined }`
39
40Visitor keys. This keys are frozen.
41
42This is an object. Keys are the type of [ESTree] nodes. Their values are an array of property names which have child nodes.
43
44For example:
45
46```
47console.log(evk.KEYS.AssignmentExpression) // → ["left", "right"]
48```
49
50### evk.getKeys(node)
51
52> type: `(node: object) => string[]`
53
54Get the visitor keys of a given AST node.
55
56This is similar to `Object.keys(node)` of ES Standard, but some keys are excluded: `parent`, `leadingComments`, `trailingComments`, and names which start with `_`.
57
58This will be used to traverse unknown nodes.
59
60For example:
61
62```js
63const node = {
64 type: "AssignmentExpression",
65 left: { type: "Identifier", name: "foo" },
66 right: { type: "Literal", value: 0 }
67}
68console.log(evk.getKeys(node)) // → ["type", "left", "right"]
69```
70
71### evk.unionWith(additionalKeys)
72
73> type: `(additionalKeys: object) => { [type: string]: string[] | undefined }`
74
75Make the union set with `evk.KEYS` and the given keys.
76
77- The order of keys is, `additionalKeys` is at first, then `evk.KEYS` is concatenated after that.
78- It removes duplicated keys as keeping the first one.
79
80For example:
81
82```js
83console.log(evk.unionWith({
84 MethodDefinition: ["decorators"]
85})) // → { ..., MethodDefinition: ["decorators", "key", "value"], ... }
86```
87
88## 📰 Change log
89
90See [GitHub releases](https://github.com/eslint/eslint-visitor-keys/releases).
91
92## 🍻 Contributing
93
94Welcome. See [ESLint contribution guidelines](https://eslint.org/docs/developer-guide/contributing/).
95
96### Development commands
97
98- `npm test` runs tests and measures code coverage.
99- `npm run lint` checks source codes with ESLint.
100- `npm run test:open-coverage` opens the code coverage report of the previous test with your default browser.
101
102
103[npm]: https://www.npmjs.com/
104[Node.js]: https://nodejs.org/
105[ESTree]: https://github.com/estree/estree