Search in sources :

Example 6 with FeatureSet

use of com.google.javascript.jscomp.parsing.parser.FeatureSet in project closure-compiler by google.

the class LazyParsedDependencyInfo method getLoadFlags.

@Override
public ImmutableMap<String, String> getLoadFlags() {
    if (loadFlags == null) {
        Map<String, String> loadFlagsBuilder = new TreeMap<>();
        boolean hadSourceInMemory = ast.getSourceFile().hasSourceInMemory();
        loadFlagsBuilder.putAll(delegate.getLoadFlags());
        FeatureSet features = ast.getFeatures(compiler);
        if (features.has(Feature.MODULES)) {
            String previousModule = loadFlagsBuilder.get("module");
            if (previousModule != null && !previousModule.equals("es6")) {
                compiler.report(JSError.make(getName(), /* lineno= */
                -1, /* charno= */
                -1, ModuleLoader.MODULE_CONFLICT, getName()));
            }
            loadFlagsBuilder.put("module", "es6");
        }
        String version = features.version();
        if (!version.equals("es3")) {
            loadFlagsBuilder.put("lang", version);
        }
        loadFlags = ImmutableMap.copyOf(loadFlagsBuilder);
        // Don't preserve the source in memory if it was loaded only for getLoadFlags().
        if (!hadSourceInMemory) {
            ast.getSourceFile().clearCachedSource();
        }
        // Don't preserve the full AST longer than necessary.  It can consume a lot of memory.
        ast = null;
    }
    return loadFlags;
}
Also used : FeatureSet(com.google.javascript.jscomp.parsing.parser.FeatureSet) TreeMap(java.util.TreeMap)

Example 7 with FeatureSet

use of com.google.javascript.jscomp.parsing.parser.FeatureSet in project closure-compiler by google.

the class ParserRunner method parse.

public static ParseResult parse(StaticSourceFile sourceFile, String sourceString, Config config, ErrorReporter errorReporter) {
    // TODO(johnlenz): unify "SourceFile", "Es6ErrorReporter" and "Config"
    String sourceName = sourceFile.getName();
    try {
        SourceFile file = new SourceFile(sourceName, sourceString);
        boolean keepGoing = config.runMode() == Config.RunMode.KEEP_GOING;
        Es6ErrorReporter es6ErrorReporter = new Es6ErrorReporter(errorReporter, keepGoing);
        com.google.javascript.jscomp.parsing.parser.Parser.Config es6config = newParserConfig(config);
        Parser p = new Parser(es6config, es6ErrorReporter, file);
        ProgramTree tree = p.parseProgram();
        Node root = null;
        List<Comment> comments = ImmutableList.of();
        FeatureSet features = p.getFeatures();
        if (tree != null && (!es6ErrorReporter.hadError() || keepGoing)) {
            IRFactory factory = IRFactory.transformTree(tree, sourceFile, config, errorReporter);
            root = factory.getResultNode();
            features = features.union(factory.getFeatures());
            root.putProp(Node.FEATURE_SET, features);
            if (config.jsDocParsingMode().shouldParseDescriptions()) {
                comments = p.getComments();
            }
        }
        return new ParseResult(root, comments, features, p.getSourceMapURL());
    } catch (Throwable t) {
        throw new RuntimeException("Exception parsing \"" + sourceName + "\"", t);
    }
}
Also used : Comment(com.google.javascript.jscomp.parsing.parser.trees.Comment) ProgramTree(com.google.javascript.jscomp.parsing.parser.trees.ProgramTree) Node(com.google.javascript.rhino.Node) Parser(com.google.javascript.jscomp.parsing.parser.Parser) FeatureSet(com.google.javascript.jscomp.parsing.parser.FeatureSet) StaticSourceFile(com.google.javascript.rhino.StaticSourceFile) SourceFile(com.google.javascript.jscomp.parsing.parser.SourceFile)

Example 8 with FeatureSet

use of com.google.javascript.jscomp.parsing.parser.FeatureSet in project closure-compiler by google.

the class ConvertChunksToESModules method convertChunkSourcesToModules.

/**
 * Move all code in a chunk into the first input and mark it as an ESModule. At this point in the
 * compilation, all input files should be scripts.
 */
