Skip to content

Commit

Permalink
Adding createFlowUnionType in place of createUnionTypeAnnotati… (#11448)
Browse files Browse the repository at this point in the history
* 🔄 createUnionTypeAnnotation => createFlowUnionType

* ➕ add createFlowUnionType if it exists (in new versions only ⚠️)

* 🔄 use createFlowUnionType for createUnionTypeAnnotation
  • Loading branch information
Beraliv committed Apr 22, 2020
1 parent 928b9f8 commit a34424a
Show file tree
Hide file tree
Showing 6 changed files with 26 additions and 4 deletions.
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) {
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`,
// 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

0 comments on commit a34424a

Please sign in to comment.