@@ -14,57 +14,64 @@ namespace ts.codefix {
14
14
const { sourceFile } = context ;
15
15
const info = getInfo ( sourceFile , context . span . start , context ) ;
16
16
if ( ! info ) return undefined ;
17
- const { node, suggestion } = info ;
17
+ const { node, suggestedSymbol } = info ;
18
18
const { target } = context . host . getCompilationSettings ( ) ;
19
- const changes = textChanges . ChangeTracker . with ( context , t => doChange ( t , sourceFile , node , suggestion , target ! ) ) ;
20
- return [ createCodeFixAction ( "spelling" , changes , [ Diagnostics . Change_spelling_to_0 , suggestion ] , fixId , Diagnostics . Fix_all_detected_spelling_errors ) ] ;
19
+ const changes = textChanges . ChangeTracker . with ( context , t => doChange ( t , sourceFile , node , suggestedSymbol , target ! ) ) ;
20
+ return [ createCodeFixAction ( "spelling" , changes , [ Diagnostics . Change_spelling_to_0 , symbolName ( suggestedSymbol ) ] , fixId , Diagnostics . Fix_all_detected_spelling_errors ) ] ;
21
21
} ,
22
22
fixIds : [ fixId ] ,
23
23
getAllCodeActions : context => codeFixAll ( context , errorCodes , ( changes , diag ) => {
24
24
const info = getInfo ( diag . file , diag . start , context ) ;
25
25
const { target } = context . host . getCompilationSettings ( ) ;
26
- if ( info ) doChange ( changes , context . sourceFile , info . node , info . suggestion , target ! ) ;
26
+ if ( info ) doChange ( changes , context . sourceFile , info . node , info . suggestedSymbol , target ! ) ;
27
27
} ) ,
28
28
} ) ;
29
29
30
- function getInfo ( sourceFile : SourceFile , pos : number , context : CodeFixContextBase ) : { node : Node , suggestion : string } | undefined {
30
+ function getInfo ( sourceFile : SourceFile , pos : number , context : CodeFixContextBase ) : { node : Node , suggestedSymbol : Symbol } | undefined {
31
31
// This is the identifier of the misspelled word. eg:
32
32
// this.speling = 1;
33
33
// ^^^^^^^
34
34
const node = getTokenAtPosition ( sourceFile , pos ) ;
35
35
const checker = context . program . getTypeChecker ( ) ;
36
36
37
- let suggestion : string | undefined ;
37
+ let suggestedSymbol : Symbol | undefined ;
38
38
if ( isPropertyAccessExpression ( node . parent ) && node . parent . name === node ) {
39
39
Debug . assert ( isIdentifierOrPrivateIdentifier ( node ) , "Expected an identifier for spelling (property access)" ) ;
40
40
let containingType = checker . getTypeAtLocation ( node . parent . expression ) ;
41
41
if ( node . parent . flags & NodeFlags . OptionalChain ) {
42
42
containingType = checker . getNonNullableType ( containingType ) ;
43
43
}
44
44
const name = node as Identifier | PrivateIdentifier ;
45
- suggestion = checker . getSuggestionForNonexistentProperty ( name , containingType ) ;
45
+ suggestedSymbol = checker . getSuggestedSymbolForNonexistentProperty ( name , containingType ) ;
46
46
}
47
47
else if ( isImportSpecifier ( node . parent ) && node . parent . name === node ) {
48
48
Debug . assert ( node . kind === SyntaxKind . Identifier , "Expected an identifier for spelling (import)" ) ;
49
49
const importDeclaration = findAncestor ( node , isImportDeclaration ) ! ;
50
50
const resolvedSourceFile = getResolvedSourceFileFromImportDeclaration ( sourceFile , context , importDeclaration ) ;
51
51
if ( resolvedSourceFile && resolvedSourceFile . symbol ) {
52
- suggestion = checker . getSuggestionForNonexistentExport ( node as Identifier , resolvedSourceFile . symbol ) ;
52
+ suggestedSymbol = checker . getSuggestedSymbolForNonexistentModule ( node as Identifier , resolvedSourceFile . symbol ) ;
53
53
}
54
54
}
55
55
else {
56
56
const meaning = getMeaningFromLocation ( node ) ;
57
57
const name = getTextOfNode ( node ) ;
58
58
Debug . assert ( name !== undefined , "name should be defined" ) ;
59
- suggestion = checker . getSuggestionForNonexistentSymbol ( node , name , convertSemanticMeaningToSymbolFlags ( meaning ) ) ;
59
+ suggestedSymbol = checker . getSuggestedSymbolForNonexistentSymbol ( node , name , convertSemanticMeaningToSymbolFlags ( meaning ) ) ;
60
60
}
61
61
62
- return suggestion === undefined ? undefined : { node, suggestion } ;
62
+ return suggestedSymbol === undefined ? undefined : { node, suggestedSymbol } ;
63
63
}
64
64
65
- function doChange ( changes : textChanges . ChangeTracker , sourceFile : SourceFile , node : Node , suggestion : string , target : ScriptTarget ) {
65
+ function doChange ( changes : textChanges . ChangeTracker , sourceFile : SourceFile , node : Node , suggestedSymbol : Symbol , target : ScriptTarget ) {
66
+ const suggestion = symbolName ( suggestedSymbol ) ;
66
67
if ( ! isIdentifierText ( suggestion , target ) && isPropertyAccessExpression ( node . parent ) ) {
67
- changes . replaceNode ( sourceFile , node . parent , createElementAccess ( node . parent . expression , createLiteral ( suggestion ) ) ) ;
68
+ const valDecl = suggestedSymbol . valueDeclaration ;
69
+ if ( isNamedDeclaration ( valDecl ) && isPrivateIdentifier ( valDecl . name ) ) {
70
+ changes . replaceNode ( sourceFile , node , createIdentifier ( suggestion ) ) ;
71
+ }
72
+ else {
73
+ changes . replaceNode ( sourceFile , node . parent , createElementAccess ( node . parent . expression , createLiteral ( suggestion ) ) ) ;
74
+ }
68
75
}
69
76
else {
70
77
changes . replaceNode ( sourceFile , node , createIdentifier ( suggestion ) ) ;
0 commit comments