Debian Codegraph @

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

raw symbols SCIP
1 /*
2 * Copyright 2009 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
20 import com.google.common.collect.Lists;
21 import com.google.javascript.jscomp.NodeTraversal.AbstractPostOrderCallback;
22 import com.google.javascript.jscomp.NodeTraversal.Callback;
23 import com.google.javascript.rhino.Node;
24 import com.google.javascript.rhino.jstype.FunctionType;
25 import com.google.javascript.rhino.jstype.JSType;
26 import com.google.javascript.rhino.jstype.ObjectType;
27
28 import java.util.Deque;
29
30
31 /**
32 * Tests for {@link InferJSDocInfo}.
33 * @author [email protected] (Nick Santos)
34 */
35 // TODO(nicksantos): A lot of this code is duplicated from
36 // TypedScopeCreatorTest. We should create a common test harness for
37 // assertions about type information.
38 public class InferJSDocInfoTest extends CompilerTestCase {
39
40 private Scope globalScope;
41
42 @Override
43 public int getNumRepetitions() {
44 return 1;
45 }
46
47 @Override
48 protected CompilerOptions getOptions() {
49 CompilerOptions options = super.getOptions();
50 options.ideMode = true;
51 return options;
52 }
53
54 private final Callback callback = new AbstractPostOrderCallback() {
55 @Override
56 public void visit(NodeTraversal t, Node n, Node parent) {
57 Scope s = t.getScope();
58 if (s.isGlobal()) {
59 globalScope = s;
60 }
61 }
62 };
63
64 @Override
65 public CompilerPass getProcessor(final Compiler compiler) {
66 return new CompilerPass() {
67 @Override
68 public void process(Node externs, Node root) {
69 MemoizedScopeCreator scopeCreator =
70 new MemoizedScopeCreator(new TypedScopeCreator(compiler));
71 Scope topScope = scopeCreator.createScope(root.getParent(), null);
72 (new TypeInferencePass(
73 compiler, compiler.getReverseAbstractInterpreter(),
74 topScope, scopeCreator)).process(externs, root);
75 NodeTraversal t = new NodeTraversal(
76 compiler, callback, scopeCreator);
77 t.traverseRoots(Lists.newArrayList(externs, root));
78 (new InferJSDocInfo(compiler)).process(externs, root);
79 }
80 };
81 }
82
83 public void testNativeCtor() {
84 testSame(
85 "/** Object. \n * @param {*=} x \n * @constructor */ " +
86 "function Object(x) {};",
87 "var x = new Object();" +
88 "/** Another object. */ var y = new Object();", null);
89 assertEquals(
90 "Object.",
91 findGlobalNameType("x").getJSDocInfo().getBlockDescription());
92 assertEquals(
93 "Object.",
94 findGlobalNameType("y").getJSDocInfo().getBlockDescription());
95 assertEquals(
96 "Object.",
97 globalScope.getVar("y").getType().getJSDocInfo().getBlockDescription());
98 }
99
100 public void testStructuralFunctions() {
101 testSame(
102 "/** Object. \n * @param {*=} x \n * @constructor */ " +
103 "function Object(x) {};",
104 "/** Function. \n * @param {*} x */ " +
105 "function fn(x) {};" +
106 "var goog = {};" +
107 "/** Another object. \n * @type {Object} */ goog.x = new Object();" +
108 "/** Another function. \n * @param {number} x */ goog.y = fn;", null);
109 assertEquals(
110 "(Object|null)",
111 globalScope.getVar("goog.x").getType().toString());
112 assertEquals(
113 "Object.",
114 globalScope.getVar("goog.x").getType().restrictByNotNullOrUndefined()
115 .getJSDocInfo().getBlockDescription());
116 assertEquals(
117 "Another function.",
118 globalScope.getVar("goog.y").getType()
119 .getJSDocInfo().getBlockDescription());
120 }
121
122 public void testInstanceObject() {
123 // Asserts that no property gets attached to the instance object.
124 testSame(
125 "/** @constructor */ function Foo() {}" +
126 "var f = new Foo();" +
127 "/** @type {number} */ f.bar = 4;");
128 ObjectType type = (ObjectType) globalScope.getVar("f").getType();
129 assertEquals("Foo", type.toString());
130 assertFalse(type.hasProperty("bar"));
131 assertNull(type.getOwnPropertyJSDocInfo("bar"));
132 }
133
134 public void testInterface() {
135 testSame(
136 "/** An interface. \n * @interface */ function Foo() {}" +
137 "var f = new Foo();" +
138 "/** @type {number} */ f.bar = 4;");
139 ObjectType type = (ObjectType) globalScope.getVar("Foo").getType();
140 assertEquals(
141 "An interface.",
142 type.getJSDocInfo().getBlockDescription());
143 }
144
145 public void testNamespacedCtor() {
146 testSame(
147 "var goog = {};" +
148 "/** Hello! \n * @constructor */ goog.Foo = function() {};" +
149 "goog.Foo.bar = goog.Foo;" +
150 "/** Bye! \n * @param {string=} opt_x */" +
151 "goog.Foo.prototype.baz = goog.Foo;" +
152 "/** Blargh */ var x = new goog.Foo();");
153 assertEquals(
154 "Hello!",
155 findGlobalNameType("x").getJSDocInfo().getBlockDescription());
156 assertEquals(
157 "Hello!",
158 findGlobalNameType("goog.Foo").getJSDocInfo().getBlockDescription());
159 assertEquals(
160 "Hello!",
161 findGlobalNameType(
162 "goog.Foo.bar").getJSDocInfo().getBlockDescription());
163
164 assertEquals(
165 "Hello!",
166 findGlobalNameType(
167 "goog.Foo.prototype.baz").getJSDocInfo().getBlockDescription());
168
169 ObjectType proto = (ObjectType) findGlobalNameType("goog.Foo.prototype");
170 assertEquals(
171 "Bye!",
172 proto.getPropertyType("baz").getJSDocInfo().getBlockDescription());
173 }
174
175 public void testAbstractMethod() {
176 testSame(
177 "/** Abstract method. \n * @type {!Function} */ var abstractMethod;" +
178 "/** @constructor */ function Foo() {}" +
179 "/** Block description. \n * @param {number} x */" +
180 "Foo.prototype.bar = abstractMethod;");
181 FunctionType abstractMethod =
182 (FunctionType) findGlobalNameType("abstractMethod");
183 assertNull(abstractMethod.getJSDocInfo());
184
185 FunctionType ctor = (FunctionType) findGlobalNameType("Foo");
186 ObjectType proto = ctor.getInstanceType().getImplicitPrototype();
187 FunctionType method = (FunctionType) proto.getPropertyType("bar");
188 assertEquals(
189 "Block description.",
190 method.getJSDocInfo().getBlockDescription());
191 assertEquals(
192 "Block description.",
193 proto.getOwnPropertyJSDocInfo("bar").getBlockDescription());
194 }
195
196 private JSType findGlobalNameType(String name) {
197 return findNameType(name, globalScope);
198 }
199
200 private JSType findNameType(String name, Scope scope) {
201 Node root = scope.getRootNode();
202 Deque<Node> queue = Lists.newLinkedList();
203 queue.push(root);
204 while (!queue.isEmpty()) {
205 Node current = queue.pop();
206 if (name.equals(current.getQualifiedName()) &&
207 current.getJSType() != null) {
208 return current.getJSType();
209 }
210
211 for (Node child : current.children()) {
212 queue.push(child);
213 }
214 }
215 return null;
216 }
217 }