Skip to content

Commit

Permalink
improve
Browse files Browse the repository at this point in the history
  • Loading branch information
liuxingbaoyu committed Apr 5, 2023
1 parent 0a1cfc0 commit 680f69f
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 10 deletions.
35 changes: 25 additions & 10 deletions packages/babel-types/scripts/generators/validators.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ import {
VISITOR_KEYS,
} from "../../lib/index.js";

const BABEL8 = !!process.env.BABEL_8_BREAKING;

const has = Function.call.bind(Object.prototype.hasOwnProperty);

function buildCases(arr) {
Expand All @@ -31,7 +33,7 @@ function addIsHelper(type, aliasKeys, deprecated) {
if (placeholderTypes.length > 0) {
cases += `
case "Placeholder":
switch ((node as t.Placeholder).expectedNode) {
switch (${BABEL8 ? "node" : "(node as t.Placeholder)"}.expectedNode) {
${buildCases(placeholderTypes)}
default:
return false;
Expand All @@ -44,19 +46,25 @@ 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}(${
BABEL8
? `node: t.Node | null | undefined, opts?: Partial<t.${type}> | null`
: "node: object | null | undefined, opts?: object | null"
}): ${result} {
${deprecated || ""}
if (!node) return false;
${
cases
? `
switch((node as t.Node).type){
switch(${BABEL8 ? "node" : "(node as t.Node)"}.type){
${cases}
default:
return false;
}`
: `if ((node as t.Node).type !== ${targetType}) return false;`
: `if (${
BABEL8 ? "node" : "(node as t.Node)"
}.type !== ${targetType}) return false;`
}
return opts == null || shallowEqual(node, opts);
Expand Down Expand Up @@ -87,16 +95,23 @@ 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}(${
BABEL8
? `node: t.Node | null | undefined, opts?: Partial<t.${type}> | null`
: "node: object | null | undefined, opts?: object | null"
}): node is t.${newType} {
deprecationWarning("is${type}", "is${newType}");
return is${newType}(node, opts);
}
Expand Down
15 changes: 15 additions & 0 deletions packages/babel-types/src/validators/generated/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3954,6 +3954,9 @@ export function isTSBaseType(

return opts == null || shallowEqual(node, opts);
}
/**
* @deprecated Use `isNumericLiteral`
*/
export function isNumberLiteral(
node: object | null | undefined,
opts?: object | null,
Expand All @@ -3965,6 +3968,9 @@ export function isNumberLiteral(

return opts == null || shallowEqual(node, opts);
}
/**
* @deprecated Use `isRegExpLiteral`
*/
export function isRegexLiteral(
node: object | null | undefined,
opts?: object | null,
Expand All @@ -3976,6 +3982,9 @@ export function isRegexLiteral(

return opts == null || shallowEqual(node, opts);
}
/**
* @deprecated Use `isRestElement`
*/
export function isRestProperty(
node: object | null | undefined,
opts?: object | null,
Expand All @@ -3987,6 +3996,9 @@ export function isRestProperty(

return opts == null || shallowEqual(node, opts);
}
/**
* @deprecated Use `isSpreadElement`
*/
export function isSpreadProperty(
node: object | null | undefined,
opts?: object | null,
Expand All @@ -3998,6 +4010,9 @@ export function isSpreadProperty(

return opts == null || shallowEqual(node, opts);
}
/**
* @deprecated Use `isImportOrExportDeclaration`
*/
export function isModuleDeclaration(
node: object | null | undefined,
opts?: object | null,
Expand Down

0 comments on commit 680f69f

Please sign in to comment.