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

Remove backward compatibility for union types #11468

Merged
merged 2 commits into from Apr 26, 2020
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
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])) {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we always have createTSUnionType in Babel 8

return t.createTSUnionType(types);
}

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

return t.createUnionTypeAnnotation(types);
return t.createFlowUnionType(types);
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We always have createFlowUnionType in Babel 8 as well 👌

}

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