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

add 12 missing NODE_FIELDS #13577

Merged
merged 6 commits into from Jul 23, 2021
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 3 additions & 1 deletion packages/babel-types/package.json
Expand Up @@ -30,7 +30,9 @@
"devDependencies": {
"@babel/generator": "workspace:*",
"@babel/parser": "workspace:*",
"chalk": "^4.1.0"
"chalk": "^4.1.0",
"fs-extra": "^10.0.0",
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I guess we can replace fs-extra by JSON.parse(fs.readFileSync(#))?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I just switched it to promisify(fs.readFile), wouldn't want to block the entire process

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

argh, didn't realize tests run on node 8 though,I'll promisify it manually

"glob": "^7.1.7"
},
"engines": {
"node": ">=6.9.0"
Expand Down
13 changes: 13 additions & 0 deletions packages/babel-types/src/ast-types/generated/index.ts
Expand Up @@ -643,6 +643,7 @@ export interface RestElement extends BaseNode {
type: "RestElement";
argument: LVal;
decorators?: Array<Decorator> | null;
optional?: boolean | null;
typeAnnotation?: TypeAnnotation | TSTypeAnnotation | Noop | null;
}

Expand All @@ -653,6 +654,7 @@ export interface RestProperty extends BaseNode {
type: "RestProperty";
argument: LVal;
decorators?: Array<Decorator> | null;
optional?: boolean | null;
typeAnnotation?: TypeAnnotation | TSTypeAnnotation | Noop | null;
}

Expand Down Expand Up @@ -751,6 +753,7 @@ export interface ArrayPattern extends BaseNode {
type: "ArrayPattern";
elements: Array<null | PatternLike>;
decorators?: Array<Decorator> | null;
optional?: boolean | null;
typeAnnotation?: TypeAnnotation | TSTypeAnnotation | Noop | null;
}

Expand Down Expand Up @@ -835,6 +838,7 @@ export interface ExportDefaultDeclaration extends BaseNode {
| TSDeclareFunction
| ClassDeclaration
| Expression;
exportKind?: "value" | null;
}

export interface ExportNamedDeclaration extends BaseNode {
Expand Down Expand Up @@ -1024,6 +1028,7 @@ export interface ClassProperty extends BaseNode {
optional?: boolean | null;
override?: boolean;
readonly?: boolean | null;
variance?: Variance | null;
}

export interface ClassPrivateProperty extends BaseNode {
Expand All @@ -1032,7 +1037,10 @@ export interface ClassPrivateProperty extends BaseNode {
value?: Expression | null;
decorators?: Array<Decorator> | null;
static: any;
definite?: boolean | null;
readonly?: boolean | null;
typeAnnotation?: TypeAnnotation | TSTypeAnnotation | Noop | null;
variance?: Variance | null;
}

export interface ClassPrivateMethod extends BaseNode {
Expand Down Expand Up @@ -1142,6 +1150,7 @@ export interface DeclareOpaqueType extends BaseNode {
id: Identifier;
typeParameters?: TypeParameterDeclaration | null;
supertype?: FlowType | null;
impltype?: FlowType | null;
}

export interface DeclareVariable extends BaseNode {
Expand Down Expand Up @@ -1651,6 +1660,8 @@ export interface TSParameterProperty extends BaseNode {
type: "TSParameterProperty";
parameter: Identifier | AssignmentPattern;
accessibility?: "public" | "private" | "protected" | null;
decorators?: Array<Decorator> | null;
override?: boolean | null;
readonly?: boolean | null;
}

Expand Down Expand Up @@ -1710,6 +1721,7 @@ export interface TSPropertySignature extends BaseNode {
typeAnnotation?: TSTypeAnnotation | null;
initializer?: Expression | null;
computed?: boolean | null;
kind: "get" | "set";
optional?: boolean | null;
readonly?: boolean | null;
}
Expand Down Expand Up @@ -1992,6 +2004,7 @@ export interface TSImportEqualsDeclaration extends BaseNode {
type: "TSImportEqualsDeclaration";
id: Identifier;
moduleReference: TSEntityName | TSExternalModuleReference;
importKind?: "type" | "value" | null;
isExport: boolean;
}

Expand Down
26 changes: 26 additions & 0 deletions packages/babel-types/src/definitions/core.ts
Expand Up @@ -900,6 +900,11 @@ defineType("RestElement", {
? assertNodeType("LVal")
: assertNodeType("Identifier", "Pattern", "MemberExpression"),
},
// For Flow
optional: {
validate: assertValueType("boolean"),
optional: true,
},
},
validate(parent, key) {
if (!process.env.BABEL_TYPES_8_BREAKING) return;
Expand Down Expand Up @@ -1206,6 +1211,10 @@ defineType("ArrayPattern", {
),
optional: true,
},
optional: {
validate: assertValueType("boolean"),
optional: true,
},
},
});

Expand Down Expand Up @@ -1440,6 +1449,7 @@ defineType("ExportDefaultDeclaration", {
"Expression",
),
},
exportKind: validateOptional(assertOneOf("value")),
},
});

Expand Down Expand Up @@ -2095,6 +2105,10 @@ defineType("ClassProperty", {
validate: assertValueType("boolean"),
optional: true,
},
variance: {
validate: assertNodeType("Variance"),
optional: true,
},
},
});

