Skip to content

Commit

Permalink
Add type definitions for assertion methods (#11883)
Browse files Browse the repository at this point in the history
Co-authored-by: Nicolò Ribaudo <nicolo.ribaudo@gmail.com>
  • Loading branch information
ExE-Boss and nicolo-ribaudo committed Nov 10, 2020
1 parent 2984f0c commit 3505eaa
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 20 deletions.
18 changes: 13 additions & 5 deletions packages/babel-types/scripts/generators/flow.js
Expand Up @@ -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`,
Expand Down
37 changes: 22 additions & 15 deletions packages/babel-types/scripts/generators/typescript.js
Expand Up @@ -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.
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 3505eaa

Please sign in to comment.