Skip to content

Commit

Permalink
fixup! feat(babel‑types): Add type definitions for Node assertion met…
Browse files Browse the repository at this point in the history
…hods
  • Loading branch information
ExE-Boss committed Aug 27, 2020
1 parent 2cb4396 commit b9acb62
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 18 deletions.
11 changes: 5 additions & 6 deletions packages/babel-types/scripts/generators/flow.js
Expand Up @@ -112,11 +112,6 @@ for (const type in t.NODE_FIELDS) {
}
}

lines.push(
// assert/
`declare function assertNode(node: ?Object): void`
);

const typeCheckLineGroups = [[], []];
for (const typeName of t.TYPES.sort()) {
let decl = `declare function is${typeName}(node: ?Object, opts?: ?Object): boolean`;
Expand All @@ -139,7 +134,11 @@ for (const typeName of t.TYPES.sort()) {
}

lines.push(...typeCheckLineGroups[0]);
lines.push(...typeCheckLineGroups[1]);
lines.push(
// assert/
`declare function assertNode(node: ?Object): void`,
...typeCheckLineGroups[1]
);

lines.push(
// builders/
Expand Down
25 changes: 13 additions & 12 deletions packages/babel-types/scripts/generators/typescript.js
Expand Up @@ -135,20 +135,13 @@ for (const type in t.NODE_FIELDS) {
}
}

lines.push(
// assert/
`export function assertNode(node: object | null | undefined): void`
);

const typeCheckLineGroups = [[], []];
for (const typeName of t.TYPES) {
const isDeprecated = typeName in t.DEPRECATED_KEYS;
const realName = isDeprecated ? t.DEPRECATED_KEYS[typeName] : typeName;

const result =
const hasTypeDefinition = Boolean(
t.NODE_FIELDS[realName] || t.FLIPPED_ALIAS_KEYS[realName]
? `node is import("./index").${realName}`
: "boolean";
);

if (isDeprecated) {
const deprecatedAssert = `/** @deprecated Use \`assert${realName}\` */`;
Expand All @@ -158,7 +151,9 @@ for (const typeName of t.TYPES) {
}

typeCheckLineGroups[0].push(
`export function is${typeName}(node: object | null | undefined, opts?: object | null): ${result};`
`export function is${typeName}(node: object | null | undefined, opts?: object | null): ${
hasTypeDefinition ? `node is ${realName}` : "boolean"
};`
);

typeCheckLineGroups[1].push(
Expand All @@ -169,13 +164,19 @@ for (const typeName of t.TYPES) {
// TypeScript 3.7: https://github.com/microsoft/TypeScript/pull/32695 will allow assert declarations
// eslint-disable-next-line max-len
`export function assert${typeName}(node: object | null | undefined, opts?: object | null): ${
result === "boolean" ? "void" : `asserts ${result}`
hasTypeDefinition
? `asserts node is import("./index").${realName}`
: "void"
};`
);
}

lines.push(...typeCheckLineGroups[0]);
lines.push(...typeCheckLineGroups[1]);
lines.push(
// assert/
`export function assertNode(node: object | null | undefined): void`,
...typeCheckLineGroups[1]
);

lines.push(
// builders/
Expand Down

0 comments on commit b9acb62

Please sign in to comment.