Skip to content

getOccurrencesAtPosition Implemented #653

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
merged 28 commits into from
Sep 11, 2014
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
28 commits
Select commit Hold shift + click to select a range
24f6e41
Added getOccs support for if/else keywords, and some tests.
DanielRosenwasser Aug 29, 2014
0632d0c
Addressed CR feedback, no longer highlighting elseifs with comments b…
DanielRosenwasser Aug 29, 2014
0a5c12c
Added test case for broken if-elses.
DanielRosenwasser Aug 29, 2014
38d7ba6
Added missing break statement.
DanielRosenwasser Aug 29, 2014
3f3dd29
Use isWhitespace in getIfElseOccurrences.
DanielRosenwasser Aug 29, 2014
fbb10cd
Added getOccs support for return keywords.
DanielRosenwasser Aug 29, 2014
7e58021
Added tests for getOccs on return keywords.
DanielRosenwasser Aug 29, 2014
ba396ed
Utilize getContainingFunction in services.
DanielRosenwasser Aug 30, 2014
e060a8e
Merge pull request #561 from Microsoft/getOccurrencesIfElseIfElseIfElse
DanielRosenwasser Sep 3, 2014
837ddda
Addressed CR feedback.
DanielRosenwasser Sep 4, 2014
294ad06
Merge branch 'getOccurrences' of https://github.com/Microsoft/TypeScr…
DanielRosenwasser Sep 4, 2014
7b5440b
Addressed more CR feedback.
DanielRosenwasser Sep 4, 2014
492e1c5
Merge pull request #593 from Microsoft/getOccurrencesReturn
DanielRosenwasser Sep 4, 2014
e6e9979
getRefs/getOccs support for 'this' keyword.
DanielRosenwasser Sep 2, 2014
a6a6d77
Added fourslash tests for 'this' keyword findOccs/findRefs.
DanielRosenwasser Sep 3, 2014
84e385d
Made a getThisContainer function.
DanielRosenwasser Sep 5, 2014
024ca6d
Addressed CR feedback.
DanielRosenwasser Sep 5, 2014
1121e11
Basic implementation without tests for findAllRefs/getOccs for 'super…
DanielRosenwasser Sep 6, 2014
1cd0b30
Added tests for getOccurrences on super.
DanielRosenwasser Sep 8, 2014
dd5d216
Merge pull request #600 from Microsoft/getOccurrencesThis
DanielRosenwasser Sep 9, 2014
131ac2f
Disabled findAllRefs for 'this'/'super'.
DanielRosenwasser Sep 9, 2014
0e93d28
Separated 'super'/'this' keyword searching to simplify logic.
DanielRosenwasser Sep 9, 2014
83c35ad
Addressed CR feedback.
DanielRosenwasser Sep 9, 2014
e1f8aa7
Merge pull request #632 from Microsoft/getOccurrencesSuperThanksForAs…
DanielRosenwasser Sep 9, 2014
6cc0305
Implemented getOccurrences for 'constructor' keywords.
DanielRosenwasser Sep 9, 2014
6ec9786
Merge pull request #639 from Microsoft/getOccurrencesConstructor
DanielRosenwasser Sep 9, 2014
480aac9
Merge branch 'master' into getOccurrences
DanielRosenwasser Sep 11, 2014
0af26bc
Merge branch 'master' into getOccurrences
DanielRosenwasser Sep 11, 2014
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
76 changes: 7 additions & 69 deletions src/compiler/checker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3468,29 +3468,6 @@ module ts {
return getTypeOfSymbol(getExportSymbolOfValueSymbolIfExported(symbol));
}

function getThisContainer(node: Node): Node {
while (true) {
node = node.parent;
if (!node) {
return node;
}
switch (node.kind) {
case SyntaxKind.FunctionDeclaration:
case SyntaxKind.FunctionExpression:
case SyntaxKind.ModuleDeclaration:
case SyntaxKind.Property:
case SyntaxKind.Method:
case SyntaxKind.Constructor:
case SyntaxKind.GetAccessor:
case SyntaxKind.SetAccessor:
case SyntaxKind.EnumDeclaration:
case SyntaxKind.SourceFile:
case SyntaxKind.ArrowFunction:
return node;
}
}
}

