From 39eb187e5c530ac1124bfeefd7c2c811493e55de Mon Sep 17 00:00:00 2001 From: Daniel Rosenwasser Date: Fri, 11 Nov 2016 01:22:06 -0800 Subject: [PATCH 1/3] Added diagnostic. --- src/compiler/diagnosticMessages.json | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/compiler/diagnosticMessages.json b/src/compiler/diagnosticMessages.json index 9a353e6fb36..1ae59b61acc 100644 --- a/src/compiler/diagnosticMessages.json +++ b/src/compiler/diagnosticMessages.json @@ -3159,6 +3159,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 From da356b075a4829e616bebe26196c5944ac9393c9 Mon Sep 17 00:00:00 2001 From: Daniel Rosenwasser Date: Fri, 11 Nov 2016 01:25:50 -0800 Subject: [PATCH 2/3] Added tests for missing 'this' property accesses. --- tests/cases/fourslash/codeFixAddForgottenThis01.ts | 10 ++++++++++ tests/cases/fourslash/codeFixAddForgottenThis02.ts | 9 +++++++++ 2 files changed, 19 insertions(+) create mode 100644 tests/cases/fourslash/codeFixAddForgottenThis01.ts create mode 100644 tests/cases/fourslash/codeFixAddForgottenThis02.ts diff --git a/tests/cases/fourslash/codeFixAddForgottenThis01.ts b/tests/cases/fourslash/codeFixAddForgottenThis01.ts new file mode 100644 index 00000000000..90d191b6ba5 --- /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 00000000000..b387f349073 --- /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 From f2c0aa4444db61ee3870c6d3dc7b8cb74cc44e05 Mon Sep 17 00:00:00 2001 From: Daniel Rosenwasser Date: Fri, 11 Nov 2016 01:27:26 -0800 Subject: [PATCH 3/3] Added fix for missing 'this.' property accesses. --- .../codefixes/fixForgottenThisPropertyAccess.ts | 16 ++++++++++++++++ src/services/codefixes/fixes.ts | 3 ++- src/services/tsconfig.json | 3 ++- 3 files changed, 20 insertions(+), 2 deletions(-) create mode 100644 src/services/codefixes/fixForgottenThisPropertyAccess.ts diff --git a/src/services/codefixes/fixForgottenThisPropertyAccess.ts b/src/services/codefixes/fixForgottenThisPropertyAccess.ts new file mode 100644 index 00000000000..c9d59dd8337 --- /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 982b6abc84b..da51f04ff8c 100644 --- a/src/services/codefixes/fixes.ts +++ b/src/services/codefixes/fixes.ts @@ -3,4 +3,5 @@ /// /// /// -/// \ No newline at end of file +/// +/// \ No newline at end of file diff --git a/src/services/tsconfig.json b/src/services/tsconfig.json index 69efe29ba39..699ffcd7612 100644 --- a/src/services/tsconfig.json +++ b/src/services/tsconfig.json @@ -91,6 +91,7 @@ "codeFixes/fixClassIncorrectlyImplementsInterface.ts", "codeFixes/fixClassDoesntImplementInheritedAbstractMember.ts", "codeFixes/fixClassSuperMustPrecedeThisAccess.ts", - "codeFixes/fixConstructorForDerivedNeedSuperCall.ts" + "codeFixes/fixConstructorForDerivedNeedSuperCall.ts", + "codefixes/fixForgottenThisPropertyAccess.ts" ] }