From b9e75d583f551da583b307e0325105d3eeed3f5d Mon Sep 17 00:00:00 2001 From: Brett Zamir Date: Wed, 23 Jun 2021 10:33:36 +0800 Subject: [PATCH] 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}'.`);