@@ -384,7 +384,7 @@ namespace FourSlash {
384384
385385 if ( exists !== negative ) {
386386 this . printErrorLog ( negative , this . getAllDiagnostics ( ) ) ;
387- throw new Error ( " Failure between markers: " + startMarkerName + ", " + endMarkerName ) ;
387+ throw new Error ( ` Failure between markers: ' ${ startMarkerName } ', ' ${ endMarkerName } '` ) ;
388388 }
389389 }
390390
@@ -638,7 +638,6 @@ namespace FourSlash {
638638 }
639639 }
640640
641-
642641 public verifyCompletionListAllowsNewIdentifier ( negative : boolean ) {
643642 const completions = this . getCompletionListAtCaret ( ) ;
644643
@@ -1479,7 +1478,7 @@ namespace FourSlash {
14791478 if ( isFormattingEdit ) {
14801479 const newContent = this . getFileContent ( fileName ) ;
14811480
1482- if ( newContent . replace ( / \s / g , "" ) !== oldContent . replace ( / \s / g , "" ) ) {
1481+ if ( this . removeWhitespace ( newContent ) !== this . removeWhitespace ( oldContent ) ) {
14831482 this . raiseError ( "Formatting operation destroyed non-whitespace content" ) ;
14841483 }
14851484 }
@@ -1545,6 +1544,10 @@ namespace FourSlash {
15451544 }
15461545 }
15471546
1547+ private removeWhitespace ( text : string ) : string {
1548+ return text . replace ( / \s / g, "" ) ;
1549+ }
1550+
15481551 public goToBOF ( ) {
15491552 this . goToPosition ( 0 ) ;
15501553 }
@@ -1862,6 +1865,44 @@ namespace FourSlash {
18621865 }
18631866 }
18641867
1868+ public verifyCodeFixAtPosition ( expectedText : string , errorCode ?: number ) {
1869+
1870+ const ranges = this . getRanges ( ) ;
1871+ if ( ranges . length == 0 ) {
1872+ this . raiseError ( "At least one range should be specified in the testfile." ) ;
1873+ }
1874+
1875+ const fileName = this . activeFile . fileName ;
1876+ const diagnostics = this . getDiagnostics ( fileName ) ;
1877+
1878+ if ( diagnostics . length === 0 ) {
1879+ this . raiseError ( "Errors expected." ) ;
1880+ }
1881+
1882+ if ( diagnostics . length > 1 && ! errorCode ) {
1883+ this . raiseError ( "When there's more than one error, you must specify the errror to fix." ) ;
1884+ }
1885+
1886+ const diagnostic = ! errorCode ? diagnostics [ 0 ] : ts . firstOrUndefined ( diagnostics , d => d . code == errorCode ) ;
1887+
1888+ const actual = this . languageService . getCodeFixesAtPosition ( fileName , diagnostic . start , diagnostic . length , [ `TS${ diagnostic . code } ` ] ) ;
1889+
1890+ if ( ! actual || actual . length == 0 ) {
1891+ this . raiseError ( "No codefixes returned." ) ;
1892+ }
1893+
1894+ if ( actual . length > 1 ) {
1895+ this . raiseError ( "More than 1 codefix returned." ) ;
1896+ }
1897+
1898+ this . applyEdits ( actual [ 0 ] . changes [ 0 ] . fileName , actual [ 0 ] . changes [ 0 ] . textChanges , /*isFormattingEdit*/ false ) ;
1899+ const actualText = this . rangeText ( ranges [ 0 ] ) ;
1900+
1901+ if ( this . removeWhitespace ( actualText ) !== this . removeWhitespace ( expectedText ) ) {
1902+ this . raiseError ( `Actual text doesn't match expected text. Actual: '${ actualText } ' Expected: '${ expectedText } '` ) ;
1903+ }
1904+ }
1905+
18651906 public verifyDocCommentTemplate ( expected ?: ts . TextInsertion ) {
18661907 const name = "verifyDocCommentTemplate" ;
18671908 const actual = this . languageService . getDocCommentTemplateAtPosition ( this . activeFile . fileName , this . currentCaretPosition ) ;
@@ -3066,6 +3107,10 @@ namespace FourSlashInterface {
30663107 this . DocCommentTemplate ( /*expectedText*/ undefined , /*expectedOffset*/ undefined , /*empty*/ true ) ;
30673108 }
30683109
3110+ public codeFixAtPosition ( expectedText : string , errorCode ?: number ) : void {
3111+ this . state . verifyCodeFixAtPosition ( expectedText , errorCode ) ;
3112+ }
3113+
30693114 public navigationBar ( json : any ) {
30703115 this . state . verifyNavigationBar ( json ) ;
30713116 }
0 commit comments