Skip to content

Commit 1879e28

Browse files
author
Andy Hanson
committed
Add helper functions for detecting 'this' identifiers
1 parent ebb17e8 commit 1879e28

File tree

6 files changed

+31
-27
lines changed

6 files changed

+31
-27
lines changed

src/compiler/binder.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2432,8 +2432,7 @@ namespace ts {
24322432
}
24332433

24342434
// If the parameter's name is 'this', then it is TypeScript syntax.
2435-
if (subtreeFlags & TransformFlags.ContainsDecorators
2436-
|| (name && isIdentifier(name) && name.originalKeywordKind === SyntaxKind.ThisKeyword)) {
2435+
if (subtreeFlags & TransformFlags.ContainsDecorators || isThisIdentifier(name)) {
24372436
transformFlags |= TransformFlags.AssertTypeScript;
24382437
}
24392438

src/compiler/checker.ts

Lines changed: 3 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -9373,7 +9373,7 @@ namespace ts {
93739373
captureLexicalThis(node, container);
93749374
}
93759375
if (isFunctionLike(container) &&
9376-
(!isInParameterInitializerBeforeContainingFunction(node) || getFunctionLikeThisParameter(container))) {
9376+
(!isInParameterInitializerBeforeContainingFunction(node) || getThisParameter(container))) {
93779377
// Note: a parameter initializer should refer to class-this unless function-this is explicitly annotated.
93789378

93799379
// If this is a function in a JS file, it might be a class method. Check if it's the RHS
@@ -15557,10 +15557,6 @@ namespace ts {
1555715557
}
1555815558
}
1555915559

15560-
function parameterIsThisKeyword(parameter: ParameterDeclaration) {
15561-
return parameter.name && (<Identifier>parameter.name).originalKeywordKind === SyntaxKind.ThisKeyword;
15562-
}
15563-
1556415560
function parameterNameStartsWithUnderscore(parameter: ParameterDeclaration) {
1556515561
return parameter.name && parameter.name.kind === SyntaxKind.Identifier && (<Identifier>parameter.name).text.charCodeAt(0) === CharacterCodes._;
1556615562
}
@@ -20141,18 +20137,8 @@ namespace ts {
2014120137
}
2014220138

2014320139
function getAccessorThisParameter(accessor: AccessorDeclaration): ParameterDeclaration {
20144-
if (accessor.parameters.length === (accessor.kind === SyntaxKind.GetAccessor ? 1 : 2) &&
20145-
accessor.parameters[0].name.kind === SyntaxKind.Identifier &&
20146-
(<Identifier>accessor.parameters[0].name).originalKeywordKind === SyntaxKind.ThisKeyword) {
20147-
return accessor.parameters[0];
20148-
}
20149-
}
20150-
20151-
function getFunctionLikeThisParameter(func: FunctionLikeDeclaration) {
20152-
if (func.parameters.length &&
20153-
func.parameters[0].name.kind === SyntaxKind.Identifier &&
20154-
(<Identifier>func.parameters[0].name).originalKeywordKind === SyntaxKind.ThisKeyword) {
20155-
return func.parameters[0];
20140+
if (accessor.parameters.length === (accessor.kind === SyntaxKind.GetAccessor ? 1 : 2)) {
20141+
return getThisParameter(accessor);
2015620142
}
2015720143
}
2015820144

src/compiler/transformers/ts.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2374,7 +2374,7 @@ namespace ts {
23742374
* @param node The parameter declaration node.
23752375
*/
23762376
function visitParameter(node: ParameterDeclaration) {
2377-
if (node.name && isIdentifier(node.name) && node.name.originalKeywordKind === SyntaxKind.ThisKeyword) {
2377+
if (parameterIsThisKeyword(node)) {
23782378
return undefined;
23792379
}
23802380

src/compiler/utilities.ts

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2707,15 +2707,35 @@ namespace ts {
27072707
});
27082708
}
27092709

2710-
export function getSetAccessorTypeAnnotationNode(accessor: AccessorDeclaration): TypeNode {
2710+
/** Get the type annotaion for the value parameter. */
2711+
export function getSetAccessorTypeAnnotationNode(accessor: SetAccessorDeclaration): TypeNode {
27112712
if (accessor && accessor.parameters.length > 0) {
2712-
const hasThis = accessor.parameters.length === 2 &&
2713-
accessor.parameters[0].name.kind === SyntaxKind.Identifier &&
2714-
(accessor.parameters[0].name as Identifier).originalKeywordKind === SyntaxKind.ThisKeyword;
2713+
const hasThis = accessor.parameters.length === 2 && parameterIsThisKeyword(accessor.parameters[0]);
27152714
return accessor.parameters[hasThis ? 1 : 0].type;
27162715
}
27172716
}
27182717

2718+
export function getThisParameter(signature: SignatureDeclaration): ParameterDeclaration | undefined {
2719+
if (signature.parameters.length) {
2720+
const thisParameter = signature.parameters[0];
2721+
if (parameterIsThisKeyword(thisParameter)) {
2722+
return thisParameter;
2723+
}
2724+
}
2725+
}
2726+
2727+
export function parameterIsThisKeyword(parameter: ParameterDeclaration): boolean {
2728+
return isThisIdentifier(parameter.name);
2729+
}
2730+
2731+
export function isThisIdentifier(node: Node | undefined): boolean {
2732+
return node && node.kind === SyntaxKind.Identifier && identifierIsThisKeyword(node as Identifier);
2733+
}
2734+
2735+
export function identifierIsThisKeyword(id: Identifier): boolean {
2736+
return id.originalKeywordKind === SyntaxKind.ThisKeyword;
2737+
}
2738+
27192739
export interface AllAccessorDeclarations {
27202740
firstAccessor: AccessorDeclaration;
27212741
secondAccessor: AccessorDeclaration;

src/services/classifier.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -954,8 +954,7 @@ namespace ts {
954954
return;
955955
case SyntaxKind.Parameter:
956956
if ((<ParameterDeclaration>token.parent).name === token) {
957-
const isThis = token.kind === SyntaxKind.Identifier && (<Identifier>token).originalKeywordKind === SyntaxKind.ThisKeyword;
958-
return isThis ? ClassificationType.keyword : ClassificationType.parameterName;
957+
return isThisIdentifier(token) ? ClassificationType.keyword : ClassificationType.parameterName;
959958
}
960959
return;
961960
}

src/services/utilities.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -376,7 +376,7 @@ namespace ts {
376376
return true;
377377
case SyntaxKind.Identifier:
378378
// 'this' as a parameter
379-
return (node as Identifier).originalKeywordKind === SyntaxKind.ThisKeyword && node.parent.kind === SyntaxKind.Parameter;
379+
return identifierIsThisKeyword(node as Identifier) && node.parent.kind === SyntaxKind.Parameter;
380380
default:
381381
return false;
382382
}

0 commit comments

Comments
 (0)