Skip to content

Commit

Permalink
Remove backward compatibility for union types (#11468)
Browse files Browse the repository at this point in the history
  • Loading branch information
Beraliv committed Apr 26, 2020
1 parent 5b3ca43 commit 453ec42
Show file tree
Hide file tree
Showing 5 changed files with 8 additions and 32 deletions.
19 changes: 4 additions & 15 deletions packages/babel-traverse/src/path/inference/inferer-reference.js
Expand Up @@ -95,15 +95,11 @@ function getTypeAnnotationBindingConstantViolations(binding, path, name) {
return;
}

if (t.isTSTypeAnnotation(types[0]) && t.createTSUnionType) {
if (t.isTSTypeAnnotation(types[0])) {
return t.createTSUnionType(types);
}

if (t.createFlowUnionType) {
return t.createFlowUnionType(types);
}

return t.createUnionTypeAnnotation(types);
return t.createFlowUnionType(types);
}

function getConstantViolationsBefore(binding, path, functions) {
Expand Down Expand Up @@ -211,22 +207,15 @@ function getConditionalAnnotation(binding, path, name) {
}

if (types.length) {
if (t.isTSTypeAnnotation(types[0]) && t.createTSUnionType) {
if (t.isTSTypeAnnotation(types[0])) {
return {
typeAnnotation: t.createTSUnionType(types),
ifStatement,
};
}

if (t.createFlowUnionType) {
return {
typeAnnotation: t.createFlowUnionType(types),
ifStatement,
};
}

return {
typeAnnotation: t.createUnionTypeAnnotation(types),
typeAnnotation: t.createFlowUnionType(types),
ifStatement,
};
}
Expand Down
16 changes: 4 additions & 12 deletions packages/babel-traverse/src/path/inference/inferers.js
Expand Up @@ -88,15 +88,11 @@ export function LogicalExpression() {
this.get("right").getTypeAnnotation(),
];

if (t.isTSTypeAnnotation(argumentTypes[0]) && t.createTSUnionType) {
if (t.isTSTypeAnnotation(argumentTypes[0])) {
return t.createTSUnionType(argumentTypes);
}

if (t.createFlowUnionType) {
return t.createFlowUnionType(argumentTypes);
}

return t.createUnionTypeAnnotation(argumentTypes);
return t.createFlowUnionType(argumentTypes);
}

export function ConditionalExpression() {
Expand All @@ -105,15 +101,11 @@ export function ConditionalExpression() {
this.get("alternate").getTypeAnnotation(),
];

if (t.isTSTypeAnnotation(argumentTypes[0]) && t.createTSUnionType) {
if (t.isTSTypeAnnotation(argumentTypes[0])) {
return t.createTSUnionType(argumentTypes);
}

if (t.createFlowUnionType) {
return t.createFlowUnionType(argumentTypes);
}

return t.createUnionTypeAnnotation(argumentTypes);
return t.createFlowUnionType(argumentTypes);
}

export function SequenceExpression() {
Expand Down
2 changes: 0 additions & 2 deletions packages/babel-types/scripts/generators/flow.js
Expand Up @@ -127,8 +127,6 @@ lines.push(
// eslint-disable-next-line max-len
`declare function createTypeAnnotationBasedOnTypeof(type: 'string' | 'number' | 'undefined' | 'boolean' | 'function' | 'object' | 'symbol'): ${NODE_PREFIX}TypeAnnotation`,
// eslint-disable-next-line max-len
`declare function createUnionTypeAnnotation(types: Array<${NODE_PREFIX}FlowType>): ${NODE_PREFIX}UnionTypeAnnotation`,
// eslint-disable-next-line max-len
`declare function createFlowUnionType(types: Array<${NODE_PREFIX}FlowType>): ${NODE_PREFIX}UnionTypeAnnotation`,
// this smells like "internal API"
// eslint-disable-next-line max-len
Expand Down
2 changes: 0 additions & 2 deletions packages/babel-types/scripts/generators/typescript.js
Expand Up @@ -143,11 +143,9 @@ lines.push(
// builders/
// eslint-disable-next-line max-len
`export function createTypeAnnotationBasedOnTypeof(type: 'string' | 'number' | 'undefined' | 'boolean' | 'function' | 'object' | 'symbol'): StringTypeAnnotation | VoidTypeAnnotation | NumberTypeAnnotation | BooleanTypeAnnotation | GenericTypeAnnotation`,
`export function createUnionTypeAnnotation<T extends FlowType>(types: [T]): T`,
`export function createFlowUnionType<T extends FlowType>(types: [T]): T`,
// this probably misbehaves if there are 0 elements, and it's not a UnionTypeAnnotation if there's only 1
// it is possible to require "2 or more" for this overload ([T, T, ...T[]]) but it requires typescript 3.0
`export function createUnionTypeAnnotation(types: ReadonlyArray<FlowType>): UnionTypeAnnotation`,
`export function createFlowUnionType(types: ReadonlyArray<FlowType>): UnionTypeAnnotation`,
// this smells like "internal API"
// eslint-disable-next-line max-len
Expand Down
1 change: 0 additions & 1 deletion packages/babel-types/src/index.js
Expand Up @@ -9,7 +9,6 @@ export * from "./asserts/generated";

// builders
export { default as createTypeAnnotationBasedOnTypeof } from "./builders/flow/createTypeAnnotationBasedOnTypeof";
export { default as createUnionTypeAnnotation } from "./builders/flow/createFlowUnionType";
export { default as createFlowUnionType } from "./builders/flow/createFlowUnionType";
export { default as createTSUnionType } from "./builders/typescript/createTSUnionType";
export * from "./builders/generated";
Expand Down

0 comments on commit 453ec42

Please sign in to comment.