@@ -2,7 +2,7 @@ import _ from 'lodash'
2
2
import addMissingProperties from './codeFixes/addMissingProperties'
3
3
import { changeSortingOfAutoImport , getIgnoreAutoImportSetting , isAutoImportEntryShouldBeIgnored } from './adjustAutoImports'
4
4
import { GetConfig } from './types'
5
- import { findChildContainingPosition , getIndentFromPos } from './utils'
5
+ import { findChildContainingPosition , getIndentFromPos , patchMethod } from './utils'
6
6
7
7
// codeFixes that I managed to put in files
8
8
const externalCodeFixes = [ addMissingProperties ]
@@ -166,31 +166,41 @@ export default (proxy: ts.LanguageService, languageService: ts.LanguageService,
166
166
const ignoreAutoImportsSetting = getIgnoreAutoImportSetting ( c )
167
167
for ( const diagnostic of semanticDiagnostics ) {
168
168
if ( ! errorCodes . includes ( diagnostic . code ) ) continue
169
- const oldFirst = tsFull . first
170
- const oldForEachExternalModuleToImportFrom = tsFull . forEachExternalModuleToImportFrom
169
+ const toUnpatch : ( ( ) => any ) [ ] = [ ]
171
170
try {
172
- tsFull . first = ( ( fixes : FixInfo [ ] ) => {
173
- const sortFn = changeSortingOfAutoImport ( c , fixes [ 0 ] ! . symbolName )
174
- fixes = _ . sortBy (
175
- fixes . filter ( ( { fix, symbolName } ) => {
176
- if ( fix . kind === ( ImportFixKind . PromoteTypeOnly as number ) ) return false
177
- const shouldBeIgnored =
178
- c ( 'autoImport.alwaysIgnoreInImportAll' ) . includes ( fix . moduleSpecifier ) ||
179
- isAutoImportEntryShouldBeIgnored ( ignoreAutoImportsSetting , fix . moduleSpecifier , symbolName )
180
- return ! shouldBeIgnored
181
- } ) ,
182
- ( { fix } ) => sortFn ( fix . moduleSpecifier ) ,
183
- )
184
- return fixes [ 0 ]
185
- } ) as any
186
- // patching is fun
187
- tsFull . forEachExternalModuleToImportFrom = ( program , host , preferences , _useAutoImportProvider , cb ) => {
188
- return oldForEachExternalModuleToImportFrom ( program , host , preferences , true , cb )
189
- }
171
+ toUnpatch . push (
172
+ patchMethod (
173
+ tsFull ,
174
+ 'first' ,
175
+ ( ) =>
176
+ ( ( fixes : FixInfo [ ] ) => {
177
+ const sortFn = changeSortingOfAutoImport ( c , fixes [ 0 ] ! . symbolName )
178
+ fixes = _ . sortBy (
179
+ fixes . filter ( ( { fix, symbolName } ) => {
180
+ if ( fix . kind === ( ImportFixKind . PromoteTypeOnly as number ) ) return false
181
+ const shouldBeIgnored =
182
+ c ( 'autoImport.alwaysIgnoreInImportAll' ) . includes ( fix . moduleSpecifier ) ||
183
+ isAutoImportEntryShouldBeIgnored ( ignoreAutoImportsSetting , fix . moduleSpecifier , symbolName )
184
+ return ! shouldBeIgnored
185
+ } ) ,
186
+ ( { fix } ) => sortFn ( fix . moduleSpecifier ) ,
187
+ )
188
+ return fixes [ 0 ]
189
+ } ) as any ,
190
+ ) ,
191
+ patchMethod (
192
+ tsFull ,
193
+ 'forEachExternalModuleToImportFrom' ,
194
+ oldForEachExternalModuleToImportFrom => ( program , host , preferences , _useAutoImportProvider , cb ) => {
195
+ return oldForEachExternalModuleToImportFrom ( program , host , preferences , true , cb )
196
+ } ,
197
+ ) ,
198
+ )
190
199
importAdder . addImportFromDiagnostic ( { ...diagnostic , file : sourceFile as any } as any , context )
191
200
} finally {
192
- tsFull . first = oldFirst
193
- tsFull . forEachExternalModuleToImportFrom = oldForEachExternalModuleToImportFrom
201
+ for ( const unpatch of toUnpatch ) {
202
+ unpatch ( )
203
+ }
194
204
}
195
205
}
196
206
return tsFull . codefix . createCombinedCodeActions ( tsFull . textChanges . ChangeTracker . with ( context , importAdder . writeFixes ) )
0 commit comments