Skip to content

Commit 039ff5f

Browse files
authored
Merge pull request #152 from moroine/feature/mongodb
feat: support variable & type having same name
2 parents 9e3f596 + 305ad98 commit 039ff5f

17 files changed

+516
-22
lines changed

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
"@babel/highlight": "^7.10.4",
2323
"commander": "^6.1.0",
2424
"lodash": "^4.17.20",
25+
"mongodb": "^4.1.3",
2526
"prettier": "^2.1.1",
2627
"shelljs": "^0.8.4",
2728
"typescript": "^4.0.2",
@@ -51,7 +52,7 @@
5152
"eslint-plugin-import": "^2.22.0",
5253
"eslint-plugin-jest": "^24.0.0",
5354
"eslint-plugin-prettier": "^3.1.4",
54-
"flow-bin": "^0.141.0",
55+
"flow-bin": "^0.161.0",
5556
"jest": "^26.4.2"
5657
},
5758
"files": [
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
// Jest Snapshot v1, https://goo.gl/fbAQLP
2+
3+
exports[`should handle exported interfaces 1`] = `
4+
"// see https://gist.github.com/thecotne/6e5969f4aaf8f253985ed36b30ac9fe0
5+
type $FlowGen$If<X: boolean, Then, Else = empty> = $Call<
6+
((true, Then, Else) => Then) & ((false, Then, Else) => Else),
7+
X,
8+
Then,
9+
Else
10+
>;
11+
12+
type $FlowGen$Assignable<A, B> = $Call<
13+
((...r: [B]) => true) & ((...r: [A]) => false),
14+
A
15+
>;
16+
17+
declare export function add<T: string | number>(
18+
a: T,
19+
b: T
20+
): $FlowGen$If<$FlowGen$Assignable<T, string>, string, number>;
21+
"
22+
`;
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
// Jest Snapshot v1, https://goo.gl/fbAQLP
2+
3+
exports[`should handle basic keywords cll 1`] = `
4+
"declare module \\"test\\" {
5+
declare interface A {
6+
bar: string;
7+
}
8+
declare export var ok: number;
9+
}
10+
"
11+
`;
12+
13+
exports[`should handle basic keywords 1`] = `
14+
"declare module \\"test\\" {
15+
declare interface A {
16+
bar: string;
17+
}
18+
declare var ok: number;
19+
}
20+
"
21+
`;
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
// Jest Snapshot v1, https://goo.gl/fbAQLP
2+
3+
exports[`should be scoped to main file 1`] = `
4+
"import { Buffer } from \\"buffer\\";
5+
export type BufferAlias = Buffer;
6+
"
7+
`;
8+
9+
exports[`should generate unique names 1`] = `
10+
"export type AuthMechanismType = string;
11+
declare export var AuthMechanism: {
12+
+MONGODB_AWS: \\"MONGODB-AWS\\",
13+
+MONGODB_CR: \\"MONGODB-CR\\",
14+
...
15+
};
16+
export type AuthMechanismType1 = $ElementType<
17+
typeof AuthMechanism,
18+
$Keys<typeof AuthMechanism>
19+
>;
20+
"
21+
`;
22+
23+
exports[`should handle variable & type having same name 1`] = `
24+
"declare export var AuthMechanism: {
25+
+MONGODB_AWS: \\"MONGODB-AWS\\",
26+
+MONGODB_CR: \\"MONGODB-CR\\",
27+
...
28+
};
29+
export type AuthMechanismType = $ElementType<
30+
typeof AuthMechanism,
31+
$Keys<typeof AuthMechanism>
32+
>;
33+
"
34+
`;
35+
36+
exports[`should support generic type rename 1`] = `
37+
"declare export var ProfilingLevel: $ReadOnly<{
38+
+off: \\"off\\",
39+
...
40+
}>;
41+
export type ProfilingLevelType = $ElementType<
42+
typeof ProfilingLevel,
43+
$Keys<typeof ProfilingLevel>
44+
>;
45+
export type Callback<T = any> = (error?: Error, result?: T) => void;
46+
declare export var callback: Callback<ProfilingLevelType>;
47+
"
48+
`;

src/__tests__/conditional.spec.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import { compiler, beautify } from "..";
2+
import "../test-matchers";
3+
4+
it("should handle exported interfaces", () => {
5+
const ts = `export function add<T extends string | number>(a: T, b: T): T extends string ? string : number;`;
6+
const result = compiler.compileDefinitionString(ts, { quiet: true });
7+
expect(beautify(result)).toMatchSnapshot();
8+
expect(result).toBeValidFlowTypeDeclarations();
9+
});
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
import { compiler, beautify } from "..";
2+
import "../test-matchers";
3+
4+
it("should handle basic keywords", () => {
5+
const ts = `
6+
declare interface A {
7+
bar: string
8+
}
9+
10+
export declare const ok: number
11+
`;
12+
13+
const result = compiler.compileDefinitionString(ts, {
14+
quiet: true,
15+
asModule: "test",
16+
});
17+
expect(beautify(result)).toMatchSnapshot();
18+
expect(result).toBeValidFlowTypeDeclarations();
19+
});
20+
21+
it("should handle basic keywords cll", () => {
22+
const ts = `
23+
declare module 'test' {
24+
interface A {
25+
bar: string
26+
}
27+
export const ok: number
28+
}`;
29+
30+
const result = compiler.compileDefinitionString(ts, {
31+
quiet: true,
32+
asModule: "test",
33+
});
34+
expect(beautify(result)).toMatchSnapshot();
35+
expect(result).toBeValidFlowTypeDeclarations();
36+
});
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
import { compiler, beautify } from "..";
2+
import "../test-matchers";
3+
4+
it("should handle variable & type having same name", () => {
5+
const ts = `
6+
export declare const AuthMechanism: {
7+
readonly MONGODB_AWS: "MONGODB-AWS";
8+
readonly MONGODB_CR: "MONGODB-CR";
9+
};
10+
export declare type AuthMechanism = typeof AuthMechanism[keyof typeof AuthMechanism];
11+
`;
12+
const result = compiler.compileDefinitionString(ts, { quiet: true });
13+
expect(beautify(result)).toMatchSnapshot();
14+
expect(result).toBeValidFlowTypeDeclarations();
15+
});
16+
17+
it("should generate unique names", () => {
18+
const ts = `
19+
export declare type AuthMechanismType = string;
20+
export declare const AuthMechanism: {
21+
readonly MONGODB_AWS: "MONGODB-AWS";
22+
readonly MONGODB_CR: "MONGODB-CR";
23+
};
24+
export declare type AuthMechanism = typeof AuthMechanism[keyof typeof AuthMechanism];
25+
`;
26+
const result = compiler.compileDefinitionString(ts, { quiet: true });
27+
expect(beautify(result)).toMatchSnapshot();
28+
expect(result).toBeValidFlowTypeDeclarations();
29+
});
30+
31+
it("should be scoped to main file", () => {
32+
const ts = `
33+
import { Buffer } from 'buffer';
34+
export declare type BufferAlias = Buffer;
35+
`;
36+
const result = compiler.compileDefinitionString(ts, { quiet: true });
37+
expect(beautify(result)).toMatchSnapshot();
38+
// FlowJs is not valid due to `import` not transpiled to typeof
39+
// expect(result).toBeValidFlowTypeDeclarations();
40+
});
41+
42+
it("should support generic type rename", () => {
43+
const ts = `
44+
export declare const ProfilingLevel: Readonly<{
45+
readonly off: "off";
46+
}>;
47+
export declare type ProfilingLevel = typeof ProfilingLevel[keyof typeof ProfilingLevel];
48+
49+
export declare type Callback<T = any> = (error?: Error, result?: T) => void;
50+
51+
export declare const callback: Callback<ProfilingLevel>;
52+
`;
53+
const result = compiler.compileDefinitionString(ts, { quiet: true });
54+
expect(beautify(result)).toMatchSnapshot();
55+
expect(result).toBeValidFlowTypeDeclarations();
56+
});

src/cli/compiler.ts

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,13 @@ import type { Options } from "../options";
1414
import { checker } from "../checker";
1515
import * as logger from "../logger";
1616
import { withEnv } from "../env";
17-
import { importEqualsTransformer, legacyModules } from "../parse/transformers";
17+
import {
18+
importEqualsTransformer,
19+
legacyModules,
20+
declarationFileTransform,
21+
} from "../parse/transformers";
1822
import { recursiveWalkTree } from "../parse";
23+
import { printFlowGenHelper } from "../printers/node";
1924

2025
const compile = withEnv<any, [SourceFile], string>(
2126
(env: any, sourceFile: SourceFile): string => {
@@ -28,7 +33,9 @@ const compile = withEnv<any, [SourceFile], string>(
2833
})
2934
.join("");
3035

31-
return output;
36+
const helpersOutputs = printFlowGenHelper(env);
37+
38+
return `${helpersOutputs}\n\n${output}`;
3239
},
3340
);
3441