function captureLexicalThis(node: Node, container: Node): void {
var classNode = container.parent && container.parent.kind === SyntaxKind.ClassDeclaration ? container.parent : undefined;
getNodeLinks(node).flags |= NodeCheckFlags.LexicalThis;
Expand All @@ -3503,11 +3480,14 @@ module ts {
}

function checkThisExpression(node: Node): Type {
var container = getThisContainer(node);
// Stop at the first arrow function so that we can
// tell whether 'this' needs to be captured.
var container = getThisContainer(node, /* includeArrowFunctions */ true);
var needToCaptureLexicalThis = false;
// skip arrow functions
while (container.kind === SyntaxKind.ArrowFunction) {
container = getThisContainer(container);

// Now skip arrow functions to get the "real" owner of 'this'.
if (container.kind === SyntaxKind.ArrowFunction) {
container = getThisContainer(container, /* includeArrowFunctions */ false);
needToCaptureLexicalThis = true;
}

Expand Down Expand Up @@ -4407,37 +4387,6 @@ module ts {
return voidType;
}

// WARNING: This has the same semantics as the forEach family of functions,
// in that traversal terminates in the event that 'visitor' supplies a truthy value.
function forEachReturnStatement<T>(body: Block, visitor: (stmt: ReturnStatement) => T): T {

return traverse(body);

function traverse(node: Node): T {
switch (node.kind) {
case SyntaxKind.ReturnStatement:
return visitor(node);
case SyntaxKind.Block:
case SyntaxKind.FunctionBlock:
case SyntaxKind.IfStatement:
case SyntaxKind.DoStatement:
case SyntaxKind.WhileStatement:
case SyntaxKind.ForStatement:
case SyntaxKind.ForInStatement:
case SyntaxKind.WithStatement:
case SyntaxKind.SwitchStatement:
case SyntaxKind.CaseClause:
case SyntaxKind.DefaultClause:
case SyntaxKind.LabelledStatement:
case SyntaxKind.TryStatement:
case SyntaxKind.TryBlock:
case SyntaxKind.CatchBlock:
case SyntaxKind.FinallyBlock:
return forEachChild(node, traverse);
}
}
}

/// Returns a set of types relating to every return expression relating to a function block.
function checkAndAggregateReturnExpressionTypes(body: Block, contextualMapper?: TypeMapper): Type[] {
var aggregatedTypes: Type[] = [];
Expand Down Expand Up @@ -5856,17 +5805,6 @@ module ts {
// TODO: Check that target label is valid
}

function getContainingFunction(node: Node): SignatureDeclaration {
while (true) {
node = node.parent;
if (!node || node.kind === SyntaxKind.FunctionDeclaration || node.kind === SyntaxKind.FunctionExpression ||
node.kind === SyntaxKind.ArrowFunction || node.kind === SyntaxKind.Method || node.kind === SyntaxKind.Constructor ||
node.kind === SyntaxKind.GetAccessor || node.kind === SyntaxKind.SetAccessor) {
return <SignatureDeclaration>node;
}
}
}

function checkReturnStatement(node: ReturnStatement) {
if (node.expression && !(getNodeLinks(node.expression).flags & NodeCheckFlags.TypeChecked)) {
var func = getContainingFunction(node);
Expand Down
101 changes: 101 additions & 0 deletions src/compiler/parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -350,6 +350,107 @@ module ts {
}
}

// Warning: This has the same semantics as the forEach family of functions,
// in that traversal terminates in the event that 'visitor' supplies a truthy value.
export function forEachReturnStatement<T>(body: Block, visitor: (stmt: ReturnStatement) => T): T {

return traverse(body);

function traverse(node: Node): T {
switch (node.kind) {
case SyntaxKind.ReturnStatement:
return visitor(node);
case SyntaxKind.Block:
case SyntaxKind.FunctionBlock:
case SyntaxKind.IfStatement:
case SyntaxKind.DoStatement:
case SyntaxKind.WhileStatement:
case SyntaxKind.ForStatement:
case SyntaxKind.ForInStatement:
case SyntaxKind.WithStatement:
case SyntaxKind.SwitchStatement:
case SyntaxKind.CaseClause:
case SyntaxKind.DefaultClause:
case SyntaxKind.LabelledStatement:
case SyntaxKind.TryStatement:
case SyntaxKind.TryBlock:
case SyntaxKind.CatchBlock:
case SyntaxKind.FinallyBlock:
return forEachChild(node, traverse);
}
}
}

export function isAnyFunction(node: Node): boolean {
if (node) {
switch (node.kind) {
case SyntaxKind.FunctionExpression:
case SyntaxKind.FunctionDeclaration:
case SyntaxKind.ArrowFunction:
case SyntaxKind.Method:
case SyntaxKind.GetAccessor:
case SyntaxKind.SetAccessor:
case SyntaxKind.Constructor:
return true;
}
}

return false;
}

export function getContainingFunction(node: Node): SignatureDeclaration {
while (true) {
node = node.parent;
if (!node || isAnyFunction(node)) {
return <SignatureDeclaration>node;
}
}
}

export function getThisContainer(node: Node, includeArrowFunctions: boolean): Node {
while (true) {
node = node.parent;
if (!node) {
return undefined;
}
switch (node.kind) {
case SyntaxKind.ArrowFunction:
if (!includeArrowFunctions) {
continue;
}
// Fall through
case SyntaxKind.FunctionDeclaration:
case SyntaxKind.FunctionExpression:
case SyntaxKind.ModuleDeclaration:
case SyntaxKind.Property:
case SyntaxKind.Method:
case SyntaxKind.Constructor:
case SyntaxKind.GetAccessor:
case SyntaxKind.SetAccessor:
case SyntaxKind.EnumDeclaration:
case SyntaxKind.SourceFile:
return node;
}
}
}

export function getSuperContainer(node: Node): Node {
while (true) {
node = node.parent;
if (!node) {
return undefined;
}
switch (node.kind) {
case SyntaxKind.Property:
case SyntaxKind.Method:
case SyntaxKind.Constructor:
case SyntaxKind.GetAccessor:
case SyntaxKind.SetAccessor:
return node;
}
}
}

export function hasRestParameters(s: SignatureDeclaration): boolean {
return s.parameters.length > 0 && (s.parameters[s.parameters.length - 1].flags & NodeFlags.Rest) !== 0;
}
Expand Down
2 changes: 1 addition & 1 deletion src/compiler/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -220,7 +220,7 @@ module ts {
LastFutureReservedWord = YieldKeyword,
FirstTypeNode = TypeReference,
LastTypeNode = ArrayType,
FirstPunctuation= OpenBraceToken,
FirstPunctuation = OpenBraceToken,
LastPunctuation = CaretEqualsToken
}

Expand Down
Loading