Add helper functions for detecting 'this' identifiers

This commit is contained in:
Andy Hanson
2016-10-05 08:54:20 -07:00
parent ebb17e8019
commit 1879e28fd3
6 changed files with 31 additions and 27 deletions

View File

@@ -2432,8 +2432,7 @@ namespace ts {
}
// If the parameter's name is 'this', then it is TypeScript syntax.
if (subtreeFlags & TransformFlags.ContainsDecorators
|| (name && isIdentifier(name) && name.originalKeywordKind === SyntaxKind.ThisKeyword)) {
if (subtreeFlags & TransformFlags.ContainsDecorators || isThisIdentifier(name)) {
transformFlags |= TransformFlags.AssertTypeScript;
}

View File

@@ -9373,7 +9373,7 @@ namespace ts {
captureLexicalThis(node, container);
}
if (isFunctionLike(container) &&
(!isInParameterInitializerBeforeContainingFunction(node) || getFunctionLikeThisParameter(container))) {
(!isInParameterInitializerBeforeContainingFunction(node) || getThisParameter(container))) {
// Note: a parameter initializer should refer to class-this unless function-this is explicitly annotated.
// 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 {
}
}
function parameterIsThisKeyword(parameter: ParameterDeclaration) {
return parameter.name && (<Identifier>parameter.name).originalKeywordKind === SyntaxKind.ThisKeyword;
}
function parameterNameStartsWithUnderscore(parameter: ParameterDeclaration) {
return parameter.name && parameter.name.kind === SyntaxKind.Identifier && (<Identifier>parameter.name).text.charCodeAt(0) === CharacterCodes._;
}
@@ -20141,18 +20137,8 @@ namespace ts {
}
function getAccessorThisParameter(accessor: AccessorDeclaration): ParameterDeclaration {
if (accessor.parameters.length === (accessor.kind === SyntaxKind.GetAccessor ? 1 : 2) &&
accessor.parameters[0].name.kind === SyntaxKind.Identifier &&
(<Identifier>accessor.parameters[0].name).originalKeywordKind === SyntaxKind.ThisKeyword) {
return accessor.parameters[0];
}
}
function getFunctionLikeThisParameter(func: FunctionLikeDeclaration) {
if (func.parameters.length &&
func.parameters[0].name.kind === SyntaxKind.Identifier &&
(<Identifier>func.parameters[0].name).originalKeywordKind === SyntaxKind.ThisKeyword) {
return func.parameters[0];
if (accessor.parameters.length === (accessor.kind === SyntaxKind.GetAccessor ? 1 : 2)) {
return getThisParameter(accessor);
}
}

View File

@@ -2374,7 +2374,7 @@ namespace ts {
* @param node The parameter declaration node.
*/
function visitParameter(node: ParameterDeclaration) {
if (node.name && isIdentifier(node.name) && node.name.originalKeywordKind === SyntaxKind.ThisKeyword) {
if (parameterIsThisKeyword(node)) {
return undefined;
}

View File

@@ -2707,15 +2707,35 @@ namespace ts {
});
}
export function getSetAccessorTypeAnnotationNode(accessor: AccessorDeclaration): TypeNode {
/** Get the type annotaion for the value parameter. */
export function getSetAccessorTypeAnnotationNode(accessor: SetAccessorDeclaration): TypeNode {
if (accessor && accessor.parameters.length > 0) {
const hasThis = accessor.parameters.length === 2 &&
accessor.parameters[0].name.kind === SyntaxKind.Identifier &&
(accessor.parameters[0].name as Identifier).originalKeywordKind === SyntaxKind.ThisKeyword;
const hasThis = accessor.parameters.length === 2 && parameterIsThisKeyword(accessor.parameters[0]);
return accessor.parameters[hasThis ? 1 : 0].type;
}
}
export function getThisParameter(signature: SignatureDeclaration): ParameterDeclaration | undefined {
if (signature.parameters.length) {
const thisParameter = signature.parameters[0];
if (parameterIsThisKeyword(thisParameter)) {
return thisParameter;
}
}
}
export function parameterIsThisKeyword(parameter: ParameterDeclaration): boolean {
return isThisIdentifier(parameter.name);
}
export function isThisIdentifier(node: Node | undefined): boolean {
return node && node.kind === SyntaxKind.Identifier && identifierIsThisKeyword(node as Identifier);
}
export function identifierIsThisKeyword(id: Identifier): boolean {
return id.originalKeywordKind === SyntaxKind.ThisKeyword;
}
export interface AllAccessorDeclarations {
firstAccessor: AccessorDeclaration;
secondAccessor: AccessorDeclaration;

View File

@@ -954,8 +954,7 @@ namespace ts {
return;
case SyntaxKind.Parameter:
if ((<ParameterDeclaration>token.parent).name === token) {
const isThis = token.kind === SyntaxKind.Identifier && (<Identifier>token).originalKeywordKind === SyntaxKind.ThisKeyword;
return isThis ? ClassificationType.keyword : ClassificationType.parameterName;
return isThisIdentifier(token) ? ClassificationType.keyword : ClassificationType.parameterName;
}
return;
}

View File

@@ -376,7 +376,7 @@ namespace ts {
return true;
case SyntaxKind.Identifier:
// 'this' as a parameter
return (node as Identifier).originalKeywordKind === SyntaxKind.ThisKeyword && node.parent.kind === SyntaxKind.Parameter;
return identifierIsThisKeyword(node as Identifier) && node.parent.kind === SyntaxKind.Parameter;
default:
return false;
}