Adding createFlowUnionType in place of createUnionTypeAnnotati… (#11448)

* 🔄 createUnionTypeAnnotation => createFlowUnionType

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

* 🔄 use createFlowUnionType for createUnionTypeAnnotation
This commit is contained in:
beraliv 2020-04-22 20:11:01 +03:00 committed by GitHub
parent 928b9f8c95
commit a34424a894
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 26 additions and 4 deletions

View File

@ -99,6 +99,10 @@ function getTypeAnnotationBindingConstantViolations(binding, path, name) {
return t.createTSUnionType(types); return t.createTSUnionType(types);
} }
if (t.createFlowUnionType) {
return t.createFlowUnionType(types);
}
return t.createUnionTypeAnnotation(types); return t.createUnionTypeAnnotation(types);
} }
@ -214,6 +218,13 @@ function getConditionalAnnotation(binding, path, name) {
}; };
} }
if (t.createFlowUnionType) {
return {
typeAnnotation: t.createFlowUnionType(types),
ifStatement,
};
}
return { return {
typeAnnotation: t.createUnionTypeAnnotation(types), typeAnnotation: t.createUnionTypeAnnotation(types),
ifStatement, ifStatement,

View File

@ -92,6 +92,10 @@ export function LogicalExpression() {
return t.createTSUnionType(argumentTypes); return t.createTSUnionType(argumentTypes);
} }
if (t.createFlowUnionType) {
return t.createFlowUnionType(argumentTypes);
}
return t.createUnionTypeAnnotation(argumentTypes); return t.createUnionTypeAnnotation(argumentTypes);
} }
@ -105,6 +109,10 @@ export function ConditionalExpression() {
return t.createTSUnionType(argumentTypes); return t.createTSUnionType(argumentTypes);
} }
if (t.createFlowUnionType) {
return t.createFlowUnionType(argumentTypes);
}
return t.createUnionTypeAnnotation(argumentTypes); return t.createUnionTypeAnnotation(argumentTypes);
} }

View File

@ -128,6 +128,8 @@ lines.push(
`declare function createTypeAnnotationBasedOnTypeof(type: 'string' | 'number' | 'undefined' | 'boolean' | 'function' | 'object' | 'symbol'): ${NODE_PREFIX}TypeAnnotation`, `declare function createTypeAnnotationBasedOnTypeof(type: 'string' | 'number' | 'undefined' | 'boolean' | 'function' | 'object' | 'symbol'): ${NODE_PREFIX}TypeAnnotation`,
// eslint-disable-next-line max-len // eslint-disable-next-line max-len
`declare function createUnionTypeAnnotation(types: Array<${NODE_PREFIX}FlowType>): ${NODE_PREFIX}UnionTypeAnnotation`, `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" // this smells like "internal API"
// eslint-disable-next-line max-len // 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>`, `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>`,

View File

@ -144,9 +144,11 @@ lines.push(
// eslint-disable-next-line max-len // eslint-disable-next-line max-len
`export function createTypeAnnotationBasedOnTypeof(type: 'string' | 'number' | 'undefined' | 'boolean' | 'function' | 'object' | 'symbol'): StringTypeAnnotation | VoidTypeAnnotation | NumberTypeAnnotation | BooleanTypeAnnotation | GenericTypeAnnotation`, `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 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 // 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 // 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 createUnionTypeAnnotation(types: ReadonlyArray<FlowType>): UnionTypeAnnotation`,
`export function createFlowUnionType(types: ReadonlyArray<FlowType>): UnionTypeAnnotation`,
// this smells like "internal API" // this smells like "internal API"
// eslint-disable-next-line max-len // eslint-disable-next-line max-len
`export function buildChildren(node: { children: ReadonlyArray<JSXText | JSXExpressionContainer | JSXSpreadChild | JSXElement | JSXFragment | JSXEmptyExpression> }): JSXElement['children']`, `export function buildChildren(node: { children: ReadonlyArray<JSXText | JSXExpressionContainer | JSXSpreadChild | JSXElement | JSXFragment | JSXEmptyExpression> }): JSXElement['children']`,

View File

@ -6,9 +6,7 @@ import removeTypeDuplicates from "../../modifications/flow/removeTypeDuplicates"
* Takes an array of `types` and flattens them, removing duplicates and * Takes an array of `types` and flattens them, removing duplicates and
* returns a `UnionTypeAnnotation` node containg them. * returns a `UnionTypeAnnotation` node containg them.
*/ */
export default function createUnionTypeAnnotation( export default function createFlowUnionType(types: Array<Object>): Object {
types: Array<Object>,
): Object {
const flattened = removeTypeDuplicates(types); const flattened = removeTypeDuplicates(types);
if (flattened.length === 1) { if (flattened.length === 1) {

View File

@ -9,7 +9,8 @@ export * from "./asserts/generated";
// builders // builders
export { default as createTypeAnnotationBasedOnTypeof } from "./builders/flow/createTypeAnnotationBasedOnTypeof"; 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 { default as createTSUnionType } from "./builders/typescript/createTSUnionType";
export * from "./builders/generated"; export * from "./builders/generated";