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 |
|
8 | Constants and utilities about visitor keys to traverse AST.
|
9 |
|
10 | ## 💿 Installation
|
11 |
|
12 | Use [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 |
|
25 | To use in an ESM file:
|
26 |
|
27 | ```js
|
28 | import * as evk from "eslint-visitor-keys"
|
29 | ```
|
30 |
|
31 | To use in a CommonJS file:
|
32 |
|
33 | ```js
|
34 | const evk = require("eslint-visitor-keys")
|
35 | ```
|
36 |
|
37 | ### evk.KEYS
|
38 |
|
39 | > type: `{ [type: string]: string[] | undefined }`
|
40 |
|
41 | Visitor keys. This keys are frozen.
|
42 |
|
43 | This is an object. Keys are the type of [ESTree] nodes. Their values are an array of property names which have child nodes.
|
44 |
|
45 | For example:
|
46 |
|
47 | ```
|
48 | console.log(evk.KEYS.AssignmentExpression) // → ["left", "right"]
|
49 | ```
|
50 |
|
51 | ### evk.getKeys(node)
|
52 |
|
53 | > type: `(node: object) => string[]`
|
54 |
|
55 | Get the visitor keys of a given AST node.
|
56 |
|
57 | This is similar to `Object.keys(node)` of ES Standard, but some keys are excluded: `parent`, `leadingComments`, `trailingComments`, and names which start with `_`.
|
58 |
|
59 | This will be used to traverse unknown nodes.
|
60 |
|
61 | For example:
|
62 |
|
63 | ```js
|
64 | const node = {
|
65 | type: "AssignmentExpression",
|
66 | left: { type: "Identifier", name: "foo" },
|
67 | right: { type: "Literal", value: 0 }
|
68 | }
|
69 | console.log(evk.getKeys(node)) // → ["type", "left", "right"]
|
70 | ```
|
71 |
|
72 | ### evk.unionWith(additionalKeys)
|
73 |
|
74 | > type: `(additionalKeys: object) => { [type: string]: string[] | undefined }`
|
75 |
|
76 | Make 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 |
|
81 | For example:
|
82 |
|
83 | ```js
|
84 | console.log(evk.unionWith({
|
85 | MethodDefinition: ["decorators"]
|
86 | })) // → { ..., MethodDefinition: ["decorators", "key", "value"], ... }
|
87 | ```
|
88 |
|
89 | ## 📰 Change log
|
90 |
|
91 | See [GitHub releases](https://github.com/eslint/eslint-visitor-keys/releases).
|
92 |
|
93 | ## 🍻 Contributing
|
94 |
|
95 | Welcome. 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
|