Skip to content

Commit

Permalink
fix: codeintel: Javascript: Enable local variable visibility for clos…
Browse files Browse the repository at this point in the history
…ures - fixes #502

rn=

(integrated from master branch change 9.2.1-742-g1ed9af7 by Mitchell <mitchellb@activestate.com>)
  • Loading branch information
mitchell-as committed Dec 11, 2015
1 parent c4c16e0 commit 5046f56
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 5 deletions.
8 changes: 3 additions & 5 deletions src/codeintel/lib/codeintel2/tree_javascript.py
Expand Up @@ -971,9 +971,7 @@ def _completion_names_from_scope(self, expr, scoperef):
# From the local scope, walk up the parent chain including matches as
# we go.
# XXX - Can we skip the global (stdlib) blob in here?
loopcount = -1
while scoperef and scoperef[0] is not None:
loopcount += 1
# Iterate over the contents of the scope.
self.log("_completion_names_from_scope:: checking scoperef: %r",
scoperef)
Expand All @@ -986,9 +984,9 @@ def _completion_names_from_scope(self, expr, scoperef):
if name and name.startswith(expr):
if name not in all_completions:
hit_elem = elem.names[name]
if loopcount and "__local__" in hit_elem.get("attributes", "").split():
# Skip things that should only be local to the
# original scope.
if elem.get("name") == "*" and "__local__" in hit_elem.get("attributes", "").split():
# Skip any locals in the global namespace, but
# ensure locals are included (e.g. via closures).
#self.log("_completion_names_from_scope:: skipping local %r",
# name)
continue
Expand Down
23 changes: 23 additions & 0 deletions src/codeintel/test2/test_javascript.py
Expand Up @@ -1240,6 +1240,29 @@ def test_scoped_variable_class_constructor(self):
[("variable", "sectionlistController")])
self.assertCompletionsAre(markup_text(content, positions[2]),
[("function", "initialize")])

def test_closure(self):
"""
Scoped variables in class constructors should be closures accessible
from class functions and throughout the constructor.
"""
content, positions = unmark_text(dedent("""\
function History(element){
var self = this;
var stack = [];
var stack_position = -1;
var stack_length = 0;
this.push = function() {
sta<1>
}
sta<2>
}
"""))
for i in xrange(2):
self.assertCompletionsInclude(markup_text(content, positions[i+1]),
[("variable", "stack"),
("variable", "stack_length"),
("variable", "stack_position")])

class CalltipTestCase(CodeIntelTestCase):
lang = "JavaScript"
Expand Down

0 comments on commit 5046f56

Please sign in to comment.