Skip to content

Commit c9879fb

Browse files
authored
fix: restore typescript 5.6 compatibility (#301)
This PR restores compatibility with TypeScript 5.6. It also updates the test suite to be more resilient and explicitly tests against TypeScript 5.5 and 5.6. I was surprised that TypeScript would break this without releasing a major version. However, if you look at the `getOriginalSourceFile` method we were using, it was marked as `@internal` in the JSDoc: https://github.com/microsoft/TypeScript/blob/c8a7d589e647e19c94150d9892909f3aa93e48eb/src/compiler/utilities.ts#L5416-L5419 Because it was marked as internal, they probably reserved the right to remove this method without a breaking version bump. The other PR to fix this issue (#270) suggested using `getSourceFileOfNode`, but that's also marked as `@internal`: https://github.com/microsoft/TypeScript/blob/11b2930fa2c9f73b0ffb725a9715b8d3c4121bbc/src/compiler/utilities.ts#L965-L975. Therefore, instead, I updated this code to use `getParseTreeNode` and `isSourceFile`, which are both exposed publicly and not marked as `internal`: https://github.com/microsoft/TypeScript/blob/11b2930fa2c9f73b0ffb725a9715b8d3c4121bbc/src/compiler/utilitiesPublic.ts#L803-L809 / https://github.com/microsoft/TypeScript/blob/11b2930fa2c9f73b0ffb725a9715b8d3c4121bbc/src/compiler/factory/nodeTests.ts#L1013-L1016 By using these methods, we should be more resilient to internal refactoring. This PR supersedes #270. Fixes #266
1 parent a7276d0 commit c9879fb

File tree

6 files changed

+45
-19
lines changed

6 files changed

+45
-19
lines changed

src/transformer.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,7 @@ export default function transformer(
135135
...tsTransformPathsContext,
136136
sourceFile,
137137
isDeclarationFile: sourceFile.isDeclarationFile,
138-
originalSourceFile: (<typeof ts>tsInstance).getOriginalSourceFile(sourceFile),
138+
originalSourceFile: ts.getParseTreeNode(sourceFile, ts.isSourceFile) || sourceFile,
139139
getVisitor() {
140140
return nodeVisitor.bind(this);
141141
},

test/config.ts

+4
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,15 @@
11
import ts from "typescript";
22
import tsThree from "typescript-3";
33
import tsFourSeven from "typescript-4.7";
4+
import tsFiveFive from "typescript-5.5";
5+
import tsFiveSix from "typescript-5.6";
46
import path from "node:path";
57

68
export const tsModules = <const>[
79
["3.6.5", tsThree, "typescript-3"],
810
["4.7.4", tsFourSeven, "typescript-4.7"],
11+
["5.5.4", tsFiveFive, "typescript-5.5"],
12+
["5.6.3", tsFiveSix, "typescript-5.6"],
913
["Latest", ts, "typescript"],
1014
];
1115

test/package.json

+3-1
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,11 @@
2121
"ts-patch": "^3.2.1",
2222
"tsp1": "npm:ts-patch@1.*.*",
2323
"tsp2": "npm:ts-patch@2.*.*",
24-
"typescript": "^5.5.4",
24+
"typescript": "^5.6.3",
2525
"typescript-3": "npm:typescript@3.6.5",
2626
"typescript-4.7": "npm:typescript@4.7.4",
27+
"typescript-5.5": "npm:typescript@5.5.4",
28+
"typescript-5.6": "npm:typescript@5.6.3",
2729
"typescript-transform-paths": "portal:../"
2830
},
2931
"workspaces": [

test/prepare.mjs

+2
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,8 @@ function patchTsModules() {
3030

3131
patchTypescript("typescript-3", patch1);
3232
patchTypescript("typescript-4.7", patch2);
33+
patchTypescript("typescript-5.5", patch);
34+
patchTypescript("typescript-5.6", patch);
3335
patchTypescript("typescript", patch);
3436
}
3537

test/tests/extras.test.ts

+8-2
Original file line numberDiff line numberDiff line change
@@ -34,14 +34,20 @@ describe(`Extra Tests`, () => {
3434
});
3535

3636
describe(`ts-node register script`, () => {
37+
/** Yarn sometimes outputs bold text, which makes these tests flakey */
38+
function stripAnsi(str: string) {
39+
// eslint-disable-next-line no-control-regex
40+
return str.replace(/\x1B\[[0-?]*[ -/]*[@-~]/g, "");
41+
}
42+
3743
test(`Works with --transpileOnly`, () => {
3844
const res = execSync("yarn g:ts-node --transpileOnly src/index.ts", { cwd: projectRoot }).toString();
39-
expect(res).toMatch(/^null($|\r?\n)/m);
45+
expect(stripAnsi(res.trim())).toEqual("null");
4046
});
4147

4248
test(`Works with --typeCheck`, () => {
4349
const res = execSync("yarn g:ts-node --typeCheck src/index.ts", { cwd: projectRoot }).toString();
44-
expect(res).toMatch(/^null($|\r?\n)/);
50+
expect(stripAnsi(res.trim())).toEqual("null");
4551
});
4652
});
4753
});

test/yarn.lock

+27-15
Original file line numberDiff line numberDiff line change
@@ -8069,9 +8069,11 @@ __metadata:
80698069
ts-patch: "npm:^3.2.1"
80708070
tsp1: "npm:ts-patch@1.*.*"
80718071
tsp2: "npm:ts-patch@2.*.*"
8072-
typescript: "npm:^5.5.4"
8072+
typescript: "npm:^5.6.3"
80738073
typescript-3: "npm:typescript@3.6.5"
80748074
typescript-4.7: "npm:typescript@4.7.4"
8075+
typescript-5.5: "npm:typescript@5.5.4"
8076+
typescript-5.6: "npm:typescript@5.6.3"
80758077
typescript-transform-paths: "portal:../"
80768078
languageName: unknown
80778079
linkType: soft
@@ -9230,6 +9232,26 @@ __metadata:
92309232
languageName: node
92319233
linkType: hard
92329234

9235+
"typescript-5.5@npm:typescript@5.5.4":
9236+
version: 5.5.4
9237+
resolution: "typescript@npm:5.5.4"
9238+
bin:
9239+
tsc: bin/tsc
9240+
tsserver: bin/tsserver
9241+
checksum: 10c0/422be60f89e661eab29ac488c974b6cc0a660fb2228003b297c3d10c32c90f3bcffc1009b43876a082515a3c376b1eefcce823d6e78982e6878408b9a923199c
9242+
languageName: node
9243+
linkType: hard
9244+
9245+
"typescript-5.6@npm:typescript@5.6.3, typescript@npm:^5.6.3":
9246+
version: 5.6.3
9247+
resolution: "typescript@npm:5.6.3"
9248+
bin:
9249+
tsc: bin/tsc
9250+
tsserver: bin/tsserver
9251+
checksum: 10c0/44f61d3fb15c35359bc60399cb8127c30bae554cd555b8e2b46d68fa79d680354b83320ad419ff1b81a0bdf324197b29affe6cc28988cd6a74d4ac60c94f9799
9252+
languageName: node
9253+
linkType: hard
9254+
92339255
"typescript-transform-paths@portal:../::locator=root-workspace-0b6124%40workspace%3A.":
92349256
version: 0.0.0-use.local
92359257
resolution: "typescript-transform-paths@portal:../::locator=root-workspace-0b6124%40workspace%3A."
@@ -9240,23 +9262,13 @@ __metadata:
92409262
languageName: node
92419263
linkType: soft
92429264

9243-
"typescript@npm:^5.5.4":
9244-
version: 5.5.4
9245-
resolution: "typescript@npm:5.5.4"
9246-
bin:
9247-
tsc: bin/tsc
9248-
tsserver: bin/tsserver
9249-
checksum: 10c0/422be60f89e661eab29ac488c974b6cc0a660fb2228003b297c3d10c32c90f3bcffc1009b43876a082515a3c376b1eefcce823d6e78982e6878408b9a923199c
9250-
languageName: node
9251-
linkType: hard
9252-
9253-
"typescript@patch:typescript@npm%3A^5.5.4#optional!builtin<compat/typescript>":
9254-
version: 5.5.4
9255-
resolution: "typescript@patch:typescript@npm%3A5.5.4#optional!builtin<compat/typescript>::version=5.5.4&hash=379a07"
9265+
"typescript@patch:typescript@npm%3A^5.6.3#optional!builtin<compat/typescript>":
9266+
version: 5.6.3
9267+
resolution: "typescript@patch:typescript@npm%3A5.6.3#optional!builtin<compat/typescript>::version=5.6.3&hash=74658d"
92569268
bin:
92579269
tsc: bin/tsc
92589270
tsserver: bin/tsserver
9259-
checksum: 10c0/73409d7b9196a5a1217b3aaad929bf76294d3ce7d6e9766dd880ece296ee91cf7d7db6b16c6c6c630ee5096eccde726c0ef17c7dfa52b01a243e57ae1f09ef07
9271+
checksum: 10c0/ac8307bb06bbfd08ae7137da740769b7d8c3ee5943188743bb622c621f8ad61d244767480f90fbd840277fbf152d8932aa20c33f867dea1bb5e79b187ca1a92f
92609272
languageName: node
92619273
linkType: hard
92629274

0 commit comments

Comments
 (0)