本文整理汇总了Java中com.google.javascript.rhino.JSDocInfoBuilder.recordType方法的典型用法代码示例。如果您正苦于以下问题:Java JSDocInfoBuilder.recordType方法的具体用法?Java JSDocInfoBuilder.recordType怎么用?Java JSDocInfoBuilder.recordType使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类com.google.javascript.rhino.JSDocInfoBuilder
的用法示例。
在下文中一共展示了JSDocInfoBuilder.recordType方法的14个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: appendPropertiesToBlock
import com.google.javascript.rhino.JSDocInfoBuilder; //导入方法依赖的package包/类
/**
* Appends all properties in the ClassDefinition to the prototype of the custom element.
*/
private void appendPropertiesToBlock(
final PolymerClassDefinition cls, Node block, String basePath) {
for (MemberDefinition prop : cls.props) {
Node propertyNode = IR.exprResult(
NodeUtil.newQName(compiler, basePath + prop.name.getString()));
// If a property string is quoted, make sure the added prototype properties are also quoted
if (prop.name.isQuotedString()) {
continue;
}
propertyNode.useSourceInfoIfMissingFromForTree(prop.name);
JSDocInfoBuilder info = JSDocInfoBuilder.maybeCopyFrom(prop.info);
JSTypeExpression propType = PolymerPassStaticUtils.getTypeFromProperty(prop, compiler);
if (propType == null) {
return;
}
info.recordType(propType);
propertyNode.getFirstChild().setJSDocInfo(info.build());
block.addChildToBack(propertyNode);
}
}
示例2: addExternModule
import com.google.javascript.rhino.JSDocInfoBuilder; //导入方法依赖的package包/类
/**
* Adds the definition equivalent to a file with:
*
* /**
* * @type {<moduleType>}
* * @const
* * /
* var <moduleName>;
*/
private void addExternModule(String moduleName, String moduleType,
Node root) {
Node reactVarNode = IR.var(IR.name(moduleName));
JSDocInfoBuilder jsDocBuilder = new JSDocInfoBuilder(true);
jsDocBuilder.recordType(new JSTypeExpression(
IR.string(moduleType), EXTERNS_SOURCE_NAME));
jsDocBuilder.recordConstancy();
reactVarNode.setJSDocInfo(jsDocBuilder.build());
root.addChildToBack(reactVarNode);
}
示例3: testPropType
import com.google.javascript.rhino.JSDocInfoBuilder; //导入方法依赖的package包/类
private void testPropType(String reactPropType, String typeExpression) {
Compiler compiler = new Compiler();
CompilerOptions options = new CompilerOptions();
// So that source dumps still have JSDoc
options.preserveTypeAnnotations = true;
compiler.initOptions(options);
// Avoid extra "use strict" boilerplate in output.
options.setEmitUseStrict(false);
compiler.disableThreads(); // Makes errors easier to track down.
Node reactPropTypeNode = compiler.parse(
SourceFile.fromCode("/src/test.js", reactPropType))
.getFirstChild().getFirstChild();
assertArrayEquals(new JSError[]{}, compiler.getErrors());
Node typeNode = PropTypesExtractor.convertPropType(reactPropTypeNode)
.typeNode;
// Easiest way to stringify the type node is to print it out as JSDoc.
JSDocInfoBuilder jsDocInfoBuilder = new JSDocInfoBuilder(true);
jsDocInfoBuilder.recordType(new JSTypeExpression(
typeNode, "/src/test.js"));
Node tempNode = IR.var(IR.name("temp"));
tempNode.setJSDocInfo(jsDocInfoBuilder.build());
String tempCode = compiler.toSource(tempNode);
assertEquals("/** @type {" + typeExpression + "} */ var temp", tempCode);
}
示例4: maybeVisitColonType
import com.google.javascript.rhino.JSDocInfoBuilder; //导入方法依赖的package包/类
private void maybeVisitColonType(NodeTraversal t, Node n, Node jsDocNode) {
Node type = n.getDeclaredTypeExpression();
boolean hasColonType = type != null;
if (n.isRest() && hasColonType) {
type = new Node(Token.ELLIPSIS, convertWithLocation(type.removeFirstChild()));
} else if (n.isMemberVariableDef()) {
if (type != null) {
type = maybeProcessOptionalProperty(n, type);
}
} else {
type = maybeProcessOptionalParameter(n, type);
}
if (type == null) {
return;
}
JSDocInfoBuilder builder = JSDocInfoBuilder.maybeCopyFrom(jsDocNode.getJSDocInfo());
JSTypeExpression typeExpression = new JSTypeExpression(type, n.getSourceFileName());
switch (n.getToken()) {
case FUNCTION:
builder.recordReturnType(typeExpression);
break;
case MEMBER_VARIABLE_DEF:
builder.recordType(typeExpression);
break;
default:
builder.recordType(typeExpression);
builder.recordInlineType();
}
jsDocNode.setJSDocInfo(builder.build());
if (hasColonType) {
n.setDeclaredTypeExpression(null);
t.reportCodeChange();
}
}
示例5: copyDeclarations
import com.google.javascript.rhino.JSDocInfoBuilder; //导入方法依赖的package包/类
/**
* When static get/set properties are transpiled, in addition to the Object.defineProperties, they
* are declared with stub GETPROP declarations so that the type checker understands that these
* properties exist on the class.
* When subclassing, we also need to declare these properties on the subclass so that the type
* checker knows they exist.
*/
private void copyDeclarations(
JavascriptClass superClass, JavascriptClass subClass, Node inheritsCall) {
for (Node staticGetProp : superClass.staticFieldAccess) {
checkState(staticGetProp.isGetProp());
String memberName = staticGetProp.getLastChild().getString();
// We only copy declarations that have corresponding Object.defineProperties
if (!superClass.definedProperties.contains(memberName)) {
continue;
}
// If the subclass already declares the property no need to redeclare it.
if (isOverriden(subClass, memberName)) {
continue;
}
Node subclassNameNode = inheritsCall.getSecondChild();
Node getprop = IR.getprop(subclassNameNode.cloneTree(), IR.string(memberName));
JSDocInfoBuilder info = JSDocInfoBuilder.maybeCopyFrom(staticGetProp.getJSDocInfo());
JSTypeExpression unknown = new JSTypeExpression(new Node(Token.QMARK), "<synthetic>");
info.recordType(unknown); // In case there wasn't a type specified on the base class.
info.addSuppression("visibility");
getprop.setJSDocInfo(info.build());
Node declaration = IR.exprResult(getprop);
declaration.useSourceInfoIfMissingFromForTree(inheritsCall);
Node parent = inheritsCall.getParent();
parent.getParent().addChildBefore(declaration, parent);
compiler.reportChangeToEnclosingScope(parent);
// Copy over field access so that subclasses of this subclass can also make the declarations
if (!subClass.definedProperties.contains(memberName)) {
subClass.staticFieldAccess.add(getprop);
subClass.definedProperties.add(memberName);
}
}
}
示例6: testCastOnLeftSideOfAssign
import com.google.javascript.rhino.JSDocInfoBuilder; //导入方法依赖的package包/类
public void testCastOnLeftSideOfAssign() {
JSDocInfoBuilder jsdoc = new JSDocInfoBuilder(false);
jsdoc.recordType(new JSTypeExpression(IR.string("number"), "<AstValidatorTest>"));
Node n = IR.exprResult(
new Node(
Token.ASSIGN,
IR.cast(IR.name("x"), jsdoc.build()),
IR.number(0)));
expectValid(n, Check.STATEMENT);
}
示例7: addTypes
import com.google.javascript.rhino.JSDocInfoBuilder; //导入方法依赖的package包/类
/**
* Inject React type definitions (if we want these to get renamed, they're
* not part of the externs). {@link Compiler#getNodeForCodeInsertion(JSModule)}
* is package-private, so we instead add the types to the React source file.
*/
private void addTypes(Node root) {
Node typesNode = null;
if (options.renameReactApi) {
typesNode = createTypesNode();
}
boolean foundReactSource = false;
for (Node inputNode : root.children()) {
if (inputNode.getToken() == Token.SCRIPT &&
inputNode.getSourceFileName() != null &&
React.isReactSourceName(inputNode.getSourceFileName())) {
if (typesNode != null) {
Node typesChildren = typesNode.getFirstChild();
typesNode.removeChildren();
inputNode.addChildrenToFront(typesChildren);
}
foundReactSource = true;
stripPropTypes = addCreateElementAlias = React.isReactMinSourceName(
inputNode.getSourceFileName());
if (addCreateElementAlias) {
// Add an alias of the form:
// /** @type {Function} */
// var React$createElement = React.createElement;
// Normally React.createElement calls are not renamed at all, due to
// React being an extern and createElement showing up in the built-in
// browser DOM externs. By adding an alias and then rewriting calls
// (see visitReactCreateElement) we allow the compiler to rename the
// function used at all the calls. This is most beneficial before
// gzip, but when after gzip there is still some benefit.
// The Function type is necessary to convince the compiler that we
// don't need the "this" type to be defined when calling the alias
// (it thinks that React is an instance of the ReactModule type, but
// it's actually a static namespace, so we can use unbound functions
// from it)
Node createElementAliasNode = IR.var(
IR.name(CREATE_ELEMENT_ALIAS_NAME),
IR.getprop(
IR.name("React"),
IR.string("createElement")));
JSDocInfoBuilder jsDocBuilder = new JSDocInfoBuilder(true);
jsDocBuilder.recordType(new JSTypeExpression(
IR.string("Function"), inputNode.getSourceFileName()));
createElementAliasNode.setJSDocInfo(jsDocBuilder.build());
inputNode.addChildToBack(createElementAliasNode);
}
compiler.reportChangeToEnclosingScope(inputNode);
break;
}
}
if (!foundReactSource) {
compiler.report(JSError.make(root, REACT_SOURCE_NOT_FOUND));
return;
}
}
示例8: visitReactCreateElement
import com.google.javascript.rhino.JSDocInfoBuilder; //导入方法依赖的package包/类
private void visitReactCreateElement(NodeTraversal t, Node callNode) {
int paramCount = callNode.getChildCount() - 1;
if (paramCount == 0) {
compiler.report(JSError.make(callNode, CREATE_ELEMENT_UNEXPECTED_PARAMS));
return;
}
if (addCreateElementAlias) {
// If we're adding aliases that means we're doing an optimized build, so
// there's no need for extra type checks.
Node functionNameNode = callNode.getFirstChild();
if (functionNameNode.getToken() == Token.GETPROP) {
functionNameNode.replaceWith(IR.name(CREATE_ELEMENT_ALIAS_NAME));
}
return;
}
if (callNode.getParent().getToken() == Token.CAST) {
// There's already a cast around the call, there's no need to add another.
return;
}
// Add casts of the form /** @type {!ReactElement.<type name>} */ around
// React.createElement calls, so that the return value of React.render will
// have the correct type (for string types assume that it's a
// ReactDOMElement).
// It's too expensive to know what the type parameter node actually refers
// to, so instead we assume that it directly references the type (this is
// the most common case, especially with JSX). This means that we will not
// add type annotations for cases such as:
// var typeAlias = SomeType;
// React.createElement(typeAlias);
Node typeNode = callNode.getChildAtIndex(1);
Node elementTypeExpressionNode;
if (typeNode.isString()) {
elementTypeExpressionNode = IR.string("ReactDOMElement");
} else {
String typeName = typeNode.getQualifiedName();
if (!reactClassesByName.containsKey(typeName)) {
return;
}
elementTypeExpressionNode =
createReactElementTypeExpressionNode(typeName);
PropTypesExtractor propTypesExtractor =
propTypesExtractorsByName.get(typeName);
if (propTypesExtractor != null) {
propTypesExtractor.visitReactCreateElement(callNode);
}
}
JSDocInfoBuilder jsDocBuilder = new JSDocInfoBuilder(true);
jsDocBuilder.recordType(new JSTypeExpression(
new Node(Token.BANG, elementTypeExpressionNode),
callNode.getSourceFileName()));
JSDocInfo jsDoc = jsDocBuilder.build();
Node callNodePrevious = callNode.getPrevious();
Node callNodeParent = callNode.getParent();
callNode.detach();
Node castNode = IR.cast(callNode, jsDoc);
castNode.useSourceInfoFrom(callNode);
if (callNodePrevious != null) {
callNodeParent.addChildAfter(castNode, callNodePrevious);
} else {
callNodeParent.addChildToFront(castNode);
}
}
示例9: visit
import com.google.javascript.rhino.JSDocInfoBuilder; //导入方法依赖的package包/类
@Override
public void visit(NodeTraversal t, Node n, Node parent) {
if (!n.hasChildren() || !NodeUtil.isBlockScopedDeclaration(n.getFirstChild())) {
return;
}
Scope scope = t.getScope();
Node nameNode = n.getFirstChild();
if (!n.isClass() && !n.isFunction() && !nameNode.hasChildren()
&& (parent == null || !NodeUtil.isEnhancedFor(parent))
&& !n.isCatch()
&& inLoop(n)) {
Node undefined = IR.name("undefined");
if (nameNode.getJSDocInfo() != null || n.getJSDocInfo() != null) {
JSDocInfoBuilder jsDoc = new JSDocInfoBuilder(false);
jsDoc.recordType(new JSTypeExpression(new Node(Token.QMARK), n.getSourceFileName()));
undefined = IR.cast(undefined, jsDoc.build());
}
undefined.useSourceInfoFromForTree(nameNode);
nameNode.addChildToFront(undefined);
compiler.reportChangeToEnclosingScope(undefined);
}
String oldName = nameNode.getString();
if (n.isLet() || n.isConst()) {
letConsts.add(n);
}
Scope hoistScope = scope.getClosestHoistScope();
if (scope != hoistScope) {
String newName = oldName;
if (hoistScope.isDeclared(oldName, true) || undeclaredNames.contains(oldName)) {
do {
newName = oldName + "$" + compiler.getUniqueNameIdSupplier().get();
} while (hoistScope.isDeclared(newName, true));
nameNode.setString(newName);
compiler.reportChangeToEnclosingScope(nameNode);
Node scopeRoot = scope.getRootNode();
renameTable.put(scopeRoot, oldName, newName);
}
Var oldVar = scope.getVar(oldName);
scope.undeclare(oldVar);
hoistScope.declare(newName, nameNode, oldVar.input);
}
}
示例10: getConstJSDoc
import com.google.javascript.rhino.JSDocInfoBuilder; //导入方法依赖的package包/类
private static JSDocInfo getConstJSDoc(JSDocInfo oldJSDoc, JSTypeExpression newType) {
JSDocInfoBuilder builder = JSDocInfoBuilder.maybeCopyFrom(oldJSDoc);
builder.recordType(newType);
builder.recordConstancy();
return builder.build();
}
示例11: visitTaggedTemplateLiteral
import com.google.javascript.rhino.JSDocInfoBuilder; //导入方法依赖的package包/类
/**
* Converts tag`a\tb${bar}` to:
* // A global (module) scoped variable
* var $jscomp$templatelit$0 = ["a\tb"]; // cooked string array
* $jscomp$templatelit$0.raw = ["a\\tb"]; // raw string array
* ...
* // A call to the tagging function
* tag($jscomp$templatelit$0, bar);
*
* See template_literal_test.js for more examples.
*
* @param n A TAGGED_TEMPLATELIT node
*/
static void visitTaggedTemplateLiteral(NodeTraversal t, Node n, boolean addTypes) {
TypeIRegistry registry = t.getCompiler().getTypeIRegistry();
TypeI stringType = createType(addTypes, registry, JSTypeNative.STRING_TYPE);
TypeI arrayType = createGenericType(addTypes, registry, JSTypeNative.ARRAY_TYPE, stringType);
TypeI templateArrayType =
createType(addTypes, registry, JSTypeNative.I_TEMPLATE_ARRAY_TYPE);
Node templateLit = n.getLastChild();
// Prepare the raw and cooked string arrays.
Node raw = createRawStringArray(templateLit, arrayType, stringType);
Node cooked = createCookedStringArray(templateLit, templateArrayType, stringType);
// Specify the type of the first argument to be ITemplateArray.
JSTypeExpression nonNullSiteObject = new JSTypeExpression(
JsDocInfoParser.parseTypeString("!ITemplateArray"), "<Es6TemplateLiterals.java>");
JSDocInfoBuilder info = new JSDocInfoBuilder(false);
info.recordType(nonNullSiteObject);
Node siteObject = withType(IR.cast(cooked, info.build()), templateArrayType);
// Create a variable representing the template literal.
Node callsiteId =
withType(
IR.name(TEMPLATELIT_VAR + t.getCompiler().getUniqueNameIdSupplier().get()),
templateArrayType);
Node var = IR.var(callsiteId, siteObject).useSourceInfoIfMissingFromForTree(n);
Node script = NodeUtil.getEnclosingScript(n);
script.addChildToFront(var);
t.reportCodeChange(var);
// Define the "raw" property on the introduced variable.
Node defineRaw =
IR.exprResult(
withType(
IR.assign(
withType(
IR.getprop(
callsiteId.cloneNode(), withType(IR.string("raw"), stringType)),
arrayType),
raw),
arrayType))
.useSourceInfoIfMissingFromForTree(n);
script.addChildAfter(defineRaw, var);
// Generate the call expression.
Node call = withType(IR.call(n.removeFirstChild(), callsiteId.cloneNode()), n.getTypeI());
for (Node child = templateLit.getFirstChild(); child != null; child = child.getNext()) {
if (!child.isString()) {
call.addChildToBack(child.removeFirstChild());
}
}
call.useSourceInfoIfMissingFromForTree(templateLit);
call.putBooleanProp(Node.FREE_CALL, !call.getFirstChild().isGetProp());
n.replaceWith(call);
t.reportCodeChange();
}
示例12: visitFunction
import com.google.javascript.rhino.JSDocInfoBuilder; //导入方法依赖的package包/类
private void visitFunction(NodeTraversal t, Node n, Node parent) {
// For member functions (eg. class Foo<T> { f() {} }), the JSDocInfo
// needs to go on the synthetic MEMBER_FUNCTION_DEF node.
boolean isMemberFunctionDef = parent.isMemberFunctionDef();
// Currently, we remove the overloading signature and drop the type information on the original
// signature.
String name = isMemberFunctionDef ? parent.getString() : n.getFirstChild().getString();
if (!name.isEmpty() && overloadStack.peek().containsKey(name)) {
compiler.report(JSError.make(n, OVERLOAD_NOT_SUPPORTED));
if (isMemberFunctionDef) {
t.reportCodeChange(parent.getParent());
parent.detach();
NodeUtil.markFunctionsDeleted(parent, compiler);
} else {
t.reportCodeChange(parent);
n.detach();
NodeUtil.markFunctionsDeleted(n, compiler);
}
Node original = overloadStack.peek().get(name);
processedOverloads.add(original);
Node paramList = original.getSecondChild();
paramList.removeChildren();
Node originalParent = original.getParent();
Node originalJsDocNode = originalParent.isMemberFunctionDef() || originalParent.isAssign()
? originalParent : original;
JSDocInfoBuilder builder = new JSDocInfoBuilder(false);
builder.recordType(new JSTypeExpression(
convertWithLocation(TypeDeclarationsIR.namedType("Function")), n.getSourceFileName()));
originalJsDocNode.setJSDocInfo(builder.build());
return;
}
overloadStack.peek().put(name, n);
Node jsDocNode = isMemberFunctionDef ? parent : n;
maybeAddGenerics(n, jsDocNode);
// Return types are colon types on the function node. Optional member functions are handled
// separately.
if (!(isMemberFunctionDef && n.isOptionalEs6Typed())) {
maybeVisitColonType(t, n, jsDocNode);
}
if (n.getLastChild().isEmpty()) {
n.replaceChild(n.getLastChild(), IR.block().useSourceInfoFrom(n));
}
if (!isMemberFunctionDef) {
maybeCreateQualifiedDeclaration(t, n, parent);
}
}
示例13: setJsDocWithType
import com.google.javascript.rhino.JSDocInfoBuilder; //导入方法依赖的package包/类
private static void setJsDocWithType(Node target, Node type) {
JSDocInfoBuilder builder = new JSDocInfoBuilder(false);
builder.recordType(new JSTypeExpression(type, ""));
target.setJSDocInfo(builder.build());
}
示例14: createUnknownTypeJsDocInfo
import com.google.javascript.rhino.JSDocInfoBuilder; //导入方法依赖的package包/类
private JSDocInfo createUnknownTypeJsDocInfo() {
JSDocInfoBuilder castToUnknownBuilder = new JSDocInfoBuilder(true);
castToUnknownBuilder.recordType(
new JSTypeExpression(JsDocInfoParser.parseTypeString("?"), ""));
return castToUnknownBuilder.build();
}