Debian Codegraph @

packages/babel-types/test/fields.js

raw
1 import * as t from "../lib/index.js";
2 import glob from "glob";
3 import path from "path";
4 import fs from "fs";
5 import { inspect } from "util";
6 import { fileURLToPath } from "url";
7
8 const dirname = path.dirname(fileURLToPath(import.meta.url));
9 const packages = path.resolve(dirname, "..", "..");
10
11 function readJson(file) {
12 return new Promise((resolve, reject) => {
13 fs.readFile(file, "utf8", (err, data) => {
14 if (err) reject(err);
15 else resolve(JSON.parse(data));
16 });
17 });
18 }
19
20 function traverse(thing, visitor) {
21 if (Array.isArray(thing)) {
22 thing.forEach(elem => traverse(elem, visitor));
23 } else if (thing instanceof Object && typeof thing.type === "string") {
24 visitor(thing);
25 for (const key in thing) {
26 const value = thing[key];
27 if (value instanceof Object) traverse(value, visitor);
28 }
29 }
30 }
31
32 const files = glob.sync(
33 path.join("babel-parser", "test", "**", "output.json"),
34 {
35 cwd: packages,
36 ignore: [
37 path.join("**", "estree*", "**"),
38 path.join("**", "is-expression-babel-parser", "**"),
39 ],
40 },
41 );
42
43 const ignoredFields = {
44 ArrowFunctionExpression: { id: true, predicate: true },
45 ClassMethod: { id: true, predicate: true },
46 ClassPrivateMethod: { id: true, predicate: true },
47 ClassPrivateProperty: { declare: true, optional: true },
48 FunctionDeclaration: { predicate: true },
49 FunctionExpression: { predicate: true },
50 ImportDeclaration: { attributes: true },
51 ObjectProperty: { method: true },
52 ObjectMethod: { method: true, id: true, predicate: true },
53 StaticBlock: { static: true },
54 TSDeclareMethod: { id: true },
55 ...(process.env.BABEL_8_BREAKING
56 ? {
57 TSFunctionType: { parameters: true, typeAnnotation: true },
58 TSMethodSignature: { parameters: true, typeAnnotation: true },
59 TSConstructorType: { parameters: true, typeAnnotation: true },
60 TSCallSignatureDeclaration: { parameters: true, typeAnnotation: true },
61 TSConstructSignatureDeclaration: {
62 parameters: true,
63 typeAnnotation: true,
64 },
65 }
66 : {
67 TSFunctionType: { params: true, returnType: true },
68 TSMethodSignature: { params: true, returnType: true },
69 TSConstructorType: { params: true, returnType: true },
70 TSCallSignatureDeclaration: { params: true, returnType: true },
71 TSConstructSignatureDeclaration: { params: true, returnType: true },
72 }),
73 };
74
75 function isEmpty(obj) {
76 for (const key in obj) return false;
77 return true;
78 }
79
80 describe("NODE_FIELDS contains all fields in", function () {
81 files.forEach(file =>
82 it(`${file}`, async function () {
83 const ast = await readJson(path.resolve(packages, file));
84 if (ast.type === "File" && ast.errors && ast.errors.length) return;
85 t[`assert${ast.type}`](ast);
86 const missingFields = {};
87 traverse(ast, node => {
88 const { type } = node;
89 switch (type) {
90 case "File":
91 case "CommentBlock":
92 case "CommentLine":
93 return;
94 }
95 if (ignoredFields[type] === true) return;
96 const fields = t.NODE_FIELDS[type];
97 if (!fields) {
98 if (!missingFields[type]) {
99 missingFields[type] = {
100 MISSING_TYPE: true,
101 };
102 }
103 return;
104 }
105 for (const field in node) {
106 switch (field) {
107 case "type":
108 case "start":
109 case "end":
110 case "loc":
111 case "range":
112 case "leadingComments":
113 case "innerComments":
114 case "trailingComments":
115 case "comments":
116 case "extra":
117 continue;
118 }
119 if (!fields[field]) {
120 if (ignoredFields[type] && ignoredFields[type][field]) continue;
121 if (!missingFields[type]) missingFields[type] = {};
122 if (!missingFields[type][field]) {
123 missingFields[type][field] = true;
124 }
125 }
126 }
127 });
128 if (!isEmpty(missingFields)) {
129 throw new Error(
130 `the following NODE_FIELDS were missing: ${inspect(missingFields)}`,
131 );
132 }
133 }),
134 );
135 });