Skip to content

Commit 04c837a

Browse files
committed
Allow testing certain code blocks on only specific TypeScript versions
1 parent 20d4f8d commit 04c837a

File tree

4 files changed

+46
-3
lines changed

4 files changed

+46
-3
lines changed

integrationTests/ts/.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
# TypeScript files are generated from templates; see test.js
2+
*.ts

integrationTests/ts/index.ts renamed to integrationTests/ts/index.ts.template

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,7 @@ const example: SomeExtension = {
1313
};
1414

1515
// The following code block requires a newer version of TypeScript
16-
/*! >=3.2
17-
16+
/*! >=3.2 !*/
1817
declare module 'graphql' {
1918
interface GraphQLObjectTypeExtensions<TSource = any, TContext = any> {
2019
someObjectExtension?: SomeExtension;
@@ -30,7 +29,7 @@ declare module 'graphql' {
3029
someArgumentExtension?: SomeExtension;
3130
}
3231
}
33-
*/
32+
/*!!*/
3433

3534
const queryType: GraphQLObjectType = new GraphQLObjectType({
3635
name: 'Query',

integrationTests/ts/package.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,9 @@
33
"test": "node test.js"
44
},
55
"dependencies": {
6+
"glob": "^7.1.6",
67
"graphql": "file:../../npmDist",
8+
"semver": "^7.3.2",
79
"typescript-2.6": "npm:[email protected]",
810
"typescript-2.7": "npm:[email protected]",
911
"typescript-2.8": "npm:[email protected]",

integrationTests/ts/test.js

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,16 +4,56 @@
44

55
const path = require('path');
66
const childProcess = require('child_process');
7+
const assert = require('assert');
8+
const fs = require('fs');
9+
10+
const glob = require('glob');
11+
const semver = require('semver');
712

813
const { dependencies } = require('./package.json');
914

1015
const tsVersions = Object.keys(dependencies)
1116
.filter((pkg) => pkg.startsWith('typescript-'))
1217
.sort((a, b) => b.localeCompare(a));
1318

19+
// To allow omitting certain code from older versions of TypeScript, we have a
20+
// "magic" comment syntax. We precede a block of code with:
21+
//
22+
// /*! SEMVER_RANGE !*/
23+
//
24+
// replacing SEMVER_RANGE with a semver range spec, such as '>=3.2'; we
25+
// terminate the block of code with:
26+
//
27+
// /*!!*/
28+
//
29+
// We will only include the code between these comments if the TypeScript
30+
// version being tested satisfies the semver range that was specified. NOTE: We
31+
// currently do not allow nesting of these blocks.
32+
const templates = glob.sync('./*.ts.template').map((filename) => {
33+
const content = fs.readFileSync(filename, 'utf8');
34+
const targetFilename = filename.replace(/\.template$/, '');
35+
assert.notEqual(filename, targetFilename);
36+
const writeFileSync = (version) => {
37+
// Captures our magic comment syntax: `/*!(CAPTURE1)!*/(CAPTURE 2)/*!!*/`
38+
const regex = /\/\*!([^!]+)!\*\/([\s\S]*?)\/\*!!\*\//g;
39+
const finalContent = content.replace(regex, (_, versionRange, payload) => {
40+
if (semver.satisfies(version, versionRange)) {
41+
return payload;
42+
}
43+
return '';
44+
});
45+
fs.writeFileSync(targetFilename, finalContent);
46+
};
47+
return { writeFileSync };
48+
});
49+
1450
for (const version of tsVersions) {
1551
console.log(`Testing on ${version} ...`);
1652

53+
for (const template of templates) {
54+
template.writeFileSync(version);
55+
}
56+
1757
const tscPath = path.join(__dirname, 'node_modules', version, 'bin/tsc');
1858
childProcess.execSync(tscPath, { stdio: 'inherit' });
1959
}

0 commit comments

Comments
 (0)