From 0150a9bb67eb35c5e1b6245cbebadf7f4202a9b5 Mon Sep 17 00:00:00 2001 From: Brett Zamir Date: Sun, 20 Jun 2021 20:26:39 +0800 Subject: [PATCH 01/23] Breaking: Switch to ESM --- .editorconfig | 15 +++++++++++++++ .eslintrc.json | 4 ++++ .gitignore | 1 + README.md | 2 +- lib/index.js | 10 ++++++---- package.json | 24 ++++++++++++++++-------- rollup.config.js | 9 +++++++++ tests/lib/index.js | 8 +++----- 8 files changed, 55 insertions(+), 18 deletions(-) create mode 100644 .editorconfig create mode 100644 rollup.config.js diff --git a/.editorconfig b/.editorconfig new file mode 100644 index 0000000..9fe93ed --- /dev/null +++ b/.editorconfig @@ -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 diff --git a/.eslintrc.json b/.eslintrc.json index 7c36f98..72507a7 100644 --- a/.eslintrc.json +++ b/.eslintrc.json @@ -1,5 +1,9 @@ { "extends": "eslint", + "parserOptions": { + "sourceType": "module", + "ecmaVersion": "2020" + }, "env": { "es6": true, "node": true diff --git a/.gitignore b/.gitignore index eb0f93d..aa803db 100644 --- a/.gitignore +++ b/.gitignore @@ -4,3 +4,4 @@ /node_modules /test.* .eslint-release-info.json +/dist diff --git a/README.md b/README.md index d7dbe65..6f249de 100644 --- a/README.md +++ b/README.md @@ -22,7 +22,7 @@ $ npm install eslint-visitor-keys ## 📖 Usage ```js -const evk = require("eslint-visitor-keys") +import evk from "eslint-visitor-keys" ``` ### evk.KEYS diff --git a/lib/index.js b/lib/index.js index cd8a326..30bd05e 100644 --- a/lib/index.js +++ b/lib/index.js @@ -2,9 +2,11 @@ * @author Toru Nagashima * See LICENSE file in root directory for full license. */ -"use strict"; +import { readFileSync } from "fs"; -const KEYS = require("./visitor-keys.json"); +const KEYS = JSON.parse( + readFileSync(new URL("./visitor-keys.json", import.meta.url)) +); // Types. const NODE_TYPES = Object.freeze(Object.keys(KEYS)); @@ -35,7 +37,7 @@ function filterKey(key) { // Public interfaces //------------------------------------------------------------------------------ -module.exports = Object.freeze({ +export default Object.freeze({ /** * Visitor keys. @@ -63,7 +65,7 @@ module.exports = Object.freeze({ const retv = Object.assign({}, KEYS); for (const type of Object.keys(additionalKeys)) { - if (retv.hasOwnProperty(type)) { + if (Object.prototype.hasOwnProperty.call(retv, type)) { const keys = new Set(additionalKeys[type]); for (const key of retv[type]) { diff --git a/package.json b/package.json index a4a7d41..222e96b 100644 --- a/package.json +++ b/package.json @@ -2,25 +2,33 @@ "name": "eslint-visitor-keys", "version": "2.1.0", "description": "Constants and utilities about visitor keys to traverse AST.", + "type": "module", "main": "lib/index.js", + "exports": { + "import": "./lib/index.js", + "require": "./dist/eslint-visitor-keys.cjs" + }, "files": [ "lib" ], "engines": { - "node": ">=10" + "node": ">=12" }, "devDependencies": { - "eslint": "^4.7.2", - "eslint-config-eslint": "^4.0.0", + "c8": "^7.7.3", + "eslint": "^7.29.0", + "eslint-config-eslint": "^7.0.0", "eslint-release": "^3.1.2", - "mocha": "^3.5.3", - "nyc": "^11.2.1", - "opener": "^1.4.3" + "mocha": "^9.0.1", + "opener": "^1.5.2", + "rollup": "^2.52.1" }, "scripts": { + "prepare": "npm run build", + "build": "rollup -c", "lint": "eslint lib tests/lib", - "test": "nyc mocha tests/lib", - "coverage": "nyc report --reporter lcov && opener coverage/lcov-report/index.html", + "test": "c8 mocha tests/lib", + "coverage": "c8 report --reporter lcov && opener coverage/lcov-report/index.html", "generate-release": "eslint-generate-release", "generate-alpharelease": "eslint-generate-prerelease alpha", "generate-betarelease": "eslint-generate-prerelease beta", diff --git a/rollup.config.js b/rollup.config.js new file mode 100644 index 0000000..7d9a537 --- /dev/null +++ b/rollup.config.js @@ -0,0 +1,9 @@ +export default { + input: "./lib/index.js", + external: ["fs"], + output: { + exports: "default", + format: "cjs", + file: "dist/eslint-visitor-keys.cjs" + } +}; diff --git a/tests/lib/index.js b/tests/lib/index.js index d13d8cc..6ba2351 100644 --- a/tests/lib/index.js +++ b/tests/lib/index.js @@ -2,11 +2,9 @@ * @author Toru Nagashima * See LICENSE file in root directory for full license. */ -"use strict"; - -const assert = require("assert"); -const fs = require("fs"); -const evk = require("../.."); +import assert from "assert"; +import fs from "fs"; +import evk from "../../lib/index.js"; const keys = JSON.parse(fs.readFileSync("lib/visitor-keys.json", "utf8")); From 9277599fb0585c8ceee6c3acc0cfcd7bd624dd14 Mon Sep 17 00:00:00 2001 From: Brett Zamir Date: Tue, 22 Jun 2021 09:44:05 +0800 Subject: [PATCH 02/23] Chore: Sync engines changes with #23; add CI to reflect #23 --- .github/workflows/ci.yml | 2 +- package.json | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index e408474..7ed8d02 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -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 diff --git a/package.json b/package.json index 222e96b..3364272 100644 --- a/package.json +++ b/package.json @@ -12,7 +12,7 @@ "lib" ], "engines": { - "node": ">=12" + "node": "^12.22.0 || ^14.17.0 || >=16.0.0" }, "devDependencies": { "c8": "^7.7.3", From ad272e2d7fb1502ee66bc7530498c0daf66e2765 Mon Sep 17 00:00:00 2001 From: Brett Zamir Date: Tue, 22 Jun 2021 09:49:31 +0800 Subject: [PATCH 03/23] Fix: Add `dist` to `files` --- package.json | 1 + 1 file changed, 1 insertion(+) diff --git a/package.json b/package.json index 3364272..b5140a8 100644 --- a/package.json +++ b/package.json @@ -9,6 +9,7 @@ "require": "./dist/eslint-visitor-keys.cjs" }, "files": [ + "dist", "lib" ], "engines": { From 3667fa0a235346166db8f0e837ead35a008a310d Mon Sep 17 00:00:00 2001 From: Brett Zamir Date: Tue, 22 Jun 2021 09:49:59 +0800 Subject: [PATCH 04/23] New: Export `package.json` --- package.json | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/package.json b/package.json index b5140a8..0a73cd7 100644 --- a/package.json +++ b/package.json @@ -5,8 +5,11 @@ "type": "module", "main": "lib/index.js", "exports": { - "import": "./lib/index.js", - "require": "./dist/eslint-visitor-keys.cjs" + ".": { + "import": "./lib/index.js", + "require": "./dist/eslint-visitor-keys.cjs" + }, + "./package.json": "./package.json" }, "files": [ "dist", From ffd412b61e5c6d063cfa103e6eb87eeaeda5ad18 Mon Sep 17 00:00:00 2001 From: Brett Zamir Date: Tue, 22 Jun 2021 16:38:51 +0800 Subject: [PATCH 05/23] Update package.json Co-authored-by: Milos Djermanovic --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index 0a73cd7..b0e7eb6 100644 --- a/package.json +++ b/package.json @@ -3,7 +3,7 @@ "version": "2.1.0", "description": "Constants and utilities about visitor keys to traverse AST.", "type": "module", - "main": "lib/index.js", + "main": "dist/eslint-visitor-keys.cjs", "exports": { ".": { "import": "./lib/index.js", From b2b93e14c38f57a3184d2f5bb70bf2f782fe6586 Mon Sep 17 00:00:00 2001 From: Brett Zamir Date: Tue, 22 Jun 2021 16:56:08 +0800 Subject: [PATCH 06/23] Docs: Show CommonJS usage as well --- README.md | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/README.md b/README.md index 6f249de..462cc79 100644 --- a/README.md +++ b/README.md @@ -21,10 +21,18 @@ $ npm install eslint-visitor-keys ## 📖 Usage +To use in an ESM file: + ```js import evk from "eslint-visitor-keys" ``` +To use in a CommonJS file: + +```js +const evk = require("eslint-visitor-keys") +``` + ### evk.KEYS > type: `{ [type: string]: string[] | undefined }` From 13561e3ba07482b3330c073ede18adc385d3a87c Mon Sep 17 00:00:00 2001 From: Brett Zamir Date: Tue, 22 Jun 2021 17:21:18 +0800 Subject: [PATCH 07/23] Refactor: Align with espree re: ESM --- .eslintrc.json | 13 ++++++++++++- package.json | 12 ++++++++---- rollup.config.js | 4 +++- 3 files changed, 23 insertions(+), 6 deletions(-) diff --git a/.eslintrc.json b/.eslintrc.json index 72507a7..86ac468 100644 --- a/.eslintrc.json +++ b/.eslintrc.json @@ -1,5 +1,8 @@ { "extends": "eslint", + "env": { + "es2020": true + }, "parserOptions": { "sourceType": "module", "ecmaVersion": "2020" @@ -7,5 +10,13 @@ "env": { "es6": true, "node": true - } + }, + "overrides": [ + { + "files": ["*.cjs"], + "parserOptions": { + "sourceType": "script" + } + } + ] } diff --git a/package.json b/package.json index b0e7eb6..e8a8a60 100644 --- a/package.json +++ b/package.json @@ -5,10 +5,14 @@ "type": "module", "main": "dist/eslint-visitor-keys.cjs", "exports": { - ".": { - "import": "./lib/index.js", - "require": "./dist/eslint-visitor-keys.cjs" - }, + ".": [ + { + "import": "./lib/index.js", + "require": "./dist/eslint-visitor-keys.cjs", + "default": "./dist/eslint-visitor-keys.cjs" + }, + "./dist/eslint-visitor-keys.cjs" + ], "./package.json": "./package.json" }, "files": [ diff --git a/rollup.config.js b/rollup.config.js index 7d9a537..ce75186 100644 --- a/rollup.config.js +++ b/rollup.config.js @@ -1,9 +1,11 @@ export default { input: "./lib/index.js", external: ["fs"], + treeshake: false, output: { exports: "default", format: "cjs", - file: "dist/eslint-visitor-keys.cjs" + file: "dist/eslint-visitor-keys.cjs", + sourcemap: true } }; From b9e75d583f551da583b307e0325105d3eeed3f5d Mon Sep 17 00:00:00 2001 From: Brett Zamir Date: Wed, 23 Jun 2021 10:33:36 +0800 Subject: [PATCH 08/23] Refactor: Switch from JSON to ESM --- lib/index.js | 6 +- lib/{visitor-keys.json => visitor-keys.js} | 172 ++++++++++----------- tests/lib/index.js | 8 +- 3 files changed, 90 insertions(+), 96 deletions(-) rename lib/{visitor-keys.json => visitor-keys.js} (56%) diff --git a/lib/index.js b/lib/index.js index 30bd05e..09a3242 100644 --- a/lib/index.js +++ b/lib/index.js @@ -2,11 +2,7 @@ * @author Toru Nagashima * See LICENSE file in root directory for full license. */ -import { readFileSync } from "fs"; - -const KEYS = JSON.parse( - readFileSync(new URL("./visitor-keys.json", import.meta.url)) -); +import KEYS from "./visitor-keys.js"; // Types. const NODE_TYPES = Object.freeze(Object.keys(KEYS)); diff --git a/lib/visitor-keys.json b/lib/visitor-keys.js similarity index 56% rename from lib/visitor-keys.json rename to lib/visitor-keys.js index e648ee1..600d25a 100644 --- a/lib/visitor-keys.json +++ b/lib/visitor-keys.js @@ -1,289 +1,289 @@ -{ - "AssignmentExpression": [ +export default { + AssignmentExpression: [ "left", "right" ], - "AssignmentPattern": [ + AssignmentPattern: [ "left", "right" ], - "ArrayExpression": [ + ArrayExpression: [ "elements" ], - "ArrayPattern": [ + ArrayPattern: [ "elements" ], - "ArrowFunctionExpression": [ + ArrowFunctionExpression: [ "params", "body" ], - "AwaitExpression": [ + AwaitExpression: [ "argument" ], - "BlockStatement": [ + BlockStatement: [ "body" ], - "BinaryExpression": [ + BinaryExpression: [ "left", "right" ], - "BreakStatement": [ + BreakStatement: [ "label" ], - "CallExpression": [ + CallExpression: [ "callee", "arguments" ], - "CatchClause": [ + CatchClause: [ "param", "body" ], - "ChainExpression": [ + ChainExpression: [ "expression" ], - "ClassBody": [ + ClassBody: [ "body" ], - "ClassDeclaration": [ + ClassDeclaration: [ "id", "superClass", "body" ], - "ClassExpression": [ + ClassExpression: [ "id", "superClass", "body" ], - "ConditionalExpression": [ + ConditionalExpression: [ "test", "consequent", "alternate" ], - "ContinueStatement": [ + ContinueStatement: [ "label" ], - "DebuggerStatement": [], - "DoWhileStatement": [ + DebuggerStatement: [], + DoWhileStatement: [ "body", "test" ], - "EmptyStatement": [], - "ExportAllDeclaration": [ + EmptyStatement: [], + ExportAllDeclaration: [ "exported", "source" ], - "ExportDefaultDeclaration": [ + ExportDefaultDeclaration: [ "declaration" ], - "ExportNamedDeclaration": [ + ExportNamedDeclaration: [ "declaration", "specifiers", "source" ], - "ExportSpecifier": [ + ExportSpecifier: [ "exported", "local" ], - "ExpressionStatement": [ + ExpressionStatement: [ "expression" ], - "ExperimentalRestProperty": [ + ExperimentalRestProperty: [ "argument" ], - "ExperimentalSpreadProperty": [ + ExperimentalSpreadProperty: [ "argument" ], - "ForStatement": [ + ForStatement: [ "init", "test", "update", "body" ], - "ForInStatement": [ + ForInStatement: [ "left", "right", "body" ], - "ForOfStatement": [ + ForOfStatement: [ "left", "right", "body" ], - "FunctionDeclaration": [ + FunctionDeclaration: [ "id", "params", "body" ], - "FunctionExpression": [ + FunctionExpression: [ "id", "params", "body" ], - "Identifier": [], - "IfStatement": [ + Identifier: [], + IfStatement: [ "test", "consequent", "alternate" ], - "ImportDeclaration": [ + ImportDeclaration: [ "specifiers", "source" ], - "ImportDefaultSpecifier": [ + ImportDefaultSpecifier: [ "local" ], - "ImportExpression": [ + ImportExpression: [ "source" ], - "ImportNamespaceSpecifier": [ + ImportNamespaceSpecifier: [ "local" ], - "ImportSpecifier": [ + ImportSpecifier: [ "imported", "local" ], - "JSXAttribute": [ + JSXAttribute: [ "name", "value" ], - "JSXClosingElement": [ + JSXClosingElement: [ "name" ], - "JSXElement": [ + JSXElement: [ "openingElement", "children", "closingElement" ], - "JSXEmptyExpression": [], - "JSXExpressionContainer": [ + JSXEmptyExpression: [], + JSXExpressionContainer: [ "expression" ], - "JSXIdentifier": [], - "JSXMemberExpression": [ + JSXIdentifier: [], + JSXMemberExpression: [ "object", "property" ], - "JSXNamespacedName": [ + JSXNamespacedName: [ "namespace", "name" ], - "JSXOpeningElement": [ + JSXOpeningElement: [ "name", "attributes" ], - "JSXSpreadAttribute": [ + JSXSpreadAttribute: [ "argument" ], - "JSXText": [], - "JSXFragment": [ + JSXText: [], + JSXFragment: [ "openingFragment", "children", "closingFragment" ], - "Literal": [], - "LabeledStatement": [ + Literal: [], + LabeledStatement: [ "label", "body" ], - "LogicalExpression": [ + LogicalExpression: [ "left", "right" ], - "MemberExpression": [ + MemberExpression: [ "object", "property" ], - "MetaProperty": [ + MetaProperty: [ "meta", "property" ], - "MethodDefinition": [ + MethodDefinition: [ "key", "value" ], - "NewExpression": [ + NewExpression: [ "callee", "arguments" ], - "ObjectExpression": [ + ObjectExpression: [ "properties" ], - "ObjectPattern": [ + ObjectPattern: [ "properties" ], - "PrivateIdentifier": [], - "Program": [ + PrivateIdentifier: [], + Program: [ "body" ], - "Property": [ + Property: [ "key", "value" ], - "PropertyDefinition": [ + PropertyDefinition: [ "key", "value" ], - "RestElement": [ + RestElement: [ "argument" ], - "ReturnStatement": [ + ReturnStatement: [ "argument" ], - "SequenceExpression": [ + SequenceExpression: [ "expressions" ], - "SpreadElement": [ + SpreadElement: [ "argument" ], - "Super": [], - "SwitchStatement": [ + Super: [], + SwitchStatement: [ "discriminant", "cases" ], - "SwitchCase": [ + SwitchCase: [ "test", "consequent" ], - "TaggedTemplateExpression": [ + TaggedTemplateExpression: [ "tag", "quasi" ], - "TemplateElement": [], - "TemplateLiteral": [ + TemplateElement: [], + TemplateLiteral: [ "quasis", "expressions" ], - "ThisExpression": [], - "ThrowStatement": [ + ThisExpression: [], + ThrowStatement: [ "argument" ], - "TryStatement": [ + TryStatement: [ "block", "handler", "finalizer" ], - "UnaryExpression": [ + UnaryExpression: [ "argument" ], - "UpdateExpression": [ + UpdateExpression: [ "argument" ], - "VariableDeclaration": [ + VariableDeclaration: [ "declarations" ], - "VariableDeclarator": [ + VariableDeclarator: [ "id", "init" ], - "WhileStatement": [ + WhileStatement: [ "test", "body" ], - "WithStatement": [ + WithStatement: [ "object", "body" ], - "YieldExpression": [ + YieldExpression: [ "argument" ] -} +}; diff --git a/tests/lib/index.js b/tests/lib/index.js index 6ba2351..fdba7a4 100644 --- a/tests/lib/index.js +++ b/tests/lib/index.js @@ -3,14 +3,12 @@ * See LICENSE file in root directory for full license. */ import assert from "assert"; -import fs from "fs"; import evk from "../../lib/index.js"; - -const keys = JSON.parse(fs.readFileSync("lib/visitor-keys.json", "utf8")); +import keys from "../../lib/visitor-keys.js"; describe("eslint-visitor-keys", () => { describe("KEYS", () => { - it("should be same as lib/visitor-keys.json", () => { + it("should be same as lib/visitor-keys.js", () => { assert.deepStrictEqual(evk.KEYS, keys); }); }); @@ -41,7 +39,7 @@ describe("eslint-visitor-keys", () => { const additionalKeys = { Program: ["body", "a"], AssignmentExpression: ["b"], additional: ["c"], MethodDefinition: ["a", "key", "b"] }; const unionKeys = evk.unionWith(additionalKeys); - it("should include all keys of lib/visitor-keys.json", () => { + it("should include all keys of lib/visitor-keys.js", () => { for (const type of Object.keys(keys)) { for (const key of keys[type]) { assert(unionKeys[type].indexOf(key) !== -1, `'${key}' should be included in '${type}'.`); From 5c7da53c29b3550384c85bcc8beca7ea823097d2 Mon Sep 17 00:00:00 2001 From: Brett Zamir Date: Wed, 23 Jun 2021 20:37:34 +0800 Subject: [PATCH 09/23] Chore: Add linting devDeps. --- package.json | 2 ++ 1 file changed, 2 insertions(+) diff --git a/package.json b/package.json index e8a8a60..44cb19e 100644 --- a/package.json +++ b/package.json @@ -26,6 +26,8 @@ "c8": "^7.7.3", "eslint": "^7.29.0", "eslint-config-eslint": "^7.0.0", + "eslint-plugin-jsdoc": "^35.4.0", + "eslint-plugin-node": "^11.1.0", "eslint-release": "^3.1.2", "mocha": "^9.0.1", "opener": "^1.5.2", From 913045a4f76f0d6b3b678a8274458b370f39c199 Mon Sep 17 00:00:00 2001 From: Brett Zamir Date: Wed, 23 Jun 2021 20:49:31 +0800 Subject: [PATCH 10/23] Refactor: Remove no longer needed `external` --- rollup.config.js | 1 - 1 file changed, 1 deletion(-) diff --git a/rollup.config.js b/rollup.config.js index ce75186..0086c64 100644 --- a/rollup.config.js +++ b/rollup.config.js @@ -1,6 +1,5 @@ export default { input: "./lib/index.js", - external: ["fs"], treeshake: false, output: { exports: "default", From b401b9688dd1ee97a9e0ee2813db420b8035a6df Mon Sep 17 00:00:00 2001 From: Brett Zamir Date: Wed, 23 Jun 2021 21:02:54 +0800 Subject: [PATCH 11/23] Test: Add `commonjs.cjs` test --- tests/lib/commonjs.cjs | 56 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 56 insertions(+) create mode 100644 tests/lib/commonjs.cjs diff --git a/tests/lib/commonjs.cjs b/tests/lib/commonjs.cjs new file mode 100644 index 0000000..47b2cf7 --- /dev/null +++ b/tests/lib/commonjs.cjs @@ -0,0 +1,56 @@ +/** + * @fileoverview Tests for checking that the commonjs entry points are still accessible + * @author Mike Reinstein + */ + +// eslint-disable-next-line strict +"use strict"; + +//------------------------------------------------------------------------------ +// Requirements +//------------------------------------------------------------------------------ + +const assert = require("assert"); +const eslintVisitorKeys = require("../../dist/eslint-visitor-keys.cjs"); + + +//------------------------------------------------------------------------------ +// Tests +//------------------------------------------------------------------------------ + +describe("commonjs", () => { + it("is an object", () => { + assert.strictEqual(typeof eslintVisitorKeys, "object"); + }); + + it("has exported keys object", () => { + assert.strictEqual(typeof eslintVisitorKeys.KEYS, "object"); + }); + + it("has key array with AST type", () => { + assert.ok(Array.isArray(eslintVisitorKeys.KEYS.ArrayExpression)); + }); + + it("has getKeys function", () => { + assert.strictEqual(typeof eslintVisitorKeys.getKeys, "function"); + }); + + it("should have getKeys which returns keys", () => { + assert.deepStrictEqual(eslintVisitorKeys.getKeys({ a: 1, b: 2 }), ["a", "b"]); + }); + + it("has unionWith function", () => { + assert.strictEqual(typeof eslintVisitorKeys.unionWith, "function"); + }); + + it("should have unionWith which includes all additional keys", () => { + const additionalKeys = { Program: ["body", "a"], AssignmentExpression: ["b"], additional: ["c"], MethodDefinition: ["a", "key", "b"] }; + const unionKeys = eslintVisitorKeys.unionWith(additionalKeys); + + for (const type of Object.keys(additionalKeys)) { + for (const key of additionalKeys[type]) { + assert(unionKeys[type].indexOf(key) !== -1, `'${key}' should be included in '${type}'.`); + } + } + }); +}); From 3cf37644e056c6215bb0d2ab08faa2f1d9fb90fe Mon Sep 17 00:00:00 2001 From: Brett Zamir Date: Wed, 23 Jun 2021 21:04:23 +0800 Subject: [PATCH 12/23] Refactor: Remove dupe `env` key --- .eslintrc.json | 4 ---- 1 file changed, 4 deletions(-) diff --git a/.eslintrc.json b/.eslintrc.json index 86ac468..9e07bd6 100644 --- a/.eslintrc.json +++ b/.eslintrc.json @@ -7,10 +7,6 @@ "sourceType": "module", "ecmaVersion": "2020" }, - "env": { - "es6": true, - "node": true - }, "overrides": [ { "files": ["*.cjs"], From 0b44ae16cd7efbf68af3f6ca803b9b7364e29de7 Mon Sep 17 00:00:00 2001 From: Brett Zamir Date: Wed, 23 Jun 2021 21:25:42 +0800 Subject: [PATCH 13/23] Breaking: Change to named exports --- lib/index.js | 73 ++++++++++++++++++++-------------------------- rollup.config.js | 1 - tests/lib/index.js | 2 +- 3 files changed, 32 insertions(+), 44 deletions(-) diff --git a/lib/index.js b/lib/index.js index 09a3242..f60f39a 100644 --- a/lib/index.js +++ b/lib/index.js @@ -29,51 +29,40 @@ function filterKey(key) { return !KEY_BLACKLIST.has(key) && key[0] !== "_"; } -//------------------------------------------------------------------------------ -// Public interfaces -//------------------------------------------------------------------------------ - -export default 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 (Object.prototype.hasOwnProperty.call(retv, 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 }; diff --git a/rollup.config.js b/rollup.config.js index 0086c64..d3fc0ff 100644 --- a/rollup.config.js +++ b/rollup.config.js @@ -2,7 +2,6 @@ export default { input: "./lib/index.js", treeshake: false, output: { - exports: "default", format: "cjs", file: "dist/eslint-visitor-keys.cjs", sourcemap: true diff --git a/tests/lib/index.js b/tests/lib/index.js index fdba7a4..c7497eb 100644 --- a/tests/lib/index.js +++ b/tests/lib/index.js @@ -3,7 +3,7 @@ * See LICENSE file in root directory for full license. */ import assert from "assert"; -import evk from "../../lib/index.js"; +import * as evk from "../../lib/index.js"; import keys from "../../lib/visitor-keys.js"; describe("eslint-visitor-keys", () => { From df8ee8ff4bd5e5b33dcfb2b5e9fcaa45b8c18f59 Mon Sep 17 00:00:00 2001 From: Brett Zamir Date: Wed, 23 Jun 2021 21:40:52 +0800 Subject: [PATCH 14/23] Update README.md Co-authored-by: Milos Djermanovic --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 462cc79..71217e3 100644 --- a/README.md +++ b/README.md @@ -24,7 +24,7 @@ $ npm install eslint-visitor-keys To use in an ESM file: ```js -import evk from "eslint-visitor-keys" +import * as evk from "eslint-visitor-keys" ``` To use in a CommonJS file: From b00ecf614202066923666d435739108ad5fdf20c Mon Sep 17 00:00:00 2001 From: Brett Zamir Date: Wed, 23 Jun 2021 21:44:17 +0800 Subject: [PATCH 15/23] Refactor: freeze in keys file --- lib/index.js | 9 --------- lib/visitor-keys.js | 13 ++++++++++++- 2 files changed, 12 insertions(+), 10 deletions(-) diff --git a/lib/index.js b/lib/index.js index f60f39a..44f73d6 100644 --- a/lib/index.js +++ b/lib/index.js @@ -4,15 +4,6 @@ */ import KEYS from "./visitor-keys.js"; -// 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); - // List to ignore keys. const KEY_BLACKLIST = new Set([ "parent", diff --git a/lib/visitor-keys.js b/lib/visitor-keys.js index 600d25a..70e4c0f 100644 --- a/lib/visitor-keys.js +++ b/lib/visitor-keys.js @@ -1,4 +1,4 @@ -export default { +const KEYS = { AssignmentExpression: [ "left", "right" @@ -287,3 +287,14 @@ export default { "argument" ] }; + +// 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); + +export default KEYS; From 0a9ae41fd3a351907217181eec181686289445f6 Mon Sep 17 00:00:00 2001 From: Brett Zamir Date: Wed, 23 Jun 2021 22:10:58 +0800 Subject: [PATCH 16/23] Update package.json Co-authored-by: Milos Djermanovic --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index 44cb19e..c6b4aef 100644 --- a/package.json +++ b/package.json @@ -36,7 +36,7 @@ "scripts": { "prepare": "npm run build", "build": "rollup -c", - "lint": "eslint lib tests/lib", + "lint": "eslint .", "test": "c8 mocha tests/lib", "coverage": "c8 report --reporter lcov && opener coverage/lcov-report/index.html", "generate-release": "eslint-generate-release", From db0b7f4493786592a2c7c634c9c987a20a4f1fe0 Mon Sep 17 00:00:00 2001 From: Brett Zamir Date: Wed, 23 Jun 2021 22:11:35 +0800 Subject: [PATCH 17/23] Update .eslintrc.json Co-authored-by: Milos Djermanovic --- .eslintrc.json | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/.eslintrc.json b/.eslintrc.json index 9e07bd6..23b5774 100644 --- a/.eslintrc.json +++ b/.eslintrc.json @@ -14,5 +14,6 @@ "sourceType": "script" } } - ] + ], + "ignorePatterns": "/dist" } From e197a19a3b5d87b8249a089992d23649f882608b Mon Sep 17 00:00:00 2001 From: Brett Zamir Date: Wed, 23 Jun 2021 22:12:14 +0800 Subject: [PATCH 18/23] Update .eslintrc.json Co-authored-by: Milos Djermanovic --- .eslintrc.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.eslintrc.json b/.eslintrc.json index 23b5774..28210f4 100644 --- a/.eslintrc.json +++ b/.eslintrc.json @@ -5,7 +5,7 @@ }, "parserOptions": { "sourceType": "module", - "ecmaVersion": "2020" + "ecmaVersion": 2020 }, "overrides": [ { From 080812fdb65ba931a994927783660fb431a20b88 Mon Sep 17 00:00:00 2001 From: Brett Zamir Date: Wed, 23 Jun 2021 22:28:40 +0800 Subject: [PATCH 19/23] Update .eslintrc.json Co-authored-by: Milos Djermanovic --- .eslintrc.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.eslintrc.json b/.eslintrc.json index 28210f4..7bea33e 100644 --- a/.eslintrc.json +++ b/.eslintrc.json @@ -15,5 +15,5 @@ } } ], - "ignorePatterns": "/dist" + "ignorePatterns": ["/dist", "/coverage"] } From 59d79c53787f3ca583139c7479cc0fccab9b42c4 Mon Sep 17 00:00:00 2001 From: Brett Zamir Date: Wed, 23 Jun 2021 23:23:16 +0800 Subject: [PATCH 20/23] Test: Split cjs and js tests --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index c6b4aef..ca952ca 100644 --- a/package.json +++ b/package.json @@ -37,7 +37,7 @@ "prepare": "npm run build", "build": "rollup -c", "lint": "eslint .", - "test": "c8 mocha tests/lib", + "test": "mocha tests/lib/**/*.cjs && c8 mocha tests/lib/**/*.js", "coverage": "c8 report --reporter lcov && opener coverage/lcov-report/index.html", "generate-release": "eslint-generate-release", "generate-alpharelease": "eslint-generate-prerelease alpha", From 1fe965999f71b4ac5f7bc78580ebe6793a741870 Mon Sep 17 00:00:00 2001 From: Brett Zamir Date: Thu, 24 Jun 2021 06:02:51 +0800 Subject: [PATCH 21/23] Refactor: Avoid freezing unused array --- lib/visitor-keys.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/visitor-keys.js b/lib/visitor-keys.js index 70e4c0f..ade8fc3 100644 --- a/lib/visitor-keys.js +++ b/lib/visitor-keys.js @@ -289,7 +289,7 @@ const KEYS = { }; // Types. -const NODE_TYPES = Object.freeze(Object.keys(KEYS)); +const NODE_TYPES = Object.keys(KEYS); // Freeze the keys. for (const type of NODE_TYPES) { From 28504940b0e09792dba295a019bab87f2751cd1b Mon Sep 17 00:00:00 2001 From: Brett Zamir Date: Thu, 24 Jun 2021 06:07:01 +0800 Subject: [PATCH 22/23] Refactor: Stop exporting map file --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index ca952ca..183b49f 100644 --- a/package.json +++ b/package.json @@ -16,7 +16,7 @@ "./package.json": "./package.json" }, "files": [ - "dist", + "dist/eslint-visitor-keys.cjs", "lib" ], "engines": { From bd8e4ffac9b737e7fbcc242818b4f4b83cf479fd Mon Sep 17 00:00:00 2001 From: Brett Zamir Date: Thu, 24 Jun 2021 06:44:05 +0800 Subject: [PATCH 23/23] Refactor: Drop `default` --- package.json | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/package.json b/package.json index 183b49f..2531df5 100644 --- a/package.json +++ b/package.json @@ -8,8 +8,7 @@ ".": [ { "import": "./lib/index.js", - "require": "./dist/eslint-visitor-keys.cjs", - "default": "./dist/eslint-visitor-keys.cjs" + "require": "./dist/eslint-visitor-keys.cjs" }, "./dist/eslint-visitor-keys.cjs" ],