Skip to content

Commit

Permalink
fix: ts 3.6.5 does not patch properly (fixes nonara#55)
Browse files Browse the repository at this point in the history
  • Loading branch information
nonara committed Aug 23, 2021
1 parent 86c2d25 commit 1babac9
Show file tree
Hide file tree
Showing 6 changed files with 40 additions and 22 deletions.
38 changes: 17 additions & 21 deletions src/installer/lib/patcher.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,32 +55,28 @@ function validate(tsModule?: TSModule, tsPackage?: TSPackage) {
return true;
}

const patchModule = (tsModule: TSModule, source?: string) => {
const patchModule = (tsModule: TSModule, tsPackage: TSPackage, source?: string) => {
const src = source || tsModule.moduleSrc!;
const funcPos = src.search(/function emitFilesAndReportErrors\(/);
if (funcPos < 0)
throw new Error(`Bad TS Code. Could not find function emitFilesAndReportErrors in ${tsModule.filename}`);
if (funcPos < 0) throw new Error(`Bad TS Code. Could not find function emitFilesAndReportErrors in ${tsModule.filename}`);

const startCode = src.substr(0, funcPos);
const restCode = src.substr(funcPos);

/* Modern TS */
let pos = restCode.search(/^\s*?var emitResult =/m);
if (pos >= 0) {
return startCode +
restCode.substr(0, pos) +
`\nts.diagnosticMap.set(program, allDiagnostics);\n` +
restCode.substr(pos);
}
/* Find emit call position */
let emitPos = restCode.search(/^\s*?var emitResult =/m);
if (emitPos < 0) emitPos = restCode.search(/^\s*?var [_\w]+? = program.emit\(/m); // TS 2.7 - 3.5
if (emitPos < 0) throw new Error(`Could not determine emit call. Please file an issue with your TS version!`);

const ver = tsPackage.version.split('.')
const majorVer = +ver[0];
const minorVer = +ver[1];
let diagnosticsArrName = (majorVer > 3 || (majorVer === 3 && minorVer > 7)) ? 'allDiagnostics' : 'diagnostics';

/* TS 2.7 - 3.5 */
pos = restCode.search(/^\s*?var [_\w]+? = program.emit\(/m);
if (pos < 0) throw new Error(
`Could not recognize diagnostics signature in emitFilesAndReportErrors(). Please open an issue with your TS version #.`
);
return startCode +
restCode.substr(0, pos) +
`\nts.diagnosticMap.set(program, diagnostics);\n` +
restCode.substr(pos);
restCode.substr(0, emitPos) +
`\nts.diagnosticMap.set(program, ${diagnosticsArrName});\n` +
restCode.substr(emitPos);
}


Expand All @@ -101,7 +97,7 @@ export function patchTSModule(tsModule: TSModule, tsPackage: TSPackage) {
const patchSrc = generatePatch(isTSC);

/* Add diagnostic modification support */
const moduleSrc = patchModule(tsModule);
const moduleSrc = patchModule(tsModule, tsPackage);

try {
if (isTSC) {
Expand All @@ -116,7 +112,7 @@ export function patchTSModule(tsModule: TSModule, tsPackage: TSPackage) {
/* Expand TSC with full typescript library (splice tsc part on top of typescript.ts code) */
fs.writeFileSync(file,
Buffer.concat([
Buffer.from(patchModule(tsModule, fs.readFileSync(tsFile, 'utf-8'))),
Buffer.from(patchModule(tsModule, tsPackage, fs.readFileSync(tsFile, 'utf-8'))),
Buffer.from(!getTSModule(tsFile).patchVersion ? patchSrc : ''),
Buffer.from(
moduleSrc.replace(/^[\s\S]+(\(function \(ts\) {\s+function countLines[\s\S]+)$/, '$1')
Expand Down
1 change: 1 addition & 0 deletions test/assets/ts/3.6/.yarnrc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
--no-lockfile true
10 changes: 10 additions & 0 deletions test/assets/ts/3.6/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"name": "module-ts36",
"version": "1.0.0",
"dependencies": {
"typescript": "3.6.5",
"ts-patch": "*",
"ts-node": "^8.10.1",
"tsconfig-paths": "^3.9.0"
}
}
1 change: 1 addition & 0 deletions test/assets/ts/3.8/.yarnrc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
--no-lockfile true
10 changes: 10 additions & 0 deletions test/assets/ts/3.8/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"name": "module-ts38",
"version": "1.0.0",
"dependencies": {
"typescript": "3.8.3",
"ts-patch": "*",
"ts-node": "^8.10.1",
"tsconfig-paths": "^3.9.0"
}
}
2 changes: 1 addition & 1 deletion test/patch/tsc.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ describe.each([ ...tsInstallationDirs.keys() ])(`TSC - %s`, (tsVersion: string)
tscScript = new vm.Script(
`(function (exports, require, module, __filename, __dirname, tscArgs, process, mockWriteFile) { \n` +
tscCode.replace(
/(^\s*?ts\.executeCommandLine\(ts.sys.+?;$)/m,
/(^\s*?ts\.executeCommandLine\(ts\.sys)/m,
`\nObject.assign(ts.sys, { args: tscArgs, writeFile: mockWriteFile });\n$1`
) +
`})`
Expand Down

0 comments on commit 1babac9

Please sign in to comment.