Skip to content

Commit 93df7f3

Browse files
authored
Merge pull request relay-tools#160 from renanmav/fix/duplicate-imports
Fix duplicate imports
2 parents 2720a86 + 0d259e2 commit 93df7f3

File tree

4 files changed

+59
-10
lines changed

4 files changed

+59
-10
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@
8989
"jest": "^24.8.0",
9090
"jest-cli": "^24.8.0",
9191
"lint-staged": "^9.2.1",
92-
"prettier": "^1.18.2",
92+
"prettier": "^1.19.1",
9393
"relay-compiler": "^7.0.0",
9494
"relay-runtime": "^7.0.0",
9595
"relay-test-utils-internal": "^7.0.0",

src/TypeScriptGenerator.ts

Lines changed: 53 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -48,9 +48,8 @@ const MODULE_IMPORT_FIELD = "MODULE_IMPORT_FIELD";
4848
const DIRECTIVE_NAME = "raw_response_type";
4949

5050
export const generate: TypeGenerator["generate"] = (schema, node, options) => {
51-
const ast: ts.Statement[] = IRVisitor.visit(
52-
node,
53-
createVisitor(schema, options)
51+
const ast: ts.Statement[] = aggregateRuntimeImports(
52+
IRVisitor.visit(node, createVisitor(schema, options))
5453
);
5554

5655
const printer = ts.createPrinter({ newLine: ts.NewLineKind.LineFeed });
@@ -68,6 +67,57 @@ export const generate: TypeGenerator["generate"] = (schema, node, options) => {
6867
return printer.printNode(ts.EmitHint.SourceFile, fullProgramAst, resultFile);
6968
};
7069

70+
function aggregateRuntimeImports(ast: ts.Statement[]) {
71+
const importNodes = ast.filter(declaration =>
72+
ts.isImportDeclaration(declaration)
73+
) as ts.ImportDeclaration[];
74+
75+
const runtimeImports = importNodes.filter(
76+
importDeclaration =>
77+
// @ts-ignore
78+
importDeclaration.moduleSpecifier.text === "relay-runtime"
79+
);
80+
81+
if (runtimeImports.length > 1) {
82+
const namedImports: string[] = [];
83+
runtimeImports.map(node => {
84+
// @ts-ignore
85+
node.importClause.namedBindings.elements.map(element => {
86+
namedImports.push(element.name.escapedText);
87+
});
88+
});
89+
90+
const importSpecifiers: ts.ImportSpecifier[] = [];
91+
namedImports.map(namedImport => {
92+
const specifier = ts.createImportSpecifier(
93+
undefined,
94+
ts.createIdentifier(namedImport)
95+
);
96+
importSpecifiers.push(specifier);
97+
});
98+
99+
const namedBindings = ts.createNamedImports(importSpecifiers);
100+
const aggregatedRuntimeImportDeclaration = ts.createImportDeclaration(
101+
undefined,
102+
undefined,
103+
ts.createImportClause(undefined, namedBindings),
104+
ts.createStringLiteral("relay-runtime")
105+
);
106+
107+
const aggregatedRuntimeImportAST = ast.reduce<ts.Statement[]>(
108+
(prev, curr) => {
109+
if (!ts.isImportDeclaration(curr)) prev.push(curr);
110+
return prev;
111+
},
112+
[aggregatedRuntimeImportDeclaration]
113+
);
114+
115+
return aggregatedRuntimeImportAST;
116+
} else {
117+
return ast;
118+
}
119+
}
120+
71121
function nullthrows<T>(obj: T | null | undefined): T {
72122
if (obj == null) {
73123
throw new Error("Obj is null");

test/__snapshots__/TypeScriptGenerator-test.ts.snap

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1535,8 +1535,7 @@ export type RefetchableFragment$key = {
15351535
15361536
15371537
// RefetchableFragmentQuery.graphql
1538-
import { FragmentReference } from "relay-runtime";
1539-
import { FragmentRefs } from "relay-runtime";
1538+
import { FragmentReference, FragmentRefs } from "relay-runtime";
15401539
export type RefetchableFragmentQueryVariables = {
15411540
id: string;
15421541
};

yarn.lock

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4854,10 +4854,10 @@ preserve@^0.2.0:
48544854
resolved "https://registry.yarnpkg.com/preserve/-/preserve-0.2.0.tgz#815ed1f6ebc65926f865b310c0713bcb3315ce4b"
48554855
integrity sha1-gV7R9uvGWSb4ZbMQwHE7yzMVzks=
48564856

4857-
prettier@^1.18.2:
4858-
version "1.18.2"
4859-
resolved "https://registry.yarnpkg.com/prettier/-/prettier-1.18.2.tgz#6823e7c5900017b4bd3acf46fe9ac4b4d7bda9ea"
4860-
integrity sha512-OeHeMc0JhFE9idD4ZdtNibzY0+TPHSpSSb9h8FqtP+YnoZZ1sl8Vc9b1sasjfymH3SonAF4QcA2+mzHPhMvIiw==
4857+
prettier@^1.19.1:
4858+
version "1.19.1"
4859+
resolved "https://registry.yarnpkg.com/prettier/-/prettier-1.19.1.tgz#f7d7f5ff8a9cd872a7be4ca142095956a60797cb"
4860+
integrity sha512-s7PoyDv/II1ObgQunCbB9PdLmUcBZcnWOcxDh7O0N/UwDEsHyqkW+Qh28jW+mVuCdx7gLB0BotYI1Y6uI9iyew==
48614861

48624862
pretty-format@^22.4.0, pretty-format@^22.4.3:
48634863
version "22.4.3"

0 commit comments

Comments
 (0)