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++) { for (const typeName of t.TYPES) {
let decl = `declare function is${t.TYPES[i]}(node: ?Object, opts?: ?Object): boolean`; const isDeprecated = !!t.DEPRECATED_KEYS[typeName];
const realName = isDeprecated ? t.DEPRECATED_KEYS[typeName] : typeName;
if (t.NODE_FIELDS[t.TYPES[i]]) { let decl = `declare function is${typeName}(node: ?Object, opts?: ?Object): boolean`;
decl += ` %checks (node instanceof ${NODE_PREFIX}${t.TYPES[i]})`; if (t.NODE_FIELDS[realName]) {
decl += ` %checks (node instanceof ${NODE_PREFIX}${realName})`;
} }
lines.push(decl); lines.push(decl);
lines.push(
`declare function assert${typeName}(node: ?Object, opts?: ?Object): void`
);
} }
lines.push( lines.push(
// assert/
`declare function assertNode(obj: any): void`,
// builders/ // builders/
// eslint-disable-next-line max-len // eslint-disable-next-line max-len
`declare function createTypeAnnotationBasedOnTypeof(type: 'string' | 'number' | 'undefined' | 'boolean' | 'function' | 'object' | 'symbol'): ${NODE_PREFIX}TypeAnnotation`, `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"); const toFunctionName = require("../utils/toFunctionName");
// For backward compat, we cannot use TS 3.7 syntax in published packages // For backward compat, we cannot use TS 3.7 syntax in published packages
const ts3_7 = process.argv.includes("--ts3.7") const ts3_7 = process.argv.includes("--ts3.7");
? (code, ...substitutions) => template(code, substitutions)
: () => ""; // TypeScript 3.7: https://github.com/microsoft/TypeScript/pull/32695 will allow assert declarations
const template = (strings, substitutions) => const asserts = ts3_7 ? assertion => `asserts ${assertion}` : () => `boolean`;
strings
.slice(1)
.reduce((res, str, i) => res + substitutions[i] + str, strings[0]);
let code = `// NOTE: This file is autogenerated. Do not modify. let code = `// NOTE: This file is autogenerated. Do not modify.
// See packages/babel-types/scripts/generators/typescript.js for script used. // 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) { for (const typeName of t.TYPES) {
const isDeprecated = !!t.DEPRECATED_KEYS[typeName];
const realName = isDeprecated ? t.DEPRECATED_KEYS[typeName] : typeName;
const result = const result =
t.NODE_FIELDS[typeName] || t.FLIPPED_ALIAS_KEYS[typeName] t.NODE_FIELDS[realName] || t.FLIPPED_ALIAS_KEYS[realName]
? `node is ${typeName}` ? `node is ${realName}`
: "boolean"; : "boolean";
if (isDeprecated) {
lines.push(`/** @deprecated Use \`is${realName}\` */`);
}
lines.push( lines.push(
`export function is${typeName}(node: object | null | undefined, opts?: object | null): ${result};`, `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 ${ 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 result === "boolean" ? "node" : result
};` )};`
); );
} }
lines.push( lines.push(
// assert/ // assert/
ts3_7`export function assertNode(obj: any): asserts obj is Node`, `export function assertNode(obj: any): ${asserts("obj is Node")}`,
// builders/ // builders/
// eslint-disable-next-line max-len // eslint-disable-next-line max-len