Skip to content

Commit a55d71e

Browse files
committed
[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)
1 parent 078e7d4 commit a55d71e

File tree

8 files changed

+288
-102
lines changed

8 files changed

+288
-102
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@
6565
"jest-junit": "^12.0.0",
6666
"patch-package": "^6.4.7",
6767
"prettier": "^2.4.1",
68-
"recast": "^0.20.4",
68+
"recast": "0.23.4",
6969
"signale": "^1.4.0",
7070
"ts-jest": "^26.0.0",
7171
"ts-morph": "^13.0.2",

patches/recast+0.20.4.patch

Lines changed: 0 additions & 17 deletions
This file was deleted.

src/convert/declarations.test.ts

Lines changed: 2 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -276,7 +276,7 @@ describe("transform declarations", () => {
276276
it("converts more complicated $Exact types", async () => {
277277
const src = dedent`type Test = $Exact<T | { foo: string }>;`;
278278
const expected = dedent`type Test = T | {
279-
foo: string
279+
foo: string;
280280
};`;
281281
expect(await transform(src)).toBe(expected);
282282
});
@@ -504,7 +504,7 @@ describe("transform declarations", () => {
504504
it("Converts React.Node to React.ReactNode in Props", async () => {
505505
const src = `type Props = {children?: React.Node};`;
506506
const expected = dedent`type Props = {
507-
children?: React.ReactNode
507+
children?: React.ReactNode;
508508
};`;
509509
expect(await transform(src)).toBe(expected);
510510
});
@@ -739,21 +739,6 @@ describe("transform declarations", () => {
739739
`;
740740
expect(await transform(src)).toBe(expected);
741741
});
742-
it("when a comment is in a type param declaration, it should preserve the newline", async () => {
743-
const src = dedent`
744-
const AThing: Array<
745-
// FlowFixMe
746-
number> = []
747-
`;
748-
749-
const expected = dedent`
750-
const AThing: Array<
751-
// FlowFixMe
752-
number> = []
753-
`;
754-
755-
expect(await transform(src)).toBe(expected);
756-
});
757742
});
758743

759744
describe("for opaque types", () => {

src/convert/expressions.test.ts

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,7 @@ describe("transform expressions", () => {
126126
|}> = new Array(0);`;
127127
const expected = dedent`
128128
const a: Array<{
129-
foo: 'bar'
129+
foo: 'bar';
130130
}> = new Array(0);`;
131131
expect(await transform(src)).toBe(expected);
132132
});
@@ -140,18 +140,23 @@ describe("transform expressions", () => {
140140
const expected = dedent`
141141
const test = () => {
142142
return class extends React.Component<Record<any, any>, {
143-
bar: string
143+
bar: string;
144144
}> {};
145145
};`;
146146
expect(await transform(src)).toBe(expected);
147147
});
148148

149-
it("should not change if there are no exact bars", async () => {
150-
const expected = dedent`
149+
it("should only add semicolon if there are no exact bars", async () => {
150+
const src = dedent`
151+
// @flow
151152
const a: Array<{
152153
foo: 'bar'
153154
}> = new Array(0);`;
154-
expect(await transform(expected)).toBe(expected);
155+
const expected = dedent`
156+
const a: Array<{
157+
foo: 'bar';
158+
}> = new Array(0);`;
159+
expect(await transform(src)).toBe(expected);
155160
});
156161

157162
it("should remove the exact object types from constructed objects", async () => {
@@ -161,7 +166,7 @@ describe("transform expressions", () => {
161166
|}>();`;
162167
const expected = dedent`
163168
const a = new Array<{
164-
foo: 'bar'
169+
foo: 'bar';
165170
}>();`;
166171
expect(await transform(src)).toBe(expected);
167172
});

src/convert/jsx-spread/jsx-spread.test.ts

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,8 @@ describe("transform spread JSX attributes", () => {
2222
const expected = `
2323
import {Flow} from 'flow-to-typescript-codemod';
2424
type Props = {
25-
it: string,
26-
foo: number
25+
it: string;
26+
foo: number;
2727
};
2828
2929
function Foobar(x: Props & Omit<Omit<Flow.ComponentProps<typeof Mine>, 'it'>, keyof Props>) {
@@ -51,8 +51,8 @@ describe("transform spread JSX attributes", () => {
5151
const expected = `
5252
import {Flow} from 'flow-to-typescript-codemod';
5353
type Props = {
54-
it: string,
55-
foo: number
54+
it: string;
55+
foo: number;
5656
};
5757
5858
const Foobar = (x: Props & Omit<Omit<Flow.ComponentProps<typeof Mine>, 'it'>, keyof Props>) => {
@@ -84,8 +84,8 @@ describe("transform spread JSX attributes", () => {
8484
const expected = `
8585
import {Flow} from 'flow-to-typescript-codemod';
8686
type Props = {
87-
it: string,
88-
foo: number
87+
it: string;
88+
foo: number;
8989
};
9090
9191
function Foobar(x: Props & Omit<Omit<Flow.ComponentProps<typeof Mine>, 'it'>, keyof Props>) {
@@ -121,8 +121,8 @@ describe("transform spread JSX attributes", () => {
121121
const expected = `
122122
import {Flow} from 'flow-to-typescript-codemod';
123123
type Props = {
124-
it: string,
125-
foo: number
124+
it: string;
125+
foo: number;
126126
};
127127
128128
class MyComponent extends React.Component<Props & Omit<Omit<Flow.ComponentProps<typeof Mine>, 'it'>, keyof Props>> {
@@ -157,8 +157,8 @@ describe("transform spread JSX attributes", () => {
157157
const expected = `
158158
import {Flow} from 'flow-to-typescript-codemod';
159159
type Props = {
160-
it: string,
161-
foo: number
160+
it: string;
161+
foo: number;
162162
};
163163
164164
class MyComponent extends React.Component<Props & Omit<Omit<Flow.ComponentProps<typeof Mine>, 'it'>, keyof Props>> {
@@ -192,8 +192,8 @@ describe("transform spread JSX attributes", () => {
192192

193193
const expected = `
194194
type Props = {
195-
it: string,
196-
foo: number
195+
it: string;
196+
foo: number;
197197
};
198198
199199
class MyComponent extends React.Component<Props> {
@@ -228,11 +228,11 @@ describe("transform spread JSX attributes", () => {
228228
const expected = `
229229
import {Flow} from 'flow-to-typescript-codemod';
230230
type State = {
231-
thing: boolean
231+
thing: boolean;
232232
};
233233
type Props = {
234-
it: string,
235-
foo: number
234+
it: string;
235+
foo: number;
236236
};
237237
238238
class MyComponent extends React.Component<Props & Omit<Omit<Flow.ComponentProps<typeof Mine>, 'it'>, keyof Props>, State> {
@@ -263,8 +263,8 @@ describe("transform spread JSX attributes", () => {
263263
const expected = `
264264
import {Flow} from 'flow-to-typescript-codemod';
265265
type Props = {
266-
it: string,
267-
foo: number
266+
it: string;
267+
foo: number;
268268
};
269269
270270
function Foobar(x: Props & Omit<Flow.ComponentProps<typeof Mine>, keyof Props>) {
@@ -292,8 +292,8 @@ describe("transform spread JSX attributes", () => {
292292
}`;
293293
const expected = `
294294
type Props = {
295-
it: string,
296-
foo: number
295+
it: string;
296+
foo: number;
297297
};
298298
299299
function Foobar(x: Props) {

src/convert/type-annotations.test.ts

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -158,15 +158,15 @@ describe("transform type annotations", () => {
158158
it("Does not convert string Object key types to records", async () => {
159159
const src = `const MyObj: {[key: string]: ValueType} = {};`;
160160
const expected = dedent`const MyObj: {
161-
[key: string]: ValueType
161+
[key: string]: ValueType;
162162
} = {};`;
163163
expect(await transform(src)).toBe(expected);
164164
});
165165

166166
it("Does not convert number Object key types to records", async () => {
167167
const src = `const MyObj: {[key: number]: ValueType} = {};`;
168168
const expected = dedent`const MyObj: {
169-
[key: number]: ValueType
169+
[key: number]: ValueType;
170170
} = {};`;
171171
expect(await transform(src)).toBe(expected);
172172
});
@@ -323,7 +323,7 @@ describe("transform type annotations", () => {
323323
`;
324324
const expected = dedent`
325325
type MockApiOptions = {
326-
errors: jest.MockedFunction<any>
326+
errors: jest.MockedFunction<any>;
327327
};
328328
`;
329329
expect(await transform(src)).toBe(expected);
@@ -342,7 +342,7 @@ describe("transform type annotations", () => {
342342
startDate: moment
343343
};`;
344344
const expected = dedent`type Test = {
345-
startDate: moment.Moment
345+
startDate: moment.Moment;
346346
};`;
347347
expect(await transform(src)).toBe(expected);
348348
});
@@ -532,7 +532,7 @@ describe("transform type annotations", () => {
532532
};`;
533533
const expected = dedent`
534534
type Props = {
535-
children: Array<MenuChildren> | MenuChildren
535+
children: Array<MenuChildren> | MenuChildren;
536536
};`;
537537
expect(await transform(src)).toBe(expected);
538538
});
@@ -595,14 +595,14 @@ describe("transform type annotations", () => {
595595
it("Does not convert {} to Record<any, any> if an object has any properties", async () => {
596596
// dedent messes up the indentation of the string
597597
const src = `function f(): {
598-
prop: boolean
598+
prop: boolean;
599599
} {return {}}
600600
let af: () => {
601-
prop: boolean
601+
prop: boolean;
602602
}
603603
class C {
604604
m(): {
605-
prop: boolean
605+
prop: boolean;
606606
} {return {}}
607607
}`;
608608

@@ -764,9 +764,9 @@ class C {
764764

765765
const expected = dedent`
766766
export type Test = {
767-
<T>(arg1: undefined | Example | Example[], arg2?: () => T | null | undefined): T,
768-
(arg1: undefined | Example | Example[]): Attributes,
769-
foo: number
767+
<T>(arg1: undefined | Example | Example[], arg2?: () => T | null | undefined): T;
768+
(arg1: undefined | Example | Example[]): Attributes;
769+
foo: number;
770770
};
771771
`;
772772
expect(await transform(src)).toBe(expected);
@@ -783,9 +783,9 @@ class C {
783783

784784
const expected = dedent`
785785
type Test = {
786-
<T>(arg1: undefined | Example | Example[], arg2?: () => T | null | undefined): T,
787-
(arg1: undefined | Example | Example[]): Attributes,
788-
foo: number
786+
<T>(arg1: undefined | Example | Example[], arg2?: () => T | null | undefined): T;
787+
(arg1: undefined | Example | Example[]): Attributes;
788+
foo: number;
789789
};
790790
`;
791791
expect(await transform(src)).toBe(expected);

0 commit comments

Comments
 (0)