Search in sources :

Example 21 with MemberDefinition

use of com.google.javascript.jscomp.PolymerPass.MemberDefinition in project closure-compiler by google.

the class PolymerClassDefinition method removeBehaviorPropsOverlappingWithElementProps.

/**
 * Removes any behavior properties from the given map that have the same name as a property in the
 * given list.
 *
 * <p>For example, if a Polymer element with a property "name" depends on a behavior with a
 * property "name", then this method removes the behavior property in favor of the element
 * property.
 */
private static void removeBehaviorPropsOverlappingWithElementProps(Map<MemberDefinition, BehaviorDefinition> behaviorProps, List<MemberDefinition> polymerElementProps) {
    if (behaviorProps == null) {
        return;
    }
    Set<String> elementPropNames = polymerElementProps.stream().map(x -> x.name.getString()).collect(Collectors.toSet());
    Iterator<Map.Entry<MemberDefinition, BehaviorDefinition>> behaviorsItr = behaviorProps.entrySet().iterator();
    while (behaviorsItr.hasNext()) {
        MemberDefinition memberDefinition = behaviorsItr.next().getKey();
        if (elementPropNames.contains(memberDefinition.name.getString())) {
            behaviorsItr.remove();
        }
    }
}
Also used : BehaviorDefinition(com.google.javascript.jscomp.PolymerBehaviorExtractor.BehaviorDefinition) Iterator(java.util.Iterator) CaseFormat(com.google.common.base.CaseFormat) MemberDefinition(com.google.javascript.jscomp.PolymerPass.MemberDefinition) Supplier(com.google.common.base.Supplier) FeatureSet(com.google.javascript.jscomp.parsing.parser.FeatureSet) Set(java.util.Set) Collectors(java.util.stream.Collectors) Preconditions.checkState(com.google.common.base.Preconditions.checkState) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) LinkedHashMap(java.util.LinkedHashMap) List(java.util.List) ImmutableList(com.google.common.collect.ImmutableList) Map(java.util.Map) IR(com.google.javascript.rhino.IR) JSDocInfo(com.google.javascript.rhino.JSDocInfo) ModuleMetadata(com.google.javascript.jscomp.modules.ModuleMetadataMap.ModuleMetadata) Nullable(javax.annotation.Nullable) MoreObjects.toStringHelper(com.google.common.base.MoreObjects.toStringHelper) Node(com.google.javascript.rhino.Node) MemberDefinition(com.google.javascript.jscomp.PolymerPass.MemberDefinition)

Example 22 with MemberDefinition

use of com.google.javascript.jscomp.PolymerPass.MemberDefinition in project closure-compiler by google.

the class PolymerPassStaticUtils method extractProperties.

/**
 * Finds a list of {@link MemberDefinition}s for the {@code properties} block of the given
 * descriptor Object literal.
 *
 * @param descriptor The Polymer properties configuration object literal node.
 * @param constructor If we are finding the properties of an ES6 class, the constructor function
 *     node for that class, otherwise null. We'll prefer JSDoc from property initialization
 *     statements in this constructor over the JSDoc within the Polymer properties configuration
 *     object.
 */
static ImmutableList<MemberDefinition> extractProperties(Node descriptor, PolymerClassDefinition.DefinitionType defType, AbstractCompiler compiler, @Nullable Node constructor) {
    Node properties = descriptor;
    if (defType == PolymerClassDefinition.DefinitionType.ObjectLiteral) {
        properties = NodeUtil.getFirstPropMatchingKey(descriptor, "properties");
    }
    if (properties == null) {
        return ImmutableList.of();
    }
    Map<String, JSDocInfo> constructorPropertyJsDoc = new HashMap<>();
    if (constructor != null) {
        collectConstructorPropertyJsDoc(constructor, constructorPropertyJsDoc);
    }
    ImmutableList.Builder<MemberDefinition> members = ImmutableList.builder();
    for (Node keyNode = properties.getFirstChild(); keyNode != null; keyNode = keyNode.getNext()) {
        // The JSDoc for a Polymer property in the constructor should win over the JSDoc in the
        // Polymer properties configuration object.
        JSDocInfo constructorJsDoc = constructorPropertyJsDoc.get(keyNode.getString());
        JSDocInfo propertiesConfigJsDoc = NodeUtil.getBestJSDocInfo(keyNode);
        JSDocInfo bestJsDoc;
        if (constructorJsDoc != null) {
            bestJsDoc = constructorJsDoc;
            if (propertiesConfigJsDoc != null) {
                // Warn if the user put JSDoc in both places.
                compiler.report(JSError.make(keyNode, POLYMER_MISPLACED_PROPERTY_JSDOC));
            }
        } else {
            bestJsDoc = propertiesConfigJsDoc;
        }
        members.add(new MemberDefinition(bestJsDoc, keyNode, keyNode.getFirstChild()));
    }
    return members.build();
}
Also used : HashMap(java.util.HashMap) ImmutableList(com.google.common.collect.ImmutableList) Node(com.google.javascript.rhino.Node) JSDocInfo(com.google.javascript.rhino.JSDocInfo) MemberDefinition(com.google.javascript.jscomp.PolymerPass.MemberDefinition)

Aggregations

MemberDefinition (com.google.javascript.jscomp.PolymerPass.MemberDefinition)22 Node (com.google.javascript.rhino.Node)21 JSDocInfo (com.google.javascript.rhino.JSDocInfo)13 JSTypeExpression (com.google.javascript.rhino.JSTypeExpression)9 BehaviorDefinition (com.google.javascript.jscomp.PolymerBehaviorExtractor.BehaviorDefinition)7 ArrayList (java.util.ArrayList)7 ImmutableList (com.google.common.collect.ImmutableList)6 LinkedHashMap (java.util.LinkedHashMap)6 FeatureSet (com.google.javascript.jscomp.parsing.parser.FeatureSet)5 JSDocInfoBuilder (com.google.javascript.rhino.JSDocInfoBuilder)4 HashMap (java.util.HashMap)4 Nullable (javax.annotation.Nullable)4 Map (java.util.Map)3 HashSet (java.util.HashSet)2 List (java.util.List)2 CaseFormat (com.google.common.base.CaseFormat)1 MoreObjects.toStringHelper (com.google.common.base.MoreObjects.toStringHelper)1 Preconditions.checkState (com.google.common.base.Preconditions.checkState)1 Supplier (com.google.common.base.Supplier)1 ModuleMetadata (com.google.javascript.jscomp.modules.ModuleMetadataMap.ModuleMetadata)1