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

Adding createFlowUnionType in place of createUnionTypeAnnotation without breaking change #11448

Merged
merged 3 commits into from Apr 22, 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
11 changes: 11 additions & 0 deletions packages/babel-traverse/src/path/inference/inferer-reference.js
Expand Up @@ -99,6 +99,10 @@ function getTypeAnnotationBindingConstantViolations(binding, path, name) {
return t.createTSUnionType(types);
}

if (t.createFlowUnionType) {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Check if it's available (in new versions only)
Old version will go further

The same for other cases below

return t.createFlowUnionType(types);
}

return t.createUnionTypeAnnotation(types);
}

Expand Down Expand Up @@ -214,6 +218,13 @@ function getConditionalAnnotation(binding, path, name) {
};
}

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

return {
typeAnnotation: t.createUnionTypeAnnotation(types),
ifStatement,
Expand Down
8 changes: 8 additions & 0 deletions packages/babel-traverse/src/path/inference/inferers.js
Expand Up @@ -92,6 +92,10 @@ export function LogicalExpression() {
return t.createTSUnionType(argumentTypes);
}

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

return t.createUnionTypeAnnotation(argumentTypes);
}

Expand All @@ -105,6 +109,10 @@ export function ConditionalExpression() {
return t.createTSUnionType(argumentTypes);
}

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

return t.createUnionTypeAnnotation(argumentTypes);
}

Expand Down
2 changes: 2 additions & 0 deletions packages/babel-types/scripts/generators/flow.js
Expand Up @@ -128,6 +128,8 @@ lines.push(
`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`,
Copy link
Contributor Author

@Beraliv Beraliv Apr 20, 2020

Choose a reason for hiding this comment

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

not removing old declaration, adding the new one
same below

// this smells like "internal API"
// eslint-disable-next-line max-len
`declare function buildChildren(node: { children: Array<${NODE_PREFIX}JSXText | ${NODE_PREFIX}JSXExpressionContainer | ${NODE_PREFIX}JSXSpreadChild | ${NODE_PREFIX}JSXElement | ${NODE_PREFIX}JSXFragment | ${NODE_PREFIX}JSXEmptyExpression> }): Array<${NODE_PREFIX}JSXText | ${NODE_PREFIX}JSXExpressionContainer | ${NODE_PREFIX}JSXSpreadChild | ${NODE_PREFIX}JSXElement | ${NODE_PREFIX}JSXFragment>`,
Expand Down
2 changes: 2 additions & 0 deletions packages/babel-types/scripts/generators/typescript.js
Expand Up @@ -144,9 +144,11 @@ lines.push(
// 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
`export function buildChildren(node: { children: ReadonlyArray<JSXText | JSXExpressionContainer | JSXSpreadChild | JSXElement | JSXFragment | JSXEmptyExpression> }): JSXElement['children']`,
Expand Down
Expand Up @@ -6,9 +6,7 @@ import removeTypeDuplicates from "../../modifications/flow/removeTypeDuplicates"
* Takes an array of `types` and flattens them, removing duplicates and
* returns a `UnionTypeAnnotation` node containg them.
*/
export default function createUnionTypeAnnotation(
types: Array<Object>,
): Object {
export default function createFlowUnionType(types: Array<Object>): Object {
const flattened = removeTypeDuplicates(types);

if (flattened.length === 1) {
Expand Down
3 changes: 2 additions & 1 deletion packages/babel-types/src/index.js
Expand Up @@ -9,7 +9,8 @@ export * from "./asserts/generated";

// builders
export { default as createTypeAnnotationBasedOnTypeof } from "./builders/flow/createTypeAnnotationBasedOnTypeof";
export { default as createUnionTypeAnnotation } from "./builders/flow/createUnionTypeAnnotation";
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