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 type definitions for validators #15549

Merged
merged 6 commits into from May 25, 2023
Merged
Show file tree
Hide file tree
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
Expand Up @@ -153,7 +153,7 @@ export default declare((api, options: Options) => {
skip();
return;
}
} else if (t.isIdentifier(expressionResult.deopt)) {
} else if (expressionResult.deopt?.isIdentifier()) {
// It's safe to hoist here if the deopt reason is an identifier (e.g. func param).
// The hoister will take care of how high up it can be hoisted.
return;
Expand Down
Expand Up @@ -768,10 +768,11 @@ You can set \`throwIfNamespace: false\` to bypass this warning.`,
const found = Object.create(null);

for (const attr of attribs) {
const { node } = attr;
const name =
t.isJSXAttribute(attr) &&
t.isJSXIdentifier(attr.name) &&
attr.name.name;
t.isJSXAttribute(node) &&
t.isJSXIdentifier(node.name) &&
node.name.name;
Comment on lines 770 to +775
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm glad this type change helped us catch a bug.


if (
runtime === "automatic" &&
Expand Down
2 changes: 1 addition & 1 deletion packages/babel-traverse/src/path/introspection.ts
Expand Up @@ -176,7 +176,7 @@ export function isCompletionRecord(
export function isStatementOrBlock(this: NodePath): boolean {
if (
this.parentPath.isLabeledStatement() ||
isBlockStatement(this.container)
isBlockStatement(this.container as t.Node)
) {
return false;
} else {
Expand Down
44 changes: 32 additions & 12 deletions packages/babel-types/scripts/generators/validators.js
Expand Up @@ -28,10 +28,15 @@ function addIsHelper(type, aliasKeys, deprecated) {
if (has(PLACEHOLDERS_FLIPPED_ALIAS, type)) {
placeholderTypes.push(...PLACEHOLDERS_FLIPPED_ALIAS[type]);
}
if (placeholderTypes.length > 0) {
if (placeholderTypes.length === 1) {
cases += `
case "Placeholder":
switch ((node as t.Placeholder).expectedNode) {
if (node.expectedNode === ${JSON.stringify(placeholderTypes[0])})
break;`;
} else if (placeholderTypes.length) {
cases += `
case "Placeholder":
switch (node.expectedNode) {
${buildCases(placeholderTypes)}
default:
return false;
Expand All @@ -44,19 +49,19 @@ function addIsHelper(type, aliasKeys, deprecated) {
? `node is t.${type}`
: "boolean";

return `export function is${type}(node: object | null | undefined, opts?: object | null): ${result} {
return `export function is${type}(node: t.Node | null | undefined, opts?: Opts<t.${type}> | null): ${result} {
${deprecated || ""}
if (!node) return false;

${
cases
? `
switch((node as t.Node).type){
switch(node.type){
${cases}
default:
return false;
}`
: `if ((node as t.Node).type !== ${targetType}) return false;`
: `if (node.type !== ${targetType}) return false;`
}

return opts == null || shallowEqual(node, opts);
Expand All @@ -69,10 +74,22 @@ export default function generateValidators() {
* This file is auto-generated! Do not modify it directly.
* To re-generate run 'make build'
*/

/* eslint-disable no-fallthrough */

import shallowEqual from "../../utils/shallowEqual";
import type * as t from "../..";
import deprecationWarning from "../../utils/deprecationWarning";
\n`;

type Opts<Object> = Partial<{
[Prop in keyof Object]: Object[Prop] extends t.Node
? t.Node | Object[Prop]
: Object[Prop] extends t.Node[]
? t.Node[] | Object[Prop]
: Object[Prop];
}>;

`;

Object.keys(VISITOR_KEYS).forEach(type => {
output += addIsHelper(type);
Expand All @@ -87,16 +104,19 @@ import deprecationWarning from "../../utils/deprecationWarning";
});

Object.keys(DEPRECATED_KEYS).forEach(type => {
output += addIsHelper(
type,
null,
`deprecationWarning("is${type}", "is${DEPRECATED_KEYS[type]}")`
);
const newType = DEPRECATED_KEYS[type];
output += `/**
* @deprecated Use \`is${newType}\`
*/
${addIsHelper(type, null, `deprecationWarning("is${type}", "is${newType}")`)}`;
});

Object.keys(DEPRECATED_ALIASES).forEach(type => {
const newType = DEPRECATED_ALIASES[type];
output += `export function is${type}(node: object | null | undefined, opts?: object | null): node is t.${newType} {
output += `/**
* @deprecated Use \`is${newType}\`
*/
export function is${type}(node: t.Node | null | undefined, opts?: Opts<t.${type}> | null): node is t.${newType} {
deprecationWarning("is${type}", "is${newType}");
return is${newType}(node, opts);
}
Expand Down