Search in sources :

Example 51 with JSTypeExpression

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();
        }
    }
}
Also used : Node(com.google.javascript.rhino.Node) JSTypeExpression(com.google.javascript.rhino.JSTypeExpression)

Example 52 with JSTypeExpression

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();
}
Also used : Ref(com.google.javascript.jscomp.GlobalNamespace.Ref) JSType(com.google.javascript.rhino.jstype.JSType) JSTypeExpression(com.google.javascript.rhino.JSTypeExpression) JSDocInfo(com.google.javascript.rhino.JSDocInfo) Name(com.google.javascript.jscomp.GlobalNamespace.Name) Test(org.junit.Test)

Example 53 with JSTypeExpression

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();
}
Also used : Ref(com.google.javascript.jscomp.GlobalNamespace.Ref) JSType(com.google.javascript.rhino.jstype.JSType) Node(com.google.javascript.rhino.Node) NodeSubject.assertNode(com.google.javascript.rhino.testing.NodeSubject.assertNode) JSTypeExpression(com.google.javascript.rhino.JSTypeExpression) JSDocInfo(com.google.javascript.rhino.JSDocInfo) Name(com.google.javascript.jscomp.GlobalNamespace.Name) Test(org.junit.Test)

Example 54 with JSTypeExpression

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));
}
Also used : JSTypeExpression(com.google.javascript.rhino.JSTypeExpression) JSDocInfo(com.google.javascript.rhino.JSDocInfo) Test(org.junit.Test)

Example 55 with JSTypeExpression

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));
}
Also used : JSTypeExpression(com.google.javascript.rhino.JSTypeExpression) JSDocInfo(com.google.javascript.rhino.JSDocInfo) Test(org.junit.Test)

Aggregations

JSTypeExpression (com.google.javascript.rhino.JSTypeExpression)101 Node (com.google.javascript.rhino.Node)67 JSDocInfo (com.google.javascript.rhino.JSDocInfo)58 Test (org.junit.Test)26 JSDocInfoBuilder (com.google.javascript.rhino.JSDocInfoBuilder)18 MemberDefinition (com.google.javascript.jscomp.PolymerPass.MemberDefinition)9 JSType (com.google.javascript.rhino.jstype.JSType)9 ArrayList (java.util.ArrayList)8 TypeDeclarationNode (com.google.javascript.rhino.Node.TypeDeclarationNode)7 Map (java.util.Map)6 NodeSubject.assertNode (com.google.javascript.jscomp.testing.NodeSubject.assertNode)4 HashMap (java.util.HashMap)4 HashSet (java.util.HashSet)4 ImmutableList (com.google.common.collect.ImmutableList)3 ImmutableMap (com.google.common.collect.ImmutableMap)3 Visibility (com.google.javascript.rhino.JSDocInfo.Visibility)3 LinkedHashMap (java.util.LinkedHashMap)3 ImmutableSet (com.google.common.collect.ImmutableSet)2 Name (com.google.javascript.jscomp.GlobalNamespace.Name)2 Ref (com.google.javascript.jscomp.GlobalNamespace.Ref)2