From a9bc9becc527e74bf2848b048d556144bad3f5c3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Nicol=C3=B2=20Ribaudo?= Date: Mon, 9 Nov 2020 21:33:24 +0100 Subject: [PATCH] Use TS3.7 when possible for `@babel/type` dts (#12317) --- Makefile | 1 + packages/babel-types/package.json | 7 +++++++ .../babel-types/scripts/generators/typescript.js | 14 +++++++++++--- 3 files changed, 19 insertions(+), 3 deletions(-) diff --git a/Makefile b/Makefile index a291d780cc..2ef6a4fe90 100644 --- a/Makefile +++ b/Makefile @@ -49,6 +49,7 @@ build-flow-typings: build-typescript-typings: $(NODE) packages/babel-types/scripts/generators/typescript.js > packages/babel-types/lib/index.d.ts + $(NODE) packages/babel-types/scripts/generators/typescript.js --ts3.7 > packages/babel-types/lib/index-ts3.7.d.ts build-standalone: build-babel-standalone diff --git a/packages/babel-types/package.json b/packages/babel-types/package.json index b81e7e0988..e566d39811 100644 --- a/packages/babel-types/package.json +++ b/packages/babel-types/package.json @@ -15,6 +15,13 @@ }, "main": "lib/index.js", "types": "lib/index.d.ts", + "typesVersions": { + ">=3.7": { + "lib/index.d.ts": [ + "lib/index-ts3.7.d.ts" + ] + } + }, "dependencies": { "@babel/helper-validator-identifier": "workspace:^7.10.4", "lodash": "^4.17.19", diff --git a/packages/babel-types/scripts/generators/typescript.js b/packages/babel-types/scripts/generators/typescript.js index 405d2e776a..07c80082ca 100644 --- a/packages/babel-types/scripts/generators/typescript.js +++ b/packages/babel-types/scripts/generators/typescript.js @@ -4,6 +4,15 @@ const t = require("../../"); 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]); + let code = `// NOTE: This file is autogenerated. Do not modify. // See packages/babel-types/scripts/generators/typescript.js for script used. @@ -130,7 +139,7 @@ for (const typeName of t.TYPES) { `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 - `// export function assert${typeName}(node: object | null | undefined, opts?: object | null): asserts ${ + ts3_7`export function assert${typeName}(node: object | null | undefined, opts?: object | null): asserts ${ result === "boolean" ? "node" : result };` ); @@ -138,8 +147,7 @@ for (const typeName of t.TYPES) { lines.push( // assert/ - // Commented out as this declaration requires TypeScript 3.7 (what do?) - `// export function assertNode(obj: any): asserts obj is Node`, + ts3_7`export function assertNode(obj: any): asserts obj is Node`, // builders/ // eslint-disable-next-line max-len