Skip to content

Commit d13b25b

Browse files
committed
fix: don't crash "fix all missing imports" in unpatched ts 5.0
1 parent 1ff1893 commit d13b25b

File tree

2 files changed

+15
-2
lines changed

2 files changed

+15
-2
lines changed

typescript/src/codeFixes.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import _ from 'lodash'
22
import addMissingProperties from './codeFixes/addMissingProperties'
33
import { changeSortingOfAutoImport, getIgnoreAutoImportSetting, isAutoImportEntryShouldBeIgnored } from './adjustAutoImports'
44
import { GetConfig } from './types'
5-
import { findChildContainingPosition, getCancellationToken, getIndentFromPos, patchMethod } from './utils'
5+
import { findChildContainingPosition, getCancellationToken, getIndentFromPos, isTsPatched, patchMethod } from './utils'
66
import namespaceAutoImports from './namespaceAutoImports'
77

88
// codeFixes that I managed to put in files
@@ -169,7 +169,8 @@ export default (proxy: ts.LanguageService, languageService: ts.LanguageService,
169169

170170
proxy.getCombinedCodeFix = (scope, fixId, formatOptions, preferences) => {
171171
const { fileName } = scope
172-
if (fixId === 'fixMissingImport') {
172+
// todo issue warning (or language status error) when configured but not patched
173+
if (fixId === 'fixMissingImport' && isTsPatched()) {
173174
const program = languageService.getProgram()!
174175
const sourceFile = program.getSourceFile(fileName)!
175176
const importAdder = tsFull.codefix.createImportAdder(

typescript/src/utils.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,18 @@ export const boostExistingSuggestions = (entries: ts.CompletionEntry[], predicat
120120
// semver: can't use compare as it incorrectly works with build postfix
121121
export const isTs5 = () => semver.major(ts.version) >= 5
122122

123+
export const isTsPatched = () => {
124+
try {
125+
const testFunction: any = () => {}
126+
const unpatch = patchMethod(ts, 'findAncestor', () => testFunction)
127+
const isPatched = ts.findAncestor === testFunction
128+
unpatch()
129+
return isPatched
130+
} catch (err) {
131+
return false
132+
}
133+
}
134+
123135
// Workaround esbuild bundle modules
124136
export const nodeModules = __WEB__
125137
? null

0 commit comments

Comments
 (0)