diff --git a/src/compiler/diagnosticMessages.json b/src/compiler/diagnosticMessages.json index 43a9cce4de6e6..5fe479959e6a2 100644 --- a/src/compiler/diagnosticMessages.json +++ b/src/compiler/diagnosticMessages.json @@ -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 diff --git a/src/services/codefixes/fixForgottenThisPropertyAccess.ts b/src/services/codefixes/fixForgottenThisPropertyAccess.ts new file mode 100644 index 0000000000000..c9d59dd8337b0 --- /dev/null +++ b/src/services/codefixes/fixForgottenThisPropertyAccess.ts @@ -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 } }] }] + }]; + } + }); +} \ No newline at end of file diff --git a/src/services/codefixes/fixes.ts b/src/services/codefixes/fixes.ts index 10d6fa5015898..3bd173e04f604 100644 --- a/src/services/codefixes/fixes.ts +++ b/src/services/codefixes/fixes.ts @@ -3,7 +3,7 @@ /// /// /// +/// /// /// /// - diff --git a/src/services/tsconfig.json b/src/services/tsconfig.json index b60e00292f51f..b4e8289f367bd 100644 --- a/src/services/tsconfig.json +++ b/src/services/tsconfig.json @@ -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", diff --git a/tests/cases/fourslash/codeFixAddForgottenThis01.ts b/tests/cases/fourslash/codeFixAddForgottenThis01.ts new file mode 100644 index 0000000000000..90d191b6ba5d0 --- /dev/null +++ b/tests/cases/fourslash/codeFixAddForgottenThis01.ts @@ -0,0 +1,10 @@ +/// + +////class C { +//// foo: number; +//// constructor() { +//// [|foo = 10|]; +//// } +////} + +verify.rangeAfterCodeFix("this.foo = 10"); \ No newline at end of file diff --git a/tests/cases/fourslash/codeFixAddForgottenThis02.ts b/tests/cases/fourslash/codeFixAddForgottenThis02.ts new file mode 100644 index 0000000000000..b387f34907303 --- /dev/null +++ b/tests/cases/fourslash/codeFixAddForgottenThis02.ts @@ -0,0 +1,9 @@ +/// + +////class C { +//// constructor(public foo) { +//// } +//// bar() { [|foo = 10|] }; +////} + +verify.rangeAfterCodeFix("this.foo = 10"); \ No newline at end of file