Skip to content

Commit

Permalink
fix: handle typescript version for the no timeout fix
Browse files Browse the repository at this point in the history
  • Loading branch information
sheetalkamat committed Mar 10, 2020
1 parent 2405fae commit d9a5867
Showing 1 changed file with 30 additions and 17 deletions.
47 changes: 30 additions & 17 deletions packages/typescript-estree/src/create-program/createWatchProgram.ts
@@ -1,5 +1,6 @@
import debug from 'debug';
import fs from 'fs';
import semver from 'semver';
import * as ts from 'typescript';
import { Extra } from '../parser-options';
import { WatchCompilerHostOfConfigFile } from './WatchCompilerHostOfConfigFile';
Expand Down Expand Up @@ -226,6 +227,10 @@ function getProgramsForProjects(
return results;
}

const isRunningNoTimeoutFix = semver.satisfies(ts.version, '>=3.9.0-beta', {
includePrerelease: true,
});

function createWatchProgram(
tsconfigPath: string,
extra: Extra,
Expand Down Expand Up @@ -313,25 +318,33 @@ function createWatchProgram(

// Since we don't want to asynchronously update program we want to disable timeout methods
// So any changes in the program will be delayed and updated when getProgram is called on watch
// But because of https://github.com/microsoft/TypeScript/pull/37308 we cannot just set it to undefined
// instead save it and call before getProgram is called
let callback: (() => void) | undefined;
watchCompilerHost.setTimeout = (cb, _ms, ...args): unknown => {
callback = cb.bind(/*this*/ undefined, ...args);
return callback;
};
watchCompilerHost.clearTimeout = (): void => {
callback = undefined;
};
if (isRunningNoTimeoutFix) {
watchCompilerHost.setTimeout = undefined;
watchCompilerHost.clearTimeout = undefined;
} else {
log('Running without timeout fix');
// But because of https://github.com/microsoft/TypeScript/pull/37308 we cannot just set it to undefined
// instead save it and call before getProgram is called
watchCompilerHost.setTimeout = (cb, _ms, ...args): unknown => {
callback = cb.bind(/*this*/ undefined, ...args);
return callback;
};
watchCompilerHost.clearTimeout = (): void => {
callback = undefined;
};
}
const watch = ts.createWatchProgram(watchCompilerHost);
const originalGetProgram = watch.getProgram;
watch.getProgram = (): ts.BuilderProgram => {
if (callback) {
callback();
}
callback = undefined;
return originalGetProgram.call(watch);
};
if (!isRunningNoTimeoutFix) {
const originalGetProgram = watch.getProgram;
watch.getProgram = (): ts.BuilderProgram => {
if (callback) {
callback();
}
callback = undefined;
return originalGetProgram.call(watch);
};
}
return watch;
}

Expand Down

0 comments on commit d9a5867

Please sign in to comment.