use of com.google.javascript.rhino.JSTypeExpression in project closure-compiler by google.
the class CheckJSDocStyle method checkParams.
private void checkParams(NodeTraversal t, Node function, JSDocInfo jsDoc) {
if (jsDoc != null && jsDoc.getType() != null) {
// which is fine.
return;
}
List<String> paramsFromJsDoc = jsDoc == null ? ImmutableList.of() : ImmutableList.copyOf(jsDoc.getParameterNames());
if (paramsFromJsDoc.isEmpty()) {
checkInlineParams(t, function, jsDoc);
} else {
Node paramList = NodeUtil.getFunctionParameters(function);
if (!paramList.hasXChildren(paramsFromJsDoc.size())) {
compiler.report(JSError.make(paramList, WRONG_NUMBER_OF_PARAMS, jsDoc.isOverride() ? "" : ""));
return;
}
Node param = paramList.getFirstChild();
for (String s : paramsFromJsDoc) {
if (param.getJSDocInfo() != null) {
t.report(param, MIXED_PARAM_JSDOC_STYLES);
}
String name = s;
JSTypeExpression paramType = jsDoc.getParameterType(name);
if (checkParam(t, param, name, paramType)) {
return;
}
param = param.getNext();
}
}
}
use of com.google.javascript.rhino.JSTypeExpression in project closure-compiler by google.
the class GlobalNamespaceTest method withoutAssignmentFirstQnameDeclarationStatementJSDocWins.
@Test
public void withoutAssignmentFirstQnameDeclarationStatementJSDocWins() {
GlobalNamespace namespace = parse(lines(//
"const X = {};", "/** @type {string} */", "X.number;", "/** @type {Object} */", "X.number;", ""));
Name nameX = namespace.getOwnSlot("X.number");
Ref declarationRef = nameX.getDeclaration();
assertThat(declarationRef).isNull();
// Make sure JSDoc on the first assignment is the JSDoc for the name
JSDocInfo jsDocInfo = nameX.getJSDocInfo();
assertThat(jsDocInfo).isNotNull();
JSTypeExpression jsTypeExpression = jsDocInfo.getType();
assertThat(jsTypeExpression).isNotNull();
JSType jsType = jsTypeExpression.evaluate(/* scope= */
null, lastCompiler.getTypeRegistry());
assertType(jsType).isString();
}
use of com.google.javascript.rhino.JSTypeExpression in project closure-compiler by google.
the class GlobalNamespaceTest method firstDeclarationJSDocAlwaysWins.
@Test
public void firstDeclarationJSDocAlwaysWins() {
GlobalNamespace namespace = parse(lines(//
"const X = {};", // later assignment should win
"/** @type {symbol} */", "X.number;", // this is the JSDoc we should use
"/** @type {number} */", "X.number = 3;", "/** @type {string} */", "X.number = 'hi';", "/** @type {Object} */", "X.number;", ""));
Name nameX = namespace.getOwnSlot("X.number");
Ref declarationRef = nameX.getDeclaration();
assertThat(declarationRef).isNotNull();
// make sure first assignment is considered to be the declaration
Node declarationNode = declarationRef.getNode();
assertNode(declarationNode).matchesQualifiedName("X.number");
Node assignNode = declarationNode.getParent();
assertNode(assignNode).isAssign();
Node valueNode = declarationNode.getNext();
assertNode(valueNode).isNumber(3);
// Make sure JSDoc on the first assignment is the JSDoc for the name
JSDocInfo jsDocInfo = nameX.getJSDocInfo();
assertThat(jsDocInfo).isNotNull();
JSTypeExpression jsTypeExpression = jsDocInfo.getType();
assertThat(jsTypeExpression).isNotNull();
JSType jsType = jsTypeExpression.evaluate(/* scope= */
null, lastCompiler.getTypeRegistry());
assertType(jsType).isNumber();
}
use of com.google.javascript.rhino.JSTypeExpression in project closure-compiler by google.
the class JsDocInfoParserTest method testInterfaceExtends.
@Test
public void testInterfaceExtends() {
JSDocInfo jsdoc = parse(" * @interface \n" + " * @extends {Extended} */");
assertThat(jsdoc.isInterface()).isTrue();
assertThat(jsdoc.getExtendedInterfacesCount()).isEqualTo(1);
List<JSTypeExpression> types = jsdoc.getExtendedInterfaces();
assertTypeEquals(createNamedType("Extended"), types.get(0));
}
use of com.google.javascript.rhino.JSTypeExpression in project closure-compiler by google.
the class JsDocInfoParserTest method testInterfaceMultiExtends1.
@Test
public void testInterfaceMultiExtends1() {
JSDocInfo jsdoc = parse(" * @interface \n" + " * @extends {Extended1} \n" + " * @extends {Extended2} */");
assertThat(jsdoc.isInterface()).isTrue();
assertThat(jsdoc.getBaseType()).isNull();
assertThat(jsdoc.getExtendedInterfacesCount()).isEqualTo(2);
List<JSTypeExpression> types = jsdoc.getExtendedInterfaces();
assertTypeEquals(createNamedType("Extended1"), types.get(0));
assertTypeEquals(createNamedType("Extended2"), types.get(1));
}
Aggregations