Use TS3.7 when possible for @babel/type dts (#12317)

This commit is contained in:
Nicolò Ribaudo 2020-11-09 21:33:24 +01:00
parent 07607fa7c7
commit a9bc9becc5
3 changed files with 19 additions and 3 deletions

View File

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

View File

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

View File

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