Add type definitions for assertion methods (#11883)

Co-authored-by: Nicolò Ribaudo <nicolo.ribaudo@gmail.com>
This commit is contained in:
ExE Boss 2020-11-10 16:42:25 +01:00 committed by GitHub
parent 2984f0cb88
commit 3505eaadba
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 35 additions and 20 deletions

View File

@ -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`,

View File

@ -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.
@ -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};`,
// 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 is${typeName}(node: object | null | undefined, opts?: object | null): ${result};`
);
if (isDeprecated) {
lines.push(`/** @deprecated Use \`assert${realName}\` */`);
}
lines.push(
`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