-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add a Flow-to-TypeScript migration (#1)
* Revert "wow" This reverts commit 7d07f40. * back to basics * does not work * match snapshot
- Loading branch information
Showing
1 changed file
with
99 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,99 @@ | ||
# Convert Flow to TypeScript | ||
|
||
Converts Flow type annotations to TypeScript type annotations on a best-effort basis. | ||
|
||
```grit | ||
Program(and { | ||
[ | ||
maybe bubble or { | ||
ImportDeclaration(leadingComments = [CommentBlock($c), ...]), | ||
ExportDeclaration(leadingComments = [CommentBlock($c), ...]) | ||
} as $node => raw("/*" + $c + "*/\n" + unparse($node)) | ||
some bubble or { ImportDeclaration(), ExportDeclaration() } as $node => raw(unparse($node)) | ||
] | ||
maybe contains bubble TypeAnnotation() as $node => raw(unparse($node)) | ||
}) | ||
``` | ||
|
||
## Transform comment-style type annotations | ||
|
||
```js | ||
//@flow | ||
/*:: import type { Foo, Sam } from '../types';*/ | ||
/*:: import type { Dog } from './animals';*/ | ||
/*:: export type DogBreed = { | ||
name: string, | ||
} */ | ||
const animal = 'dog'; | ||
|
||
function checkDog(dog/*: Dog */)/*: string */ { | ||
return dog.name; | ||
}; | ||
|
||
function multiLine({ | ||
foo, | ||
bar | ||
}/*: { | ||
foo: string, | ||
bar: string | ||
} */) { | ||
console.log(foo); | ||
} | ||
|
||
const checkAnimalBreed = async ( | ||
{ breed, dog } /*: { | ||
breed: DogBreed, | ||
dog: Dog, | ||
} */, | ||
)/*: boolean */ => { | ||
return dog.breed === breed.name; | ||
}; | ||
|
||
const checkBoolean = async ()/*: boolean */ => { | ||
return false; | ||
}; | ||
|
||
export default checkAnimalBreed; | ||
``` | ||
|
||
```js | ||
//@flow | ||
import type { Foo, Sam } from '../types'; | ||
import type { Dog } from './animals'; | ||
export type DogBreed = { | ||
name: string, | ||
} | ||
const animal = 'dog'; | ||
|
||
function checkDog(dog: Dog): string { | ||
return dog.name; | ||
} | ||
|
||
function multiLine({ | ||
foo, | ||
bar | ||
}: { | ||
foo: string, | ||
bar: string | ||
}) { | ||
console.log(foo); | ||
} | ||
|
||
const checkAnimalBreed = async ( | ||
{ | ||
breed, | ||
dog | ||
}: { | ||
breed: DogBreed, | ||
dog: Dog, | ||
} | ||
): boolean => { | ||
return dog.breed === breed.name; | ||
}; | ||
|
||
const checkBoolean = async (): boolean => { | ||
return false; | ||
}; | ||
|
||
export default checkAnimalBreed; | ||
``` |