Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Quick fix to correct qualified names to indexed access types #17462

Merged
merged 5 commits into from
Jul 27, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions src/compiler/diagnosticMessages.json
Original file line number Diff line number Diff line change
Expand Up @@ -3657,6 +3657,10 @@
"category": "Message",
"code": 90025
},
"Rewrite as the indexed access type '{0}'.": {
"category": "Message",
"code": 90026
},

"Convert function to an ES2015 class": {
"category": "Message",
Expand Down
5 changes: 0 additions & 5 deletions src/harness/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -74,11 +74,6 @@
"../services/formatting/tokenRange.ts",
"../services/codeFixProvider.ts",
"../services/codefixes/fixes.ts",
"../services/codefixes/fixExtendsInterfaceBecomesImplements.ts",
"../services/codefixes/fixClassIncorrectlyImplementsInterface.ts",
"../services/codefixes/fixClassDoesntImplementInheritedAbstractMember.ts",
"../services/codefixes/fixClassSuperMustPrecedeThisAccess.ts",
"../services/codefixes/fixConstructorForDerivedNeedSuperCall.ts",
"../services/codefixes/helpers.ts",
"../services/codefixes/importFixes.ts",
"../services/codefixes/fixUnusedIdentifier.ts",
Expand Down
27 changes: 27 additions & 0 deletions src/services/codefixes/correctQualifiedNameToIndexedAccessType.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
/* @internal */
namespace ts.codefix {
registerCodeFix({
errorCodes: [Diagnostics.Cannot_access_0_1_because_0_is_a_type_but_not_a_namespace_Did_you_mean_to_retrieve_the_type_of_the_property_1_in_0_with_0_1.code],
getCodeActions: (context: CodeFixContext) => {
const sourceFile = context.sourceFile;
const token = getTokenAtPosition(sourceFile, context.span.start, /*includeJsDocComment*/ false);
const qualifiedName = getAncestor(token, SyntaxKind.QualifiedName) as QualifiedName;
Debug.assert(!!qualifiedName, "Expected position to be owned by a qualified name.");
if (!isIdentifier(qualifiedName.left)) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This example doesn't seem to trigger the right error, though we can apply the fix manually to fix the issue:

module M {
    export interface I {
         foo: string;
    }
}
let a: M.I.foo;

Please add a test.

return undefined;
}
const leftText = qualifiedName.left.getText(sourceFile);
const rightText = qualifiedName.right.getText(sourceFile);
const replacement = createIndexedAccessTypeNode(
createTypeReferenceNode(qualifiedName.left, /*typeArguments*/ undefined),
createLiteralTypeNode(createLiteral(rightText)));
const changeTracker = textChanges.ChangeTracker.fromCodeFixContext(context);
changeTracker.replaceNode(sourceFile, qualifiedName, replacement);

return [{
description: formatStringFromArgs(getLocaleSpecificMessage(Diagnostics.Rewrite_as_the_indexed_access_type_0), [`${leftText}["${rightText}"]`]),
changes: changeTracker.getChanges()
}];
}
});
}
1 change: 1 addition & 0 deletions src/services/codefixes/fixes.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/// <reference path="correctQualifiedNameToIndexedAccessType.ts" />
/// <reference path="fixClassIncorrectlyImplementsInterface.ts" />
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks like there is an analogous entry to this in src/harness/tsconfig.json. Not sure why it's there, or if we might want to remove it. Or alternatively, add a reference to correctQualifiedNameToIndexedAccessType.ts.

@weswigham do you know if building for tests will correctly trigger a rebuild when we make changes to correctQualifiedNameToIndexedAccessType.ts?

