Handel getRefrences for labels
This commit is contained in:
parent
69c653d5e8
commit
bfd13347d4
5 changed files with 92 additions and 5 deletions
|
@ -1264,7 +1264,7 @@ module ts {
|
|||
(<LabelledStatement>node.parent).label === node;
|
||||
}
|
||||
|
||||
function isLabel(node: Node): boolean {
|
||||
function isLabelName(node: Node): boolean {
|
||||
return isLabelOfLabeledStatement(node) || isJumpStatementTarget(node);
|
||||
}
|
||||
|
||||
|
@ -1881,6 +1881,7 @@ module ts {
|
|||
return node;
|
||||
}
|
||||
switch (node.kind) {
|
||||
case SyntaxKind.SourceFile:
|
||||
case SyntaxKind.Method:
|
||||
case SyntaxKind.FunctionDeclaration:
|
||||
case SyntaxKind.FunctionExpression:
|
||||
|
@ -2105,12 +2106,21 @@ module ts {
|
|||
}
|
||||
|
||||
// Labels
|
||||
if (isLabel(node)) {
|
||||
if (isLabelName(node)) {
|
||||
var labelName = (<Identifier>node).text;
|
||||
var labelDefinition = isJumpStatementTarget(node) ? getTargetLabel((<BreakOrContinueStatement>node.parent), (<Identifier>node).text) : node;
|
||||
|
||||
/// TODO handel labels
|
||||
return undefined;
|
||||
var labelScope: Node;
|
||||
|
||||
if (isJumpStatementTarget(node)) {
|
||||
var labelDefinition = getTargetLabel((<BreakOrContinueStatement>node.parent), (<Identifier>node).text);
|
||||
labelScope = labelDefinition ? labelDefinition.parent : getContainerNode(node);
|
||||
}
|
||||
else {
|
||||
// it is a label definition and not a target, the scope is the labeledStatement
|
||||
labelScope = node.parent;
|
||||
}
|
||||
|
||||
return getLabelReferencesInNode(labelScope, labelName);
|
||||
}
|
||||
|
||||
var symbol = typeChecker.getSymbolInfo(node);
|
||||
|
@ -2192,6 +2202,23 @@ module ts {
|
|||
return positions;
|
||||
}
|
||||
|
||||
function getLabelReferencesInNode(container: Node, labelName: string): ReferenceEntry[] {
|
||||
var result: ReferenceEntry[] = [];
|
||||
var sourceFile = container.getSourceFile();
|
||||
var possiblePositions = getPossibleSymbolReferencePositions(sourceFile, labelName, container.getStart(), container.getEnd());
|
||||
if (possiblePositions && possiblePositions.length > 0) {
|
||||
possiblePositions.forEach(position => {
|
||||
cancellationToken.throwIfCancellationRequested();
|
||||
|
||||
var node = getNodeAtPosition(sourceFile, position);
|
||||
if (node && isLabelName(node) && node.getWidth() === labelName.length) {
|
||||
result.push(getReferenceEntry(node));
|
||||
}
|
||||
});
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
function getReferencesInNode(container: Node, searchSymbol: Symbol, result: ReferenceEntry[]): void {
|
||||
var searchSymbolName = searchSymbol.getName();
|
||||
|
||||
|
|
23
tests/cases/fourslash/referencesForLabel.ts
Normal file
23
tests/cases/fourslash/referencesForLabel.ts
Normal file
|
@ -0,0 +1,23 @@
|
|||
/// <reference path='fourslash.ts'/>
|
||||
|
||||
// Valid References for a label
|
||||
|
||||
/////*1*/label: while (true) {
|
||||
//// if (false) break /*2*/label;
|
||||
//// if (true) continue /*3*/label;
|
||||
////}
|
||||
////
|
||||
/////*4*/label: while (false) { }
|
||||
////var label = "label";
|
||||
|
||||
goTo.marker("1");
|
||||
verify.referencesCountIs(3);
|
||||
|
||||
goTo.marker("2");
|
||||
verify.referencesCountIs(3);
|
||||
|
||||
goTo.marker("3");
|
||||
verify.referencesCountIs(3);
|
||||
|
||||
goTo.marker("4");
|
||||
verify.referencesCountIs(1);
|
12
tests/cases/fourslash/referencesForLabel2.ts
Normal file
12
tests/cases/fourslash/referencesForLabel2.ts
Normal file
|
@ -0,0 +1,12 @@
|
|||
/// <reference path='fourslash.ts'/>
|
||||
|
||||
// References to undefined label
|
||||
|
||||
////var label = "label";
|
||||
////while (true) {
|
||||
//// if (false) break /*1*/label;
|
||||
//// if (true) continue label;
|
||||
////}
|
||||
|
||||
goTo.marker("1");
|
||||
verify.referencesCountIs(2);
|
10
tests/cases/fourslash/referencesForLabel3.ts
Normal file
10
tests/cases/fourslash/referencesForLabel3.ts
Normal file
|
@ -0,0 +1,10 @@
|
|||
/// <reference path='fourslash.ts'/>
|
||||
|
||||
// References to unused label
|
||||
|
||||
/////*1*/label: while (true) {
|
||||
//// var label = "label";
|
||||
////}
|
||||
|
||||
goTo.marker("1");
|
||||
verify.referencesCountIs(1);
|
15
tests/cases/fourslash/referencesForLabel4.ts
Normal file
15
tests/cases/fourslash/referencesForLabel4.ts
Normal file
|
@ -0,0 +1,15 @@
|
|||
/// <reference path='fourslash.ts'/>
|
||||
|
||||
// References to a label outside function bounderies
|
||||
|
||||
/////*1*/label: function foo(label) {
|
||||
//// while (true) {
|
||||
//// break /*2*/label;
|
||||
//// }
|
||||
////}
|
||||
|
||||
goTo.marker("1");
|
||||
verify.referencesCountIs(2);
|
||||
|
||||
goTo.marker("2");
|
||||
verify.referencesCountIs(2);
|
Loading…
Reference in a new issue