Skip to content

Commit 7b0bd09

Browse files
author
Andy Hanson
committed
findAllReferences: Make "isWriteAccess" handle special declaration kinds
1 parent 73ee2fe commit 7b0bd09

18 files changed

+51
-36
lines changed

src/compiler/checker.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12750,7 +12750,7 @@ namespace ts {
1275012750
function getContextualTypeForBinaryOperand(node: Expression): Type {
1275112751
const binaryExpression = <BinaryExpression>node.parent;
1275212752
const operator = binaryExpression.operatorToken.kind;
12753-
if (operator >= SyntaxKind.FirstAssignment && operator <= SyntaxKind.LastAssignment) {
12753+
if (isAssignmentOperator(operator)) {
1275412754
// Don't do this for special property assignments to avoid circularity
1275512755
if (getSpecialPropertyAssignmentKind(binaryExpression) !== SpecialPropertyAssignmentKind.None) {
1275612756
return undefined;
@@ -17305,7 +17305,7 @@ namespace ts {
1730517305
}
1730617306

1730717307
function checkAssignmentOperator(valueType: Type): void {
17308-
if (produceDiagnostics && operator >= SyntaxKind.FirstAssignment && operator <= SyntaxKind.LastAssignment) {
17308+
if (produceDiagnostics && isAssignmentOperator(operator)) {
1730917309
// TypeScript 1.0 spec (April 2014): 4.17
1731017310
// An assignment of the form
1731117311
// VarExpr = ValueExpr

src/compiler/utilities.ts

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1785,6 +1785,23 @@ namespace ts {
17851785
}
17861786
}
17871787

1788+
/* @internal */
1789+
// See GH#16030
1790+
export function isAnyDeclarationName(name: Node): boolean {
1791+
switch (name.kind) {
1792+
case SyntaxKind.Identifier:
1793+
case SyntaxKind.StringLiteral:
1794+
case SyntaxKind.NumericLiteral:
1795+
if (isDeclaration(name.parent)) {
1796+
return name.parent.name === name;
1797+
}
1798+
const binExp = name.parent.parent;
1799+
return isBinaryExpression(binExp) && getSpecialPropertyAssignmentKind(binExp) !== SpecialPropertyAssignmentKind.None && getNameOfDeclaration(binExp) === name;
1800+
default:
1801+
return false;
1802+
}
1803+
}
1804+
17881805
export function isLiteralComputedPropertyDeclarationName(node: Node) {
17891806
return (node.kind === SyntaxKind.StringLiteral || node.kind === SyntaxKind.NumericLiteral) &&
17901807
node.parent.kind === SyntaxKind.ComputedPropertyName &&

src/services/findAllReferences.ts

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -176,7 +176,7 @@ namespace ts.FindAllReferences {
176176
fileName: node.getSourceFile().fileName,
177177
textSpan: getTextSpan(node),
178178
isWriteAccess: isWriteAccess(node),
179-
isDefinition: isDeclarationName(node) || isLiteralComputedPropertyDeclarationName(node),
179+
isDefinition: isAnyDeclarationName(node) || isLiteralComputedPropertyDeclarationName(node),
180180
isInString
181181
};
182182
}
@@ -242,22 +242,20 @@ namespace ts.FindAllReferences {
242242

243243
/** A node is considered a writeAccess iff it is a name of a declaration or a target of an assignment */
244244
function isWriteAccess(node: Node): boolean {
245-
if (node.kind === SyntaxKind.Identifier && isDeclarationName(node)) {
245+
if (isAnyDeclarationName(node)) {
246246
return true;
247247
}
248248

249-
const parent = node.parent;
250-
if (parent) {
251-
if (parent.kind === SyntaxKind.PostfixUnaryExpression || parent.kind === SyntaxKind.PrefixUnaryExpression) {
249+
const { parent } = node;
250+
switch (parent && parent.kind) {
251+
case SyntaxKind.PostfixUnaryExpression:
252+
case SyntaxKind.PrefixUnaryExpression:
252253
return true;
253-
}
254-
else if (parent.kind === SyntaxKind.BinaryExpression && (<BinaryExpression>parent).left === node) {
255-
const operator = (<BinaryExpression>parent).operatorToken.kind;
256-
return SyntaxKind.FirstAssignment <= operator && operator <= SyntaxKind.LastAssignment;
257-
}
254+
case SyntaxKind.BinaryExpression:
255+
return (<BinaryExpression>parent).left === node && isAssignmentOperator((<BinaryExpression>parent).operatorToken.kind);
256+
default:
257+
return false;
258258
}
259-
260-
return false;
261259
}
262260
}
263261

tests/cases/fourslash/findAllRefsForModuleGlobal.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
// @Filename: /b.ts
77
/////// <reference types="[|foo|]" />
88
////import { x } from "[|foo|]";
9-
////declare module "[|{| "isDefinition": true |}foo|]" {}
9+
////declare module "[|{| "isWriteAccess": true, "isDefinition": true |}foo|]" {}
1010

1111
verify.noErrors();
1212

tests/cases/fourslash/getOccurrencesIsDefinitionOfNumberNamedProperty.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/// <reference path='fourslash.ts' />
2-
////let o = { [|{| "isDefinition": true |}1|]: 12 };
2+
////let o = { [|{| "isWriteAccess": true, "isDefinition": true |}1|]: 12 };
33
////let y = o[[|1|]];
44

55
const ranges = test.ranges();

tests/cases/fourslash/getOccurrencesIsDefinitionOfStringNamedProperty.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/// <reference path='fourslash.ts' />
2-
////let o = { "[|{| "isDefinition": true |}x|]": 12 };
2+
////let o = { "[|{| "isWriteAccess": true, "isDefinition": true |}x|]": 12 };
33
////let y = o.[|x|];
44

55
const ranges = test.ranges();

tests/cases/fourslash/referencesBloomFilters.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
////function blah2() { container["[|searchProp|]"] };
1313

1414
// @Filename: redeclaration.ts
15-
////container = { "[|{| "isDefinition": true |}searchProp|]" : 18 };
15+
////container = { "[|{| "isWriteAccess": true, "isDefinition": true |}searchProp|]" : 18 };
1616

1717
const ranges = test.ranges();
1818
const [r0, r1, r2, r3] = ranges;

tests/cases/fourslash/referencesBloomFilters2.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
// Ensure BloomFilter building logic is correct, by having one reference per file
44

55
// @Filename: declaration.ts
6-
////var container = { [|{| "isDefinition": true |}42|]: 1 };
6+
////var container = { [|{| "isWriteAccess": true, "isDefinition": true |}42|]: 1 };
77

88
// @Filename: expression.ts
99
////function blah() { return (container[[|42|]]) === 2; };
@@ -12,7 +12,7 @@
1212
////function blah2() { container["[|42|]"] };
1313

1414
// @Filename: redeclaration.ts
15-
////container = { "[|{| "isDefinition": true |}42|]" : 18 };
15+
////container = { "[|{| "isWriteAccess": true, "isDefinition": true |}42|]" : 18 };
1616

1717
const ranges = test.ranges();
1818
const [r0, r1, r2, r3] = ranges;

tests/cases/fourslash/referencesBloomFilters3.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55

66
// @Filename: declaration.ts
7-
////enum Test { "[|{| "isDefinition": true |}42|]" = 1 };
7+
////enum Test { "[|{| "isWriteAccess": true, "isDefinition": true |}42|]" = 1 };
88

99
// @Filename: expression.ts
1010
////(Test[[|42|]]);

tests/cases/fourslash/referencesForAmbients.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
/// <reference path='fourslash.ts'/>
22

3-
////declare module "[|{| "isDefinition": true |}foo|]" {
3+
////declare module "[|{| "isWriteAccess": true, "isDefinition": true |}foo|]" {
44
//// var [|{| "isWriteAccess": true, "isDefinition": true |}f|]: number;
55
////}
66
////
7-
////declare module "[|{| "isDefinition": true |}bar|]" {
7+
////declare module "[|{| "isWriteAccess": true, "isDefinition": true |}bar|]" {
88
//// export import [|{| "isWriteAccess": true, "isDefinition": true |}foo|] = require("[|foo|]");
99
//// var f2: typeof [|foo|].[|f|];
1010
////}

0 commit comments

Comments
 (0)