/// <reference path="fixAddMissingMember.ts" />
/// <reference path="fixSpelling.ts" />
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,18 @@
tests/cases/compiler/errorForUsingPropertyOfTypeAsType01.ts(6,12): error TS2713: Cannot access 'Foo.bar' because 'Foo' is a type, but not a namespace. Did you mean to retrieve the type of the property 'bar' in 'Foo' with 'Foo["bar"]'?
tests/cases/compiler/errorForUsingPropertyOfTypeAsType01.ts(14,12): error TS2503: Cannot find namespace 'Foo'.
tests/cases/compiler/errorForUsingPropertyOfTypeAsType01.ts(22,12): error TS2713: Cannot access 'Foo.bar' because 'Foo' is a type, but not a namespace. Did you mean to retrieve the type of the property 'bar' in 'Foo' with 'Foo["bar"]'?
tests/cases/compiler/errorForUsingPropertyOfTypeAsType01.ts(29,12): error TS2713: Cannot access 'Foo.bar' because 'Foo' is a type, but not a namespace. Did you mean to retrieve the type of the property 'bar' in 'Foo' with 'Foo["bar"]'?
tests/cases/compiler/errorForUsingPropertyOfTypeAsType01.ts(36,12): error TS2702: 'Foo' only refers to a type, but is being used as a namespace here.
tests/cases/compiler/errorForUsingPropertyOfTypeAsType01.ts(7,18): error TS2694: Namespace 'Test1' has no exported member 'Foo'.
tests/cases/compiler/errorForUsingPropertyOfTypeAsType01.ts(15,12): error TS2503: Cannot find namespace 'Foo'.
tests/cases/compiler/errorForUsingPropertyOfTypeAsType01.ts(16,18): error TS2694: Namespace 'Test2' has no exported member 'Foo'.
tests/cases/compiler/errorForUsingPropertyOfTypeAsType01.ts(24,12): error TS2713: Cannot access 'Foo.bar' because 'Foo' is a type, but not a namespace. Did you mean to retrieve the type of the property 'bar' in 'Foo' with 'Foo["bar"]'?
tests/cases/compiler/errorForUsingPropertyOfTypeAsType01.ts(25,18): error TS2694: Namespace 'Test3' has no exported member 'Foo'.
tests/cases/compiler/errorForUsingPropertyOfTypeAsType01.ts(32,12): error TS2713: Cannot access 'Foo.bar' because 'Foo' is a type, but not a namespace. Did you mean to retrieve the type of the property 'bar' in 'Foo' with 'Foo["bar"]'?
tests/cases/compiler/errorForUsingPropertyOfTypeAsType01.ts(33,18): error TS2694: Namespace 'Test4' has no exported member 'Foo'.
tests/cases/compiler/errorForUsingPropertyOfTypeAsType01.ts(40,12): error TS2702: 'Foo' only refers to a type, but is being used as a namespace here.
tests/cases/compiler/errorForUsingPropertyOfTypeAsType01.ts(41,18): error TS2694: Namespace 'Test5' has no exported member 'Foo'.
tests/cases/compiler/errorForUsingPropertyOfTypeAsType01.ts(44,20): error TS2694: Namespace 'Test5' has no exported member 'Foo'.
tests/cases/compiler/errorForUsingPropertyOfTypeAsType01.ts(44,24): error TS1003: Identifier expected.


==== tests/cases/compiler/errorForUsingPropertyOfTypeAsType01.ts (5 errors) ====
==== tests/cases/compiler/errorForUsingPropertyOfTypeAsType01.ts (12 errors) ====
namespace Test1 {
export interface Foo {
bar: string;
Expand All @@ -14,6 +21,9 @@ tests/cases/compiler/errorForUsingPropertyOfTypeAsType01.ts(36,12): error TS2702
var x: Foo.bar = "";
~~~~~~~
!!! error TS2713: Cannot access 'Foo.bar' because 'Foo' is a type, but not a namespace. Did you mean to retrieve the type of the property 'bar' in 'Foo' with 'Foo["bar"]'?
var y: Test1.Foo.bar = "";
~~~
!!! error TS2694: Namespace 'Test1' has no exported member 'Foo'.
}

namespace Test2 {
Expand All @@ -24,6 +34,9 @@ tests/cases/compiler/errorForUsingPropertyOfTypeAsType01.ts(36,12): error TS2702
var x: Foo.bar = "";
~~~
!!! error TS2503: Cannot find namespace 'Foo'.
var y: Test2.Foo.bar = "";
~~~
!!! error TS2694: Namespace 'Test2' has no exported member 'Foo'.
}

namespace Test3 {
Expand All @@ -34,6 +47,9 @@ tests/cases/compiler/errorForUsingPropertyOfTypeAsType01.ts(36,12): error TS2702
var x: Foo.bar = "";
~~~~~~~
!!! error TS2713: Cannot access 'Foo.bar' because 'Foo' is a type, but not a namespace. Did you mean to retrieve the type of the property 'bar' in 'Foo' with 'Foo["bar"]'?
var y: Test3.Foo.bar = "";
~~~
!!! error TS2694: Namespace 'Test3' has no exported member 'Foo'.
}

namespace Test4 {
Expand All @@ -43,6 +59,9 @@ tests/cases/compiler/errorForUsingPropertyOfTypeAsType01.ts(36,12): error TS2702
var x: Foo.bar = "";
~~~~~~~
!!! error TS2713: Cannot access 'Foo.bar' because 'Foo' is a type, but not a namespace. Did you mean to retrieve the type of the property 'bar' in 'Foo' with 'Foo["bar"]'?
var y: Test4.Foo.bar = "";
~~~
!!! error TS2694: Namespace 'Test4' has no exported member 'Foo'.
}

namespace Test5 {
Expand All @@ -52,4 +71,13 @@ tests/cases/compiler/errorForUsingPropertyOfTypeAsType01.ts(36,12): error TS2702
var x: Foo.bar = "";
~~~
!!! error TS2702: 'Foo' only refers to a type, but is being used as a namespace here.
}
var y: Test5.Foo.bar = "";
~~~
!!! error TS2694: Namespace 'Test5' has no exported member 'Foo'.
}