@@ -40,7 +47,11 @@ const reset = (options?: Options): void => {
4047
namespaceManager.reset();
4148
};
4249

43-
const transformers = [legacyModules(), importEqualsTransformer()];
50+
const getTransformers = (options?: Options) => [
51+
legacyModules(),
52+
importEqualsTransformer(),
53+
declarationFileTransform(options),
54+
];
4455

4556
/**
4657
* Compiles typescript files
@@ -54,8 +65,8 @@ export default {
5465
checker.current = typeChecker;
5566
},
5667

57-
getTransformers() {
58-
return transformers;
68+
getTransformers(options?: Options) {
69+
return getTransformers(options);
5970
},
6071

6172
compileTest: (path: string, target: string): void => {
@@ -76,7 +87,7 @@ export default {
7687
return transform(
7788
//$todo Flow has problems when switching variables instead of literals
7889
createSourceFile("/dev/null", string, languageVersion, true),
79-
transformers,
90+
getTransformers(options),
8091
compilerOptions,
8192
).transformed[0];
8293
}
@@ -124,7 +135,7 @@ export default {
124135
languageVersion,
125136
true,
126137
),
127-
transformers,
138+
getTransformers(options),
128139
compilerOptions,
129140
).transformed[0];
130141
}
@@ -170,7 +181,7 @@ export default {
170181
languageVersion,
171182
true,
172183
),
173-
transformers,
184+
getTransformers(options),
174185
compilerOptions,
175186
).transformed[0];
176187
}

src/cli/index.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,10 @@ program
2727
"adds '// @flow' to the generated files (for libs)",
2828
)
2929
.option("--compile-tests", "compile any <filename>-tests.ts files found")
30+
.option(
31+
"--as-module [asModule]",
32+
"wrap the output as a module declaration (for libs)",
33+
)
3034
.arguments("[files...]")
3135
.action((files, options) => {
3236
runner({
@@ -40,6 +44,7 @@ program
4044
compileTests: options.compileTests,
4145
out: options.outputFile,
4246
version: pkg.version,
47+
asModule: options.asModule,
4348
}).compile(files);
4449
});
4550

src/cli/runner.h.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ export type RunnerOptions = {
99
flowTypedFormat: boolean;
1010
addFlowHeader: boolean;
1111
compileTests: boolean;
12+
asModule: string;
1213
};
1314

1415
export type Mode = "directory" | "directory-flow-typed" | "flow-typed" | "file";

0 commit comments

Comments
 (0)