Skip to content

Commit

Permalink
Use TS3.7 when possible for @babel/type dts (#12317)
Browse files Browse the repository at this point in the history
  • Loading branch information
nicolo-ribaudo committed Nov 9, 2020
1 parent 07607fa commit a9bc9be
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 3 deletions.
1 change: 1 addition & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
7 changes: 7 additions & 0 deletions packages/babel-types/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
14 changes: 11 additions & 3 deletions packages/babel-types/scripts/generators/typescript.js
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -130,16 +139,15 @@ 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
};`
);
}

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
Expand Down

0 comments on commit a9bc9be

Please sign in to comment.