|
| 1 | +/* @internal */ |
| 2 | +namespace ts.codefix { |
| 3 | + const fixId = "fixNoPropertyAccessFromIndexSignature"; |
| 4 | + const errorCodes = [ |
| 5 | + Diagnostics.Property_0_comes_from_an_index_signature_so_it_must_be_accessed_with_0.code |
| 6 | + ]; |
| 7 | + |
| 8 | + registerCodeFix({ |
| 9 | + errorCodes, |
| 10 | + fixIds: [fixId], |
| 11 | + getCodeActions(context) { |
| 12 | + const { sourceFile, span } = context; |
| 13 | + const property = getPropertyAccessExpression(sourceFile, span.start); |
| 14 | + const changes = textChanges.ChangeTracker.with(context, t => doChange(t, context.sourceFile, property)); |
| 15 | + return [createCodeFixAction(fixId, changes, [Diagnostics.Use_element_access_for_0, property.name.text], fixId, Diagnostics.Use_element_access_for_all_undeclared_properties)]; |
| 16 | + }, |
| 17 | + getAllCodeActions: context => |
| 18 | + codeFixAll(context, errorCodes, (changes, diag) => doChange(changes, diag.file, getPropertyAccessExpression(diag.file, diag.start))) |
| 19 | + }); |
| 20 | + |
| 21 | + function doChange(changes: textChanges.ChangeTracker, sourceFile: SourceFile, node: PropertyAccessExpression): void { |
| 22 | + const argumentsExpression = factory.createStringLiteral(node.name.text); |
| 23 | + changes.replaceNode( |
| 24 | + sourceFile, |
| 25 | + node, |
| 26 | + isPropertyAccessChain(node) ? |
| 27 | + factory.createElementAccessChain(node.expression, node.questionDotToken, argumentsExpression) : |
| 28 | + factory.createElementAccessExpression(node.expression, argumentsExpression) |
| 29 | + ); |
| 30 | + } |
| 31 | + |
| 32 | + function getPropertyAccessExpression(sourceFile: SourceFile, pos: number): PropertyAccessExpression { |
| 33 | + return cast(getTokenAtPosition(sourceFile, pos).parent, isPropertyAccessExpression); |
| 34 | + } |
| 35 | +} |
0 commit comments