private void convertChunkSourcesToModules() {
    for (JSChunk chunk : compiler.getModuleGraph().getAllChunks()) {
        if (chunk.getInputs().isEmpty()) {
            continue;
        }
        CompilerInput firstInput = null;
        for (CompilerInput input : chunk.getInputs()) {
            Node astRoot = input.getAstRoot(compiler);
            FeatureSet scriptFeatures = NodeUtil.getFeatureSetOfScript(astRoot);
            checkState(!scriptFeatures.contains(FeatureSet.ES2015_MODULES));
            if (firstInput == null) {
                firstInput = input;
                scriptFeatures = scriptFeatures.union(FeatureSet.ES2015_MODULES);
                astRoot.putProp(Node.FEATURE_SET, scriptFeatures);
                Node moduleBody = new Node(Token.MODULE_BODY);
                moduleBody.srcref(astRoot);
                moduleBody.addChildrenToFront(astRoot.removeChildren());
                astRoot.addChildToFront(moduleBody);
                compiler.reportChangeToEnclosingScope(moduleBody);
            } else {
                Node firstInputAstRoot = firstInput.getAstRoot(compiler);
                FeatureSet firstInputScriptFeatures = NodeUtil.getFeatureSetOfScript(firstInputAstRoot);
                FeatureSet combinedFeatureSet = firstInputScriptFeatures.union(NodeUtil.getFeatureSetOfScript(astRoot));
                astRoot.putProp(Node.FEATURE_SET, combinedFeatureSet);
                Node moduleBody = firstInputAstRoot.getFirstChild();
                checkState(moduleBody != null && moduleBody.isModuleBody());
                moduleBody.addChildrenToBack(astRoot.removeChildren());
                compiler.reportChangeToEnclosingScope(firstInputAstRoot);
                compiler.reportChangeToChangeScope(astRoot);
            }
        }
    }
}
Also used : Node(com.google.javascript.rhino.Node) FeatureSet(com.google.javascript.jscomp.parsing.parser.FeatureSet)

Example 9 with FeatureSet

use of com.google.javascript.jscomp.parsing.parser.FeatureSet in project closure-compiler by google.

the class PolymerBehaviorExtractor method extractBehaviors.

/**
 * Extracts all Behaviors from an array literal, recursively. Entries in the array can be
 * object literals or array literals (of other behaviors). Behavior names must be
 * global, fully qualified names.
 * @see https://github.com/Polymer/polymer/blob/0.8-preview/PRIMER.md#behaviors
 * @return A list of all {@code BehaviorDefinitions} in the array.
 */
