Skip to content

Commit 0dc7424

Browse files
Added codefix for replacing qualified names with indexed access types.
1 parent e9330d4 commit 0dc7424

File tree

3 files changed

+32
-0
lines changed

3 files changed

+32
-0
lines changed

src/compiler/diagnosticMessages.json

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3657,6 +3657,10 @@
36573657
"category": "Message",
36583658
"code": 90025
36593659
},
3660+
"Rewrite as the indexed access type '{0}'.": {
3661+
"category": "Message",
3662+
"code": 90026
3663+
},
36603664

36613665
"Convert function to an ES2015 class": {
36623666
"category": "Message",
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
/* @internal */
2+
namespace ts.codefix {
3+
registerCodeFix({
4+
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],
5+
getCodeActions: (context: CodeFixContext) => {
6+
const sourceFile = context.sourceFile;
7+
const token = getTokenAtPosition(sourceFile, context.span.start, /*includeJsDocComment*/ false);
8+
const qualifiedName = getAncestor(token, SyntaxKind.QualifiedName) as QualifiedName;
9+
Debug.assert(!!qualifiedName, "Expected position to be owned by a qualified name.");
10+
if (!isIdentifier(qualifiedName.left)) {
11+
return undefined;
12+
}
13+
const leftText = qualifiedName.left.getText(sourceFile);
14+
const rightText = qualifiedName.right.getText(sourceFile);
15+
const replacement = createIndexedAccessTypeNode(
16+
createTypeReferenceNode(qualifiedName.left, /*typeArguments*/ undefined),
17+
createLiteralTypeNode(createLiteral(rightText)));
18+
const changeTracker = textChanges.ChangeTracker.fromCodeFixContext(context);
19+
changeTracker.replaceNode(sourceFile, qualifiedName, replacement);
20+
21+
return [{
22+
description: formatStringFromArgs(getLocaleSpecificMessage(Diagnostics.Rewrite_as_the_indexed_access_type_0), [`${leftText}["${rightText}"]`]),
23+
changes: changeTracker.getChanges()
24+
}];
25+
}
26+
});
27+
}

src/services/codefixes/fixes.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
/// <reference path="correctQualifiedNameToIndexedAccessType.ts" />
12
/// <reference path="fixClassIncorrectlyImplementsInterface.ts" />
23
/// <reference path="fixAddMissingMember.ts" />
34
/// <reference path="fixSpelling.ts" />

0 commit comments

Comments
 (0)