-
Notifications
You must be signed in to change notification settings - Fork 12.5k
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
Changes from all commits
e9330d4
0dc7424
935b895
3205ca6
c659fe9
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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)) { | ||
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() | ||
}]; | ||
} | ||
}); | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
/// <reference path="correctQualifiedNameToIndexedAccessType.ts" /> | ||
/// <reference path="fixClassIncorrectlyImplementsInterface.ts" /> | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Looks like there is an analogous entry to this in @weswigham do you know if building for tests will correctly trigger a rebuild when we make changes to |
||
/// <reference path="fixAddMissingMember.ts" /> | ||
/// <reference path="fixSpelling.ts" /> | ||
|
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"]`); |
There was a problem hiding this comment.
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:
Please add a test.