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

fix: Don't throw in getTypeAnnotation when using TS+inference #15383

Merged
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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);
});
});
});