Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Respect BABEL_8_BREAKING option in babel-types fields test #17000

Merged
merged 3 commits into from
Dec 4, 2024
Merged
Changes from 1 commit
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
Prev Previous commit
perf: improve fields test performance
up to 10% faster because the Object.keys(missingFields).length check is replaced with a simple null check
  • Loading branch information
JLHwung committed Dec 4, 2024
commit 7e12b8d46830ab7225f61067189f99d348acd8f2
14 changes: 9 additions & 5 deletions packages/babel-types/test/fields.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ const ignoredFields = {

describe("NODE_FIELDS contains all fields, and the visitor order is correct, in", function () {
const reportedVisitorOrders = new Set();
const testingOnBabel8 = IS_BABEL_8();
const { traverseFast, VISITOR_KEYS } = t;

it.each(files)("%s", async file => {
const fixturePath = path.dirname(file);
Expand All @@ -44,13 +46,13 @@ describe("NODE_FIELDS contains all fields, and the visitor order is correct, in"
}
}
}
if (isBabel8Test !== IS_BABEL_8()) return;
if (isBabel8Test !== testingOnBabel8) return;

const ast = JSON.parse(readFileSync(file, "utf8"));
if (ast.type === "File" && ast.errors && ast.errors.length) return;
t[`assert${ast.type}`](ast);
const missingFields = {};
t.traverseFast(ast, node => {
let missingFields = null;
traverseFast(ast, node => {
const { type } = node;
switch (type) {
case "File":
Expand All @@ -62,6 +64,7 @@ describe("NODE_FIELDS contains all fields, and the visitor order is correct, in"
if (ignoredFields[type] === true) return;
const fields = t.NODE_FIELDS[type];
if (!fields) {
if (missingFields === null) missingFields = {};
if (!missingFields[type]) {
missingFields[type] = {
MISSING_TYPE: true,
Expand All @@ -85,14 +88,15 @@ describe("NODE_FIELDS contains all fields, and the visitor order is correct, in"
}
if (!fields[field]) {
if (ignoredFields[type] && ignoredFields[type][field]) continue;
if (missingFields === null) missingFields = {};
if (!missingFields[type]) missingFields[type] = {};
if (!missingFields[type][field]) {
missingFields[type][field] = true;
}
}
}

if (Object.keys(missingFields).length) {
if (missingFields !== null) {
throw new Error(
`The following NODE_FIELDS were missing: ${inspect(missingFields)}`,
);
Expand All @@ -108,7 +112,7 @@ describe("NODE_FIELDS contains all fields, and the visitor order is correct, in"
return;
}

const keys = t.VISITOR_KEYS[type];
const keys = VISITOR_KEYS[type];
for (let prev, i = 0; i < keys.length; i++) {
if (!node[prev]) {
prev = keys[i];
Expand Down
Loading