UNPKG

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