ImmutableList<BehaviorDefinition> extractBehaviors(Node behaviorArray) {
    if (behaviorArray == null) {
        return ImmutableList.of();
    }
    if (!behaviorArray.isArrayLit()) {
        compiler.report(JSError.make(behaviorArray, PolymerPassErrors.POLYMER_INVALID_BEHAVIOR_ARRAY));
        return ImmutableList.of();
    }
    ImmutableList.Builder<BehaviorDefinition> behaviors = ImmutableList.builder();
    for (Node behaviorName : behaviorArray.children()) {
        if (behaviorName.isObjectLit()) {
            PolymerPassStaticUtils.switchDollarSignPropsToBrackets(behaviorName, compiler);
            PolymerPassStaticUtils.quoteListenerAndHostAttributeKeys(behaviorName, compiler);
            if (NodeUtil.getFirstPropMatchingKey(behaviorName, "is") != null) {
                compiler.report(JSError.make(behaviorName, PolymerPassErrors.POLYMER_INVALID_BEHAVIOR));
            }
            behaviors.add(new BehaviorDefinition(PolymerPassStaticUtils.extractProperties(behaviorName, PolymerClassDefinition.DefinitionType.ObjectLiteral, compiler), getBehaviorFunctionsToCopy(behaviorName), getNonPropertyMembersToCopy(behaviorName), !NodeUtil.isInFunction(behaviorName), (FeatureSet) NodeUtil.getEnclosingScript(behaviorName).getProp(Node.FEATURE_SET)));
            continue;
        }
        Name behaviorGlobalName = globalNames.getSlot(behaviorName.getQualifiedName());
        boolean isGlobalDeclaration = true;
        if (behaviorGlobalName == null) {
            compiler.report(JSError.make(behaviorName, PolymerPassErrors.POLYMER_UNQUALIFIED_BEHAVIOR));
            continue;
        }
        Ref behaviorDeclaration = behaviorGlobalName.getDeclaration();
        // Use any set as a backup declaration, even if it's local.
        if (behaviorDeclaration == null) {
            List<Ref> behaviorRefs = behaviorGlobalName.getRefs();
            for (Ref ref : behaviorRefs) {
                if (ref.isSet()) {
                    isGlobalDeclaration = false;
                    behaviorDeclaration = ref;
                    break;
                }
            }
        }
        if (behaviorDeclaration == null) {
            compiler.report(JSError.make(behaviorName, PolymerPassErrors.POLYMER_UNQUALIFIED_BEHAVIOR));
            continue;
        }
        Node behaviorDeclarationNode = behaviorDeclaration.getNode();
        JSDocInfo behaviorInfo = NodeUtil.getBestJSDocInfo(behaviorDeclarationNode);
        if (behaviorInfo == null || !behaviorInfo.isPolymerBehavior()) {
            compiler.report(JSError.make(behaviorDeclarationNode, PolymerPassErrors.POLYMER_UNANNOTATED_BEHAVIOR));
        }
        Node behaviorValue = NodeUtil.getRValueOfLValue(behaviorDeclarationNode);
        if (behaviorValue == null) {
            compiler.report(JSError.make(behaviorName, PolymerPassErrors.POLYMER_UNQUALIFIED_BEHAVIOR));
        } else if (behaviorValue.isArrayLit()) {
            // Individual behaviors can also be arrays of behaviors. Parse them recursively.
            behaviors.addAll(extractBehaviors(behaviorValue));
        } else if (behaviorValue.isObjectLit()) {
            PolymerPassStaticUtils.switchDollarSignPropsToBrackets(behaviorValue, compiler);
            PolymerPassStaticUtils.quoteListenerAndHostAttributeKeys(behaviorValue, compiler);
            if (NodeUtil.getFirstPropMatchingKey(behaviorValue, "is") != null) {
                compiler.report(JSError.make(behaviorValue, PolymerPassErrors.POLYMER_INVALID_BEHAVIOR));
            }
            behaviors.add(new BehaviorDefinition(PolymerPassStaticUtils.extractProperties(behaviorValue, PolymerClassDefinition.DefinitionType.ObjectLiteral, compiler), getBehaviorFunctionsToCopy(behaviorValue), getNonPropertyMembersToCopy(behaviorValue), isGlobalDeclaration, (FeatureSet) NodeUtil.getEnclosingScript(behaviorValue).getProp(Node.FEATURE_SET)));
        } else {
            compiler.report(JSError.make(behaviorName, PolymerPassErrors.POLYMER_UNQUALIFIED_BEHAVIOR));
        }
    }
    return behaviors.build();
}
Also used : Ref(com.google.javascript.jscomp.GlobalNamespace.Ref) ImmutableList(com.google.common.collect.ImmutableList) Node(com.google.javascript.rhino.Node) FeatureSet(com.google.javascript.jscomp.parsing.parser.FeatureSet) JSDocInfo(com.google.javascript.rhino.JSDocInfo) Name(com.google.javascript.jscomp.GlobalNamespace.Name)

Example 10 with FeatureSet

use of com.google.javascript.jscomp.parsing.parser.FeatureSet in project closure-compiler by google.

the class PolymerClassDefinition method extractFromCallNode.

/**
 * Validates the class definition and if valid, destructively extracts the class definition from
 * the AST.
 */
