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

Commit

Permalink
Breaking: Switch to ESM (#24)
Browse files Browse the repository at this point in the history
* 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]>
  • Loading branch information
brettz9 and mdjermanovic authored Jun 24, 2021
1 parent 7279e73 commit f584b12
Show file tree
Hide file tree
Showing 11 changed files with 265 additions and 162 deletions.
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

0 comments on commit f584b12

Please sign in to comment.