Search in sources :

Example 56 with Name

use of com.google.javascript.jscomp.GlobalNamespace.Name in project closure-compiler by google.

the class InlineAndCollapseProperties method referencesCollapsibleProperty.

/**
 * Returns whether a ReferenceCollection for some aliasing variable references a property on the
 * original aliased variable that may be collapsed in CollapseProperties.
 *
 * <p>See {@link Name#canCollapse} for what can/cannot be collapsed.
 */
private static boolean referencesCollapsibleProperty(ReferenceCollection aliasRefs, Name aliasedName, GlobalNamespace namespace) {
    for (Reference ref : aliasRefs.references) {
        if (ref.getParent() == null) {
            continue;
        }
        if (NodeUtil.isNormalOrOptChainGetProp(ref.getParent())) {
            // e.g. if the reference is "alias.b.someProp", this will be "b".
            String propertyName = ref.getParent().getString();
            // e.g. if the aliased name is "originalName", this will be "originalName.b".
            String originalPropertyName = aliasedName.getName() + "." + propertyName;
            Name originalProperty = namespace.getOwnSlot(originalPropertyName);
            // If the original property isn't in the namespace or can't be collapsed, keep going.
            if (originalProperty == null || !originalProperty.canCollapse()) {
                continue;
            }
            return true;
        }
    }
    return false;
}
Also used : Name(com.google.javascript.jscomp.GlobalNamespace.Name)

Example 57 with Name

use of com.google.javascript.jscomp.GlobalNamespace.Name in project closure-compiler by google.

the class GlobalNamespaceTest method esModuleLevelNamesAreCaptured.

@Test
public void esModuleLevelNamesAreCaptured() {
    GlobalNamespace namespace = parseAndGatherModuleData("class Foo {} Foo.Bar = 0; export {Foo};");
    ModuleMetadata metadata = lastCompiler.getModuleMetadataMap().getModulesByPath().get("test.js");
    Name x = namespace.getNameFromModule(metadata, "Foo.Bar");
    assertThat(x).isNotNull();
    assertThat(x.getDeclaration()).isNotNull();
}
Also used : ModuleMetadata(com.google.javascript.jscomp.modules.ModuleMetadataMap.ModuleMetadata) Name(com.google.javascript.jscomp.GlobalNamespace.Name) Test(org.junit.Test)

Example 58 with Name

use of com.google.javascript.jscomp.GlobalNamespace.Name in project closure-compiler by google.

the class GlobalNamespaceTest method rescanningExistingNodesDoesNotCreateDuplicateRefs.

@Test
public void rescanningExistingNodesDoesNotCreateDuplicateRefs() {
    GlobalNamespace namespace = parse("class Foo {} const Bar = Foo; const Baz = Bar;");
    Name foo = namespace.getOwnSlot("Foo");
    Name bar = namespace.getOwnSlot("Bar");
    Name baz = namespace.getOwnSlot("Baz");
    Collection<Ref> originalFooRefs = ImmutableList.copyOf(foo.getRefs());
    Collection<Ref> originalBarRefs = ImmutableList.copyOf(bar.getRefs());
    Collection<Ref> originalBazRefs = ImmutableList.copyOf(baz.getRefs());
    // Rescan all of the nodes for which we got refs as if they were newly added
    Node root = lastCompiler.getJsRoot();
    ImmutableSet.Builder<AstChange> astChangeSetBuilder = ImmutableSet.builder();
    for (Name name : ImmutableList.of(foo, bar, baz)) {
        for (Ref ref : name.getRefs()) {
            astChangeSetBuilder.add(createGlobalAstChangeForNode(root, ref.getNode()));
        }
    }
    namespace.scanNewNodes(astChangeSetBuilder.build());
    // We should get the same Name objects
    assertThat(namespace.getOwnSlot("Foo")).isEqualTo(foo);
    assertThat(namespace.getOwnSlot("Bar")).isEqualTo(bar);
    assertThat(namespace.getOwnSlot("Baz")).isEqualTo(baz);
    // ...and they should contain the same refs with no duplicates added
    assertThat(foo.getRefs()).containsExactlyElementsIn(originalFooRefs).inOrder();
    assertThat(bar.getRefs()).containsExactlyElementsIn(originalBarRefs).inOrder();
    assertThat(baz.getRefs()).containsExactlyElementsIn(originalBazRefs).inOrder();
}
Also used : Ref(com.google.javascript.jscomp.GlobalNamespace.Ref) ImmutableSet(com.google.common.collect.ImmutableSet) AstChange(com.google.javascript.jscomp.GlobalNamespace.AstChange) Node(com.google.javascript.rhino.Node) NodeSubject.assertNode(com.google.javascript.rhino.testing.NodeSubject.assertNode) Name(com.google.javascript.jscomp.GlobalNamespace.Name) Test(org.junit.Test)

