@@ -5811,17 +5811,32 @@ namespace ts {
58115811 return undefined ;
58125812 }
58135813
5814- if ( node . kind !== SyntaxKind . Identifier &&
5815- // TODO (drosen): This should be enabled in a later release - currently breaks rename.
5816- // node.kind !== SyntaxKind.ThisKeyword &&
5817- // node.kind !== SyntaxKind.SuperKeyword &&
5818- node . kind !== SyntaxKind . StringLiteral &&
5819- ! isLiteralNameOfPropertyDeclarationOrIndexAccess ( node ) ) {
5820- return undefined ;
5814+ switch ( node . kind ) {
5815+ case SyntaxKind . NumericLiteral :
5816+ if ( ! isLiteralNameOfPropertyDeclarationOrIndexAccess ( node ) ) {
5817+ break ;
5818+ }
5819+ // Fallthrough
5820+ case SyntaxKind . Identifier :
5821+ case SyntaxKind . ThisKeyword :
5822+ // case SyntaxKind.SuperKeyword: TODO:GH#9268
5823+ case SyntaxKind . StringLiteral :
5824+ return getReferencedSymbolsForNode ( node , program . getSourceFiles ( ) , findInStrings , findInComments ) ;
58215825 }
5826+ return undefined ;
5827+ }
58225828
5823- Debug . assert ( node . kind === SyntaxKind . Identifier || node . kind === SyntaxKind . NumericLiteral || node . kind === SyntaxKind . StringLiteral ) ;
5824- return getReferencedSymbolsForNode ( node , program . getSourceFiles ( ) , findInStrings , findInComments ) ;
5829+ function isThis ( node : Node ) : boolean {
5830+ switch ( node . kind ) {
5831+ case SyntaxKind . ThisKeyword :
5832+ // case SyntaxKind.ThisType: TODO: GH#9267
5833+ return true ;
5834+ case SyntaxKind . Identifier :
5835+ // 'this' as a parameter
5836+ return ( node as Identifier ) . originalKeywordKind === SyntaxKind . ThisKeyword && node . parent . kind === SyntaxKind . Parameter ;
5837+ default :
5838+ return false ;
5839+ }
58255840 }
58265841
58275842 function getReferencedSymbolsForNode ( node : Node , sourceFiles : SourceFile [ ] , findInStrings : boolean , findInComments : boolean ) : ReferencedSymbol [ ] {
@@ -5841,7 +5856,7 @@ namespace ts {
58415856 }
58425857 }
58435858
5844- if ( node . kind === SyntaxKind . ThisKeyword || node . kind === SyntaxKind . ThisType ) {
5859+ if ( isThis ( node ) ) {
58455860 return getReferencesForThisKeyword ( node , sourceFiles ) ;
58465861 }
58475862
@@ -6376,7 +6391,7 @@ namespace ts {
63766391 cancellationToken . throwIfCancellationRequested ( ) ;
63776392
63786393 const node = getTouchingWord ( sourceFile , position ) ;
6379- if ( ! node || ( node . kind !== SyntaxKind . ThisKeyword && node . kind !== SyntaxKind . ThisType ) ) {
6394+ if ( ! node || ! isThis ( node ) ) {
63806395 return ;
63816396 }
63826397
@@ -8003,11 +8018,11 @@ namespace ts {
80038018
80048019 const node = getTouchingWord ( sourceFile , position , /*includeJsDocComment*/ true ) ;
80058020
8006- // Can only rename an identifier.
80078021 if ( node ) {
80088022 if ( node . kind === SyntaxKind . Identifier ||
80098023 node . kind === SyntaxKind . StringLiteral ||
8010- isLiteralNameOfPropertyDeclarationOrIndexAccess ( node ) ) {
8024+ isLiteralNameOfPropertyDeclarationOrIndexAccess ( node ) ||
8025+ isThis ( node ) ) {
80118026 const symbol = typeChecker . getSymbolAtLocation ( node ) ;
80128027
80138028 // Only allow a symbol to be renamed if it actually has at least one declaration.
@@ -8054,6 +8069,26 @@ namespace ts {
80548069 }
80558070 }
80568071 }
8072+ else if ( node . kind === SyntaxKind . ThisKeyword ) {
8073+ const container = ts . getThisContainer ( node , /*includeArrowFunctions*/ false ) ;
8074+ // Only allow rename to change a function with a 'this' type to one with a regular parameter,
8075+ // e.g. `function(this: number) { return this; }` to `function(x: number) { return x; }`
8076+ if ( isFunctionLike ( container ) ) {
8077+ const sig = typeChecker . getSignatureFromDeclaration ( container ) ;
8078+ if ( sig . thisType ) {
8079+ return {
8080+ canRename : true ,
8081+ kind : ScriptElementKind . parameterElement ,
8082+ displayName : "this" ,
8083+ localizedErrorMessage : undefined ,
8084+ fullDisplayName : "this" ,
8085+ kindModifiers : "" ,
8086+ triggerSpan : createTriggerSpanForNode ( node , sourceFile )
8087+ } ;
8088+ }
8089+ }
8090+ // fallthrough to error
8091+ }
80578092 }
80588093 }
80598094
0 commit comments