import lol = Test5.Foo.
~~~
!!! error TS2694: Namespace 'Test5' has no exported member 'Foo'.

!!! error TS1003: Identifier expected.
15 changes: 14 additions & 1 deletion tests/baselines/reference/errorForUsingPropertyOfTypeAsType01.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ namespace Test1 {
}

var x: Foo.bar = "";
var y: Test1.Foo.bar = "";
}

namespace Test2 {
Expand All @@ -13,6 +14,7 @@ namespace Test2 {
}

var x: Foo.bar = "";
var y: Test2.Foo.bar = "";
}

namespace Test3 {
Expand All @@ -21,26 +23,32 @@ namespace Test3 {
}

var x: Foo.bar = "";
var y: Test3.Foo.bar = "";
}

namespace Test4 {
export type Foo = { bar: number }
| { bar: string }

var x: Foo.bar = "";
var y: Test4.Foo.bar = "";
}

namespace Test5 {
export type Foo = { bar: number }
| { wat: string }

var x: Foo.bar = "";
}
var y: Test5.Foo.bar = "";
}

import lol = Test5.Foo.

//// [errorForUsingPropertyOfTypeAsType01.js]
var Test1;
(function (Test1) {
var x = "";
var y = "";
})(Test1 || (Test1 = {}));
var Test2;
(function (Test2) {
Expand All @@ -51,16 +59,21 @@ var Test2;
}());
Test2.Foo = Foo;
var x = "";
var y = "";
})(Test2 || (Test2 = {}));
var Test3;
(function (Test3) {
var x = "";
var y = "";
})(Test3 || (Test3 = {}));
var Test4;
(function (Test4) {
var x = "";
var y = "";
})(Test4 || (Test4 = {}));
var Test5;
(function (Test5) {
var x = "";
var y = "";
})(Test5 || (Test5 = {}));
var lol = Test5.Foo.;
9 changes: 8 additions & 1 deletion tests/cases/compiler/errorForUsingPropertyOfTypeAsType01.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ namespace Test1 {
}

var x: Foo.bar = "";
var y: Test1.Foo.bar = "";
}

namespace Test2 {
Expand All @@ -12,6 +13,7 @@ namespace Test2 {
}

var x: Foo.bar = "";
var y: Test2.Foo.bar = "";
}

namespace Test3 {
Expand All @@ -20,18 +22,23 @@ namespace Test3 {
}

var x: Foo.bar = "";
var y: Test3.Foo.bar = "";
}

namespace Test4 {
export type Foo = { bar: number }
| { bar: string }

var x: Foo.bar = "";
var y: Test4.Foo.bar = "";
}

namespace Test5 {
export type Foo = { bar: number }
| { wat: string }

var x: Foo.bar = "";
}
var y: Test5.Foo.bar = "";
}

import lol = Test5.Foo.
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
/// <reference path='fourslash.ts' />


//// namespace Container {
//// export interface Foo {
//// bar: string;
//// }
//// }
//// const x: [|Container.Foo.bar|] = ""

verify.not.codeFixAvailable();
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
/// <reference path='fourslash.ts' />

//// export interface Foo {
//// bar: string;
//// }
//// const x: [|Foo.bar|] = ""

verify.rangeAfterCodeFix(`Foo["bar"]`);