@@ -92,18 +92,6 @@ namespace FourSlash {
92
92
end : number ;
93
93
}
94
94
95
- export interface CodeFixIdentifier {
96
- /**
97
- * Error code to search over for codefix.
98
- */
99
- code : number ;
100
- /**
101
- * In a file where there is more than one error with code `code`, `count` refers
102
- * to which 0-indexed codefix, sorted by order of occurence, to consider.
103
- */
104
- count : number ;
105
- }
106
-
107
95
export import IndentStyle = ts . IndentStyle ;
108
96
109
97
const entityMap = ts . createMap ( {
@@ -1609,12 +1597,6 @@ namespace FourSlash {
1609
1597
return runningOffset ;
1610
1598
}
1611
1599
1612
- private applyCodeAction ( action : ts . CodeAction ) : void {
1613
- for ( const filechange of action . changes ) {
1614
- this . applyEdits ( filechange . fileName , filechange . textChanges , /*isFormattingEdit*/ false ) ;
1615
- }
1616
- }
1617
-
1618
1600
public copyFormatOptions ( ) : ts . FormatCodeSettings {
1619
1601
return ts . clone ( this . formatCodeSettings ) ;
1620
1602
}
@@ -2034,31 +2016,22 @@ namespace FourSlash {
2034
2016
2035
2017
/**
2036
2018
* Compares expected text to the text that would be in the sole range
2037
- * (ie: [|...|]) in the file after applying the codefix corresponding
2038
- * to the error with errorCode, or of the sole error in the source file.
2019
+ * (ie: [|...|]) in the file after applying the codefix sole codefix
2020
+ * in the source file.
2039
2021
*
2040
2022
* Because codefixes are only applied on the working file, it is unsafe
2041
2023
* to apply this more than once (consider a refactoring across files).
2042
2024
*/
2043
- public verifyRangeAfterCodeFix ( expectedText : string , codeFixIdentifier ?: CodeFixIdentifier ) {
2025
+ public verifyRangeAfterCodeFix ( expectedText : string ) {
2044
2026
const ranges = this . getRanges ( ) ;
2045
2027
if ( ranges . length !== 1 ) {
2046
2028
this . raiseError ( "Exactly one range should be specified in the testfile." ) ;
2047
2029
}
2048
2030
2049
2031
const fileName = this . activeFile . fileName ;
2050
- const codeFix : ts . CodeAction = this . getCodeFix ( fileName , codeFixIdentifier ) ;
2051
2032
2052
- if ( ! codeFix ) {
2053
- this . raiseError ( "Should find exactly one codefix, but none found." ) ;
2054
- }
2055
-
2056
- const fileChange = ts . find ( codeFix . changes , change => change . fileName === fileName ) ;
2057
- if ( ! fileChange ) {
2058
- this . raiseError ( "CodeFix found doesn't provide any changes in this file." ) ;
2059
- }
2033
+ this . applyCodeFixActions ( fileName , this . getCodeFixActions ( fileName ) ) ;
2060
2034
2061
- this . applyEdits ( fileChange . fileName , fileChange . textChanges , /*isFormattingEdit*/ false ) ;
2062
2035
const actualText = this . rangeText ( ranges [ 0 ] ) ;
2063
2036
2064
2037
if ( this . removeWhitespace ( actualText ) !== this . removeWhitespace ( expectedText ) ) {
@@ -2069,33 +2042,16 @@ namespace FourSlash {
2069
2042
/**
2070
2043
* Applies fixes for the errors in fileName and compares the results to
2071
2044
* expectedContents after all fixes have been applied.
2072
- *
2073
- * It is safe to apply this multiple times in a single test.
2074
- *
2045
+
2075
2046
* Note: applying one codefix may generate another (eg: remove duplicate implements
2076
2047
* may generate an extends -> interface conversion fix).
2077
2048
* @param expectedContents The contents of the file after the fixes are applied.
2078
2049
* @param fileName The file to check. If not supplied, the current open file is used.
2079
- * @param errorsToFix An array of errors for which quickfixes will be applied. If not
2080
- * supplied, all codefixes in the file are applied until none are left, starting from
2081
- * the first available codefix.
2082
- *
2083
2050
*/
2084
- public verifyFileAfterCodeFix ( expectedContents : string , fileName ?: string , codeFixIdentifier ?: CodeFixIdentifier ) {
2051
+ public verifyFileAfterCodeFix ( expectedContents : string , fileName ?: string ) {
2085
2052
fileName = fileName ? fileName : this . activeFile . fileName ;
2086
2053
2087
- const codeFix = this . getCodeFix ( fileName , codeFixIdentifier ) ;
2088
-
2089
- if ( codeFix === undefined ) {
2090
- if ( codeFixIdentifier ) {
2091
- this . raiseError ( `Couldn't find the ${ codeFixIdentifier . count } 'th error with code ${ codeFixIdentifier . code } .` ) ;
2092
- }
2093
- else {
2094
- this . raiseError ( "No code fix could be found." ) ;
2095
- }
2096
- }
2097
-
2098
- this . applyCodeAction ( codeFix ) ;
2054
+ this . applyCodeFixActions ( fileName , this . getCodeFixActions ( fileName ) ) ;
2099
2055
2100
2056
const actualContents : string = this . getFileContent ( fileName ) ;
2101
2057
if ( this . removeWhitespace ( actualContents ) !== this . removeWhitespace ( expectedContents ) ) {
@@ -2106,30 +2062,31 @@ namespace FourSlash {
2106
2062
/**
2107
2063
* Rerieves a codefix satisfying the parameters, or undefined if no such codefix is found.
2108
2064
* @param fileName Path to file where error should be retrieved from.
2109
- * @param error We get the `error.count`'th codefix with code `error.code`.
2110
- *
2111
- * If undefined, we get the first codefix available.
2112
2065
*/
2113
- private getCodeFix ( fileName : string , error ?: CodeFixIdentifier ) : ts . CodeAction | undefined {
2066
+ private getCodeFixActions ( fileName : string ) : ts . CodeAction [ ] {
2114
2067
const diagnostics : ts . Diagnostic [ ] = this . getDiagnostics ( fileName ) ;
2115
- const errorCount = error ? error . count : 0 ;
2116
2068
2117
- let countSeen = 0 ;
2069
+ let actions : ts . CodeAction [ ] = undefined ;
2118
2070
for ( const diagnostic of diagnostics ) {
2119
- if ( error && error . code !== diagnostic . code ) {
2120
- continue ;
2121
- }
2122
- const action = this . languageService . getCodeFixesAtPosition ( fileName , diagnostic . start , diagnostic . length , [ diagnostic . code ] ) ;
2123
- if ( action ) {
2124
- if ( action . length > errorCount - countSeen ) {
2125
- return action [ errorCount - countSeen ] ;
2126
- }
2127
- else {
2128
- countSeen += action . length ;
2129
- }
2071
+ const newActions = this . languageService . getCodeFixesAtPosition ( fileName , diagnostic . start , diagnostic . length , [ diagnostic . code ] ) ;
2072
+ if ( newActions && newActions . length ) {
2073
+ actions = actions ? actions . concat ( newActions ) : newActions ;
2130
2074
}
2131
2075
}
2132
- return undefined ;
2076
+ return actions ;
2077
+ }
2078
+
2079
+ private applyCodeFixActions ( fileName : string , actions : ts . CodeAction [ ] ) : void {
2080
+ if ( ! ( actions && actions . length === 1 ) ) {
2081
+ this . raiseError ( `Should find exactly one codefix, but ${ actions ? actions . length : "none" } found.` ) ;
2082
+ }
2083
+
2084
+ const fileChanges = ts . find ( actions [ 0 ] . changes , change => change . fileName === fileName ) ;
2085
+ if ( ! fileChanges ) {
2086
+ this . raiseError ( "The CodeFix found doesn't provide any changes in this file." ) ;
2087
+ }
2088
+
2089
+ this . applyEdits ( fileChanges . fileName , fileChanges . textChanges , /*isFormattingEdit*/ false ) ;
2133
2090
}
2134
2091
2135
2092
public verifyDocCommentTemplate ( expected ?: ts . TextInsertion ) {
@@ -2404,8 +2361,8 @@ namespace FourSlash {
2404
2361
}
2405
2362
}
2406
2363
2407
- public verifyCodeFixAvailable ( negative : boolean , errorCode ?: number ) {
2408
- const codeFix = this . getCodeFix ( this . activeFile . fileName , errorCode ? { code : errorCode , count : 0 } : undefined ) ;
2364
+ public verifyCodeFixAvailable ( negative : boolean ) {
2365
+ const codeFix = this . getCodeFixActions ( this . activeFile . fileName ) ;
2409
2366
2410
2367
if ( negative && codeFix ) {
2411
2368
this . raiseError ( `verifyCodeFixAvailable failed - expected no fixes but found one.` ) ;
@@ -3199,8 +3156,8 @@ namespace FourSlashInterface {
3199
3156
this . state . verifyBraceCompletionAtPosition ( this . negative , openingBrace ) ;
3200
3157
}
3201
3158
3202
- public codeFixAvailable ( errorCode ?: number ) {
3203
- this . state . verifyCodeFixAvailable ( this . negative , errorCode ) ;
3159
+ public codeFixAvailable ( ) {
3160
+ this . state . verifyCodeFixAvailable ( this . negative ) ;
3204
3161
}
3205
3162
}
3206
3163
@@ -3385,12 +3342,12 @@ namespace FourSlashInterface {
3385
3342
this . DocCommentTemplate ( /*expectedText*/ undefined , /*expectedOffset*/ undefined , /*empty*/ true ) ;
3386
3343
}
3387
3344
3388
- public rangeAfterCodeFix ( expectedText : string , codeFixidentifier ?: FourSlash . CodeFixIdentifier ) : void {
3389
- this . state . verifyRangeAfterCodeFix ( expectedText , codeFixidentifier ) ;
3345
+ public rangeAfterCodeFix ( expectedText : string ) : void {
3346
+ this . state . verifyRangeAfterCodeFix ( expectedText ) ;
3390
3347
}
3391
3348
3392
- public fileAfterCodeFix ( expectedContents : string , fileName ?: string , codeFixidentifier ?: FourSlash . CodeFixIdentifier ) : void {
3393
- this . state . verifyFileAfterCodeFix ( expectedContents , fileName , codeFixidentifier ) ;
3349
+ public fileAfterCodeFix ( expectedContents : string , fileName ?: string ) : void {
3350
+ this . state . verifyFileAfterCodeFix ( expectedContents , fileName ) ;
3394
3351
}
3395
3352
3396
3353
public navigationBar ( json : any ) {
0 commit comments