Skip to content

Commit 620fe9c

Browse files
authored
Merge pull request #150 from moroine/feature/mongodb
Improve flowgen
2 parents e8e74af + e4e2780 commit 620fe9c

File tree

8 files changed

+128
-5
lines changed

8 files changed

+128
-5
lines changed

src/__tests__/__snapshots__/interface-exports.spec.ts.snap

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,12 @@ exports[`should handle exported interfaces 1`] = `
66
}
77
"
88
`;
9+
10+
exports[`should handle exported interfaces within a module 1`] = `
11+
"declare module \\"my-module\\" {
12+
declare export interface UnaryFunction<T, R> {
13+
(source: T): R;
14+
}
15+
}
16+
"
17+
`;

src/__tests__/__snapshots__/interfaces.spec.ts.snap

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,21 @@ exports[`should handle interface merging 2`] = `
6363
"
6464
`;
6565
66+
exports[`should handle mutli-extends pattern 1`] = `
67+
"declare interface Shape {
68+
color: string;
69+
}
70+
declare interface PenStroke {
71+
penWidth: number;
72+
}
73+
declare type Square = {
74+
sideLength: number,
75+
...
76+
} & Shape &
77+
PenStroke;
78+
"
79+
`;
80+
6681
exports[`should handle single interface 1`] = `
6782
"declare interface User {
6883
firstName: string;

src/__tests__/__snapshots__/utility-types.spec.ts.snap

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,32 @@
11
// Jest Snapshot v1, https://goo.gl/fbAQLP
22

3+
exports[`should handle Omit type 1`] = `
4+
"declare type A = $Diff<
5+
{
6+
a: string,
7+
b: number,
8+
...
9+
},
10+
{ a: any }
11+
>;
12+
declare type B = $Diff<
13+
{
14+
a: string,
15+
b: number,
16+
...
17+
},
18+
{ a: any, b: any }
19+
>;
20+
declare type O = {
21+
a: string,
22+
b: number,
23+
...
24+
};
25+
declare type U = \\"a\\";
26+
declare type C = $Diff<O, { [key: U]: any }>;
27+
"
28+
`;
29+
330
exports[`should handle utility types 1`] = `
431
"declare type A = $ReadOnly<{
532
a: number,

src/__tests__/interface-exports.spec.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,3 +10,15 @@ it("should handle exported interfaces", () => {
1010
expect(beautify(result)).toMatchSnapshot();
1111
expect(result).toBeValidFlowTypeDeclarations();
1212
});
13+
14+
it("should handle exported interfaces within a module", () => {
15+
const ts = `declare module "my-module" {
16+
export interface UnaryFunction<T, R> {
17+
(source: T): R;
18+
}
19+
}
20+
`;
21+
const result = compiler.compileDefinitionString(ts, { quiet: true });
22+
expect(beautify(result)).toMatchSnapshot();
23+
expect(result).toBeValidFlowTypeDeclarations();
24+
});

src/__tests__/interfaces.spec.ts

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -230,3 +230,21 @@ interface ArrayBinding {
230230
expect(beautify(result)).toMatchSnapshot();
231231
expect(result).toBeValidFlowTypeDeclarations();
232232
});
233+
234+
it("should handle mutli-extends pattern", () => {
235+
const ts = `
236+
interface Shape {
237+
color: string;
238+
}
239+
240+
interface PenStroke {
241+
penWidth: number;
242+
}
243+
interface Square extends Shape, PenStroke {
244+
sideLength: number;
245+
}
246+
`;
247+
const result = compiler.compileDefinitionString(ts);
248+
expect(beautify(result)).toMatchSnapshot();
249+
expect(result).toBeValidFlowTypeDeclarations();
250+
});

src/__tests__/utility-types.spec.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,3 +30,17 @@ type F2<T, U> = Record<T, U>
3030
expect(beautify(result)).toMatchSnapshot();
3131
expect(result).toBeValidFlowTypeDeclarations();
3232
});
33+
34+
it("should handle Omit type", () => {
35+
const ts = `
36+
type A = Omit<{ a: string, b: number }, "a">
37+
type B = Omit<{ a: string, b: number }, "a" | "b">
38+
39+
type O = { a: string, b: number };
40+
type U = "a";
41+
type C = Omit<O, U>;
42+
`;
43+
const result = compiler.compileDefinitionString(ts, { quiet: true });
44+
expect(beautify(result)).toMatchSnapshot();
45+
expect(result).toBeValidFlowTypeDeclarations();
46+
});

src/printers/declarations.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -190,6 +190,8 @@ const interfaceHeritageClause = type => {
190190
if (typeof name === "function") {
191191
// @ts-expect-error todo(flow->ts)
192192
return name(type.typeArguments);
193+
} else {
194+
return name;
193195
}
194196
} else {
195197
return printers.node.printType(type);

src/printers/identifiers.ts

Lines changed: 31 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,31 @@
33
import * as printers from "./index";
44
import { opts } from "../options";
55
import { withEnv } from "../env";
6+
import ts from "typescript";
7+
8+
const Record = ([key, value]: [any, any], isInexact = opts().inexact) => {
9+
const valueType = printers.node.printType(value);
10+
11+
switch (key.kind) {
12+
case ts.SyntaxKind.LiteralType:
13+
return `{ ${printers.node.printType(key)}: ${valueType}${
14+
isInexact ? ", ..." : ""
15+
}}`;
16+
case ts.SyntaxKind.UnionType:
17+
if (key.types.every(t => t.kind === ts.SyntaxKind.LiteralType)) {
18+
const fields = key.types.reduce((acc, t) => {
19+
acc += `${printers.node.printType(t)}: ${valueType},\n`;
20+
return acc;
21+
}, "");
22+
return `{ ${fields}${isInexact ? "..." : ""}}`;
23+
}
24+
// Fallthrough
25+
default:
26+
return `{[key: ${printers.node.printType(key)}]: ${valueType}${
27+
isInexact ? ", ..." : ""
28+
}}`;
29+
}
30+
};
631

732
const identifiers = Object.create(null);
833
Object.assign(identifiers, {
@@ -22,11 +47,12 @@ Object.assign(identifiers, {
2247
typeArguments[0],
2348
)}>`;
2449
},
25-
Record: ([key, value]: [any, any]) => {
26-
const isInexact = opts().inexact;
27-
return `{[key: ${printers.node.printType(key)}]: ${printers.node.printType(
28-
value,
29-
)}${isInexact ? ", ..." : ""}}`;
50+
Record,
51+
Omit: ([obj, keys]: [any, any]) => {
52+
return `$Diff<${printers.node.printType(obj)},${Record(
53+
[keys, { kind: ts.SyntaxKind.AnyKeyword }],
54+
false,
55+
)}>`;
3056
},
3157
});
3258

0 commit comments

Comments
 (0)