diff --git a/packages/babel-types/scripts/generators/flow.js b/packages/babel-types/scripts/generators/flow.js index ade85fc49820..2a91703353db 100644 --- a/packages/babel-types/scripts/generators/flow.js +++ b/packages/babel-types/scripts/generators/flow.js @@ -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`, diff --git a/packages/babel-types/scripts/generators/typescript.js b/packages/babel-types/scripts/generators/typescript.js index 07c80082cac8..8ef2cb12ea01 100644 --- a/packages/babel-types/scripts/generators/typescript.js +++ b/packages/babel-types/scripts/generators/typescript.js @@ -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};` + ); + + 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