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.", " */", ""));
}
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 */ ");
}
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");
}
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));
}
}
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));
}
Aggregations