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

feat: add JSXSpreadChild and tool to build keys out of AST definitions #36

Merged
merged 14 commits into from
Mar 29, 2022
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
73 changes: 38 additions & 35 deletions lib/visitor-keys.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,6 @@
* @type {VisitorKeys}
*/
const KEYS = {
AssignmentExpression: [
"left",
"right"
],
AssignmentPattern: [
"left",
"right"
],
ArrayExpression: [
"elements"
],
Expand All @@ -24,16 +16,24 @@ const KEYS = {
"params",
"body"
],
AssignmentExpression: [
"left",
"right"
],
AssignmentPattern: [
"left",
"right"
],
AwaitExpression: [
"argument"
],
BlockStatement: [
"body"
],
BinaryExpression: [
"left",
"right"
],
BlockStatement: [
"body"
],
BreakStatement: [
"label"
],
Expand Down Expand Up @@ -75,6 +75,12 @@ const KEYS = {
"test"
],
EmptyStatement: [],
ExperimentalRestProperty: [
"argument"
],
ExperimentalSpreadProperty: [
"argument"
],
ExportAllDeclaration: [
"exported",
"source"
Expand All @@ -94,18 +100,6 @@ const KEYS = {
ExpressionStatement: [
"expression"
],
ExperimentalRestProperty: [
"argument"
],
ExperimentalSpreadProperty: [
"argument"
],
brettz9 marked this conversation as resolved.
Show resolved Hide resolved
ForStatement: [
"init",
"test",
"update",
"body"
],
ForInStatement: [
"left",
"right",
Expand All @@ -116,6 +110,12 @@ const KEYS = {
"right",
"body"
],
ForStatement: [
"init",
"test",
"update",
"body"
],
FunctionDeclaration: [
"id",
"params",
Expand Down Expand Up @@ -156,6 +156,7 @@ const KEYS = {
JSXClosingElement: [
"name"
],
JSXClosingFragment: [],
JSXElement: [
"openingElement",
"children",
Expand All @@ -165,6 +166,11 @@ const KEYS = {
JSXExpressionContainer: [
"expression"
],
JSXFragment: [
"openingFragment",
"children",
"closingFragment"
],
JSXIdentifier: [],
JSXMemberExpression: [
"object",
Expand All @@ -178,22 +184,19 @@ const KEYS = {
"name",
"attributes"
],
JSXOpeningFragment: [],
JSXSpreadAttribute: [
"argument"
],
JSXText: [],
JSXFragment: [
"openingFragment",
"children",
"closingFragment"
JSXSpreadChild: [
"expression"
],
brettz9 marked this conversation as resolved.
Show resolved Hide resolved
JSXClosingFragment: [],
JSXOpeningFragment: [],
Literal: [],
JSXText: [],
LabeledStatement: [
"label",
"body"
],
Literal: [],
LogicalExpression: [
"left",
"right"
Expand Down Expand Up @@ -248,14 +251,14 @@ const KEYS = {
"body"
],
Super: [],
SwitchStatement: [
"discriminant",
"cases"
],
SwitchCase: [
"test",
"consequent"
],
SwitchStatement: [
"discriminant",
"cases"
],
TaggedTemplateExpression: [
"tag",
"quasi"
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
8 changes: 8 additions & 0 deletions tests/lib/fixtures/new-keys-on-old.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
export interface AssignmentExpression {
type: "AssignmentExpression";
operator: AssignmentOperator;
up: Expression;
down: Expression;
left: Pattern | MemberExpression;
right: Expression;
}
8 changes: 8 additions & 0 deletions tests/lib/fixtures/new-keys.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
export interface NewFangledExpression {
type: "NewFangledExpression";
operator: AssignmentOperator;
up: Expression;
down: Expression;
left: Pattern | MemberExpression;
right: Expression;
}
67 changes: 67 additions & 0 deletions tests/lib/get-keys-from-ts.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
/**
* @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";
import backwardCompatibleKeys from "../../tools/backwardCompatibleKeys.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, ...backwardCompatibleKeys });

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);
});

it("sorts keys alphabetically if new", async () => {
const { keys: actual } = await getKeysFromTsFile(
"./tests/lib/fixtures/new-keys.d.ts"
);

const expected = {
NewFangledExpression: [
"down",
"left",
"right",
"up"
]
};

expect(actual).to.deep.equal(expected);
});

it("sorts extra keys at end alphabetically", async () => {
const { keys: actual } = await getKeysFromTsFile(
"./tests/lib/fixtures/new-keys-on-old.d.ts"
);

const expected = {
AssignmentExpression: [
"left",
"right",
"down",
"up"
]
};

expect(actual).to.deep.equal(expected);
});
});
10 changes: 10 additions & 0 deletions tools/backwardCompatibleKeys.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
const backwardCompatibleKeys = {
brettz9 marked this conversation as resolved.
Show resolved Hide resolved
ExperimentalRestProperty: [
"argument"
],
ExperimentalSpreadProperty: [
"argument"
]
};

export default backwardCompatibleKeys;
55 changes: 55 additions & 0 deletions tools/build-keys-from-ts.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
/**
* @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";
import backwardCompatibleKeys from "./backwardCompatibleKeys.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, ...backwardCompatibleKeys });

// 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