Search in sources :

Example 41 with JSTypeExpression

use of com.google.javascript.rhino.JSTypeExpression in project closure-compiler by google.

the class JSDocInfoPrinterTest method testDeprecated.

@Test
public void testDeprecated() {
    builder.recordDeprecated();
    builder.recordDeprecationReason("See {@link otherClass} for more info.");
    builder.recordType(new JSTypeExpression(JsDocInfoParser.parseTypeString("string"), "<testDeprecated>"));
    JSDocInfo info = builder.buildAndReset();
    assertThat(jsDocInfoPrinter.print(info)).isEqualTo(LINE_JOINER.join("/**", " * @type {string}", " * @deprecated See {@link otherClass} for more info.", " */", ""));
}
Also used : JSTypeExpression(com.google.javascript.rhino.JSTypeExpression) JSDocInfo(com.google.javascript.rhino.JSDocInfo) Test(org.junit.Test)

Example 42 with JSTypeExpression

use of com.google.javascript.rhino.JSTypeExpression in project closure-compiler by google.

the class JSDocInfoPrinterTest method testTypes.

@Test
public void testTypes() {
    builder.recordReturnType(new JSTypeExpression(JsDocInfoParser.parseTypeString("number|string"), "<testTypes>"));
    JSDocInfo info = builder.buildAndReset();
    assertThat(jsDocInfoPrinter.print(info)).isEqualTo("/**\n * @return {(number|string)}\n */\n");
    builder.recordParameter("foo", new JSTypeExpression(new Node(Token.ITER_REST, IR.string("number")), "<testTypes>"));
    info = builder.buildAndReset();
    assertThat(jsDocInfoPrinter.print(info)).isEqualTo("/**\n * @param {...number} foo\n */\n");
    builder.recordTypedef(new JSTypeExpression(new Node(Token.QMARK), "<testTypes>"));
    info = builder.buildAndReset();
    assertThat(jsDocInfoPrinter.print(info)).isEqualTo("/** @typedef {?} */ ");
    builder.recordType(new JSTypeExpression(new Node(Token.VOID), "<testTypes>"));
    info = builder.buildAndReset();
    assertThat(jsDocInfoPrinter.print(info)).isEqualTo("/** @type {void} */ ");
    // Object types
    builder.recordEnumParameterType(new JSTypeExpression(JsDocInfoParser.parseTypeString("{foo:number,bar:string}"), "<testTypes>"));
    info = builder.buildAndReset();
    assertThat(jsDocInfoPrinter.print(info)).isEqualTo("/** @enum {{foo:number,bar:string}} */ ");
    builder.recordEnumParameterType(new JSTypeExpression(JsDocInfoParser.parseTypeString("{foo:(number|string)}"), "<testTypes>"));
    info = builder.buildAndReset();
    assertThat(jsDocInfoPrinter.print(info)).isEqualTo("/** @enum {{foo:(number|string)}} */ ");
    // Nullable/non-nullable types.
    builder.recordType(new JSTypeExpression(JsDocInfoParser.parseTypeString("?Object"), "<testTypes>"));
    info = builder.buildAndReset();
    assertThat(jsDocInfoPrinter.print(info)).isEqualTo("/** @type {?Object} */ ");
    builder.recordType(new JSTypeExpression(JsDocInfoParser.parseTypeString("!Object"), "<testTypes>"));
    info = builder.buildAndReset();
    assertThat(jsDocInfoPrinter.print(info)).isEqualTo("/** @type {!Object} */ ");
    // Array types
    builder.recordType(new JSTypeExpression(JsDocInfoParser.parseTypeString("!Array<(number|string)>"), "<testTypes>"));
    info = builder.buildAndReset();
    assertThat(jsDocInfoPrinter.print(info)).isEqualTo("/** @type {!Array<(number|string)>} */ ");
    builder.recordType(new JSTypeExpression(JsDocInfoParser.parseTypeString("Array"), "<testTypes>"));
    builder.recordInlineType();
    info = builder.buildAndReset();
    assertThat(jsDocInfoPrinter.print(info)).isEqualTo("/** Array */ ");
    // Other template types
    builder.recordType(new JSTypeExpression(JsDocInfoParser.parseTypeString("!Set<number|string>"), "<testTypes>"));
    info = builder.buildAndReset();
    assertThat(jsDocInfoPrinter.print(info)).isEqualTo("/** @type {!Set<(number|string)>} */ ");
    builder.recordType(new JSTypeExpression(JsDocInfoParser.parseTypeString("!Map<!Foo, !Bar<!Baz|string>>"), "<testTypes>"));
    info = builder.buildAndReset();
    assertThat(jsDocInfoPrinter.print(info)).isEqualTo("/** @type {!Map<!Foo,!Bar<(!Baz|string)>>} */ ");
    builder.recordType(new JSTypeExpression(JsDocInfoParser.parseTypeString("Map"), "<testTypes>"));
    builder.recordInlineType();
    info = builder.buildAndReset();
    assertThat(jsDocInfoPrinter.print(info)).isEqualTo("/** Map */ ");
}
Also used : Node(com.google.javascript.rhino.Node) JSTypeExpression(com.google.javascript.rhino.JSTypeExpression) JSDocInfo(com.google.javascript.rhino.JSDocInfo) Test(org.junit.Test)

