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

feat(babel‑types): Add type definitions for Node assertion methods #11883

Merged
Show file tree
Hide file tree
Changes from all 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
18 changes: 13 additions & 5 deletions packages/babel-types/scripts/generators/flow.js
Expand Up @@ -113,17 +113,25 @@ for (const type in t.NODE_FIELDS) {
}
}

for (let i = 0; i < t.TYPES.length; i++) {
let decl = `declare function is${t.TYPES[i]}(node: ?Object, opts?: ?Object): boolean`;
for (const typeName of t.TYPES) {
const isDeprecated = !!t.DEPRECATED_KEYS[typeName];
const realName = isDeprecated ? t.DEPRECATED_KEYS[typeName] : typeName;

if (t.NODE_FIELDS[t.TYPES[i]]) {
decl += ` %checks (node instanceof ${NODE_PREFIX}${t.TYPES[i]})`;
let decl = `declare function is${typeName}(node: ?Object, opts?: ?Object): boolean`;
if (t.NODE_FIELDS[realName]) {
decl += ` %checks (node instanceof ${NODE_PREFIX}${realName})`;
}

lines.push(decl);

lines.push(
`declare function assert${typeName}(node: ?Object, opts?: ?Object): void`
);
}

lines.push(
// assert/
`declare function assertNode(obj: any): void`,

// builders/
// eslint-disable-next-line max-len
`declare function createTypeAnnotationBasedOnTypeof(type: 'string' | 'number' | 'undefined' | 'boolean' | 'function' | 'object' | 'symbol'): ${NODE_PREFIX}TypeAnnotation`,
Expand Down
37 changes: 22 additions & 15 deletions packages/babel-types/scripts/generators/typescript.js
Expand Up @@ -5,13 +5,10 @@ const stringifyValidator = require("../utils/stringifyValidator");
const toFunctionName = require("../utils/toFunctionName");

// For backward compat, we cannot use TS 3.7 syntax in published packages
const ts3_7 = process.argv.includes("--ts3.7")
? (code, ...substitutions) => template(code, substitutions)
: () => "";
const template = (strings, substitutions) =>
strings
.slice(1)
.reduce((res, str, i) => res + substitutions[i] + str, strings[0]);
const ts3_7 = process.argv.includes("--ts3.7");

// TypeScript 3.7: https://github.com/microsoft/TypeScript/pull/32695 will allow assert declarations
const asserts = ts3_7 ? assertion => `asserts ${assertion}` : () => `boolean`;

let code = `// NOTE: This file is autogenerated. Do not modify.
// See packages/babel-types/scripts/generators/typescript.js for script used.
Expand Down Expand Up @@ -130,24 +127,34 @@ for (const type in t.NODE_FIELDS) {
}

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

const result =
t.NODE_FIELDS[typeName] || t.FLIPPED_ALIAS_KEYS[typeName]
? `node is ${typeName}`
t.NODE_FIELDS[realName] || t.FLIPPED_ALIAS_KEYS[realName]
? `node is ${realName}`
: "boolean";

if (isDeprecated) {
lines.push(`/** @deprecated Use \`is${realName}\` */`);
}
lines.push(
`export function is${typeName}(node: object | null | undefined, opts?: object | null): ${result};`
);

if (isDeprecated) {
lines.push(`/** @deprecated Use \`assert${realName}\` */`);
}
lines.push(
`export function is${typeName}(node: object | null | undefined, opts?: object | null): ${result};`,
// TypeScript 3.7: https://github.com/microsoft/TypeScript/pull/32695 will allow assert declarations
// eslint-disable-next-line max-len
ts3_7`export function assert${typeName}(node: object | null | undefined, opts?: object | null): asserts ${
`export function assert${typeName}(node: object | null | undefined, opts?: object | null): ${asserts(
result === "boolean" ? "node" : result
};`
)};`
);
}

lines.push(
// assert/
ts3_7`export function assertNode(obj: any): asserts obj is Node`,
`export function assertNode(obj: any): ${asserts("obj is Node")}`,

// builders/
// eslint-disable-next-line max-len
Expand Down