@@ -2263,23 +2263,22 @@ namespace FourSlash {
2263
2263
}
2264
2264
2265
2265
/**
2266
- * Compares expected text to the text that would be in the sole range
2267
- * (ie: [|...|]) in the file after applying the codefix sole codefix
2268
- * in the source file.
2269
- *
2270
- * Because codefixes are only applied on the working file, it is unsafe
2271
- * to apply this more than once (consider a refactoring across files).
2266
+ * Finds and applies a code action corresponding to the supplied parameters.
2267
+ * If index is undefined, applies the unique code action available.
2268
+ * @param errorCode The error code that generated the code action.
2269
+ * @param index The nth (0-index-based) codeaction available generated by errorCode.
2272
2270
*/
2273
- public verifyRangeAfterCodeFix ( expectedText : string , includeWhiteSpace ?: boolean , errorCode ?: number , index ?: number ) {
2271
+ public getAndApplyCodeActions ( errorCode ?: number , index ?: number ) {
2272
+ const fileName = this . activeFile . fileName ;
2273
+ this . applyCodeActions ( this . getCodeFixActions ( fileName , errorCode ) , index ) ;
2274
+ }
2275
+
2276
+ public verifyRangeIs ( expectedText : string , includeWhiteSpace ?: boolean ) {
2274
2277
const ranges = this . getRanges ( ) ;
2275
2278
if ( ranges . length !== 1 ) {
2276
2279
this . raiseError ( "Exactly one range should be specified in the testfile." ) ;
2277
2280
}
2278
2281
2279
- const fileName = this . activeFile . fileName ;
2280
-
2281
- this . applyCodeAction ( fileName , this . getCodeFixActions ( fileName , errorCode ) , index ) ;
2282
-
2283
2282
const actualText = this . rangeText ( ranges [ 0 ] ) ;
2284
2283
2285
2284
const result = includeWhiteSpace
@@ -2291,6 +2290,16 @@ namespace FourSlash {
2291
2290
}
2292
2291
}
2293
2292
2293
+ /**
2294
+ * Compares expected text to the text that would be in the sole range
2295
+ * (ie: [|...|]) in the file after applying the codefix sole codefix
2296
+ * in the source file.
2297
+ */
2298
+ public verifyRangeAfterCodeFix ( expectedText : string , includeWhiteSpace ?: boolean , errorCode ?: number , index ?: number ) {
2299
+ this . getAndApplyCodeActions ( errorCode , index ) ;
2300
+ this . verifyRangeIs ( expectedText , includeWhiteSpace ) ;
2301
+ }
2302
+
2294
2303
/**
2295
2304
* Applies fixes for the errors in fileName and compares the results to
2296
2305
* expectedContents after all fixes have been applied.
@@ -2303,7 +2312,7 @@ namespace FourSlash {
2303
2312
public verifyFileAfterCodeFix ( expectedContents : string , fileName ?: string ) {
2304
2313
fileName = fileName ? fileName : this . activeFile . fileName ;
2305
2314
2306
- this . applyCodeAction ( fileName , this . getCodeFixActions ( fileName ) ) ;
2315
+ this . applyCodeActions ( this . getCodeFixActions ( fileName ) ) ;
2307
2316
2308
2317
const actualContents : string = this . getFileContent ( fileName ) ;
2309
2318
if ( this . removeWhitespace ( actualContents ) !== this . removeWhitespace ( expectedContents ) ) {
@@ -2341,11 +2350,10 @@ namespace FourSlash {
2341
2350
return actions ;
2342
2351
}
2343
2352
2344
- private applyCodeAction ( fileName : string , actions : ts . CodeAction [ ] , index ?: number ) : void {
2353
+ private applyCodeActions ( actions : ts . CodeAction [ ] , index ?: number ) : void {
2345
2354
if ( index === undefined ) {
2346
2355
if ( ! ( actions && actions . length === 1 ) ) {
2347
- const actionText = ( actions && actions . length ) ? JSON . stringify ( actions ) : "none" ;
2348
- this . raiseError ( `Should find exactly one codefix, but found ${ actionText } ` ) ;
2356
+ this . raiseError ( `Should find exactly one codefix, but ${ actions ? actions . length : "none" } found.` ) ;
2349
2357
}
2350
2358
index = 0 ;
2351
2359
}
@@ -2355,12 +2363,11 @@ namespace FourSlash {
2355
2363
}
2356
2364
}
2357
2365
2358
- const fileChanges = ts . find ( actions [ index ] . changes , change => change . fileName === fileName ) ;
2359
- if ( ! fileChanges ) {
2360
- this . raiseError ( "The CodeFix found doesn't provide any changes in this file." ) ;
2361
- }
2366
+ const changes = actions [ index ] . changes ;
2362
2367
2363
- this . applyEdits ( fileChanges . fileName , fileChanges . textChanges , /*isFormattingEdit*/ false ) ;
2368
+ for ( const change of changes ) {
2369
+ this . applyEdits ( change . fileName , change . textChanges , /*isFormattingEdit*/ false ) ;
2370
+ }
2364
2371
}
2365
2372
2366
2373
public verifyImportFixAtPosition ( expectedTextArray : string [ ] , errorCode ?: number ) {
@@ -2748,7 +2755,7 @@ namespace FourSlash {
2748
2755
2749
2756
const codeActions = this . languageService . getRefactorCodeActions ( this . activeFile . fileName , formattingOptions , markerPos , refactorNameToApply ) ;
2750
2757
2751
- this . applyCodeAction ( this . activeFile . fileName , codeActions ) ;
2758
+ this . applyCodeActions ( codeActions ) ;
2752
2759
const actualContent = this . getFileContent ( this . activeFile . fileName ) ;
2753
2760
2754
2761
if ( this . normalizeNewlines ( actualContent ) !== this . normalizeNewlines ( expectedContent ) ) {
@@ -3795,6 +3802,14 @@ namespace FourSlashInterface {
3795
3802
this . state . verifyFileAfterApplyingRefactorAtMarker ( markerName , expectedContent , refactorNameToApply , formattingOptions ) ;
3796
3803
}
3797
3804
3805
+ public rangeIs ( expectedText : string , includeWhiteSpace ?: boolean ) : void {
3806
+ this . state . verifyRangeIs ( expectedText , includeWhiteSpace ) ;
3807
+ }
3808
+
3809
+ public getAndApplyCodeFix ( errorCode ?: number , index ?: number ) : void {
3810
+ this . state . getAndApplyCodeActions ( errorCode , index ) ;
3811
+ }
3812
+
3798
3813
public importFixAtPosition ( expectedTextArray : string [ ] , errorCode ?: number ) : void {
3799
3814
this . state . verifyImportFixAtPosition ( expectedTextArray , errorCode ) ;
3800
3815
}
0 commit comments