Commit f59f982
Pinterest contributions (#7)
* [Dep] upgrade to Recast from 0.20.4 to 0.23.4
**What?**
Upgrade [recast](https://github.com/benjamn/recast) from `0.20.4` to `0.23.4` for more modern parsers. See [changes](benjamn/[email protected]).
As part of the upgrade, the `[email protected]` patch is removed as it is no longer needed.
**Why?**
When the codemod with `[email protected]` removes comments, the parentheses can be incorrectly removed.
Upgrading to `[email protected]` fixes this.
For example, the following Flow type:
```js
const a =
// $FlowFixMe
(1 + 1) * 2;
```
was incorrectly converted to:
```ts
const a = 1 + 1 * 2;
```
With the new version, it gets correctly converted to:
```ts
const a = (1 + 1) * 2;
```
Also, codemod with `[email protected]` keeps the following annotation as is:
```js
type Props = {
it: string,
foo: number
};
````
With [this fix](benjamn/recast#1157) from v0.21.2, the Flow syntax gets correctly converted to the expected TypeScript syntax:
```ts
type Props = {
it: string;
foo: number;
};
```
Lastly, v0.23.4 correctly handles unary expressions by [wrapping unary expressions in parens](benjamn/recast#1361)
* Handle indexed access, `$Partial`, `$ReadOnlySet`, and `$ReadOnlyMap`
**What**
Add the following support (Flow syntax --> TypeScript syntax):
- `T[K]` --> `T[K]`
- `T?.[K]` --> `NonNullable<T>[K] | null | undefined`
- `$Partial<T>` --> `Partial<T>`
- `$ReadOnlySet<T>` --> `ReadonlySet<T>`
- `$ReadOnlyMap<K, V>` --> `ReadonlyMap<K, V>`
**Why**
To support more Flow syntax.
* Fix bugs with maybe function types and interaction types
**Maybe function types**
Flow code
```js
?() => void
```
was incorrectly transformed to:
```ts
() => void | null | undefined
```
It is now correctly transformed to:
```ts
(() => void) | null | undefined
```
**Intersection types**
Flow code
```js
(A | B) & (C | D)
```
was incorrectly transformed to:
```ts
(A | B & C | D)
```
It is now correctly transformed to:
```ts
((A | B) & (C | D))
```
* Fix false positives of privates types
**What**
Fix cases where `A$B` are public types instead of private types:
1. Relay generated types such as `PinRep_pin$data``
2. Type Alias with prefix '$IMPORTED_TYPE$'
**Why**
To avoid false positives in the private types detection
* Add override to force JSX through comment
**What**
If a file contain the comment `@jsx`, treat it as a JSX file.
**Why**
A mock file for a `.tsx` file must have the `.tsx` file extension for jest to find it.
However, if the mock file does not contain any JSX, the codemod would use the file extension `.ts`.
As a workaround, we introduce the ability to add a `@jsx` comment to the mock file to force the codemod to treat it as a JSX file. This ensures the mock file us
es the `.tsx` extension, even if it doesn't contain any JSX.
* Strip flow comments
**What**
- Remove flow-specific ESLint error suppression comments
- Make sure to retain non-flow comments at the top of files
**Why**
The flow-related comments are no longer needed.
Co-authored-by: Jack Hsu <[email protected]>
* [react-router-dom] Improve handling of react-router-dom types
**What**
The following Flow code:
```js
import { type Location, type Match, type RouterHistory } from 'react-router-dom';
type Props = { match: Match };
```
gets correctly converted to TypeScript code:
```ts
import { Location, History as RouterHistory } from 'history';
import { match } from 'react-router-dom';
type Props = {
match: match<{ [key: string]: string | undefined }>
};
```
**Why**
For the following types from `react-router-dom`:
- The `Location` type is moved to `history`.
- The `RouterHistory` type is moved to `history`, and is aliased to `History` instead.
- The `Match` type becomes a generic type `match`.
* [react] improve handling of React types
1. Handle named imports
In addition to supporting `React.*` types:
```js
import * as React from 'react';
type T = React.Node | React.Element | React.ChildrenArray;
```
We also support named imports:
```js
import { type Node as ReactNode, type Element as ReactElement, type ChildrenArray } from 'react';
type T = ReactNode | ReactElement | ChildrenArray;
```
NOTE: To avoid name conflicts with the DOM `Node` and `Element` type, it is important to alias `React.Node` as `ReactNode` and `React.Element` as `ReactElement`.
2. Strip type annotations from React function component return types in favor of inference
Flow:
```js
const App = ({ message }: AppProps): React.Node => <div>{message}</div>;
```
TypeScript:
```ts
const App = ({ message }: AppProps) => <div>{message}</div>;
```
Why not simply change `React.Node` to `React.ReactNode`?
If we leave it as `React.ReactNode`, we'll receive the error `'Component' cannot be used as a JSX component`. To address this, we simply strip out the type annotation from the return type of the function component and allow TypeScript to infer it.
3. Strip out `AbstractComponent` in favor of inference
Flow:
```js
// @flow
const C: React.AbstractComponent<Props, mixed> = React.forwardRef<Props, Ref>(Comp);
export default (forwardRef<Props, Ref>(Comp): React.AbstractComponent<Props, mixed>);
```
TypeScript:
```ts
const C = forwardRef<Ref, Props>(Comp);
export default forwardRef<Ref, Props>(Comp);
```
Why? Because there is no `React.AbstractComponent` equivalent in TypeScript. We can simply strip it out and allow TypeScript to infer the type.
4. Reverse params for `forwardRef`
Flow:
```js
forwardRef<Props, Ref>(Comp);
```
TypeScript:
```ts
forwardRef<Ref, Props>(Comp);
```
Why? Because the arguments are swapped in TypeScript.
5. Rename `ElementConfig`, `ElementProps`, `Portal`, and `StatelessFunctionalComponent`
- `ElementConfig` --> `ComponentProps`
- `ElementProps` --> `ComponentProps`
- `Portal` --> `PortalProps`
- `StatelessFunctionalComponent` --> `FC`
---------
Co-authored-by: Mark Molinaro <[email protected]>1 parent 078e7d4 commit f59f982
File tree
25 files changed
+969
-236
lines changed- patches
- src
- convert
- jsx-spread
- migrate
- utils
- test
- regression/__snapshots__
- test-files
25 files changed
+969
-236
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1 | 1 | | |
2 | | - | |
| 2 | + | |
3 | 3 | | |
4 | 4 | | |
5 | 5 | | |
6 | 6 | | |
7 | 7 | | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
8 | 13 | | |
9 | 14 | | |
10 | 15 | | |
| |||
83 | 88 | | |
84 | 89 | | |
85 | 90 | | |
86 | | - | |
| 91 | + | |
87 | 92 | | |
88 | 93 | | |
89 | 94 | | |
| |||
94 | 99 | | |
95 | 100 | | |
96 | 101 | | |
97 | | - | |
| 102 | + | |
98 | 103 | | |
99 | | - | |
| 104 | + | |
100 | 105 | | |
101 | | - | |
| 106 | + | |
102 | 107 | | |
103 | 108 | | |
104 | 109 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
65 | 65 | | |
66 | 66 | | |
67 | 67 | | |
68 | | - | |
| 68 | + | |
69 | 69 | | |
70 | 70 | | |
71 | 71 | | |
| |||
This file was deleted.
This file was deleted.
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
107 | 107 | | |
108 | 108 | | |
109 | 109 | | |
| 110 | + | |
| 111 | + | |
| 112 | + | |
| 113 | + | |
| 114 | + | |
| 115 | + | |
110 | 116 | | |
111 | 117 | | |
112 | 118 | | |
| |||
168 | 174 | | |
169 | 175 | | |
170 | 176 | | |
| 177 | + | |
| 178 | + | |
| 179 | + | |
| 180 | + | |
| 181 | + | |
| 182 | + | |
| 183 | + | |
| 184 | + | |
| 185 | + | |
| 186 | + | |
| 187 | + | |
| 188 | + | |
| 189 | + | |
| 190 | + | |
| 191 | + | |
| 192 | + | |
| 193 | + | |
| 194 | + | |
| 195 | + | |
| 196 | + | |
| 197 | + | |
| 198 | + | |
| 199 | + | |
| 200 | + | |
| 201 | + | |
| 202 | + | |
| 203 | + | |
| 204 | + | |
| 205 | + | |
| 206 | + | |
| 207 | + | |
| 208 | + | |
| 209 | + | |
| 210 | + | |
| 211 | + | |
| 212 | + | |
| 213 | + | |
| 214 | + | |
| 215 | + | |
| 216 | + | |
| 217 | + | |
| 218 | + | |
171 | 219 | | |
172 | 220 | | |
173 | 221 | | |
| |||
276 | 324 | | |
277 | 325 | | |
278 | 326 | | |
279 | | - | |
| 327 | + | |
280 | 328 | | |
281 | 329 | | |
282 | 330 | | |
| |||
504 | 552 | | |
505 | 553 | | |
506 | 554 | | |
507 | | - | |
| 555 | + | |
508 | 556 | | |
509 | 557 | | |
510 | 558 | | |
| |||
519 | 567 | | |
520 | 568 | | |
521 | 569 | | |
522 | | - | |
523 | | - | |
524 | | - | |
525 | | - | |
526 | | - | |
527 | | - | |
528 | | - | |
529 | | - | |
530 | | - | |
531 | | - | |
532 | | - | |
| 570 | + | |
533 | 571 | | |
534 | 572 | | |
535 | 573 | | |
536 | 574 | | |
537 | 575 | | |
538 | 576 | | |
539 | 577 | | |
540 | | - | |
| 578 | + | |
541 | 579 | | |
542 | 580 | | |
543 | 581 | | |
544 | 582 | | |
545 | 583 | | |
546 | 584 | | |
547 | 585 | | |
548 | | - | |
| 586 | + | |
549 | 587 | | |
550 | 588 | | |
551 | 589 | | |
552 | 590 | | |
553 | | - | |
| 591 | + | |
554 | 592 | | |
555 | 593 | | |
556 | 594 | | |
557 | 595 | | |
558 | | - | |
| 596 | + | |
559 | 597 | | |
560 | 598 | | |
561 | 599 | | |
| |||
565 | 603 | | |
566 | 604 | | |
567 | 605 | | |
| 606 | + | |
| 607 | + | |
| 608 | + | |
| 609 | + | |
| 610 | + | |
| 611 | + | |
| 612 | + | |
| 613 | + | |
| 614 | + | |
| 615 | + | |
| 616 | + | |
| 617 | + | |
| 618 | + | |
568 | 619 | | |
569 | 620 | | |
570 | 621 | | |
| |||
739 | 790 | | |
740 | 791 | | |
741 | 792 | | |
742 | | - | |
743 | | - | |
744 | | - | |
745 | | - | |
746 | | - | |
747 | | - | |
748 | | - | |
749 | | - | |
750 | | - | |
751 | | - | |
752 | | - | |
753 | | - | |
754 | | - | |
755 | | - | |
756 | | - | |
757 | 793 | | |
758 | 794 | | |
759 | 795 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
62 | 62 | | |
63 | 63 | | |
64 | 64 | | |
| 65 | + | |
| 66 | + | |
| 67 | + | |
| 68 | + | |
| 69 | + | |
| 70 | + | |
| 71 | + | |
| 72 | + | |
| 73 | + | |
| 74 | + | |
| 75 | + | |
| 76 | + | |
| 77 | + | |
| 78 | + | |
| 79 | + | |
| 80 | + | |
| 81 | + | |
| 82 | + | |
| 83 | + | |
| 84 | + | |
| 85 | + | |
| 86 | + | |
| 87 | + | |
| 88 | + | |
| 89 | + | |
| 90 | + | |
| 91 | + | |
| 92 | + | |
| 93 | + | |
| 94 | + | |
65 | 95 | | |
66 | 96 | | |
67 | 97 | | |
| |||
106 | 136 | | |
107 | 137 | | |
108 | 138 | | |
| 139 | + | |
109 | 140 | | |
110 | 141 | | |
111 | 142 | | |
| |||
114 | 145 | | |
115 | 146 | | |
116 | 147 | | |
| 148 | + | |
| 149 | + | |
| 150 | + | |
| 151 | + | |
| 152 | + | |
| 153 | + | |
| 154 | + | |
| 155 | + | |
| 156 | + | |
| 157 | + | |
| 158 | + | |
| 159 | + | |
117 | 160 | | |
118 | 161 | | |
119 | 162 | | |
| |||
260 | 303 | | |
261 | 304 | | |
262 | 305 | | |
| 306 | + | |
| 307 | + | |
| 308 | + | |
| 309 | + | |
| 310 | + | |
| 311 | + | |
| 312 | + | |
| 313 | + | |
| 314 | + | |
| 315 | + | |
| 316 | + | |
| 317 | + | |
| 318 | + | |
263 | 319 | | |
264 | 320 | | |
265 | 321 | | |
| |||
0 commit comments