Debian Codegraph @

test/com/google/javascript/jscomp/SyntacticScopeCreatorTest.java

raw symbols SCIP
1 /*
2 * Copyright 2007 The Closure Compiler Authors.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 package com.google.javascript.jscomp;
18
19 import com.google.javascript.rhino.Node;
20 import com.google.javascript.rhino.Token;
21
22 import junit.framework.TestCase;
23
24 /**
25 * Tests for {@link SyntacticScopeCreator}.
26 *
27 */
28 public class SyntacticScopeCreatorTest extends TestCase {
29
30 /**
31 * Helper to create a top-level scope from a JavaScript string
32 */
33 private static Scope getScope(String js) {
34 Compiler compiler = new Compiler();
35 Node root = compiler.parseTestCode(js);
36 assertEquals(0, compiler.getErrorCount());
37 Scope scope =
38 new SyntacticScopeCreator(compiler).createScope(root, null);
39 return scope;
40 }
41
42 /**
43 * Helper to traverse the tree creating the Scope object everywhere.
44 */
45 private static void testScopes(String js, int errorCount) {
46 Compiler compiler = new Compiler();
47 Node root = compiler.parseTestCode(js);
48 NodeTraversal.traverse(
49 compiler, root, new NodeTraversal.AbstractPostOrderCallback() {
50 @Override
51 public
52 void visit(NodeTraversal t, Node n, Node parent) {
53 t.getScope();
54 }
55 });
56 assertEquals(errorCount, compiler.getErrorCount());
57 }
58
59 public void testFunctionScope() {
60 Scope scope = getScope("function foo() {}\n" +
61 "var x = function bar(a1) {};" +
62 "[function bar2() { var y; }];" +
63 "if (true) { function z() {} }"
64 );
65 assertTrue(scope.isDeclared("foo", false));
66 assertTrue(scope.isDeclared("x", false));
67 assertTrue(scope.isDeclared("z", false));
68
69 // The following should not be declared in this scope
70 assertFalse(scope.isDeclared("a1", false));
71 assertFalse(scope.isDeclared("bar", false));
72 assertFalse(scope.isDeclared("bar2", false));
73 assertFalse(scope.isDeclared("y", false));
74 assertFalse(scope.isDeclared("", false));
75 }
76
77 public void testScopeRootNode() {
78 String js = "function foo() {\n" +
79 " var x = 10;" +
80 "}";
81 Compiler compiler = new Compiler();
82 Node root = compiler.parseTestCode(js);
83 assertEquals(0, compiler.getErrorCount());
84
85 Scope globalScope =
86 new SyntacticScopeCreator(compiler).createScope(root, null);
87 assertEquals(root, globalScope.getRootNode());
88
89 Node fooNode = root.getFirstChild();
90 assertEquals(Token.FUNCTION, fooNode.getType());
91 Scope fooScope =
92 new SyntacticScopeCreator(compiler).createScope(fooNode, null);
93 assertEquals(fooNode, fooScope.getRootNode());
94 assertTrue(fooScope.isDeclared("x", false));
95 }
96
97 public void testRedeclaration1() {
98 String js = "var a; var a;";
99 int errors = createGlobalScopeHelper(js);
100 assertEquals(1, errors);
101 }
102
103 public void testRedeclaration2() {
104 String js = "var a; /** @suppress {duplicate} */ var a;";
105 int errors = createGlobalScopeHelper(js);
106 assertEquals(0, errors);
107 }
108
109 public void testRedeclaration3() {
110 String js = " /** @suppress {duplicate} */ var a; var a; ";
111 int errors = createGlobalScopeHelper(js);
112 assertEquals(0, errors);
113 }
114
115
116 public void testFunctionScopeArguments() {
117 // A var declaration doesn't mask arguments
118 testScopes("function f() {var arguments}", 0);
119
120 testScopes("var f = function arguments() {}", 1);
121 testScopes("var f = function (arguments) {}", 1);
122 testScopes("function f() {try {} catch(arguments) {}}", 1);
123 }
124
125 /**
126 * Parse the supplied JS and create the global SyntaticScope object.
127 * @return The error count.
128 */
129 private int createGlobalScopeHelper(String js) {
130 Compiler compiler = new Compiler();
131 CompilerOptions options = new CompilerOptions();
132 options.checkSymbols = true;
133 compiler.initOptions(options);
134
135 Node root = compiler.parseTestCode(js);
136 assertEquals(0, compiler.getErrorCount());
137 Scope globalScope =
138 new SyntacticScopeCreator(compiler).createScope(root, null);
139 assertEquals(root, globalScope.getRootNode());
140 return compiler.getErrorCount();
141 }
142 }