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

Commit f584b12

Browse files
Breaking: Switch to ESM (#24)
* Breaking: Switch to ESM * Chore: Sync engines changes with #23; add CI to reflect #23 * Fix: Add `dist` to `files` * New: Export `package.json` * Update package.json Co-authored-by: Milos Djermanovic <[email protected]> * Docs: Show CommonJS usage as well * Refactor: Align with espree re: ESM * Refactor: Switch from JSON to ESM * Chore: Add linting devDeps. * Refactor: Remove no longer needed `external` * Test: Add `commonjs.cjs` test * Refactor: Remove dupe `env` key * Breaking: Change to named exports * Update README.md Co-authored-by: Milos Djermanovic <[email protected]> * Refactor: freeze in keys file * Update package.json Co-authored-by: Milos Djermanovic <[email protected]> * Update .eslintrc.json Co-authored-by: Milos Djermanovic <[email protected]> * Update .eslintrc.json Co-authored-by: Milos Djermanovic <[email protected]> * Update .eslintrc.json Co-authored-by: Milos Djermanovic <[email protected]> * Test: Split cjs and js tests * Refactor: Avoid freezing unused array * Refactor: Stop exporting map file * Refactor: Drop `default` Co-authored-by: Milos Djermanovic <[email protected]>
1 parent 7279e73 commit f584b12

File tree

11 files changed

+265
-162
lines changed

11 files changed

+265
-162
lines changed

.editorconfig

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
; EditorConfig file: https://EditorConfig.org
2+
; Install the "EditorConfig" plugin into your editor to use
3+
4+
root = true
5+
6+
[*]
7+
charset = utf-8
8+
end_of_line = lf
9+
insert_final_newline = true
10+
indent_style = space
11+
indent_size = 4
12+
trim_trailing_whitespace = true
13+
14+
[package.json]
15+
indent_size = 2

.eslintrc.json

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,19 @@
11
{
22
"extends": "eslint",
33
"env": {
4-
"es6": true,
5-
"node": true
6-
}
4+
"es2020": true
5+
},
6+
"parserOptions": {
7+
"sourceType": "module",
8+
"ecmaVersion": 2020
9+
},
10+
"overrides": [
11+
{
12+
"files": ["*.cjs"],
13+
"parserOptions": {
14+
"sourceType": "script"
15+
}
16+
}
17+
],
18+
"ignorePatterns": ["/dist", "/coverage"]
719
}

.github/workflows/ci.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ jobs:
2525
strategy:
2626
matrix:
2727
os: [ubuntu-latest]
28-
node: [14.x, 12.x, 10.x]
28+
node: [16.x, 14.x, 12.x, "12.22.0"]
2929
runs-on: ${{ matrix.os }}
3030
steps:
3131
- uses: actions/checkout@v2

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,4 @@
44
/node_modules
55
/test.*
66
.eslint-release-info.json
7+
/dist

README.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,14 @@ $ npm install eslint-visitor-keys
2121

2222
## 📖 Usage
2323

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+
2432
```js
2533
const evk = require("eslint-visitor-keys")
2634
```

lib/index.js

Lines changed: 32 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,7 @@
22
* @author Toru Nagashima <https://github.com/mysticatea>
33
* See LICENSE file in root directory for full license.
44
*/
5-
"use strict";
6-
7-
const KEYS = require("./visitor-keys.json");
8-
9-
// Types.
10-
const NODE_TYPES = Object.freeze(Object.keys(KEYS));
11-
12-
// Freeze the keys.
13-
for (const type of NODE_TYPES) {
14-
Object.freeze(KEYS[type]);
15-
}
16-
Object.freeze(KEYS);
5+
import KEYS from "./visitor-keys.js";
176

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

34-
//------------------------------------------------------------------------------
35-
// Public interfaces
36-
//------------------------------------------------------------------------------
37-
38-
module.exports = Object.freeze({
39-
40-
/**
41-
* Visitor keys.
42-
* @type {{ [type: string]: string[] | undefined }}
43-
*/
44-
KEYS,
45-
46-
/**
47-
* Get visitor keys of a given node.
48-
* @param {Object} node The AST node to get keys.
49-
* @returns {string[]} Visitor keys of the node.
50-
*/
51-
getKeys(node) {
52-
return Object.keys(node).filter(filterKey);
53-
},
54-
55-
// Disable valid-jsdoc rule because it reports syntax error on the type of @returns.
56-
// eslint-disable-next-line valid-jsdoc
57-
/**
58-
* Make the union set with `KEYS` and given keys.
59-
* @param {Object} additionalKeys The additional keys.
60-
* @returns {{ [type: string]: string[] | undefined }} The union set.
61-
*/
62-
unionWith(additionalKeys) {
63-
const retv = Object.assign({}, KEYS);
23+
/**
24+
* Get visitor keys of a given node.
25+
* @param {Object} node The AST node to get keys.
26+
* @returns {string[]} Visitor keys of the node.
27+
*/
28+
export function getKeys(node) {
29+
return Object.keys(node).filter(filterKey);
30+
}
6431

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

69-
for (const key of retv[type]) {
70-
keys.add(key);
71-
}
42+
for (const type of Object.keys(additionalKeys)) {
43+
if (Object.prototype.hasOwnProperty.call(retv, type)) {
44+
const keys = new Set(additionalKeys[type]);
7245

73-
retv[type] = Object.freeze(Array.from(keys));
74-
} else {
75-
retv[type] = Object.freeze(Array.from(additionalKeys[type]));
46+
for (const key of retv[type]) {
47+
keys.add(key);
7648
}
77-
}
7849

79-
return Object.freeze(retv);
50+
retv[type] = Object.freeze(Array.from(keys));
51+
} else {
52+
retv[type] = Object.freeze(Array.from(additionalKeys[type]));
53+
}
8054
}
81-
});
55+
56+
return Object.freeze(retv);
57+
}
58+
59+
export { KEYS };

0 commit comments

Comments
 (0)