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

Commit 34ef31d

Browse files
Convert remaining files to TypeScript (#658)
1 parent a99ab23 commit 34ef31d

16 files changed

+203
-351
lines changed

.eslintrc.yml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -542,7 +542,8 @@ overrides:
542542
'@typescript-eslint/no-throw-literal': error
543543
'@typescript-eslint/no-type-alias': off # TODO consider
544544
'@typescript-eslint/no-unnecessary-boolean-literal-compare': error
545-
'@typescript-eslint/no-unnecessary-condition': error
545+
# FIXME: Disabled to enable checking conditions that would be prevented by types anyway
546+
'@typescript-eslint/no-unnecessary-condition': off
546547
'@typescript-eslint/no-unnecessary-qualifier': error
547548
'@typescript-eslint/no-unnecessary-type-arguments': error
548549
'@typescript-eslint/no-unnecessary-type-assertion': error

.mocharc.yml

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
throw-deprecation: true
22
check-leaks: true
33
require:
4-
- '@babel/register'
5-
- ts-node/register
4+
- './resources/register.js'

cspell.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
"serializable",
2323
"subcommand",
2424
"charsets",
25+
"downlevel",
2526

2627
// TODO: remove bellow words
2728
"Graphi", // GraphiQL
File renamed without changes.

package-lock.json

Lines changed: 57 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,13 @@
66
"private": true,
77
"main": "index.js",
88
"types": "index.d.ts",
9+
"typesVersions": {
10+
"<3.8": {
11+
"*": [
12+
"ts3.4/*"
13+
]
14+
}
15+
},
916
"sideEffects": false,
1017
"homepage": "https://github.com/graphql/express-graphql",
1118
"bugs": {
@@ -40,7 +47,7 @@
4047
"check:spelling": "cspell '**/*'",
4148
"check:integrations": "mocha --full-trace integrationTests/*-test.js",
4249
"build": "node resources/build.js",
43-
"start": "node -r @babel/register examples/index.js"
50+
"start": "node -r ./resources/register.js examples/index.ts"
4451
},
4552
"dependencies": {
4653
"accepts": "^1.3.7",
@@ -53,10 +60,13 @@
5360
"@babel/plugin-transform-flow-strip-types": "7.10.4",
5461
"@babel/preset-env": "7.10.4",
5562
"@babel/register": "7.10.4",
63+
"@types/accepts": "1.3.5",
5664
"@types/body-parser": "1.19.0",
5765
"@types/chai": "4.2.11",
5866
"@types/connect": "3.4.33",
67+
"@types/content-type": "1.1.3",
5968
"@types/express": "4.17.6",
69+
"@types/http-errors": "1.6.3",
6070
"@types/mocha": "7.0.2",
6171
"@types/multer": "1.4.3",
6272
"@types/node": "14.0.14",
@@ -70,6 +80,7 @@
7080
"chai": "4.2.0",
7181
"connect": "3.7.0",
7282
"cspell": "4.0.63",
83+
"downlevel-dts": "0.5.0",
7384
"eslint": "7.3.1",
7485
"eslint-plugin-flowtype": "5.2.0",
7586
"eslint-plugin-import": "2.22.0",

resources/build.js

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -6,29 +6,33 @@ const fs = require('fs');
66
const path = require('path');
77
const assert = require('assert');
88

9-
const babel = require('@babel/core');
9+
const ts = require('typescript');
10+
const { main: downlevel } = require('downlevel-dts');
1011

12+
const tsConfig = require('../tsconfig.json');
13+
14+
const {
15+
transformLoadFileStaticallyFromNPM,
16+
} = require('./load-statically-from-npm');
1117
const { rmdirRecursive, readdirRecursive, showStats } = require('./utils');
1218

1319
if (require.main === module) {
1420
rmdirRecursive('./dist');
1521
fs.mkdirSync('./dist');
1622

1723
const srcFiles = readdirRecursive('./src', { ignoreDir: /^__.*__$/ });
18-
for (const filepath of srcFiles) {
19-
const srcPath = path.join('./src', filepath);
20-
const destPath = path.join('./dist', filepath);
21-
22-
fs.mkdirSync(path.dirname(destPath), { recursive: true });
23-
if (filepath.endsWith('.js')) {
24-
fs.copyFileSync(srcPath, destPath + '.flow');
25-
26-
const cjs = babelBuild(srcPath, { envName: 'cjs' });
27-
fs.writeFileSync(destPath, cjs);
28-
} else if (filepath.endsWith('d.ts')) {
29-
fs.copyFileSync(srcPath, destPath);
30-
}
31-
}
24+
const { options } = ts.convertCompilerOptionsFromJson(
25+
tsConfig.compilerOptions,
26+
process.cwd(),
27+
);
28+
const program = ts.createProgram({
29+
rootNames: srcFiles.map((filepath) => path.join('./src', filepath)),
30+
options,
31+
});
32+
program.emit(undefined, undefined, undefined, undefined, {
33+
after: [transformLoadFileStaticallyFromNPM],
34+
});
35+
downlevel('./dist', './dist/ts3.4');
3236

3337
fs.copyFileSync('./LICENSE', './dist/LICENSE');
3438
fs.copyFileSync('./README.md', './dist/README.md');
@@ -40,10 +44,6 @@ if (require.main === module) {
4044
showStats();
4145
}
4246

43-
function babelBuild(srcPath, options) {
44-
return babel.transformFileSync(srcPath, options).code + '\n';
45-
}
46-
4747
function buildPackageJSON() {
4848
const packageJSON = require('../package.json');
4949
delete packageJSON.private;

resources/load-statically-from-npm.js

Lines changed: 16 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,9 @@
44

55
const fs = require('fs');
66

7+
const ts = require('typescript');
8+
79
/**
8-
* Eliminates function call to `invariant` if the condition is met.
9-
*
1010
* Transforms:
1111
*
1212
* loadFileStaticallyFromNPM(<npm path>)
@@ -15,23 +15,19 @@ const fs = require('fs');
1515
*
1616
* "<file content>"
1717
*/
18-
module.exports = function inlineInvariant(context) {
19-
return {
20-
visitor: {
21-
CallExpression(path) {
22-
const { node } = path;
23-
24-
if (
25-
node.callee.type === 'Identifier' &&
26-
node.callee.name === 'loadFileStaticallyFromNPM'
27-
) {
28-
const npmPath = node.arguments[0].value;
29-
const filePath = require.resolve(npmPath);
30-
const content = fs.readFileSync(filePath, 'utf-8');
31-
32-
path.replaceWith(context.types.stringLiteral(content));
33-
}
34-
},
35-
},
18+
module.exports.transformLoadFileStaticallyFromNPM = function (context) {
19+
return function visit(node) {
20+
if (ts.isCallExpression(node)) {
21+
if (
22+
ts.isIdentifier(node.expression) &&
23+
node.expression.text === 'loadFileStaticallyFromNPM'
24+
) {
25+
const npmPath = node.arguments[0].text;
26+
const filePath = require.resolve(npmPath);
27+
const content = fs.readFileSync(filePath, 'utf-8');
28+
return ts.createStringLiteral(content);
29+
}
30+
}
31+
return ts.visitEachChild(node, visit, context);
3632
};
3733
};

resources/register.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
'use strict';
2+
3+
const {
4+
transformLoadFileStaticallyFromNPM,
5+
} = require('./load-statically-from-npm');
6+
7+
require('ts-node').register({
8+
transformers: () => ({
9+
after: [transformLoadFileStaticallyFromNPM],
10+
}),
11+
});

0 commit comments

Comments
 (0)