Skip to content

Commit c179d9a

Browse files
authored
Merge pull request #16045 from aozgaa/codeFixAddMissingMethod
Code fix add missing method
2 parents 1f3c2b3 + 40f22ec commit c179d9a

14 files changed

+370
-96
lines changed

src/compiler/checker.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2291,7 +2291,7 @@ namespace ts {
22912291

22922292
function typeToString(type: Type, enclosingDeclaration?: Node, flags?: TypeFormatFlags): string {
22932293
const typeNode = nodeBuilder.typeToTypeNode(type, enclosingDeclaration, toNodeBuilderFlags(flags) | NodeBuilderFlags.IgnoreErrors | NodeBuilderFlags.WriteTypeParametersInQualifiedName);
2294-
Debug.assert(typeNode !== undefined, "should always get typenode?");
2294+
Debug.assert(typeNode !== undefined, "should always get typenode");
22952295
const options = { removeComments: true };
22962296
const writer = createTextWriter("");
22972297
const printer = createPrinter(options);

src/compiler/diagnosticMessages.json

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3559,11 +3559,11 @@
35593559
"category": "Message",
35603560
"code": 90015
35613561
},
3562-
"Add declaration for missing property '{0}'.": {
3562+
"Declare property '{0}'.": {
35633563
"category": "Message",
35643564
"code": 90016
35653565
},
3566-
"Add index signature for missing property '{0}'.": {
3566+
"Add index signature for property '{0}'.": {
35673567
"category": "Message",
35683568
"code": 90017
35693569
},
@@ -3587,7 +3587,15 @@
35873587
"category": "Message",
35883588
"code": 90022
35893589
},
3590-
3590+
"Declare method '{0}'.": {
3591+
"category": "Message",
3592+
"code": 90023
3593+
},
3594+
"Declare static method '{0}'.": {
3595+
"category": "Message",
3596+
"code": 90024
3597+
},
3598+
35913599
"Convert function to an ES2015 class": {
35923600
"category": "Message",
35933601
"code": 95001

src/compiler/types.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2535,7 +2535,6 @@ namespace ts {
25352535
getNonNullableType(type: Type): Type;
25362536

25372537
/** Note that the resulting nodes cannot be checked. */
2538-
25392538
typeToTypeNode(type: Type, enclosingDeclaration?: Node, flags?: NodeBuilderFlags): TypeNode;
25402539
/** Note that the resulting nodes cannot be checked. */
25412540
signatureToSignatureDeclaration(signature: Signature, kind: SyntaxKind, enclosingDeclaration?: Node, flags?: NodeBuilderFlags): SignatureDeclaration;

src/harness/fourslash.ts

Lines changed: 36 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -2263,23 +2263,22 @@ namespace FourSlash {
22632263
}
22642264

22652265
/**
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.
22722270
*/
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) {
22742277
const ranges = this.getRanges();
22752278
if (ranges.length !== 1) {
22762279
this.raiseError("Exactly one range should be specified in the testfile.");
22772280
}
22782281

2279-
const fileName = this.activeFile.fileName;
2280-
2281-
this.applyCodeAction(fileName, this.getCodeFixActions(fileName, errorCode), index);
2282-
22832282
const actualText = this.rangeText(ranges[0]);
22842283

22852284
const result = includeWhiteSpace
@@ -2291,6 +2290,16 @@ namespace FourSlash {
22912290
}
22922291
}
22932292

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+
22942303
/**
22952304
* Applies fixes for the errors in fileName and compares the results to
22962305
* expectedContents after all fixes have been applied.
@@ -2303,7 +2312,7 @@ namespace FourSlash {
23032312
public verifyFileAfterCodeFix(expectedContents: string, fileName?: string) {
23042313
fileName = fileName ? fileName : this.activeFile.fileName;
23052314

2306-
this.applyCodeAction(fileName, this.getCodeFixActions(fileName));
2315+
this.applyCodeActions(this.getCodeFixActions(fileName));
23072316

23082317
const actualContents: string = this.getFileContent(fileName);
23092318
if (this.removeWhitespace(actualContents) !== this.removeWhitespace(expectedContents)) {
@@ -2341,11 +2350,10 @@ namespace FourSlash {
23412350
return actions;
23422351
}
23432352

2344-
private applyCodeAction(fileName: string, actions: ts.CodeAction[], index?: number): void {
2353+
private applyCodeActions(actions: ts.CodeAction[], index?: number): void {
23452354
if (index === undefined) {
23462355
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.`);
23492357
}
23502358
index = 0;
23512359
}
@@ -2355,12 +2363,11 @@ namespace FourSlash {
23552363
}
23562364
}
23572365

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;
23622367

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+
}
23642371
}
23652372

23662373
public verifyImportFixAtPosition(expectedTextArray: string[], errorCode?: number) {
@@ -2748,7 +2755,7 @@ namespace FourSlash {
27482755

27492756
const codeActions = this.languageService.getRefactorCodeActions(this.activeFile.fileName, formattingOptions, markerPos, refactorNameToApply);
27502757

2751-
this.applyCodeAction(this.activeFile.fileName, codeActions);
2758+
this.applyCodeActions(codeActions);
27522759
const actualContent = this.getFileContent(this.activeFile.fileName);
27532760

27542761
if (this.normalizeNewlines(actualContent) !== this.normalizeNewlines(expectedContent)) {
@@ -3795,6 +3802,14 @@ namespace FourSlashInterface {
37953802
this.state.verifyFileAfterApplyingRefactorAtMarker(markerName, expectedContent, refactorNameToApply, formattingOptions);
37963803
}
37973804

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+
37983813
public importFixAtPosition(expectedTextArray: string[], errorCode?: number): void {
37993814
this.state.verifyImportFixAtPosition(expectedTextArray, errorCode);
38003815
}

0 commit comments

Comments
 (0)