|
| 1 | +/* @internal */ |
| 2 | +namespace ts.codefix { |
| 3 | + registerCodeFix({ |
| 4 | + errorCodes: [Diagnostics.Property_0_does_not_exist_on_type_1_Did_you_mean_2.code, |
| 5 | + Diagnostics.Cannot_find_name_0_Did_you_mean_1.code], |
| 6 | + getCodeActions: getActionsForCorrectSpelling |
| 7 | + }); |
| 8 | + |
| 9 | + function getActionsForCorrectSpelling(context: CodeFixContext): CodeAction[] | undefined { |
| 10 | + const sourceFile = context.sourceFile; |
| 11 | + |
| 12 | + // This is the identifier of the misspelled word. eg: |
| 13 | + // this.speling = 1; |
| 14 | + // ^^^^^^^ |
| 15 | + const node = getTokenAtPosition(sourceFile, context.span.start); |
| 16 | + const checker = context.program.getTypeChecker(); |
| 17 | + let suggestion: string; |
| 18 | + if (node.kind === SyntaxKind.Identifier && isPropertyAccessExpression(node.parent)) { |
| 19 | + const containingType = checker.getTypeAtLocation(node.parent.expression); |
| 20 | + suggestion = checker.getSuggestionForNonexistentProperty(node as Identifier, containingType); |
| 21 | + } |
| 22 | + else { |
| 23 | + const meaning = getMeaningFromLocation(node); |
| 24 | + suggestion = checker.getSuggestionForNonexistentSymbol(node, getTextOfNode(node), convertSemanticMeaningToSymbolFlags(meaning)); |
| 25 | + } |
| 26 | + if (suggestion) { |
| 27 | + return [{ |
| 28 | + description: formatStringFromArgs(getLocaleSpecificMessage(Diagnostics.Change_spelling_to_0), [suggestion]), |
| 29 | + changes: [{ |
| 30 | + fileName: sourceFile.fileName, |
| 31 | + textChanges: [{ |
| 32 | + span: { start: node.getStart(), length: node.getWidth() }, |
| 33 | + newText: suggestion |
| 34 | + }], |
| 35 | + }], |
| 36 | + }]; |
| 37 | + } |
| 38 | + } |
| 39 | + |
| 40 | + function convertSemanticMeaningToSymbolFlags(meaning: SemanticMeaning): SymbolFlags { |
| 41 | + let flags = 0; |
| 42 | + if (meaning & SemanticMeaning.Namespace) { |
| 43 | + flags |= SymbolFlags.Namespace; |
| 44 | + } |
| 45 | + if (meaning & SemanticMeaning.Type) { |
| 46 | + flags |= SymbolFlags.Type; |
| 47 | + } |
| 48 | + if (meaning & SemanticMeaning.Value) { |
| 49 | + flags |= SymbolFlags.Value; |
| 50 | + } |
| 51 | + return flags; |
| 52 | + } |
| 53 | +} |
0 commit comments