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