@@ -2422,7 +2422,20 @@ Actual: ${stringify(fullActual)}`);
24222422 */
24232423 public getAndApplyCodeActions ( errorCode ?: number , index ?: number ) {
24242424 const fileName = this . activeFile . fileName ;
2425- this . applyCodeActions ( this . getCodeFixes ( fileName , errorCode ) , index ) ;
2425+ const fixes = this . getCodeFixes ( fileName , errorCode ) ;
2426+ if ( index === undefined ) {
2427+ if ( ! ( fixes && fixes . length === 1 ) ) {
2428+ this . raiseError ( `Should find exactly one codefix, but ${ fixes ? fixes . length : "none" } found. ${ fixes ? fixes . map ( a => `${ Harness . IO . newLine ( ) } "${ a . description } "` ) : "" } ` ) ;
2429+ }
2430+ index = 0 ;
2431+ }
2432+ else {
2433+ if ( ! ( fixes && fixes . length >= index + 1 ) ) {
2434+ this . raiseError ( `Should find at least ${ index + 1 } codefix(es), but ${ fixes ? fixes . length : "none" } found.` ) ;
2435+ }
2436+ }
2437+
2438+ this . applyChanges ( fixes [ index ] . changes ) ;
24262439 }
24272440
24282441 public applyCodeActionFromCompletion ( markerName : string , options : FourSlashInterface . VerifyCompletionActionOptions ) {
@@ -2433,12 +2446,12 @@ Actual: ${stringify(fullActual)}`);
24332446 if ( codeActions . length !== 1 ) {
24342447 this . raiseError ( `Expected one code action, got ${ codeActions . length } ` ) ;
24352448 }
2449+ const codeAction = ts . first ( codeActions ) ;
24362450
2437- if ( codeActions [ 0 ] . description !== options . description ) {
2451+ if ( codeAction . description !== options . description ) {
24382452 this . raiseError ( `Expected description to be:\n${ options . description } \ngot:\n${ codeActions [ 0 ] . description } ` ) ;
24392453 }
2440-
2441- this . applyCodeActions ( codeActions ) ;
2454+ this . applyChanges ( codeAction . changes ) ;
24422455
24432456 this . verifyNewContentAfterChange ( options , ts . flatMap ( codeActions , a => a . changes . map ( c => c . fileName ) ) ) ;
24442457 }
@@ -2483,26 +2496,6 @@ Actual: ${stringify(fullActual)}`);
24832496 this . verifyNewContent ( { newFileContent } , changes ) ;
24842497 }
24852498
2486- /**
2487- * Applies fixes for the errors in fileName and compares the results to
2488- * expectedContents after all fixes have been applied.
2489- *
2490- * Note: applying one codefix may generate another (eg: remove duplicate implements
2491- * may generate an extends -> interface conversion fix).
2492- * @param expectedContents The contents of the file after the fixes are applied.
2493- * @param fileName The file to check. If not supplied, the current open file is used.
2494- */
2495- public verifyFileAfterCodeFix ( expectedContents : string , fileName ?: string , index ?: number ) {
2496- fileName = fileName ? fileName : this . activeFile . fileName ;
2497-
2498- this . applyCodeActions ( this . getCodeFixes ( fileName ) , index ) ;
2499-
2500- const actualContents : string = this . getFileContent ( fileName ) ;
2501- if ( this . removeWhitespace ( actualContents ) !== this . removeWhitespace ( expectedContents ) ) {
2502- this . raiseError ( `Actual text doesn't match expected text. Actual:\n${ actualContents } \n\nExpected:\n${ expectedContents } ` ) ;
2503- }
2504- }
2505-
25062499 public verifyCodeFix ( options : FourSlashInterface . VerifyCodeFixOptions ) {
25072500 const fileName = this . activeFile . fileName ;
25082501 const actions = this . getCodeFixes ( fileName , options . errorCode , options . preferences ) ;
@@ -2607,22 +2600,6 @@ Actual: ${stringify(fullActual)}`);
26072600 } ) ;
26082601 }
26092602
2610- private applyCodeActions ( actions : ReadonlyArray < ts . CodeAction > , index ?: number ) : void {
2611- if ( index === undefined ) {
2612- if ( ! ( actions && actions . length === 1 ) ) {
2613- this . raiseError ( `Should find exactly one codefix, but ${ actions ? actions . length : "none" } found. ${ actions ? actions . map ( a => `${ Harness . IO . newLine ( ) } "${ a . description } "` ) : "" } ` ) ;
2614- }
2615- index = 0 ;
2616- }
2617- else {
2618- if ( ! ( actions && actions . length >= index + 1 ) ) {
2619- this . raiseError ( `Should find at least ${ index + 1 } codefix(es), but ${ actions ? actions . length : "none" } found.` ) ;
2620- }
2621- }
2622-
2623- this . applyChanges ( actions [ index ] . changes ) ;
2624- }
2625-
26262603 private applyChanges ( changes : ReadonlyArray < ts . FileTextChanges > ) : void {
26272604 for ( const change of changes ) {
26282605 this . applyEdits ( change . fileName , change . textChanges , /*isFormattingEdit*/ false ) ;
@@ -4364,10 +4341,6 @@ namespace FourSlashInterface {
43644341 this . state . verifyRangeAfterCodeFix ( expectedText , includeWhiteSpace , errorCode , index ) ;
43654342 }
43664343
4367- public fileAfterCodeFix ( expectedContents : string , fileName ?: string , index ?: number ) {
4368- this . state . verifyFileAfterCodeFix ( expectedContents , fileName , index ) ;
4369- }
4370-
43714344 public codeFixAll ( options : VerifyCodeFixAllOptions ) : void {
43724345 this . state . verifyCodeFixAll ( options ) ;
43734346 }
0 commit comments