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

Commit

Permalink
chore: tool to build keys out of AST definitions
Browse files Browse the repository at this point in the history
Also:
1. Removes `ExperimentalRestProperty`, `ExperimentalSpreadProperty`
2. Adds `JSXSpreadChild`
  • Loading branch information
ESLint Jenkins authored and brettz9 committed Feb 12, 2022
1 parent c57689f commit ba5237d
Show file tree
Hide file tree
Showing 5 changed files with 547 additions and 6 deletions.
9 changes: 3 additions & 6 deletions lib/visitor-keys.js
Original file line number Diff line number Diff line change
Expand Up @@ -75,12 +75,6 @@ const KEYS = {
"test"
],
EmptyStatement: [],
ExperimentalRestProperty: [
"argument"
],
ExperimentalSpreadProperty: [
"argument"
],
ExportAllDeclaration: [
"exported",
"source"
Expand Down Expand Up @@ -188,6 +182,9 @@ const KEYS = {
JSXSpreadAttribute: [
"argument"
],
JSXSpreadChild: [
"expression"
],
JSXText: [],
LabeledStatement: [
"label",
Expand Down
7 changes: 7 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,18 @@
"node": "^12.22.0 || ^14.17.0 || >=16.0.0"
},
"devDependencies": {
"@types/estree": "^0.0.51",
"@types/estree-jsx": "^0.0.1",
"@typescript-eslint/parser": "^5.11.0",
"c8": "^7.7.3",
"chai": "^4.3.6",
"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.2.0",
"esquery": "^1.4.0",
"json-diff": "^0.7.1",
"mocha": "^9.0.1",
"opener": "^1.5.2",
"rollup": "^2.52.1",
Expand All @@ -43,6 +49,7 @@
"lint": "eslint .",
"tsc": "tsc",
"tsd": "tsd",
"build-keys": "node tools/build-keys-from-ts",
"test": "mocha tests/lib/**/*.cjs && c8 mocha tests/lib/**/*.js && npm run tsd",
"coverage": "c8 report --reporter lcov && opener coverage/lcov-report/index.html",
"generate-release": "eslint-generate-release",
Expand Down
32 changes: 32 additions & 0 deletions tests/lib/get-keys-from-ts.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/**
* @fileoverview Tests for checking that our build tool can retrieve keys out of TypeScript AST.
* @author Brett Zamir
*/

import { diffString } from "json-diff";
import { expect } from "chai";
import { alphabetizeKeyInterfaces, getKeysFromTsFile } from "../../tools/get-keys-from-ts.js";
import { KEYS } from "../../lib/index.js";

describe("getKeysFromTsFile", () => {
it("gets keys", async () => {
const { keys, tsInterfaceDeclarations } = await getKeysFromTsFile(
"./node_modules/@types/estree/index.d.ts"
);
const { keys: jsxKeys } = await getKeysFromTsFile(
"./node_modules/@types/estree-jsx/index.d.ts",
{
supplementaryDeclarations: tsInterfaceDeclarations
}
);

const actual = alphabetizeKeyInterfaces({ ...keys, ...jsxKeys });

const expected = KEYS;

// eslint-disable-next-line no-console -- Mocha's may drop diffs so show with json-diff
console.log("JSON Diffs:", diffString(actual, expected) || "(none)");

expect(actual).to.deep.equal(expected);
});
});
54 changes: 54 additions & 0 deletions tools/build-keys-from-ts.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
/**
* @fileoverview Script to build our visitor keys based on TypeScript AST.
*
* Uses `get-keys-from-ts.js` to read the files and build the keys and then
* merges them in alphabetical order of Node type before writing to file.
*
* @author Brett Zamir
*/

import fs from "fs";
import { alphabetizeKeyInterfaces, getKeysFromTsFile } from "./get-keys-from-ts.js";

const { promises: { writeFile } } = fs;

(async () => {
const { keys, tsInterfaceDeclarations } = await getKeysFromTsFile("./node_modules/@types/estree/index.d.ts");
const { keys: jsxKeys } = await getKeysFromTsFile(
"./node_modules/@types/estree-jsx/index.d.ts",
{
supplementaryDeclarations: tsInterfaceDeclarations
}
);

const mergedKeys = alphabetizeKeyInterfaces({ ...keys, ...jsxKeys });

// eslint-disable-next-line no-console -- CLI
console.log("keys", mergedKeys);

writeFile(
"./lib/visitor-keys.js",
// eslint-disable-next-line indent -- Readability
`/**
* @typedef {import('./index.js').VisitorKeys} VisitorKeys
*/
/**
* @type {VisitorKeys}
*/
const KEYS = ${JSON.stringify(mergedKeys, null, 4).replace(/"(.*?)":/gu, "$1:")};
// Types.
const NODE_TYPES = Object.keys(KEYS);
// Freeze the keys.
for (const type of NODE_TYPES) {
Object.freeze(KEYS[type]);
}
Object.freeze(KEYS);
export default KEYS;
`
);

})();
Loading

0 comments on commit ba5237d

Please sign in to comment.