Skip to content

Commit

Permalink
fix: Don't throw in getTypeAnnotation when using TS+inference (#15383)
Browse files Browse the repository at this point in the history
  • Loading branch information
liuxingbaoyu committed Jan 15, 2024
1 parent 98c0885 commit 1200542
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 7 deletions.
Expand Up @@ -155,7 +155,6 @@ function inferAnnotationFromBinaryExpression(

if (operator !== "===" && operator !== "==") return;

//
let typeofPath: NodePath<t.UnaryExpression>;
let typePath: NodePath<t.Expression>;
if (left.isUnaryExpression({ operator: "typeof" })) {
Expand Down
12 changes: 6 additions & 6 deletions packages/babel-traverse/src/path/inference/util.ts
Expand Up @@ -8,23 +8,23 @@ import {
import type * as t from "@babel/types";

export function createUnionType(
types: Array<t.FlowType | t.TSType>,
): t.FlowType | t.TSType {
types: (t.FlowType | t.TSType)[],
): t.FlowType | t.TSType | undefined {
if (process.env.BABEL_8_BREAKING) {
if (isFlowType(types[0])) {
if (types.every(v => isFlowType(v))) {
return createFlowUnionType(types as t.FlowType[]);
}
if (isTSType(types[0])) {
if (types.every(v => isTSType(v))) {
return createTSUnionType(types as t.TSType[]);
}
} else {
if (isFlowType(types[0])) {
if (types.every(v => isFlowType(v))) {
if (createFlowUnionType) {
return createFlowUnionType(types as t.FlowType[]);
}

return createUnionTypeAnnotation(types as t.FlowType[]);
} else {
} else if (types.every(v => isTSType(v))) {
if (createTSUnionType) {
return createTSUnionType(types as t.TSType[]);
}
Expand Down
7 changes: 7 additions & 0 deletions packages/babel-traverse/test/inference.js
Expand Up @@ -701,5 +701,12 @@ describe("inference with TypeScript", function () {
const path = tsGetPath(input).get("body.0.expression");
expect(path.isGenericType("Array")).toBe(true);
});
it("should not throw both flow and ts types", () => {
const path = tsGetPath(
`const bar = 0 ? mkList() : [];function mkList(): any[] {return [];}`,
).get("body.0.declarations.0");
// TODO: This should be true
expect(path.isGenericType("Array")).toBe(false);
});
});
});

0 comments on commit 1200542

Please sign in to comment.