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
transform-spread: create TS types (not Flow) when using TS #11378
Changes from 10 commits
87f459d
e0f3fc2
4740c97
ae08396
d9c792d
aa8c16a
fb48f70
7e2c323
65331a3
93d5af2
5e9c920
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
function foo() { | ||
const x = 1 ? a() : b(); | ||
|
||
return [...x]; | ||
} | ||
|
||
function a(): number[] { | ||
return []; | ||
} | ||
|
||
function b(): number[] { | ||
return []; | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
{ | ||
"plugins": ["transform-spread"], | ||
"presets": ["typescript"] | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
import { TSUnionType } from "../generated"; | ||
import removeTypeDuplicates from "../../modifications/typescript/removeTypeDuplicates"; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. using typescript implementation here: https://github.com/babel/babel/pull/11378/files#diff-31b76153974cd0302007f05a570dfde6R10 |
||
|
||
/** | ||
* Takes an array of `types` and flattens them, removing duplicates and | ||
* returns a `UnionTypeAnnotation` node containg them. | ||
*/ | ||
export default function createTSUnionType( | ||
typeAnnotations: Array<Object>, | ||
): Object { | ||
const types = typeAnnotations.map(type => type.typeAnnotations); | ||
const flattened = removeTypeDuplicates(types); | ||
|
||
if (flattened.length === 1) { | ||
return flattened[0]; | ||
} else { | ||
return TSUnionType(flattened); | ||
Beraliv marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
import { | ||
isTSAnyKeyword, | ||
isTSUnionType, | ||
isTSBaseType, | ||
} from "../../validators/generated"; | ||
|
||
/** | ||
* Dedupe type annotations. | ||
*/ | ||
export default function removeTypeDuplicates( | ||
nodes: Array<Object>, | ||
): Array<Object> { | ||
const generics = {}; | ||
const bases = {}; | ||
|
||
// store union type groups to circular references | ||
const typeGroups = []; | ||
|
||
const types = []; | ||
|
||
for (let i = 0; i < nodes.length; i++) { | ||
const node = nodes[i]; | ||
if (!node) continue; | ||
|
||
// detect duplicates | ||
if (types.indexOf(node) >= 0) { | ||
continue; | ||
} | ||
|
||
// this type matches anything | ||
if (isTSAnyKeyword(node.type)) { | ||
return [node]; | ||
} | ||
|
||
// Analogue of FlowBaseAnnotation | ||
if (isTSBaseType(node)) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. use |
||
bases[node.type] = node; | ||
continue; | ||
} | ||
|
||
if (isTSUnionType(node)) { | ||
if (typeGroups.indexOf(node.types) < 0) { | ||
nodes = nodes.concat(node.types); | ||
typeGroups.push(node.types); | ||
} | ||
continue; | ||
} | ||
|
||
// TODO: add generic types | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. First I tried Now I'm sure it does NOT cover all cases |
||
|
||
types.push(node); | ||
} | ||
|
||
// add back in bases | ||
for (const type of Object.keys(bases)) { | ||
types.push(bases[type]); | ||
} | ||
|
||
// add back in generics | ||
for (const name of Object.keys(generics)) { | ||
types.push(generics[name]); | ||
} | ||
|
||
return types; | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4611,6 +4611,36 @@ export function isTSType(node: ?Object, opts?: Object): boolean { | |
|
||
return false; | ||
} | ||
export function isTSBaseType(node: ?Object, opts?: Object): boolean { | ||
if (!node) return false; | ||
|
||
const nodeType = node.type; | ||
if ( | ||
nodeType === "TSBaseType" || | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. moved logic from commit aa8c16a#diff-31b76153974cd0302007f05a570dfde6R47 here |
||
"TSAnyKeyword" === nodeType || | ||
"TSBooleanKeyword" === nodeType || | ||
"TSBigIntKeyword" === nodeType || | ||
"TSNeverKeyword" === nodeType || | ||
"TSNullKeyword" === nodeType || | ||
"TSNumberKeyword" === nodeType || | ||
"TSObjectKeyword" === nodeType || | ||
"TSStringKeyword" === nodeType || | ||
"TSSymbolKeyword" === nodeType || | ||
"TSUndefinedKeyword" === nodeType || | ||
"TSUnknownKeyword" === nodeType || | ||
"TSVoidKeyword" === nodeType || | ||
"TSThisType" === nodeType || | ||
"TSLiteralType" === nodeType | ||
) { | ||
if (typeof opts === "undefined") { | ||
return true; | ||
} else { | ||
return shallowEqual(node, opts); | ||
} | ||
} | ||
|
||
return false; | ||
} | ||
export function isNumberLiteral(node: ?Object, opts?: Object): boolean { | ||
console.trace( | ||
"The node type NumberLiteral has been renamed to NumericLiteral", | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is technically a breaking change: it makes the next
@babel/traverse
version incompatible with@babel/types
<= 7.9.5, because it doesn't contain this function.We should use
t.createTSUnionType?.(types)
instead.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oh, this is true, thanks for mentioning that
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@nicolo-ribaudo it will change the behaviour as returns
UnionTypeAnnotation
and now it returnsundefined
I added if-check in 5e9c920
✅