Search in sources :

Example 16 with Name

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

the class GlobalNamespaceTest method testCannotCollapseAliasedConstructorProperty_consideringStaticInheritance.

@Test
public void testCannotCollapseAliasedConstructorProperty_consideringStaticInheritance() {
    this.assumeStaticInheritanceIsNotUsed = false;
    GlobalNamespace namespace = parse(lines("/** @constructor */", "var Foo = function() {}", "", "Foo.prop = prop;", "use(Foo);"));
    Name fooProp = namespace.getSlot("Foo.prop");
    assertThat(fooProp.canCollapse()).isFalse();
}
Also used : Name(com.google.javascript.jscomp.GlobalNamespace.Name) Test(org.junit.Test)

Example 17 with Name

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

the class GlobalNamespaceTest method testScanFromNodeNoticesHasOwnProperty.

@Test
public void testScanFromNodeNoticesHasOwnProperty() {
    GlobalNamespace namespace = parse("const x = {bar: 0}; const y = x; y.hasOwnProperty('bar');");
    Name xName = namespace.getOwnSlot("x");
    Name yName = namespace.getOwnSlot("y");
    assertThat(xName.usesHasOwnProperty()).isFalse();
    assertThat(yName.usesHasOwnProperty()).isTrue();
    // Replace "const baz = y.bar" with "const baz = x.bar"
    Node root = lastCompiler.getJsRoot();
    Node yDotHasOwnProperty = // SCRIPT
    root.getFirstChild().getLastChild().getFirstFirstChild();
    // `y`
    Node yNode = yDotHasOwnProperty.getFirstChild();
    assertNode(yDotHasOwnProperty).matchesQualifiedName("y.hasOwnProperty");
    Node xNode = IR.name("x");
    yNode.replaceWith(xNode);
    // Rescan the new nodes
    // In this case the new node is `x.hasOwnProperty`, since that's the full, new qualified name.
    namespace.scanNewNodes(ImmutableSet.of(createGlobalAstChangeForNode(root, yDotHasOwnProperty)));
    assertThat(xName.usesHasOwnProperty()).isTrue();
}
Also used : 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 18 with Name

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

the class GlobalNamespaceTest method localAssignmentWillNotBeConsideredADeclaration.

@Test
public void localAssignmentWillNotBeConsideredADeclaration() {
    GlobalNamespace namespace = parse("");
    Name n = namespace.createNameForTesting("a");
    Ref set1 = n.addSingleRefForTesting(Ref.Type.SET_FROM_GLOBAL);
    Ref localSet = n.addSingleRefForTesting(Ref.Type.SET_FROM_LOCAL);
    assertThat(n.getRefs()).containsExactly(set1, localSet).inOrder();
    assertThat(n.getDeclaration()).isEqualTo(set1);
    assertThat(n.getGlobalSets()).isEqualTo(1);
    assertThat(n.getLocalSets()).isEqualTo(1);
    n.removeRef(set1);
    // local set will not be used as the declaration
    assertThat(n.getDeclaration()).isNull();
    assertThat(n.getGlobalSets()).isEqualTo(0);
}
Also used : Ref(com.google.javascript.jscomp.GlobalNamespace.Ref) Name(com.google.javascript.jscomp.GlobalNamespace.Name) Test(org.junit.Test)

Example 19 with Name

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

the class GlobalNamespaceTest method hook.

@Test
public void hook() {
    GlobalNamespace namespace = parse("var a = a ? a : {}");
    Name a = namespace.getSlot("a");
    assertThat(a).isNotNull();
    assertThat(a.getRefs()).hasSize(3);
    assertThat(a.getLocalSets()).isEqualTo(0);
    assertThat(a.getGlobalSets()).isEqualTo(1);
}
Also used : Name(com.google.javascript.jscomp.GlobalNamespace.Name) Test(org.junit.Test)

Example 20 with Name

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

the class GlobalNamespaceTest method testObjectLitSpreadAliasInAssign.

@Test
public void testObjectLitSpreadAliasInAssign() {
    GlobalNamespace namespace = parse("const ns = {a: 3}; const x = {}; ({a: x.y} = {...ns});");
    Name ns = namespace.getSlot("ns");
    assertThat(ns.getGlobalSets()).isEqualTo(1);
    assertThat(ns.getAliasingGets()).isEqualTo(1);
    Name nsA = namespace.getSlot("ns.a");
    assertThat(nsA.getGlobalSets()).isEqualTo(1);
    assertThat(nsA.getAliasingGets()).isEqualTo(0);
    Name xY = namespace.getSlot("x.y");
    // TODO(b/117673791): this should be 1
    assertThat(xY.getGlobalSets()).isEqualTo(0);
}
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