Expand Down Expand Up @@ -2123,6 +2137,18 @@ defineType("ClassPrivateProperty", {
),
optional: true,
},
readonly: {
validate: assertValueType("boolean"),
optional: true,
},
definite: {
validate: assertValueType("boolean"),
optional: true,
},
variance: {
validate: assertNodeType("Variance"),
optional: true,
},
},
});

Expand Down
1 change: 1 addition & 0 deletions packages/babel-types/src/definitions/flow.ts
Expand Up @@ -121,6 +121,7 @@ defineType("DeclareOpaqueType", {
id: validateType("Identifier"),
typeParameters: validateOptionalType("TypeParameterDeclaration"),
supertype: validateOptionalType("FlowType"),
impltype: validateOptionalType("FlowType"),
},
});

Expand Down
18 changes: 18 additions & 0 deletions packages/babel-types/src/definitions/typescript.ts
Expand Up @@ -49,6 +49,17 @@ defineType("TSParameterProperty", {
parameter: {
validate: assertNodeType("Identifier", "AssignmentPattern"),
},
override: {
validate: assertValueType("boolean"),
optional: true,
},
decorators: {
validate: chain(
assertValueType("array"),
assertEach(assertNodeType("Decorator")),
),
optional: true,
},
},
});

Expand Down Expand Up @@ -110,6 +121,9 @@ defineType("TSPropertySignature", {
readonly: validateOptional(bool),
typeAnnotation: validateOptionalType("TSTypeAnnotation"),
initializer: validateOptionalType("Expression"),
kind: {
validate: assertOneOf("get", "set"),
},
},
});

Expand Down Expand Up @@ -494,6 +508,10 @@ defineType("TSImportEqualsDeclaration", {
"TSEntityName",
"TSExternalModuleReference",
]),
importKind: {
validate: assertOneOf("type", "value"),
optional: true,
},
},
});

Expand Down
107 changes: 107 additions & 0 deletions packages/babel-types/test/fields.js
@@ -0,0 +1,107 @@
import * as t from "../lib";
import glob from "glob";
import path from "path";
import fs from "fs-extra";
import { inspect } from "util";

// eslint-disable-next-line no-restricted-globals
const packages = path.resolve(__dirname, "..", "..");

function traverse(thing, visitor) {
if (Array.isArray(thing)) {
thing.forEach(elem => traverse(elem, visitor));
} else if (thing instanceof Object && typeof thing.type === "string") {
visitor(thing);
for (const key in thing) {
const value = thing[key];
if (value instanceof Object) traverse(value, visitor);
}
}
}

const files = glob.sync(
path.join("babel-parser", "test", "**", "output.json"),
{
cwd: packages,
ignore: [
path.join("**", "estree*", "**"),
path.join("**", "is-expression-babel-parser", "**"),
],
},
);

const ignoredFields = {
ArrowFunctionExpression: { id: true, predicate: true },
ClassMethod: { id: true, predicate: true },
ClassPrivateMethod: { id: true, predicate: true },
ClassPrivateProperty: { declare: true, optional: true },
FunctionDeclaration: { predicate: true },
FunctionExpression: { predicate: true },
ImportDeclaration: { attributes: true },
ObjectProperty: { method: true },
ObjectMethod: { method: true, id: true, predicate: true },
StaticBlock: { static: true },
TSDeclareMethod: { id: true },
};

function isEmpty(obj) {
for (const key in obj) return false;
return true;
}

