Skip to content
This repository was archived by the owner on Sep 27, 2023. It is now read-only.

Commit f26cb4c

Browse files
just-be-devtgriesser
authored andcommitted
Reuse formatterFactory for basic output boilerplate
1 parent dc4acba commit f26cb4c

File tree

2 files changed

+36
-45
lines changed

2 files changed

+36
-45
lines changed

src/formatGeneratedModule.ts

Lines changed: 29 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -2,31 +2,44 @@ import { FormatModule } from "relay-compiler";
22
import * as ts from "typescript";
33
import addAnyTypeCast from "./addAnyTypeCast";
44

5+
interface FormatterOptions {
6+
makeImports: (moduleDetails: Parameters<FormatModule>[0]) => string;
7+
append?: string;
8+
}
9+
10+
const defaultFormatOptions: FormatterOptions = {
11+
makeImports({ documentType }) {
12+
return documentType
13+
? `import { ${documentType} } from "relay-runtime";`
14+
: "";
15+
},
16+
};
17+
518
export const formatterFactory = (
6-
compilerOptions: ts.CompilerOptions = {}
7-
): FormatModule => ({
8-
moduleName,
9-
documentType,
10-
docText,
11-
concreteText,
12-
typeText,
13-
hash,
14-
sourceHash
15-
}) => {
16-
const documentTypeImport = documentType
17-
? `import { ${documentType} } from "relay-runtime";`
18-
: "";
19+
compilerOptions: ts.CompilerOptions = {},
20+
{ makeImports }: FormatterOptions = defaultFormatOptions
21+
): FormatModule => (details) => {
22+
const {
23+
documentType,
24+
docText,
25+
concreteText,
26+
typeText,
27+
hash,
28+
sourceHash,
29+
} = details;
30+
1931
const docTextComment = docText ? "\n/*\n" + docText.trim() + "\n*/\n" : "";
20-
let nodeStatement = `const node: ${documentType ||
21-
"never"} = ${concreteText};`;
32+
let nodeStatement = `const node: ${
33+
documentType || "never"
34+
} = ${concreteText};`;
2235
if (compilerOptions.noImplicitAny) {
2336
nodeStatement = addAnyTypeCast(nodeStatement).trim();
2437
}
2538
return `/* tslint:disable */
2639
/* eslint-disable */
2740
// @ts-nocheck
2841
${hash ? `/* ${hash} */\n` : ""}
29-
${documentTypeImport}
42+
${makeImports(details)}
3043
${typeText || ""}
3144
3245
${docTextComment}

src/hooks.ts

Lines changed: 7 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { FormatModule, LocalArgumentDefinition } from "relay-compiler";
2-
import addAnyTypeCast from "./addAnyTypeCast";
2+
import { formatterFactory } from "./formatGeneratedModule";
33
import relayCompilerLanguageTypescript from "./index";
44
import { loadCompilerOptions } from "./loadCompilerOptions";
55

@@ -8,13 +8,7 @@ export default function relayHooksTypescriptCompiler() {
88

99
const formatModule: FormatModule = (opts) => {
1010
const {
11-
// moduleName,
1211
documentType,
13-
docText,
14-
concreteText,
15-
typeText,
16-
hash,
17-
sourceHash,
1812
definition,
1913
definition: { name, metadata },
2014
} = opts;
@@ -125,28 +119,12 @@ export default function relayHooksTypescriptCompiler() {
125119
allImports.push(typeImports.join("\n"));
126120
}
127121

128-
const docTextComment = docText ? "\n/*\n" + docText.trim() + "\n*/\n" : "";
129-
let nodeStatement = `const node: ${
130-
documentType || "never"
131-
} = ${concreteText};`;
132-
if (compilerOptions.noImplicitAny) {
133-
nodeStatement = addAnyTypeCast(nodeStatement).trim();
134-
}
135-
return `/* tslint:disable */
136-
/* eslint-disable */
137-
// @ts-nocheck
138-
${hash ? `/* ${hash} */\n` : ""}
139-
${allImports.join("\n")}
140-
${typeText || ""}
141-
142-
${docTextComment}
143-
${nodeStatement}
144-
(node as any).hash = '${sourceHash}';
145-
146-
export default node;
147-
148-
${allHooks.join("\n")}
149-
`;
122+
return formatterFactory(compilerOptions, {
123+
makeImports() {
124+
return allImports.join("\n");
125+
},
126+
append: allHooks.join("\n"),
127+
})(opts);
150128
};
151129

152130
return {

0 commit comments

Comments
 (0)