Skip to content

Commit

Permalink
feat: add a Flow-to-TypeScript migration (#1)
Browse files Browse the repository at this point in the history
* Revert "wow"

This reverts commit 7d07f40.

* back to basics

* does not work

* match snapshot
  • Loading branch information
morgante authored Jan 23, 2023
1 parent 491643b commit 836e739
Showing 1 changed file with 99 additions and 0 deletions.
99 changes: 99 additions & 0 deletions .grit/patterns/FlowToTypeScript.md
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;
```

0 comments on commit 836e739

Please sign in to comment.