Example 59 with Name

use of com.google.javascript.jscomp.GlobalNamespace.Name in project closure-compiler by google.

the class GlobalNamespaceTest method testGoogProvideName_multipleProvidesForName.

@Test
public void testGoogProvideName_multipleProvidesForName() {
    GlobalNamespace namespace = parse("goog.provide('a.b'); goog.provide('a.c');");
    Name a = namespace.getSlot("a");
    assertThat(a).isNotNull();
    assertThat(a.getGlobalSets()).isEqualTo(0);
}
Also used : Name(com.google.javascript.jscomp.GlobalNamespace.Name) Test(org.junit.Test)

Example 60 with Name

use of com.google.javascript.jscomp.GlobalNamespace.Name in project closure-compiler by google.

the class GlobalNamespaceTest method testObjectPatternRestNestedInDeclaration.

@Test
public void testObjectPatternRestNestedInDeclaration() {
    GlobalNamespace namespace = parse("const ns = {a: 3, b: {}}; const {a, b: {...c}} = ns;");
    Name ns = namespace.getSlot("ns");
    assertThat(ns.getGlobalSets()).isEqualTo(1);
    assertThat(ns.getTotalGets()).isEqualTo(1);
    assertThat(ns.getAliasingGets()).isEqualTo(1);
    Name nsB = namespace.getSlot("ns.b");
    assertThat(nsB.getGlobalSets()).isEqualTo(1);
    assertThat(nsB.getTotalGets()).isEqualTo(1);
    assertThat(nsB.getAliasingGets()).isEqualTo(1);
}
Also used : Name(com.google.javascript.jscomp.GlobalNamespace.Name) Test(org.junit.Test)

Aggregations

Name (com.google.javascript.jscomp.GlobalNamespace.Name)95 Test (org.junit.Test)72 Ref (com.google.javascript.jscomp.GlobalNamespace.Ref)25 Node (com.google.javascript.rhino.Node)22 ModuleMetadata (com.google.javascript.jscomp.modules.ModuleMetadataMap.ModuleMetadata)12 NodeSubject.assertNode (com.google.javascript.rhino.testing.NodeSubject.assertNode)12 JSDocInfo (com.google.javascript.rhino.JSDocInfo)5 AstChange (com.google.javascript.jscomp.GlobalNamespace.AstChange)2 Inlinability (com.google.javascript.jscomp.GlobalNamespace.Inlinability)2 JSTypeExpression (com.google.javascript.rhino.JSTypeExpression)2 JSType (com.google.javascript.rhino.jstype.JSType)2 LinkedHashSet (java.util.LinkedHashSet)2 ImmutableList (com.google.common.collect.ImmutableList)1 ImmutableSet (com.google.common.collect.ImmutableSet)1 PropagateConstantAnnotationsOverVars (com.google.javascript.jscomp.Normalize.PropagateConstantAnnotationsOverVars)1 Module (com.google.javascript.jscomp.modules.Module)1 FeatureSet (com.google.javascript.jscomp.parsing.parser.FeatureSet)1 QualifiedName (com.google.javascript.rhino.QualifiedName)1 ArrayList (java.util.ArrayList)1