@Nullable
static PolymerClassDefinition extractFromCallNode(Node callNode, AbstractCompiler compiler, GlobalNamespace globalNames) {
    Node descriptor = NodeUtil.getArgumentForCallOrNew(callNode, 0);
    if (descriptor == null || !descriptor.isObjectLit()) {
        // report bad class definition
        compiler.report(JSError.make(callNode, PolymerPassErrors.POLYMER_DESCRIPTOR_NOT_VALID));
        return null;
    }
    int paramCount = callNode.getChildCount() - 1;
    if (paramCount != 1) {
        compiler.report(JSError.make(callNode, PolymerPassErrors.POLYMER_UNEXPECTED_PARAMS));
        return null;
    }
    Node elName = NodeUtil.getFirstPropMatchingKey(descriptor, "is");
    if (elName == null) {
        compiler.report(JSError.make(callNode, PolymerPassErrors.POLYMER_MISSING_IS));
        return null;
    }
    Node target;
    if (NodeUtil.isNameDeclaration(callNode.getGrandparent())) {
        target = IR.name(callNode.getParent().getString());
    } else if (callNode.getParent().isAssign()) {
        target = callNode.getParent().getFirstChild().cloneTree();
    } else {
        String elNameStringBase = elName.isQualifiedName() ? elName.getQualifiedName().replace('.', '$') : elName.getString();
        String elNameString = CaseFormat.LOWER_HYPHEN.to(CaseFormat.UPPER_CAMEL, elNameStringBase);
        elNameString += "Element";
        target = IR.name(elNameString);
    }
    JSDocInfo classInfo = NodeUtil.getBestJSDocInfo(target);
    JSDocInfo ctorInfo = null;
    Node constructor = NodeUtil.getFirstPropMatchingKey(descriptor, "factoryImpl");
    if (constructor == null) {
        constructor = NodeUtil.emptyFunction();
        compiler.reportChangeToChangeScope(constructor);
        constructor.useSourceInfoFromForTree(callNode);
    } else {
        ctorInfo = NodeUtil.getBestJSDocInfo(constructor);
    }
    Node baseClass = NodeUtil.getFirstPropMatchingKey(descriptor, "extends");
    String nativeBaseElement = baseClass == null ? null : baseClass.getString();
    Node behaviorArray = NodeUtil.getFirstPropMatchingKey(descriptor, "behaviors");
    PolymerBehaviorExtractor behaviorExtractor = new PolymerBehaviorExtractor(compiler, globalNames);
    ImmutableList<BehaviorDefinition> behaviors = behaviorExtractor.extractBehaviors(behaviorArray);
    List<MemberDefinition> allProperties = new ArrayList<>();
    for (BehaviorDefinition behavior : behaviors) {
        overwriteMembersIfPresent(allProperties, behavior.props);
    }
    overwriteMembersIfPresent(allProperties, PolymerPassStaticUtils.extractProperties(descriptor, DefinitionType.ObjectLiteral, compiler));
    FeatureSet newFeatures = null;
    if (!behaviors.isEmpty()) {
        newFeatures = behaviors.get(0).features;
        for (int i = 1; i < behaviors.size(); i++) {
            newFeatures = newFeatures.union(behaviors.get(i).features);
        }
    }
    return new PolymerClassDefinition(DefinitionType.ObjectLiteral, callNode, target, descriptor, classInfo, new MemberDefinition(ctorInfo, null, constructor), nativeBaseElement, allProperties, behaviors, newFeatures);
}
Also used : Node(com.google.javascript.rhino.Node) ArrayList(java.util.ArrayList) FeatureSet(com.google.javascript.jscomp.parsing.parser.FeatureSet) JSDocInfo(com.google.javascript.rhino.JSDocInfo) BehaviorDefinition(com.google.javascript.jscomp.PolymerBehaviorExtractor.BehaviorDefinition) MemberDefinition(com.google.javascript.jscomp.PolymerPass.MemberDefinition) Nullable(javax.annotation.Nullable)

Aggregations

FeatureSet (com.google.javascript.jscomp.parsing.parser.FeatureSet)17 Node (com.google.javascript.rhino.Node)14 MemberDefinition (com.google.javascript.jscomp.PolymerPass.MemberDefinition)4 JSDocInfo (com.google.javascript.rhino.JSDocInfo)4 Nullable (javax.annotation.Nullable)3 ImmutableList (com.google.common.collect.ImmutableList)2 BehaviorDefinition (com.google.javascript.jscomp.PolymerBehaviorExtractor.BehaviorDefinition)2 ArrayList (java.util.ArrayList)2 Name (com.google.javascript.jscomp.GlobalNamespace.Name)1 Ref (com.google.javascript.jscomp.GlobalNamespace.Ref)1 Polyfill (com.google.javascript.jscomp.PolyfillUsageFinder.Polyfill)1 Parser (com.google.javascript.jscomp.parsing.parser.Parser)1 SourceFile (com.google.javascript.jscomp.parsing.parser.SourceFile)1 Comment (com.google.javascript.jscomp.parsing.parser.trees.Comment)1 ProgramTree (com.google.javascript.jscomp.parsing.parser.trees.ProgramTree)1 JSDocInfoBuilder (com.google.javascript.rhino.JSDocInfoBuilder)1 JSTypeExpression (com.google.javascript.rhino.JSTypeExpression)1 StaticSourceFile (com.google.javascript.rhino.StaticSourceFile)1 LinkedHashMap (java.util.LinkedHashMap)1 TreeMap (java.util.TreeMap)1