Example 43 with JSTypeExpression

use of com.google.javascript.rhino.JSTypeExpression in project closure-compiler by google.

the class JSDocInfoPrinterTest method testParam.

@Test
public void testParam() {
    builder.recordParameter("foo", new JSTypeExpression(JsDocInfoParser.parseTypeString("number"), "<testParam>"));
    builder.recordParameter("bar", new JSTypeExpression(JsDocInfoParser.parseTypeString("string"), "<testParam>"));
    JSDocInfo info = builder.buildAndReset();
    assertThat(jsDocInfoPrinter.print(info)).isEqualTo("/**\n * @param {number} foo\n * @param {string} bar\n */\n");
    builder.recordParameter("foo", new JSTypeExpression(new Node(Token.EQUALS, IR.string("number")), "<testParam>"));
    info = builder.buildAndReset();
    assertThat(jsDocInfoPrinter.print(info)).isEqualTo("/**\n * @param {number=} foo\n */\n");
    builder.recordParameter("foo", new JSTypeExpression(new Node(Token.ITER_REST, IR.string("number")), "<testParam>"));
    info = builder.buildAndReset();
    assertThat(jsDocInfoPrinter.print(info)).isEqualTo("/**\n * @param {...number} foo\n */\n");
    builder.recordParameter("foo", new JSTypeExpression(new Node(Token.ITER_REST, IR.empty()), "<testParam>"));
    info = builder.buildAndReset();
    assertThat(jsDocInfoPrinter.print(info)).isEqualTo("/**\n * @param {...} foo\n */\n");
    builder.recordParameter("foo", null);
    info = builder.buildAndReset();
    assertThat(jsDocInfoPrinter.print(info)).isEqualTo("/**\n * @param foo\n */\n");
}
Also used : Node(com.google.javascript.rhino.Node) JSTypeExpression(com.google.javascript.rhino.JSTypeExpression) JSDocInfo(com.google.javascript.rhino.JSDocInfo) Test(org.junit.Test)

Example 44 with JSTypeExpression

use of com.google.javascript.rhino.JSTypeExpression in project closure-compiler by google.

the class CheckJSDoc method validateRestParameter.

/**
 * Check that a rest parameter has JSDoc marked as variadic.
 */
private void validateRestParameter(Node restParam) {
    if (!restParam.isRest() || !restParam.getParent().isParamList()) {
        return;
    }
    Node paramList = restParam.getParent();
    JSDocInfo inlineInfo = restParam.getFirstChild().getJSDocInfo();
    JSDocInfo functionInfo = NodeUtil.getBestJSDocInfo(paramList.getParent());
    final JSTypeExpression paramTypeAnnotation;
    if (inlineInfo != null) {
        paramTypeAnnotation = inlineInfo.getType();
    } else if (functionInfo != null) {
        if (restParam.getFirstChild().isName()) {
            String paramName = restParam.getFirstChild().getString();
            paramTypeAnnotation = functionInfo.getParameterType(paramName);
        } else {
            // destructuring rest param. use the nth JSDoc parameter if present. the name will not match
            int indexOfRest = paramList.getIndexOfChild(restParam);
            paramTypeAnnotation = functionInfo.getParameterCount() >= indexOfRest ? functionInfo.getParameterType(functionInfo.getParameterNameAt(indexOfRest)) : null;
        }
    } else {
        paramTypeAnnotation = null;
    }
    if (paramTypeAnnotation != null && paramTypeAnnotation.getRoot().getToken() != Token.ITER_REST) {
        compiler.report(JSError.make(restParam, BAD_REST_PARAMETER_ANNOTATION));
    }
}
Also used : Node(com.google.javascript.rhino.Node) JSTypeExpression(com.google.javascript.rhino.JSTypeExpression) JSDocInfo(com.google.javascript.rhino.JSDocInfo)

Example 45 with JSTypeExpression

use of com.google.javascript.rhino.JSTypeExpression in project closure-compiler by google.

the class CheckMissingOverrideTypes method recordMissingReturnAnnotation.

private void recordMissingReturnAnnotation(Node fnNode, FunctionType fnType, JSDocInfo.Builder builder) {
    checkState(fnNode.isFunction(), fnNode);
    builder.recordReturnType(new JSTypeExpression(typeToTypeAst(fnType.getReturnType()), JSDOC_FILE_NAME));
}
Also used : JSTypeExpression(com.google.javascript.rhino.JSTypeExpression)

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