Skip to content

Commit a40bf5e

Browse files
vilchik-elenapynicolas
authored andcommitted
Fix scope of function declaration (#286)
1 parent 1ee599c commit a40bf5e

File tree

4 files changed

+14
-8
lines changed

4 files changed

+14
-8
lines changed

its/ruling/src/test/expected/javascript-UnusedVariable.json

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -653,18 +653,13 @@
653653
194,
654654
],
655655
'project:dojo-1.8.1/dojox/mvc/_atBindingMixin.js.uncompressed.js':[
656-
10,
657-
14,
658656
32,
659657
],
660658
'project:dojo-1.8.1/dojox/mvc/parserExtension.js.uncompressed.js':[
661659
36,
662660
64,
663661
64,
664662
],
665-
'project:dojo-1.8.1/dojox/mvc/sync.js.uncompressed.js':[
666-
55,
667-
],
668663
'project:dojo-1.8.1/dojox/rails.js.uncompressed.js':[
669664
82,
670665
],

javascript-frontend/src/main/java/org/sonar/javascript/tree/symbols/HoistedSymbolVisitor.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -204,7 +204,7 @@ public void visitCatchBlock(CatchBlockTree tree) {
204204

205205
@Override
206206
public void visitFunctionDeclaration(FunctionDeclarationTree tree) {
207-
symbolModel.declareSymbol(tree.name().name(), Symbol.Kind.FUNCTION, currentScope)
207+
symbolModel.declareSymbol(tree.name().name(), Symbol.Kind.FUNCTION, getFunctionScope())
208208
.addUsage(Usage.create(tree.name(), Usage.Kind.DECLARATION));
209209

210210
enterScope(tree);

javascript-frontend/src/test/java/org/sonar/javascript/tree/symbols/ScopeTest.java

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ private Set<String> symbols(Scope scope) {
5959

6060
@Test
6161
public void scopes_number() throws Exception {
62-
assertThat(SYMBOL_MODEL.getScopes()).hasSize(23);
62+
assertThat(SYMBOL_MODEL.getScopes()).hasSize(25);
6363
}
6464

6565
@Test
@@ -69,7 +69,7 @@ public void test_global_scope() throws Exception {
6969
assertThat(scope.isGlobal()).isTrue();
7070
assertThat(scope.isBlock()).isFalse();
7171
assertThat(scopeAtLine(1)).isEqualTo(scope);
72-
assertThat(symbols(scope)).containsOnly("a", "b", "f", "const1", "let1", "c", "A", "notBlock", "gen", "catch1", "try2", "identifier", "foobar");
72+
assertThat(symbols(scope)).containsOnly("a", "b", "f", "const1", "let1", "c", "A", "notBlock", "gen", "catch1", "try2", "identifier", "foobar", "globalFunction");
7373
}
7474

7575
@Test
@@ -316,4 +316,11 @@ public void test_identifier_scope() throws Exception {
316316
assertThat(identifier.scope()).isEqualTo(usageScopes.get(identifier.identifierToken().line()));
317317
}
318318
}
319+
320+
@Test
321+
public void test_global_function_declaration_in_block() throws Exception {
322+
Symbol globalFunction = SYMBOL_MODEL.getSymbols("globalFunction").iterator().next();
323+
assertThat(globalFunction.scope().isGlobal()).isTrue();
324+
325+
}
319326
}

javascript-frontend/src/test/resources/ast/resolve/scope.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,3 +111,7 @@ if (true) {
111111
function foobar() {
112112
console.log(identifier);
113113
}
114+
115+
if (condition) {
116+
function globalFunction(){}
117+
}

0 commit comments

Comments
 (0)