Search in sources :

Example 26 with JSDocInfoBuilder

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

the class Es6TypedToEs6Converter method visitTypeAlias.

private void visitTypeAlias(NodeTraversal t, Node n, Node parent) {
    String alias = n.getString();
    if (t.getScope().isDeclared(alias, true)) {
        compiler.report(JSError.make(n, TYPE_ALIAS_ALREADY_DECLARED, alias));
    }
    JSDocInfoBuilder builder = JSDocInfoBuilder.maybeCopyFrom(n.getJSDocInfo());
    builder.recordTypedef(new JSTypeExpression(convertWithLocation(n.getFirstChild()), n.getSourceFileName()));
    Node newName = maybeGetQualifiedNameNode(IR.name(n.getString())).useSourceInfoIfMissingFromForTree(n);
    Node newDec1 = NodeUtil.newQNameDeclaration(compiler, newName.getQualifiedName(), null, builder.build()).useSourceInfoFromForTree(n);
    parent.replaceChild(n, newDec1);
    t.reportCodeChange();
}
Also used : TypeDeclarationNode(com.google.javascript.rhino.Node.TypeDeclarationNode) Node(com.google.javascript.rhino.Node) JSTypeExpression(com.google.javascript.rhino.JSTypeExpression) JSDocInfoBuilder(com.google.javascript.rhino.JSDocInfoBuilder)

Example 27 with JSDocInfoBuilder

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

the class Es6TypedToEs6Converter method visitFunction.

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

Example 28 with JSDocInfoBuilder

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

the class Es6ToEs3ClassSideInheritance method copyDeclarations.

/**
 * 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>");
        // In case there wasn't a type specified on the base class.
        info.recordType(unknown);
        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);
        }
    }
}
Also used : Node(com.google.javascript.rhino.Node) JSTypeExpression(com.google.javascript.rhino.JSTypeExpression) JSDocInfoBuilder(com.google.javascript.rhino.JSDocInfoBuilder)

Example 29 with JSDocInfoBuilder

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

the class VarCheck method visit.

@Override
public void visit(NodeTraversal t, Node n, Node parent) {
    if (n.isName()) {
        String varName = n.getString();
        // Only a function can have an empty name.
        if (varName.isEmpty()) {
            // Name is optional for function expressions
            // x = function() {...}
            // Arrow functions are also expressions and cannot have a name
            // x = () => {...}
            // Member functions have an empty NAME node string, because the actual name is stored on the
            // MEMBER_FUNCTION_DEF object that contains the FUNCTION.
            // class C { foo() {...} }
            // x = { foo() {...} }
            checkState(NodeUtil.isFunctionExpression(parent) || NodeUtil.isMethodDeclaration(parent));
            return;
        }
        // elsewhere. If so, mark it as a duplicate.
        if ((parent.isVar() || NodeUtil.isFunctionDeclaration(parent)) && varsToDeclareInExterns.contains(varName)) {
            createSynthesizedExternVar(varName);
            JSDocInfoBuilder builder = JSDocInfoBuilder.maybeCopyFrom(n.getJSDocInfo());
            builder.addSuppression("duplicate");
            n.setJSDocInfo(builder.build());
        }
        // Check that the var has been declared.
        Scope scope = t.getScope();
        Var var = scope.getVar(varName);
        if (var == null) {
            if ((NodeUtil.isFunctionExpression(parent) || NodeUtil.isClassExpression(parent)) && n == parent.getFirstChild()) {
            // e.g. [ function foo() {} ], it's okay if "foo" isn't defined in the
            // current scope.
            } else if (NodeUtil.isNonlocalModuleExportName(n)) {
            // e.g. "export {a as b}" or "import {b as a} from './foo.js'
            // where b is defined in a module's export entries but not in any module scope.
            } else {
                boolean isArguments = scope.isFunctionScope() && ARGUMENTS.equals(varName);
                // The extern checks are stricter, don't report a second error.
                if (!isArguments && !(strictExternCheck && t.getInput().isExtern())) {
                    t.report(n, UNDEFINED_VAR_ERROR, varName);
                }
                if (validityCheck) {
                    // some variable/generated some code with an undefined reference.
                    throw new IllegalStateException("Unexpected variable " + varName);
                } else {
                    createSynthesizedExternVar(varName);
                    scope.getGlobalScope().declare(varName, n, compiler.getSynthesizedExternsInput());
                }
            }
            return;
        }
        CompilerInput currInput = t.getInput();
        CompilerInput varInput = var.input;
        if (currInput == varInput || currInput == null || varInput == null) {
            // The variable was defined in the same file. This is fine.
            return;
        }
        // Check module dependencies.
        JSModule currModule = currInput.getModule();
        JSModule varModule = varInput.getModule();
        JSModuleGraph moduleGraph = compiler.getModuleGraph();
        if (!validityCheck && varModule != currModule && varModule != null && currModule != null) {
            if (moduleGraph.dependsOn(currModule, varModule)) {
            // The module dependency was properly declared.
            } else {
                if (scope.isGlobal()) {
                    if (moduleGraph.dependsOn(varModule, currModule)) {
                        // The variable reference violates a declared module dependency.
                        t.report(n, VIOLATED_MODULE_DEP_ERROR, currModule.getName(), varModule.getName(), varName);
                    } else {
                        // The variable reference is between two modules that have no
                        // dependency relationship. This should probably be considered an
                        // error, but just issue a warning for now.
                        t.report(n, MISSING_MODULE_DEP_ERROR, currModule.getName(), varModule.getName(), varName);
                    }
                } else {
                    t.report(n, STRICT_MODULE_DEP_ERROR, currModule.getName(), varModule.getName(), varName);
                }
            }
        }
    }
}
Also used : JSDocInfoBuilder(com.google.javascript.rhino.JSDocInfoBuilder)

Example 30 with JSDocInfoBuilder

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

the class JsdocUtil method markConstant.

static JSDocInfo markConstant(JSDocInfo oldJSDoc) {
    JSDocInfoBuilder builder = JSDocInfoBuilder.maybeCopyFrom(oldJSDoc);
    builder.recordConstancy();
    return builder.build();
}
Also used : JSDocInfoBuilder(com.google.javascript.rhino.JSDocInfoBuilder)

Aggregations

JSDocInfoBuilder (com.google.javascript.rhino.JSDocInfoBuilder)40 Node (com.google.javascript.rhino.Node)27 JSTypeExpression (com.google.javascript.rhino.JSTypeExpression)18 TypeDeclarationNode (com.google.javascript.rhino.Node.TypeDeclarationNode)10 JSDocInfo (com.google.javascript.rhino.JSDocInfo)8 MemberDefinition (com.google.javascript.jscomp.PolymerPass.MemberDefinition)4 Visibility (com.google.javascript.rhino.JSDocInfo.Visibility)2 ArrayList (java.util.ArrayList)2 HashSet (java.util.HashSet)2 ClassDeclarationMetadata (com.google.javascript.jscomp.Es6RewriteClass.ClassDeclarationMetadata)1 FeatureSet (com.google.javascript.jscomp.parsing.parser.FeatureSet)1 TypeI (com.google.javascript.rhino.TypeI)1 TypeIRegistry (com.google.javascript.rhino.TypeIRegistry)1 HashMap (java.util.HashMap)1 Map (java.util.Map)1