Search in sources :

Example 76 with Name

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

the class GlobalNamespaceTest method testCannotCollapseAliasedObjectLitProperty.

@Test
public void testCannotCollapseAliasedObjectLitProperty() {
    GlobalNamespace namespace = parse("var foo = {prop: 0}; use(foo);");
    Name fooProp = namespace.getSlot("foo.prop");
    // We should not convert foo.prop -> foo$prop because use(foo) might read foo.prop
    assertThat(fooProp.canCollapse()).isFalse();
}
Also used : Name(com.google.javascript.jscomp.GlobalNamespace.Name) Test(org.junit.Test)

Example 77 with Name

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

the class GlobalNamespaceTest method testStaticInheritedReferencesDontReferToSuperclass.

@Test
public void testStaticInheritedReferencesDontReferToSuperclass() {
    GlobalNamespace namespace = parse(lines("class Superclass {", "  static staticMethod() {}", "}", "class Subclass extends Superclass {}", "Subclass.staticMethod();", "Subclass.staticMethod?.();", "Subclass?.staticMethod();"));
    Name superclass = namespace.getOwnSlot("Superclass");
    assertThat(superclass.getSubclassingGets()).isEqualTo(1);
    Name superclassStaticMethod = namespace.getOwnSlot("Superclass.staticMethod");
    assertThat(superclassStaticMethod.getRefs()).hasSize(1);
    assertThat(superclassStaticMethod.getDeclaration()).isNotNull();
    Name subclassStaticMethod = namespace.getOwnSlot("Subclass.staticMethod");
    // 2 references:
    // `Subclass.staticMethod()`
    // `Subclass.staticMethod?.()`
    // `SubClass?.staticMethod()` is a reference to `SubClass`, but not
    // to `SubClass.staticmethod`.
    assertThat(subclassStaticMethod.getRefs()).hasSize(2);
    assertThat(subclassStaticMethod.getDeclaration()).isNull();
    assertThat(subclassStaticMethod.getCallGets()).isEqualTo(2);
    Name subclass = namespace.getOwnSlot("Subclass");
    assertThat(subclass.getRefs()).hasSize(2);
    // `class Subclass` is the declaration reference
    assertThat(subclass.getDeclaration()).isNotNull();
    // `SubClass?.staticMethod` is an aliasing get on `SubClass`
    assertThat(subclass.getAliasingGets()).isEqualTo(1);
}
Also used : Name(com.google.javascript.jscomp.GlobalNamespace.Name) Test(org.junit.Test)

Example 78 with Name

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

the class GlobalNamespaceTest method testObjectLitSpreadAliasInDeclaration.

@Test
public void testObjectLitSpreadAliasInDeclaration() {
    GlobalNamespace namespace = parse("const ns = {a: 3}; const {a} = {...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 a = namespace.getSlot("a");
    assertThat(a.getGlobalSets()).isEqualTo(1);
}
Also used : Name(com.google.javascript.jscomp.GlobalNamespace.Name) Test(org.junit.Test)

Example 79 with Name

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

the class GlobalNamespaceTest method testGoogProvideNamespace_explicitAssignment.

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

Example 80 with Name

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

the class GlobalNamespaceTest method testObjectPatternAliasInForOf.

@Test
public void testObjectPatternAliasInForOf() {
    GlobalNamespace namespace = parse("const ns = {a: 3}; for (const {a: b} of [ns]) {}");
    Name bar = namespace.getSlot("ns");
    assertThat(bar.getGlobalSets()).isEqualTo(1);
    assertThat(bar.getAliasingGets()).isEqualTo(1);
    // GlobalNamespace ignores for-of and array literals, not realizing that `b` reads `ns.a`
    Name nsA = namespace.getSlot("ns.a");
    assertThat(nsA.getGlobalSets()).isEqualTo(1);
    assertThat(nsA.getAliasingGets()).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