forked from microsoft/TypeScript
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathfixNoPropertyAccessFromIndexSignature.ts
35 lines (32 loc) · 1.71 KB
/
fixNoPropertyAccessFromIndexSignature.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
/* @internal */
namespace ts.codefix {
const fixId = "fixNoPropertyAccessFromIndexSignature";
const errorCodes = [
Diagnostics.Property_0_comes_from_an_index_signature_so_it_must_be_accessed_with_0.code
];
registerCodeFix({
errorCodes,
fixIds: [fixId],
getCodeActions(context) {
const { sourceFile, span } = context;
const property = getPropertyAccessExpression(sourceFile, span.start);
const changes = textChanges.ChangeTracker.with(context, t => doChange(t, context.sourceFile, property));
return [createCodeFixAction(fixId, changes, [Diagnostics.Use_element_access_for_0, property.name.text], fixId, Diagnostics.Use_element_access_for_all_undeclared_properties)];
},
getAllCodeActions: context =>
codeFixAll(context, errorCodes, (changes, diag) => doChange(changes, diag.file, getPropertyAccessExpression(diag.file, diag.start)))
});
function doChange(changes: textChanges.ChangeTracker, sourceFile: SourceFile, node: PropertyAccessExpression): void {
const argumentsExpression = factory.createStringLiteral(node.name.text);
changes.replaceNode(
sourceFile,
node,
isPropertyAccessChain(node) ?
factory.createElementAccessChain(node.expression, node.questionDotToken, argumentsExpression) :
factory.createElementAccessExpression(node.expression, argumentsExpression)
);
}
function getPropertyAccessExpression(sourceFile: SourceFile, pos: number): PropertyAccessExpression {
return cast(getTokenAtPosition(sourceFile, pos).parent, isPropertyAccessExpression);
}
}