describe("NODE_FIELDS contains all fields in", function () {
files.forEach(file =>
it(`${file}`, async function () {
const ast = await fs.readJson(path.resolve(packages, file));
if (ast.type === "File" && ast.errors && ast.errors.length) return;
t[`assert${ast.type}`](ast);
const missingFields = {};
traverse(ast, node => {
const { type } = node;
switch (type) {
case "File":
case "CommentBlock":
case "CommentLine":
return;
}
if (ignoredFields[type] === true) return;
const fields = t.NODE_FIELDS[type];
if (!fields) {
if (!missingFields[type]) {
missingFields[type] = {
MISSING_TYPE: true,
};
}
return;
}
for (const field in node) {
switch (field) {
case "type":
case "start":
case "end":
case "loc":
case "range":
case "leadingComments":
case "innerComments":
case "trailingComments":
case "comments":
case "extra":
continue;
}
if (!fields[field]) {
if (ignoredFields[type] && ignoredFields[type][field]) continue;
if (!missingFields[type]) missingFields[type] = {};
if (!missingFields[type][field]) {
missingFields[type][field] = true;
}
}
}
});
if (!isEmpty(missingFields)) {
throw new Error(
`the following NODE_FIELDS were missing: ${inspect(missingFields)}`,
);
}
}),
);
});
35 changes: 34 additions & 1 deletion yarn.lock
Expand Up @@ -3603,6 +3603,8 @@ __metadata:
"@babel/helper-validator-identifier": "workspace:^7.14.5"
"@babel/parser": "workspace:*"
chalk: ^4.1.0
fs-extra: ^10.0.0
glob: ^7.1.7
to-fast-properties: ^2.0.0
languageName: unknown
linkType: soft
Expand Down Expand Up @@ -8536,6 +8538,17 @@ __metadata:
languageName: node
linkType: hard

"fs-extra@npm:^10.0.0":
version: 10.0.0
resolution: "fs-extra@npm:10.0.0"
dependencies:
graceful-fs: ^4.2.0
jsonfile: ^6.0.1
universalify: ^2.0.0
checksum: 84632d143fe3125b8c3c2b1fedbbdfcfb84fc3e087522b4e138cc07edf574619925713a6609f6d5e53ede2e31ab319c7d528ea4a4a770ba6622a16bf4447cd8b
languageName: node
linkType: hard

"fs-minipass@npm:^2.0.0":
version: 2.1.0
resolution: "fs-minipass@npm:2.1.0"
Expand Down Expand Up @@ -8937,7 +8950,7 @@ fsevents@^1.2.7:
languageName: node
linkType: hard

"graceful-fs@npm:^4.0.0, graceful-fs@npm:^4.1.11, graceful-fs@npm:^4.1.15, graceful-fs@npm:^4.1.2, graceful-fs@npm:^4.1.6, graceful-fs@npm:^4.1.9, graceful-fs@npm:^4.2.2, graceful-fs@npm:^4.2.3, graceful-fs@npm:^4.2.4":
"graceful-fs@npm:^4.0.0, graceful-fs@npm:^4.1.11, graceful-fs@npm:^4.1.15, graceful-fs@npm:^4.1.2, graceful-fs@npm:^4.1.6, graceful-fs@npm:^4.1.9, graceful-fs@npm:^4.2.0, graceful-fs@npm:^4.2.2, graceful-fs@npm:^4.2.3, graceful-fs@npm:^4.2.4":
version: 4.2.6
resolution: "graceful-fs@npm:4.2.6"
checksum: 84d39c7756892553da990a9db7e45f844b3309b37b5a00174cbb4748476f2250c54f24594d4d252f64f085c65c2fdac7c809419bf6d55f0e6e42eb07ac0f5bf2
Expand Down Expand Up @@ -10715,6 +10728,19 @@ fsevents@^1.2.7:
languageName: node
linkType: hard

"jsonfile@npm:^6.0.1":
version: 6.1.0
resolution: "jsonfile@npm:6.1.0"
dependencies:
graceful-fs: ^4.1.6
universalify: ^2.0.0
dependenciesMeta:
graceful-fs:
optional: true
checksum: 9419c886abc6f8a5088cbb222b7bc17c76e8ee9f6c0e5c38781a4e09488166084f25247bc0b58e025b08c43064c82ae860ad89a992e35fc8cfae639323b7edbc
languageName: node
linkType: hard

"jsonify@npm:~0.0.0":
version: 0.0.0
resolution: "jsonify@npm:0.0.0"
Expand Down Expand Up @@ -14964,6 +14990,13 @@ typescript@~4.2.3:
languageName: node
linkType: hard

"universalify@npm:^2.0.0":
version: 2.0.0
resolution: "universalify@npm:2.0.0"
checksum: 36bfbdc97bd4b483596e66ea65e20663f5ab9ec3650157d99b075b7f97afcdefe46bbb23f89171dd75595d398cea3769a5b6d7130f5c66cae2a0f00904780f62
languageName: node
linkType: hard

"unset-value@npm:^1.0.0":
version: 1.0.0
resolution: "unset-value@npm:1.0.0"
Expand Down