-
Notifications
You must be signed in to change notification settings - Fork 13.4k
Get occurrences for switch/case/default/break keywords. #549
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
DanielRosenwasser
merged 13 commits into
getReferences
from
getOccurrences_switchCaseDefault
Aug 28, 2014
Merged
Changes from all commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
5d15cd2
Beginning special casing for getOccurrencesAtPosition.
DanielRosenwasser 558be4e
Implemented getOccsAtPos for try-catch-finally.
DanielRosenwasser 1f77198
Made getOccs more resilient.
DanielRosenwasser 8ab4df0
Added tests.
DanielRosenwasser 88f37e5
Support for switch/case/default/break in getOccs
DanielRosenwasser 3825c9b
Handled function boundaries.
DanielRosenwasser 232e513
Moved null-guards to appropriate places, added helpers.
DanielRosenwasser 50d0cdc
Better coverage against function boundaries.
DanielRosenwasser ea613fd
Replaced ES5 functions with analogous core.ts ones.
DanielRosenwasser 04456a2
Made 'isAnyFunction' more exhaustive as it should be.
DanielRosenwasser 0ce39a3
Addressed CR feedback.
DanielRosenwasser fd93a3b
What's in a name anyhow?
DanielRosenwasser 813f28d
Removed assertion.
DanielRosenwasser File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -1314,12 +1314,13 @@ module ts { | |
|
|
||
| function isAnyFunction(node: Node): boolean { | ||
| switch (node.kind) { | ||
| case SyntaxKind.FunctionExpression: | ||
| case SyntaxKind.FunctionDeclaration: | ||
| case SyntaxKind.ArrowFunction: | ||
| case SyntaxKind.Method: | ||
| case SyntaxKind.FunctionExpression: | ||
| case SyntaxKind.GetAccessor: | ||
| case SyntaxKind.SetAccessor: | ||
| case SyntaxKind.ArrowFunction: | ||
| case SyntaxKind.Constructor: | ||
| return true; | ||
| } | ||
| return false; | ||
|
|
@@ -1933,7 +1934,9 @@ module ts { | |
| current = child; | ||
| continue outer; | ||
| } | ||
| if (child.end > position) break; | ||
| if (child.end > position) { | ||
| break; | ||
| } | ||
| } | ||
| return current; | ||
| } | ||
|
|
@@ -2160,13 +2163,143 @@ module ts { | |
| return undefined; | ||
| } | ||
|
|
||
| if (node.kind !== SyntaxKind.Identifier && | ||
| !isLiteralNameOfPropertyDeclarationOrIndexAccess(node) && | ||
| !isNameOfExternalModuleImportOrDeclaration(node)) { | ||
| if (node.kind === SyntaxKind.Identifier || isLiteralNameOfPropertyDeclarationOrIndexAccess(node) || isNameOfExternalModuleImportOrDeclaration(node)) { | ||
| return getReferencesForNode(node, [sourceFile]); | ||
| } | ||
|
|
||
| switch (node.kind) { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this comment should be on the actual parent/hasKind functions. |
||
| case SyntaxKind.TryKeyword: | ||
| case SyntaxKind.CatchKeyword: | ||
| case SyntaxKind.FinallyKeyword: | ||
| if (hasKind(parent(parent(node)), SyntaxKind.TryStatement)) { | ||
| return getTryCatchFinallyOccurrences(<TryStatement>node.parent.parent); | ||
| } | ||
| break; | ||
| case SyntaxKind.SwitchKeyword: | ||
| if (hasKind(node.parent, SyntaxKind.SwitchStatement)) { | ||
| return getSwitchCaseDefaultOccurrences(<SwitchStatement>node.parent); | ||
| } | ||
| break; | ||
| case SyntaxKind.CaseKeyword: | ||
| case SyntaxKind.DefaultKeyword: | ||
| if (hasKind(parent(parent(node)), SyntaxKind.SwitchStatement)) { | ||
| return getSwitchCaseDefaultOccurrences(<SwitchStatement>node.parent.parent); | ||
| } | ||
| break; | ||
| case SyntaxKind.BreakKeyword: | ||
| if (hasKind(node.parent, SyntaxKind.BreakStatement)) { | ||
| return getBreakStatementOccurences(<BreakOrContinueStatement>node.parent); | ||
| } | ||
| break; | ||
| } | ||
|
|
||
| return undefined; | ||
|
|
||
| function getTryCatchFinallyOccurrences(tryStatement: TryStatement): ReferenceEntry[] { | ||
| var keywords: Node[] = []; | ||
|
|
||
| pushKeywordIf(keywords, tryStatement.getFirstToken(), SyntaxKind.TryKeyword); | ||
|
|
||
| if (tryStatement.catchBlock) { | ||
| pushKeywordIf(keywords, tryStatement.catchBlock.getFirstToken(), SyntaxKind.CatchKeyword); | ||
| } | ||
|
|
||
| if (tryStatement.finallyBlock) { | ||
| pushKeywordIf(keywords, tryStatement.finallyBlock.getFirstToken(), SyntaxKind.FinallyKeyword); | ||
| } | ||
|
|
||
| return keywordsToReferenceEntries(keywords); | ||
| } | ||
|
|
||
| function getSwitchCaseDefaultOccurrences(switchStatement: SwitchStatement) { | ||
| var keywords: Node[] = []; | ||
|
|
||
| pushKeywordIf(keywords, switchStatement.getFirstToken(), SyntaxKind.SwitchKeyword); | ||
|
|
||
| // Go through each clause in the switch statement, collecting the clause keywords. | ||
| forEach(switchStatement.clauses, clause => { | ||
| pushKeywordIf(keywords, clause.getFirstToken(), SyntaxKind.CaseKeyword, SyntaxKind.DefaultKeyword); | ||
|
|
||
| // For each clause, also recursively traverse the statements where we can find analogous breaks. | ||
| forEachChild(clause, function aggregateBreakKeywords(node: Node): void { | ||
| switch (node.kind) { | ||
| case SyntaxKind.BreakStatement: | ||
| // If the break statement has a label, it cannot be part of a switch block. | ||
| if (!(<BreakOrContinueStatement>node).label) { | ||
| pushKeywordIf(keywords, node.getFirstToken(), SyntaxKind.BreakKeyword); | ||
| } | ||
| // Fall through | ||
| case SyntaxKind.ForStatement: | ||
| case SyntaxKind.ForInStatement: | ||
| case SyntaxKind.DoStatement: | ||
| case SyntaxKind.WhileStatement: | ||
| case SyntaxKind.SwitchStatement: | ||
| return; | ||
| } | ||
|
|
||
| // Do not cross function boundaries. | ||
| if (!isAnyFunction(node)) { | ||
| forEachChild(node, aggregateBreakKeywords); | ||
| } | ||
| }); | ||
| }); | ||
|
|
||
| return keywordsToReferenceEntries(keywords); | ||
| } | ||
|
|
||
| function getBreakStatementOccurences(breakStatement: BreakOrContinueStatement): ReferenceEntry[]{ | ||
| // TODO (drosen): Deal with labeled statements. | ||
| if (breakStatement.label) { | ||
| return undefined; | ||
| } | ||
|
|
||
| for (var owner = node.parent; owner; owner = owner.parent) { | ||
| switch (owner.kind) { | ||
| case SyntaxKind.ForStatement: | ||
| case SyntaxKind.ForInStatement: | ||
| case SyntaxKind.DoStatement: | ||
| case SyntaxKind.WhileStatement: | ||
| // TODO (drosen): Handle loops! | ||
| return undefined; | ||
|
|
||
| case SyntaxKind.SwitchStatement: | ||
| return getSwitchCaseDefaultOccurrences(<SwitchStatement>owner); | ||
|
|
||
| default: | ||
| if (isAnyFunction(owner)) { | ||
| return undefined; | ||
| } | ||
| } | ||
| } | ||
|
|
||
| return undefined; | ||
| } | ||
|
|
||
| return getReferencesForNode(node, [sourceFile]); | ||
| // returns true if 'node' is defined and has a matching 'kind'. | ||
| function hasKind(node: Node, kind: SyntaxKind) { | ||
| return !!(node && node.kind === kind); | ||
| } | ||
|
|
||
| // Null-propagating 'parent' function. | ||
| function parent(node: Node): Node { | ||
| return node && node.parent; | ||
| } | ||
|
|
||
| function pushKeywordIf(keywordList: Node[], token: Node, ...expected: SyntaxKind[]): void { | ||
| if (!token) { | ||
| return; | ||
| } | ||
|
|
||
| if (contains(<SyntaxKind[]>expected, token.kind)) { | ||
| keywordList.push(token); | ||
| } | ||
| } | ||
|
|
||
| function keywordsToReferenceEntries(keywords: Node[]): ReferenceEntry[]{ | ||
| return map(keywords, keyword => | ||
| new ReferenceEntry(filename, TypeScript.TextSpan.fromBounds(keyword.getStart(), keyword.end), /* isWriteAccess */ false) | ||
| ); | ||
| } | ||
| } | ||
|
|
||
| function getReferencesAtPosition(filename: string, position: number): ReferenceEntry[] { | ||
|
|
@@ -2284,13 +2417,13 @@ module ts { | |
| var container = getContainerNode(declarations[i]); | ||
|
|
||
| if (scope && scope !== container) { | ||
| // Diffrent declarations have diffrent containers, bail out | ||
| // Different declarations have different containers, bail out | ||
| return undefined; | ||
| } | ||
|
|
||
| if (container.kind === SyntaxKind.SourceFile && !isExternalModule(<SourceFile>container)) { | ||
| // This is a global variable and not an external module, any declaration defined | ||
| // withen this scope is visible outside the file | ||
| // within this scope is visible outside the file | ||
| return undefined; | ||
| } | ||
|
|
||
|
|
@@ -2957,7 +3090,7 @@ module ts { | |
| // ["// hack 1", "// ", "hack 1", undefined, "hack"] | ||
| // | ||
| // Here are the relevant capture groups: | ||
| // 0) The full match for hte entire regex. | ||
| // 0) The full match for the entire regex. | ||
| // 1) The preamble to the message portion. | ||
| // 2) The message portion. | ||
| // 3...N) The descriptor that was matched - by index. 'undefined' for each | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,29 @@ | ||
| /// <reference path='fourslash.ts' /> | ||
|
|
||
| ////[|sw/*1*/itch|] (10) { | ||
| //// [|/*2*/case|] 1: | ||
| //// [|cas/*3*/e|] 2: | ||
| //// [|c/*4*/ase|] 4: | ||
| //// [|c/*5*/ase|] 8: | ||
| //// foo: switch (20) { | ||
| //// case 1: | ||
| //// case 2: | ||
| //// break; | ||
| //// default: | ||
| //// break foo; | ||
| //// } | ||
| //// [|cas/*6*/e|] 0xBEEF: | ||
| //// [|defa/*7*/ult|]: | ||
| //// [|bre/*9*/ak|]; | ||
| //// [|/*8*/case|] 16: | ||
| ////} | ||
|
|
||
|
|
||
| for (var i = 1; i <= test.markers().length; i++) { | ||
| goTo.marker("" + i); | ||
| verify.occurrencesAtPositionCount(9); | ||
|
|
||
| test.ranges().forEach(range => { | ||
| verify.occurrencesAtPositionContains(range, false); | ||
| }); | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,29 @@ | ||
| /// <reference path='fourslash.ts' /> | ||
|
|
||
| ////switch (10) { | ||
| //// case 1: | ||
| //// case 2: | ||
| //// case 4: | ||
| //// case 8: | ||
| //// foo: [|swi/*1*/tch|] (20) { | ||
| //// [|/*2*/case|] 1: | ||
| //// [|cas/*3*/e|] 2: | ||
| //// [|b/*4*/reak|]; | ||
| //// [|defaul/*5*/t|]: | ||
| //// break foo; | ||
| //// } | ||
| //// case 0xBEEF: | ||
| //// default: | ||
| //// break; | ||
| //// case 16: | ||
| ////} | ||
|
|
||
|
|
||
| for (var i = 1; i <= test.markers().length; i++) { | ||
| goTo.marker("" + i); | ||
| verify.occurrencesAtPositionCount(5); | ||
|
|
||
| test.ranges().forEach(range => { | ||
| verify.occurrencesAtPositionContains(range, false); | ||
| }); | ||
| } |
57 changes: 57 additions & 0 deletions
57
tests/cases/fourslash/getOccurrencesSwitchCaseDefaultBroken.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,57 @@ | ||
| /// <reference path='fourslash.ts' /> | ||
|
|
||
| ////swi/*1*/tch(10) { | ||
| //// case 1: | ||
| //// case 2: | ||
| //// c/*2*/ase 4: | ||
| //// case 8: | ||
| //// case 0xBEEF: | ||
| //// de/*4*/fult: | ||
| //// break; | ||
| //// /*5*/cas 16: | ||
| //// c/*3*/ase 12: | ||
| //// function f() { | ||
| //// br/*11*/eak; | ||
| //// /*12*/break; | ||
| //// } | ||
| ////} | ||
| //// | ||
| ////sw/*6*/itch (10) { | ||
| //// de/*7*/fault | ||
| //// case 1: | ||
| //// case 2 | ||
| //// | ||
| //// c/*8*/ose 4: | ||
| //// case 8: | ||
| //// case 0xBEEF: | ||
| //// bre/*9*/ak; | ||
| //// case 16: | ||
| //// () => bre/*10*/ak; | ||
| ////} | ||
|
|
||
| for (var i = 1; i <= test.markers().length; i++) { | ||
| goTo.marker("" + i); | ||
|
|
||
| switch (i) { | ||
| case 1: | ||
| case 2: | ||
| case 3: | ||
| verify.occurrencesAtPositionCount(8); | ||
| break; | ||
| case 4: | ||
| case 5: | ||
| case 8: | ||
| verify.occurrencesAtPositionCount(1); | ||
| break; | ||
| case 6: | ||
| case 7: | ||
| case 9: | ||
| verify.occurrencesAtPositionCount(8); | ||
| break; | ||
| case 10: | ||
| case 11: | ||
| case 12: | ||
| verify.occurrencesAtPositionCount(0); | ||
| break; | ||
| } | ||
| } |
25 changes: 25 additions & 0 deletions
25
tests/cases/fourslash/getOccurrencesSwitchCaseDefaultNegatives.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,25 @@ | ||
| /// <reference path='fourslash.ts' /> | ||
|
|
||
| ////switch/*1*/ (10) { | ||
| //// case/*2*/ 1: | ||
| //// case/*3*/ 2: | ||
| //// case/*4*/ 4: | ||
| //// case/*5*/ 8: | ||
| //// foo: switch/*6*/ (20) { | ||
| //// case/*7*/ 1: | ||
| //// case/*8*/ 2: | ||
| //// break/*9*/; | ||
| //// default/*10*/: | ||
| //// break foo; | ||
| //// } | ||
| //// case/*11*/ 0xBEEF: | ||
| //// default/*12*/: | ||
| //// break/*13*/; | ||
| //// case 16/*14*/: | ||
| ////} | ||
|
|
||
|
|
||
| for (var i = 1; i <= test.markers().length; i++) { | ||
| goTo.marker("" + i); | ||
| verify.occurrencesAtPositionCount(0); | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
please wrap this. if it's too long to fit in a CR, it's too long :)