Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

improve asserts in generated typescript definitions #12341

Merged
merged 1 commit into from Nov 16, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
7 changes: 4 additions & 3 deletions packages/babel-types/scripts/generators/typescript.js
Expand Up @@ -8,7 +8,7 @@ const toFunctionName = require("../utils/toFunctionName");
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`;
const asserts = ts3_7 ? assertion => `asserts ${assertion}` : () => `void`;

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 @@ -319,9 +319,10 @@ lines.push(
// the MemberExpression implication is incidental, but it follows from the implementation
// eslint-disable-next-line max-len
`export function matchesPattern(node: Node | null | undefined, match: string | ReadonlyArray<string>, allowPartial?: boolean): node is MemberExpression`,
// TypeScript 3.7: ": asserts n is T"
// eslint-disable-next-line max-len
`export function validate<T extends Node, K extends keyof T>(n: Node | null | undefined, key: K, value: T[K]): void`,
`export function validate<T extends Node, K extends keyof T>(n: Node | null | undefined, key: K, value: T[K]): ${asserts(
"n is T"
)}`,
`export function validate(n: Node, key: string, value: any): void;`
);

Expand Down