Merge pull request #13759 from Microsoft/fixMissingThis
Quick fix to add missing 'this.' for property accesses
This commit is contained in:
commit
d38bc54002
6 changed files with 41 additions and 1 deletions
|
@ -3267,6 +3267,10 @@
|
|||
"category": "Message",
|
||||
"code": 90007
|
||||
},
|
||||
"Add 'this.' to unresolved variable.": {
|
||||
"category": "Message",
|
||||
"code": 90008
|
||||
},
|
||||
"Adding a tsconfig.json file will help organize projects that contain both TypeScript and JavaScript files. Learn more at https://aka.ms/tsconfig": {
|
||||
"category": "Error",
|
||||
"code": 90009
|
||||
|
|
16
src/services/codefixes/fixForgottenThisPropertyAccess.ts
Normal file
16
src/services/codefixes/fixForgottenThisPropertyAccess.ts
Normal file
|
@ -0,0 +1,16 @@
|
|||
/* @internal */
|
||||
namespace ts.codefix {
|
||||
registerCodeFix({
|
||||
errorCodes: [Diagnostics.Cannot_find_name_0_Did_you_mean_the_instance_member_this_0.code],
|
||||
getCodeActions: (context: CodeFixContext) => {
|
||||
const sourceFile = context.sourceFile;
|
||||
const token = getTokenAtPosition(sourceFile, context.span.start);
|
||||
const start = token.getStart(sourceFile);
|
||||
|
||||
return [{
|
||||
description: getLocaleSpecificMessage(Diagnostics.Add_this_to_unresolved_variable),
|
||||
changes: [{ fileName: sourceFile.fileName, textChanges: [{ newText: "this.", span: { start, length: 0 } }] }]
|
||||
}];
|
||||
}
|
||||
});
|
||||
}
|
|
@ -3,7 +3,7 @@
|
|||
/// <reference path="fixClassSuperMustPrecedeThisAccess.ts" />
|
||||
/// <reference path="fixConstructorForDerivedNeedSuperCall.ts" />
|
||||
/// <reference path="fixExtendsInterfaceBecomesImplements.ts" />
|
||||
/// <reference path="fixForgottenThisPropertyAccess.ts" />
|
||||
/// <reference path='unusedIdentifierFixes.ts' />
|
||||
/// <reference path='importFixes.ts' />
|
||||
/// <reference path='helpers.ts' />
|
||||
|
||||
|
|
|
@ -83,6 +83,7 @@
|
|||
"codefixes/fixClassDoesntImplementInheritedAbstractMember.ts",
|
||||
"codefixes/fixClassSuperMustPrecedeThisAccess.ts",
|
||||
"codefixes/fixConstructorForDerivedNeedSuperCall.ts",
|
||||
"codefixes/fixForgottenThisPropertyAccess.ts",
|
||||
"codefixes/fixes.ts",
|
||||
"codefixes/helpers.ts",
|
||||
"codefixes/importFixes.ts",
|
||||
|
|
10
tests/cases/fourslash/codeFixAddForgottenThis01.ts
Normal file
10
tests/cases/fourslash/codeFixAddForgottenThis01.ts
Normal file
|
@ -0,0 +1,10 @@
|
|||
/// <reference path='fourslash.ts' />
|
||||
|
||||
////class C {
|
||||
//// foo: number;
|
||||
//// constructor() {
|
||||
//// [|foo = 10|];
|
||||
//// }
|
||||
////}
|
||||
|
||||
verify.rangeAfterCodeFix("this.foo = 10");
|
9
tests/cases/fourslash/codeFixAddForgottenThis02.ts
Normal file
9
tests/cases/fourslash/codeFixAddForgottenThis02.ts
Normal file
|
@ -0,0 +1,9 @@
|
|||
/// <reference path='fourslash.ts' />
|
||||
|
||||
////class C {
|
||||
//// constructor(public foo) {
|
||||
//// }
|
||||
//// bar() { [|foo = 10|] };
|
||||
////}
|
||||
|
||||
verify.rangeAfterCodeFix("this.foo = 10");
|
Loading…
Reference in a new issue