Skip to content

Commit 2f2cf4f

Browse files
asterikxalloydependabot-preview[bot]n1ru4lsibelius
authored
add tests (#1)
* Update CHANGELOG.md [skip ci] * Bump version to: 13.0.3 [skip ci] * chore: bump typescript from 4.1.4 to 4.1.5 (relay-tools#268) Bumps [typescript](https://github.com/Microsoft/TypeScript) from 4.1.4 to 4.1.5. - [Release notes](https://github.com/Microsoft/TypeScript/releases) - [Commits](microsoft/TypeScript@v4.1.4...v4.1.5) Signed-off-by: dependabot-preview[bot] <[email protected]> Co-authored-by: dependabot-preview[bot] <27856297+dependabot-preview[bot]@users.noreply.github.com> * Update CHANGELOG.md [skip ci] * Bump version to: 13.0.4 [skip ci] * chore: bump @types/node from 14.14.25 to 14.14.28 Bumps [@types/node](https://github.com/DefinitelyTyped/DefinitelyTyped/tree/HEAD/types/node) from 14.14.25 to 14.14.28. - [Release notes](https://github.com/DefinitelyTyped/DefinitelyTyped/releases) - [Commits](https://github.com/DefinitelyTyped/DefinitelyTyped/commits/HEAD/types/node) Signed-off-by: dependabot-preview[bot] <[email protected]> * fix: revert breaking changes introduced in 13.0.3 (relay-tools#274) * Revert "refactor: use factory functions instead of deprecated functions" This reverts commit 812d17e. * Revert "refactor: remove deprecated typescript function calls in favor of the factory; replace @ts-ignores with proper code (microsoft/TypeScript#40263 (comment))" This reverts commit 55c58be. * Revert "refactor: some of the statements are redundant. It is not necessary to write to write only properties." This reverts commit 2cbd1de. * Revert "refactor: address all the typescript deprecations by using the factory" This reverts commit 72e0b4a. * chore: run CI github action on pull request * fix: typo * Update CHANGELOG.md [skip ci] * Bump version to: 13.0.5 [skip ci] * Update CHANGELOG.md [skip ci] * Bump version to: 13.0.6 [skip ci] * feat: include .d.ts types in release (relay-tools#309) * feat: drop typescript 3 and Node.js 10 support (relay-tools#275) * refactor: address all the typescript deprecations by using the factory * refactor: some of the statements are redundant. It is not necessary to write to write only properties. * refactor: remove deprecated typescript function calls in favor of the factory; replace @ts-ignores with proper code (microsoft/TypeScript#40263 (comment)) * refactor: use factory functions instead of deprecated functions * feat: bump peerDependencies version * dps: upgrade to latest typescript version * chore: replace rm with rimraf for cross platform support * chore: replace fixture tests with inline snapshot tests * docs: add notice about minimum TypeScript version. * chore: drop node 10 support * Update CHANGELOG.md [skip ci] * Bump version to: 13.0.7 [skip ci] * Update CHANGELOG.md [skip ci] * Bump version to: 13.0.8 [skip ci] * feat: replace require calls by static (top level) or dynamic imports based on ts compiler options * feat: add tests for require call replacement logic * chore: add comment * fix: parse contents of tsconfig file into compiler options Converts strings to enum values etc. * fix: remove support for dynamic imports * fix: import default Co-authored-by: Eloy Durn <[email protected]> Co-authored-by: dependabot-preview[bot] <27856297+dependabot-preview[bot]@users.noreply.github.com> Co-authored-by: Laurin Quast <[email protected]> Co-authored-by: Sibelius Seraphini <[email protected]> Co-authored-by: Tim Griesser <[email protected]> Co-authored-by: Erik Müller <[email protected]>
1 parent 21f6bf5 commit 2f2cf4f

16 files changed

+410
-173
lines changed

.github/workflows/ci.yml

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
11
name: CI
22
on:
33
push:
4+
pull_request:
45
jobs:
56
ci:
67
name: Continuous Integration
78
runs-on: ubuntu-latest
89
strategy:
910
matrix:
10-
node-version: [10.x, 12.x, 14.x, 15.x]
11+
node-version: [12.x, 14.x, 15.x]
1112
steps:
1213
- uses: actions/checkout@v2
1314
- uses: actions/setup-node@v2

CHANGELOG.md

+105
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,108 @@
1+
# v13.0.8 (Fri Apr 02 2021)
2+
3+
4+
5+
---
6+
7+
# v13.0.7 (Fri Apr 02 2021)
8+
9+
#### 🐛 Bug Fix
10+
11+
- feat: drop typescript 3 and Node.js 10 support [#275](https://github.com/relay-tools/relay-compiler-language-typescript/pull/275) ([@n1ru4l](https://github.com/n1ru4l))
12+
- feat: include .d.ts types in release [#309](https://github.com/relay-tools/relay-compiler-language-typescript/pull/309) ([@tgriesser](https://github.com/tgriesser))
13+
14+
#### Authors: 2
15+
16+
- Laurin Quast ([@n1ru4l](https://github.com/n1ru4l))
17+
- Tim Griesser ([@tgriesser](https://github.com/tgriesser))
18+
19+
---
20+
21+
# v13.0.6 (Mon Feb 15 2021)
22+
23+
#### 🐛 Bug Fix
24+
25+
- chore: bump @types/node from 14.14.25 to 14.14.28 [#273](https://github.com/relay-tools/relay-compiler-language-typescript/pull/273) ([@dependabot-preview[bot]](https://github.com/dependabot-preview[bot]))
26+
27+
#### Authors: 1
28+
29+
- [@dependabot-preview[bot]](https://github.com/dependabot-preview[bot])
30+
31+
---
32+
33+
# v13.0.5 (Mon Feb 15 2021)
34+
35+
#### 🐛 Bug Fix
36+
37+
- fix: revert breaking changes introduced in 13.0.3 [#274](https://github.com/relay-tools/relay-compiler-language-typescript/pull/274) ([@n1ru4l](https://github.com/n1ru4l))
38+
39+
#### Authors: 1
40+
41+
- Laurin Quast ([@n1ru4l](https://github.com/n1ru4l))
42+
43+
---
44+
45+
# v13.0.4 (Thu Feb 11 2021)
46+
47+
#### 🐛 Bug Fix
48+
49+
- chore: bump typescript from 4.1.4 to 4.1.5 [#268](https://github.com/relay-tools/relay-compiler-language-typescript/pull/268) ([@dependabot-preview[bot]](https://github.com/dependabot-preview[bot]))
50+
51+
#### Authors: 1
52+
53+
- [@dependabot-preview[bot]](https://github.com/dependabot-preview[bot])
54+
55+
---
56+
57+
# v13.0.3 (Thu Feb 11 2021)
58+
59+
#### 🐛 Bug Fix
60+
61+
- Bump relay-runtime from 10.0.1 to 10.1.2 [#241](https://github.com/relay-tools/relay-compiler-language-typescript/pull/241) ([@dependabot-preview[bot]](https://github.com/dependabot-preview[bot]))
62+
- fix: bring package in a state where build and test do not fail [#267](https://github.com/relay-tools/relay-compiler-language-typescript/pull/267) ([@n1ru4l](https://github.com/n1ru4l))
63+
- Bump typescript from 4.1.3 to 4.1.4 [#266](https://github.com/relay-tools/relay-compiler-language-typescript/pull/266) ([@dependabot-preview[bot]](https://github.com/dependabot-preview[bot]))
64+
- Bump ts-jest from 26.5.0 to 26.5.1 [#265](https://github.com/relay-tools/relay-compiler-language-typescript/pull/265) ([@dependabot-preview[bot]](https://github.com/dependabot-preview[bot]))
65+
- Bump relay-test-utils-internal from 10.0.1 to 10.1.3 [#252](https://github.com/relay-tools/relay-compiler-language-typescript/pull/252) ([@dependabot-preview[bot]](https://github.com/dependabot-preview[bot]))
66+
- Bump ts-jest from 26.4.4 to 26.5.0 [#260](https://github.com/relay-tools/relay-compiler-language-typescript/pull/260) ([@dependabot-preview[bot]](https://github.com/dependabot-preview[bot]))
67+
- Bump lint-staged from 10.5.3 to 10.5.4 [#263](https://github.com/relay-tools/relay-compiler-language-typescript/pull/263) ([@dependabot-preview[bot]](https://github.com/dependabot-preview[bot]))
68+
- Bump @types/node from 14.14.22 to 14.14.25 [#262](https://github.com/relay-tools/relay-compiler-language-typescript/pull/262) ([@dependabot-preview[bot]](https://github.com/dependabot-preview[bot]))
69+
- Bump lint-staged from 9.4.2 to 10.5.3 [#229](https://github.com/relay-tools/relay-compiler-language-typescript/pull/229) ([@dependabot-preview[bot]](https://github.com/dependabot-preview[bot]))
70+
- Bump relay-compiler from 10.1.2 to 10.1.3 [#253](https://github.com/relay-tools/relay-compiler-language-typescript/pull/253) ([@dependabot-preview[bot]](https://github.com/dependabot-preview[bot]))
71+
- Bump graphql from 15.4.0 to 15.5.0 [#256](https://github.com/relay-tools/relay-compiler-language-typescript/pull/256) ([@dependabot-preview[bot]](https://github.com/dependabot-preview[bot]))
72+
- Bump @types/relay-runtime from 10.1.7 to 10.1.8 [#258](https://github.com/relay-tools/relay-compiler-language-typescript/pull/258) ([@dependabot-preview[bot]](https://github.com/dependabot-preview[bot]))
73+
- Bump tslint from 5.20.0 to 6.1.3 [#240](https://github.com/relay-tools/relay-compiler-language-typescript/pull/240) ([@dependabot-preview[bot]](https://github.com/dependabot-preview[bot]))
74+
- Bump @types/node from 14.14.21 to 14.14.22 [#251](https://github.com/relay-tools/relay-compiler-language-typescript/pull/251) ([@dependabot-preview[bot]](https://github.com/dependabot-preview[bot]))
75+
- Bump babel-plugin-relay from 10.1.2 to 10.1.3 [#254](https://github.com/relay-tools/relay-compiler-language-typescript/pull/254) ([@dependabot-preview[bot]](https://github.com/dependabot-preview[bot]))
76+
- Bump relay-runtime from 10.1.2 to 10.1.3 [#255](https://github.com/relay-tools/relay-compiler-language-typescript/pull/255) ([@dependabot-preview[bot]](https://github.com/dependabot-preview[bot]))
77+
- Bump relay-compiler from 10.0.1 to 10.1.2 [#247](https://github.com/relay-tools/relay-compiler-language-typescript/pull/247) ([@dependabot-preview[bot]](https://github.com/dependabot-preview[bot]))
78+
- Bump husky from 3.0.9 to 4.3.8 [#248](https://github.com/relay-tools/relay-compiler-language-typescript/pull/248) ([@dependabot-preview[bot]](https://github.com/dependabot-preview[bot]))
79+
- Bump jest from 24.9.0 to 26.6.3 [#220](https://github.com/relay-tools/relay-compiler-language-typescript/pull/220) ([@dependabot-preview[bot]](https://github.com/dependabot-preview[bot]))
80+
- [Security] Bump yargs-parser from 13.1.1 to 13.1.2 [#245](https://github.com/relay-tools/relay-compiler-language-typescript/pull/245) ([@dependabot-preview[bot]](https://github.com/dependabot-preview[bot]))
81+
- Bump jest-cli from 24.9.0 to 26.6.3 [#249](https://github.com/relay-tools/relay-compiler-language-typescript/pull/249) ([@dependabot-preview[bot]](https://github.com/dependabot-preview[bot]))
82+
- Bump @types/node from 8.10.51 to 14.14.21 [#250](https://github.com/relay-tools/relay-compiler-language-typescript/pull/250) ([@dependabot-preview[bot]](https://github.com/dependabot-preview[bot]))
83+
- Bump concurrently from 5.0.0 to 5.3.0 [#237](https://github.com/relay-tools/relay-compiler-language-typescript/pull/237) ([@dependabot-preview[bot]](https://github.com/dependabot-preview[bot]))
84+
- Bump @artsy/auto-config from 1.0.1 to 1.0.2 [#238](https://github.com/relay-tools/relay-compiler-language-typescript/pull/238) ([@dependabot-preview[bot]](https://github.com/dependabot-preview[bot]))
85+
- Bump babel-plugin-relay from 10.0.1 to 10.1.2 [#239](https://github.com/relay-tools/relay-compiler-language-typescript/pull/239) ([@dependabot-preview[bot]](https://github.com/dependabot-preview[bot]))
86+
- Bump prettier from 1.19.1 to 2.2.1 [#242](https://github.com/relay-tools/relay-compiler-language-typescript/pull/242) ([@dependabot-preview[bot]](https://github.com/dependabot-preview[bot]))
87+
- Bump ts-node from 8.10.2 to 9.1.1 [#243](https://github.com/relay-tools/relay-compiler-language-typescript/pull/243) ([@dependabot-preview[bot]](https://github.com/dependabot-preview[bot]))
88+
- Bump @types/jest from 24.0.20 to 26.0.20 [#244](https://github.com/relay-tools/relay-compiler-language-typescript/pull/244) ([@dependabot-preview[bot]](https://github.com/dependabot-preview[bot]))
89+
- Bump ts-jest from 22.4.6 to 26.4.4 [#221](https://github.com/relay-tools/relay-compiler-language-typescript/pull/221) ([@dependabot-preview[bot]](https://github.com/dependabot-preview[bot]))
90+
- Bump typescript from 3.6.4 to 4.1.3 [#233](https://github.com/relay-tools/relay-compiler-language-typescript/pull/233) ([@dependabot-preview[bot]](https://github.com/dependabot-preview[bot]))
91+
- Bump @types/relay-runtime from 10.0.1 to 10.1.7 [#236](https://github.com/relay-tools/relay-compiler-language-typescript/pull/236) ([@dependabot-preview[bot]](https://github.com/dependabot-preview[bot]))
92+
- Bump graphql from 15.3.0 to 15.4.0 [#215](https://github.com/relay-tools/relay-compiler-language-typescript/pull/215) ([@dependabot-preview[bot]](https://github.com/dependabot-preview[bot]))
93+
- [Security] Bump lodash from 4.17.15 to 4.17.20 [#214](https://github.com/relay-tools/relay-compiler-language-typescript/pull/214) ([@dependabot-preview[bot]](https://github.com/dependabot-preview[bot]))
94+
- [Security] Bump tree-kill from 1.2.1 to 1.2.2 [#213](https://github.com/relay-tools/relay-compiler-language-typescript/pull/213) ([@dependabot-preview[bot]](https://github.com/dependabot-preview[bot]))
95+
- [Security] Bump handlebars from 4.4.5 to 4.7.6 [#210](https://github.com/relay-tools/relay-compiler-language-typescript/pull/210) ([@dependabot-preview[bot]](https://github.com/dependabot-preview[bot]))
96+
- Bump @types/invariant from 2.2.30 to 2.2.34 [#207](https://github.com/relay-tools/relay-compiler-language-typescript/pull/207) ([@dependabot-preview[bot]](https://github.com/dependabot-preview[bot]))
97+
- [Security] Bump ini from 1.3.5 to 1.3.8 [#232](https://github.com/relay-tools/relay-compiler-language-typescript/pull/232) ([@dependabot-preview[bot]](https://github.com/dependabot-preview[bot]))
98+
99+
#### Authors: 2
100+
101+
- [@dependabot-preview[bot]](https://github.com/dependabot-preview[bot])
102+
- Laurin Quast ([@n1ru4l](https://github.com/n1ru4l))
103+
104+
---
105+
1106
# v13.0.2 (Mon Nov 02 2020)
2107

3108
#### 🐛 Bug Fix

README.md

+2
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ yarn add graphql relay-compiler --dev
1414
yarn add typescript relay-compiler-language-typescript --dev
1515
```
1616

17+
**Note:** Starting with version 14.0.0 relay-compiler-language-typescript requires a minimum TypeScript version of 4.2.3 being installed in your project.
18+
1719
## Configuration
1820

1921
### relay-compiler

package.json

+11-9
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "relay-compiler-language-typescript",
3-
"version": "13.0.2",
3+
"version": "13.0.8",
44
"description": "A language plugin for Relay that adds TypeScript support, including emitting type definitions.",
55
"keywords": [
66
"graphql",
@@ -28,11 +28,12 @@
2828
}
2929
],
3030
"main": "lib/index.js",
31+
"types": "lib/index.d.ts",
3132
"files": [
3233
"lib"
3334
],
3435
"scripts": {
35-
"build": "rm -rf lib && tsc --project tsconfig.build.json",
36+
"build": "rimraf lib && tsc --project tsconfig.build.json",
3637
"lint": "tslint -c tslint.json --project tsconfig.json",
3738
"pr-check": "scripts/pr-check.sh",
3839
"prettier": "prettier --write '{src,types,test}/**/*.ts'",
@@ -81,7 +82,7 @@
8182
"@types/graphql": "^14.2.3",
8283
"@types/invariant": "2.2.34",
8384
"@types/jest": "^26.0.20",
84-
"@types/node": "14.14.25",
85+
"@types/node": "14.14.28",
8586
"@types/relay-compiler": "^8.0.0",
8687
"@types/relay-runtime": "^10.0.1",
8788
"babel-plugin-relay": "^10.0.0",
@@ -97,18 +98,19 @@
9798
"relay-compiler": "^10.0.1",
9899
"relay-runtime": "^10.0.1",
99100
"relay-test-utils-internal": "^10.0.1",
101+
"rimraf": "^3.0.2",
100102
"ts-jest": "^26.4.4",
101103
"ts-node": "^9.1.1",
102104
"tslint": "^6.1.3",
103105
"tslint-config-prettier": "^1.18.0",
104-
"typescript": "4.1.4"
106+
"typescript": "4.2.3"
105107
},
106108
"peerDependencies": {
107-
"@types/react-relay": ">=7.0.0",
108-
"@types/relay-runtime": ">=6.0.7",
109-
"relay-compiler": ">=9.0.0",
110-
"relay-runtime": ">=9.0.0",
111-
"typescript": ">=3.6.4"
109+
"@types/react-relay": ">=7.0.17",
110+
"@types/relay-runtime": ">=10.1.8",
111+
"relay-compiler": ">=10.1.3",
112+
"relay-runtime": ">=10.1.3",
113+
"typescript": ">=4.2.3"
112114
},
113115
"publishConfig": {
114116
"registry": "https://registry.npmjs.org/"

src/FindGraphQLTags.ts

+7-7
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import * as util from "util";
33

44
import {
55
GraphQLTag,
6-
GraphQLTagFinder
6+
GraphQLTagFinder,
77
} from "relay-compiler/lib/language/RelayLanguagePluginInterface";
88

99
function isCreateContainerFunction(
@@ -65,7 +65,7 @@ function visit(node: ts.Node, addGraphQLTag: (tag: GraphQLTag) => void): void {
6565
break;
6666
}
6767
if (ts.isObjectLiteralExpression(fragmentSpec)) {
68-
fragmentSpec.properties.forEach(prop => {
68+
fragmentSpec.properties.forEach((prop) => {
6969
invariant(
7070
ts.isPropertyAssignment(prop) &&
7171
prop.questionToken == null &&
@@ -90,7 +90,7 @@ function visit(node: ts.Node, addGraphQLTag: (tag: GraphQLTag) => void): void {
9090
addGraphQLTag({
9191
keyName: (propAssignment.name as ts.Identifier).text,
9292
template: getGraphQLText(taggedTemplate),
93-
sourceLocationOffset: getSourceLocationOffset(taggedTemplate)
93+
sourceLocationOffset: getSourceLocationOffset(taggedTemplate),
9494
});
9595
});
9696
} else {
@@ -111,7 +111,7 @@ function visit(node: ts.Node, addGraphQLTag: (tag: GraphQLTag) => void): void {
111111
addGraphQLTag({
112112
keyName: null,
113113
template: getGraphQLText(taggedTemplate),
114-
sourceLocationOffset: getSourceLocationOffset(taggedTemplate)
114+
sourceLocationOffset: getSourceLocationOffset(taggedTemplate),
115115
});
116116
}
117117
// Visit remaining arguments
@@ -130,7 +130,7 @@ function visit(node: ts.Node, addGraphQLTag: (tag: GraphQLTag) => void): void {
130130
addGraphQLTag({
131131
keyName: null,
132132
template: getGraphQLText(taggedTemplate),
133-
sourceLocationOffset: getSourceLocationOffset(taggedTemplate)
133+
sourceLocationOffset: getSourceLocationOffset(taggedTemplate),
134134
});
135135
}
136136
}
@@ -167,7 +167,7 @@ function getSourceLocationOffset(quasi: ts.TaggedTemplateExpression) {
167167
const loc = quasi.getSourceFile().getLineAndCharacterOfPosition(pos);
168168
return {
169169
line: loc.line + 1,
170-
column: loc.character + 1
170+
column: loc.character + 1,
171171
};
172172
}
173173

@@ -180,6 +180,6 @@ function invariant(condition: boolean, msg: string, ...args: any[]) {
180180
export const find: GraphQLTagFinder = (text, filePath) => {
181181
const result: GraphQLTag[] = [];
182182
const ast = ts.createSourceFile(filePath, text, ts.ScriptTarget.Latest, true);
183-
visit(ast, tag => result.push(tag));
183+
visit(ast, (tag) => result.push(tag));
184184
return result;
185185
};

src/formatGeneratedModule.ts

+54-23
Original file line numberDiff line numberDiff line change
@@ -2,36 +2,63 @@ import { FormatModule } from "relay-compiler";
22
import * as ts from "typescript";
33
import addAnyTypeCast from "./addAnyTypeCast";
44

5-
const createRequireRegex = () => /require\("(.*)"\)/g;
6-
const escapeRegexString = (str: string) =>
7-
str.replace(/[-\/\\^$*+?.()|[\]{}]/g, "\\$&");
5+
const createRequireRegex = () => /require\('(.*)'\)/g;
86

9-
// collects all require calls and converts them to top level imports
10-
const requireToImport = (content: string) => {
7+
function getModuleName(path: string) {
8+
const [moduleName] = path.replace("./", "").split(".");
9+
return moduleName;
10+
}
11+
12+
// collects all require calls and converts them top-level imports
13+
const requireToImport = (content: string): string => {
14+
const requireRegex = createRequireRegex();
15+
16+
// collect all require paths (unique)
1117
const requirePaths = new Set<string>();
12-
const regex = createRequireRegex();
13-
let result: null | RegExpExecArray = null;
1418
while (true) {
15-
result = regex.exec(content);
16-
if (result === null) {
19+
const res = requireRegex.exec(content);
20+
if (res === null) {
1721
break;
1822
}
19-
requirePaths.add(result[1]);
23+
requirePaths.add(res[1]);
2024
}
21-
22-
for (const requirePath of requirePaths) {
23-
const [baseName] = requirePath.replace("./", "").split(".");
24-
content =
25-
`import ${baseName} from "${requirePath.replace(".ts", "")}";\n` +
26-
content.replace(
27-
new RegExp(escapeRegexString(`require('${requirePath}')`), "g"),
28-
baseName
29-
);
30-
}
31-
25+
// replace all require paths
26+
Array.from(requirePaths).forEach((requirePath) => {
27+
content = content.replace(
28+
`require('${requirePath}')`,
29+
getModuleName(requirePath)
30+
);
31+
});
32+
// create top-level imports
33+
const topLevelImports = Array.from(requirePaths)
34+
.sort()
35+
.map(
36+
(requirePath) =>
37+
`import ${getModuleName(requirePath)} from "${requirePath.replace(
38+
".ts",
39+
""
40+
)}";`
41+
);
42+
// add top-level imports
43+
content = `${topLevelImports.join("\n")}
44+
${content}`;
3245
return content;
3346
};
3447

48+
type FormatContentOptions = {
49+
replaceRequire: boolean;
50+
};
51+
52+
function formatContent(
53+
rawContent: string,
54+
options: FormatContentOptions
55+
): string {
56+
if (!options.replaceRequire) {
57+
return rawContent;
58+
}
59+
return requireToImport(rawContent);
60+
}
61+
3562
export const formatterFactory = (
3663
compilerOptions: ts.CompilerOptions = {}
3764
): FormatModule => ({
@@ -43,14 +70,16 @@ export const formatterFactory = (
4370
hash,
4471
sourceHash,
4572
}) => {
73+
const { noImplicitAny, module = -1 } = compilerOptions;
74+
4675
const documentTypeImport = documentType
4776
? `import { ${documentType} } from "relay-runtime";`
4877
: "";
4978
const docTextComment = docText ? "\n/*\n" + docText.trim() + "\n*/\n" : "";
5079
let nodeStatement = `const node: ${
5180
documentType || "never"
5281
} = ${concreteText};`;
53-
if (compilerOptions.noImplicitAny) {
82+
if (noImplicitAny) {
5483
nodeStatement = addAnyTypeCast(nodeStatement).trim();
5584
}
5685
const rawContent = `${typeText || ""}
@@ -66,6 +95,8 @@ export default node;
6695
// @ts-nocheck
6796
${hash ? `/* ${hash} */\n` : ""}
6897
${documentTypeImport}
69-
${requireToImport(rawContent)}`;
98+
${formatContent(rawContent, {
99+
replaceRequire: module >= ts.ModuleKind.ES2015,
100+
})}`;
70101
return content;
71102
};

src/index.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,6 @@ export default function plugin(): PluginInterface {
1010
outputExtension: "ts",
1111
findGraphQLTags: find,
1212
formatModule: formatterFactory(loadCompilerOptions()),
13-
typeGenerator: TypeScriptGenerator
13+
typeGenerator: TypeScriptGenerator,
1414
};
1515
}

src/loadCompilerOptions.ts

+12-3
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,18 @@ export const loadCompilerOptions = (): ts.CompilerOptions => {
55
if (!configFileName) {
66
return {};
77
}
8-
const result = ts.readConfigFile(configFileName, ts.sys.readFile);
9-
if (result.error) {
8+
const configFile = ts.readConfigFile(configFileName, ts.sys.readFile);
9+
if (configFile.error) {
1010
return {};
1111
}
12-
return result.config.compilerOptions;
12+
// parse config file contents (to convert strings to enum values etc.)
13+
const parsedConfig = ts.parseJsonConfigFileContent(
14+
configFile.config,
15+
ts.sys,
16+
"./"
17+
);
18+
if (parsedConfig.errors.length > 0) {
19+
return {};
20+
}
21+
return parsedConfig.options;
1322
};

0 commit comments

Comments
 (0)