Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
transform-spread: create TS types (not Flow) when using TS (#11378)
* β add test fixture * β add removeTypeDuplicates for typescript * β add createTSUnionType for typescript * π fix ConditionalExpression for typescript * π fix ConditionalExpression * π fix added test case * β add new line at the end of the file * π types.every(f) => f(types[0]) * π bug => foo * β add TSBaseType * β add conditions NOT to break backward compatibility
- Loading branch information
Showing
11 changed files
with
170 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
13 changes: 13 additions & 0 deletions
13
packages/babel-traverse/test/fixtures/type-reference/input.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 []; | ||
} |
4 changes: 4 additions & 0 deletions
4
packages/babel-traverse/test/fixtures/type-reference/options.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
{ | ||
"plugins": ["transform-spread"], | ||
"presets": ["typescript"] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
19 changes: 19 additions & 0 deletions
19
packages/babel-types/src/builders/typescript/createTSUnionType.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
import { TSUnionType } from "../generated"; | ||
import removeTypeDuplicates from "../../modifications/typescript/removeTypeDuplicates"; | ||
|
||
/** | ||
* 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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
65 changes: 65 additions & 0 deletions
65
packages/babel-types/src/modifications/typescript/removeTypeDuplicates.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)) { | ||
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 | ||
|
||
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@Beraliv shouldn't this be aligned?
like createTSUnionType/createFlowUnionType