11import _ from 'lodash'
22import { getCompletionsAtPosition as getCompletionsAtPositionRaw } from '../src/completionsAtPosition'
33import { Configuration } from '../src/types'
4+ import codeActionsDecorateProxy from '../src/codeActions/decorateProxy'
5+ import { dedentString } from '../src/utils'
46import { defaultConfigFunc , entrypoint , sharedLanguageService , settingsOverride , currentTestingContext } from './shared'
57
68interface CompletionPartMatcher {
@@ -25,6 +27,10 @@ interface CodeActionMatcher {
2527
2628const { languageService, languageServiceHost, updateProject, getCurrentFile } = sharedLanguageService
2729
30+ const fakeProxy = { } as Pick < typeof languageService , 'getApplicableRefactors' | 'getEditsForRefactor' >
31+
32+ codeActionsDecorateProxy ( fakeProxy as typeof languageService , languageService , languageServiceHost , defaultConfigFunc )
33+
2834export const getCompletionsAtPosition = ( pos : number , { fileName = entrypoint , shouldHave } : { fileName ?: string ; shouldHave ?: boolean } = { } ) => {
2935 if ( pos === undefined ) throw new Error ( 'getCompletionsAtPosition: pos is undefined' )
3036 const result = getCompletionsAtPositionRaw (
@@ -70,7 +76,8 @@ export const overrideSettings = (newOverrides: Partial<Configuration>) => {
7076 } )
7177}
7278
73- export const fourslashLikeTester = ( contents : string , fileName = entrypoint ) => {
79+ export const fourslashLikeTester = ( contents : string , fileName = entrypoint , { dedent = false } : { dedent ? } = { } ) => {
80+ if ( dedent ) contents = dedentString ( contents )
7481 const [ positive , _negative , numberedPositions ] = fileContentsSpecialPositions ( contents , fileName )
7582
7683 const ranges = positive . reduce < number [ ] [ ] > (
@@ -141,17 +148,25 @@ export const fourslashLikeTester = (contents: string, fileName = entrypoint) =>
141148 if ( ! ranges [ mark ] ) throw new Error ( `No range with index ${ mark } found, highest index is ${ ranges . length - 1 } ` )
142149 const start = ranges [ mark ] ! [ 0 ] !
143150 const end = ranges [ mark ] ! [ 0 ] !
144- const appliableRefactors = languageService . getApplicableRefactors ( fileName , { pos : start , end } , { } , 'invoked' )
145- const appliableNames = appliableRefactors . map ( appliableRefactor => appliableRefactor . actions . map ( action => action . description ) )
151+ const appliableRefactors = fakeProxy . getApplicableRefactors ( fileName , { pos : start , end } , { } , 'invoked' )
152+ const appliableNames = appliableRefactors . flatMap ( appliableRefactor => appliableRefactor . actions . map ( action => action . description ) )
146153 const { refactorName, newContent } = matcher
147154 if ( newContent ) {
148155 expect ( appliableNames , `at marker ${ mark } ` ) . toContain ( refactorName )
149156 const actionsGroup = appliableRefactors . find ( appliableRefactor =>
150157 appliableRefactor . actions . find ( action => action . description === refactorName ) ,
151158 ) !
152159 const action = actionsGroup . actions . find ( action => action . description === refactorName ) !
153- const { edits } = languageService . getEditsForRefactor ( fileName , { } , { pos : start , end } , actionsGroup . name , action . name , { } ) !
154- const a = tsFull . textChanges . applyChanges ( getCurrentFile ( ) , edits [ 0 ] ! . textChanges )
160+ const { edits } = fakeProxy . getEditsForRefactor (
161+ fileName ,
162+ ts . getDefaultFormatCodeSettings ( ) ,
163+ { pos : start , end } ,
164+ actionsGroup . name ,
165+ action . name ,
166+ { } ,
167+ ) !
168+ const newContentsActual = tsFull . textChanges . applyChanges ( getCurrentFile ( ) , edits [ 0 ] ! . textChanges )
169+ expect ( dedentString ( newContent ) , `at marker ${ mark } ` ) . toEqual ( newContentsActual )
155170 } else {
156171 expect ( appliableNames , `at marker ${ mark } ` ) . not . toContain ( refactorName )
157172 }
0 commit comments