Skip to content

Commit

Permalink
Improve type definitions for validators (#15549)
Browse files Browse the repository at this point in the history
* improve

* fix

* fix bug

* fix

* review

* improve
  • Loading branch information
liuxingbaoyu committed May 25, 2023
1 parent 560b8ac commit d54bc3c
Show file tree
Hide file tree
Showing 5 changed files with 986 additions and 993 deletions.
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;

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

0 comments on commit d54bc3c

Please sign in to comment.