Skip to content
This repository has been archived by the owner on Aug 20, 2024. It is now read-only.

Breaking: Switch to ESM #24

Merged
merged 23 commits into from
Jun 24, 2021
Merged
Show file tree
Hide file tree
Changes from 20 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
; EditorConfig file: https://EditorConfig.org
; Install the "EditorConfig" plugin into your editor to use

root = true

[*]
charset = utf-8
end_of_line = lf
insert_final_newline = true
indent_style = space
indent_size = 4
trim_trailing_whitespace = true

[package.json]
indent_size = 2
18 changes: 15 additions & 3 deletions .eslintrc.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,19 @@
{
"extends": "eslint",
"env": {
"es6": true,
"node": true
}
"es2020": true
},
"parserOptions": {
"sourceType": "module",
"ecmaVersion": 2020
},
"overrides": [
{
"files": ["*.cjs"],
"parserOptions": {
"sourceType": "script"
}
}
],
"ignorePatterns": ["/dist", "/coverage"]
}
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ jobs:
strategy:
matrix:
os: [ubuntu-latest]
node: [14.x, 12.x, 10.x]
node: [16.x, 14.x, 12.x, "12.22.0"]
runs-on: ${{ matrix.os }}
steps:
- uses: actions/checkout@v2
Expand Down
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@
/node_modules
/test.*
.eslint-release-info.json
/dist
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,14 @@ $ npm install eslint-visitor-keys

## 📖 Usage

To use in an ESM file:

```js
import * as evk from "eslint-visitor-keys"
```

To use in a CommonJS file:

```js
const evk = require("eslint-visitor-keys")
```
Expand Down
86 changes: 32 additions & 54 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,7 @@
* @author Toru Nagashima <https://github.com/mysticatea>
* See LICENSE file in root directory for full license.
*/
"use strict";

const KEYS = require("./visitor-keys.json");

// Types.
const NODE_TYPES = Object.freeze(Object.keys(KEYS));

// Freeze the keys.
for (const type of NODE_TYPES) {
Object.freeze(KEYS[type]);
}
Object.freeze(KEYS);
import KEYS from "./visitor-keys.js";

// List to ignore keys.
const KEY_BLACKLIST = new Set([
Expand All @@ -31,51 +20,40 @@ function filterKey(key) {
return !KEY_BLACKLIST.has(key) && key[0] !== "_";
}

//------------------------------------------------------------------------------
// Public interfaces
//------------------------------------------------------------------------------

module.exports = Object.freeze({

/**
* Visitor keys.
* @type {{ [type: string]: string[] | undefined }}
*/
KEYS,

/**
* Get visitor keys of a given node.
* @param {Object} node The AST node to get keys.
* @returns {string[]} Visitor keys of the node.
*/
getKeys(node) {
return Object.keys(node).filter(filterKey);
},

// Disable valid-jsdoc rule because it reports syntax error on the type of @returns.
// eslint-disable-next-line valid-jsdoc
/**
* Make the union set with `KEYS` and given keys.
* @param {Object} additionalKeys The additional keys.
* @returns {{ [type: string]: string[] | undefined }} The union set.
*/
unionWith(additionalKeys) {
const retv = Object.assign({}, KEYS);
/**
* Get visitor keys of a given node.
* @param {Object} node The AST node to get keys.
* @returns {string[]} Visitor keys of the node.
*/
export function getKeys(node) {
return Object.keys(node).filter(filterKey);
}

for (const type of Object.keys(additionalKeys)) {
if (retv.hasOwnProperty(type)) {
const keys = new Set(additionalKeys[type]);
// Disable valid-jsdoc rule because it reports syntax error on the type of @returns.
// eslint-disable-next-line valid-jsdoc
/**
* Make the union set with `KEYS` and given keys.
* @param {Object} additionalKeys The additional keys.
* @returns {{ [type: string]: string[] | undefined }} The union set.
*/
export function unionWith(additionalKeys) {
const retv = Object.assign({}, KEYS);

for (const key of retv[type]) {
keys.add(key);
}
for (const type of Object.keys(additionalKeys)) {
if (Object.prototype.hasOwnProperty.call(retv, type)) {
const keys = new Set(additionalKeys[type]);

retv[type] = Object.freeze(Array.from(keys));
} else {
retv[type] = Object.freeze(Array.from(additionalKeys[type]));
for (const key of retv[type]) {
keys.add(key);
}
}

return Object.freeze(retv);
retv[type] = Object.freeze(Array.from(keys));
} else {
retv[type] = Object.freeze(Array.from(additionalKeys[type]));
}
}
});

return Object.freeze(retv);
}

export { KEYS };
Loading