diff --git a/packages/typescript-estree/src/create-program/createWatchProgram.ts b/packages/typescript-estree/src/create-program/createWatchProgram.ts index c8548970692..682e1d7c731 100644 --- a/packages/typescript-estree/src/create-program/createWatchProgram.ts +++ b/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'; @@ -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, @@ -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; }