Skip to content

Commit 024ca6d

Browse files
Addressed CR feedback.
1 parent 84e385d commit 024ca6d

File tree

3 files changed

+24
-34
lines changed

3 files changed

+24
-34
lines changed

src/compiler/checker.ts

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3461,11 +3461,14 @@ module ts {
34613461
}
34623462

34633463
function checkThisExpression(node: Node): Type {
3464-
var container = getThisContainerOrArrowFunction(node);
3464+
// Stop at the first arrow function so that we can
3465+
// tell whether 'this' needs to be captured.
3466+
var container = getThisContainer(node, /* includeArrowFunctions */ true);
34653467
var needToCaptureLexicalThis = false;
3466-
// skip arrow functions
3467-
while (container.kind === SyntaxKind.ArrowFunction) {
3468-
container = getThisContainerOrArrowFunction(container);
3468+
3469+
// Now skip arrow functions to get the "real" owner of 'this'.
3470+
if (container.kind === SyntaxKind.ArrowFunction) {
3471+
container = getThisContainer(container, /* includeArrowFunctions */ false);
34693472
needToCaptureLexicalThis = true;
34703473
}
34713474

src/compiler/parser.ts

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -407,13 +407,18 @@ module ts {
407407
}
408408
}
409409

410-
export function getThisContainerOrArrowFunction(node: Node): Node {
410+
export function getThisContainer(node: Node, includeArrowFunctions: boolean): Node {
411411
while (true) {
412412
node = node.parent;
413413
if (!node) {
414414
return node;
415415
}
416416
switch (node.kind) {
417+
case SyntaxKind.ArrowFunction:
418+
if (!includeArrowFunctions) {
419+
continue;
420+
}
421+
// Fall through
417422
case SyntaxKind.FunctionDeclaration:
418423
case SyntaxKind.FunctionExpression:
419424
case SyntaxKind.ModuleDeclaration:
@@ -424,20 +429,11 @@ module ts {
424429
case SyntaxKind.SetAccessor:
425430
case SyntaxKind.EnumDeclaration:
426431
case SyntaxKind.SourceFile:
427-
case SyntaxKind.ArrowFunction:
428432
return node;
429433
}
430434
}
431435
}
432436

433-
export function getThisContainer(node: Node): Node {
434-
do {
435-
node = getThisContainerOrArrowFunction(node);
436-
} while (node.kind === SyntaxKind.ArrowFunction);
437-
438-
return node;
439-
}
440-
441437
export function hasRestParameters(s: SignatureDeclaration): boolean {
442438
return s.parameters.length > 0 && (s.parameters[s.parameters.length - 1].flags & NodeFlags.Rest) !== 0;
443439
}

src/services/services.ts

Lines changed: 11 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -2629,7 +2629,7 @@ module ts {
26292629

26302630
function getReferencesForThisKeyword(thisKeyword: Node, sourceFiles: SourceFile[]) {
26312631
// Get the owner" of the 'this' keyword.
2632-
var thisContainer = getThisContainer(thisKeyword);
2632+
var thisContainer = getThisContainer(thisKeyword, /* includeArrowFunctions */ false);
26332633

26342634
var searchSpaceNode: Node;
26352635

@@ -2645,9 +2645,13 @@ module ts {
26452645
searchSpaceNode = thisContainer.parent; // should be the owning class
26462646
staticFlag &= thisContainer.flags
26472647
break;
2648+
case SyntaxKind.SourceFile:
2649+
if (isExternalModule(<SourceFile>thisContainer)) {
2650+
return undefined;
2651+
}
2652+
// Fall through
26482653
case SyntaxKind.FunctionDeclaration:
26492654
case SyntaxKind.FunctionExpression:
2650-
case SyntaxKind.SourceFile:
26512655
searchSpaceNode = thisContainer;
26522656
break;
26532657
default:
@@ -2679,8 +2683,9 @@ module ts {
26792683
return;
26802684
}
26812685

2682-
// Get the owner" of the 'this' keyword.
2683-
var container = getThisContainer(node);
2686+
// Get the owner of the 'this' keyword.
2687+
// This *should* be a node that occurs somewhere within searchSpaceNode.
2688+
var container = getThisContainer(node, /* includeArrowFunctions */ false);
26842689

26852690
switch (container.kind) {
26862691
case SyntaxKind.Property:
@@ -2702,7 +2707,7 @@ module ts {
27022707
break;
27032708
case SyntaxKind.SourceFile:
27042709
// Add all 'this' keywords that belong to the top-level scope.
2705-
if (searchSpaceNode.kind === SyntaxKind.SourceFile) {
2710+
if (searchSpaceNode.kind === SyntaxKind.SourceFile && !isExternalModule(<SourceFile>searchSpaceNode)) {
27062711
result.push(getReferenceEntryFromNode(node));
27072712
}
27082713
break;
@@ -2970,21 +2975,7 @@ module ts {
29702975
}
29712976
else if (parent.kind === SyntaxKind.BinaryExpression && (<BinaryExpression>parent).left === node) {
29722977
var operator = (<BinaryExpression>parent).operator;
2973-
switch (operator) {
2974-
case SyntaxKind.AsteriskEqualsToken:
2975-
case SyntaxKind.SlashEqualsToken:
2976-
case SyntaxKind.PercentEqualsToken:
2977-
case SyntaxKind.MinusEqualsToken:
2978-
case SyntaxKind.LessThanLessThanEqualsToken:
2979-
case SyntaxKind.GreaterThanGreaterThanEqualsToken:
2980-
case SyntaxKind.GreaterThanGreaterThanGreaterThanEqualsToken:
2981-
case SyntaxKind.BarEqualsToken:
2982-
case SyntaxKind.CaretEqualsToken:
2983-
case SyntaxKind.AmpersandEqualsToken:
2984-
case SyntaxKind.PlusEqualsToken:
2985-
case SyntaxKind.EqualsToken:
2986-
return true;
2987-
}
2978+
return SyntaxKind.FirstAssignment <= operator && operator <= SyntaxKind.LastAssignment;
29882979
}
29892980
}
29902981

0 commit comments

Comments
 (0)