Search in sources :

Example 26 with Name

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

the class GlobalNamespaceTest method testGoogProvideNamespace_assignmentToProperty.

@Test
public void testGoogProvideNamespace_assignmentToProperty() {
    GlobalNamespace namespace = parse("goog.provide('a.b'); a.b.Class = class {};");
    Name abClass = namespace.getSlot("a.b.Class");
    assertThat(abClass).isNotNull();
    assertThat(abClass.getGlobalSets()).isEqualTo(1);
    assertThat(abClass.getParent()).isEqualTo(namespace.getSlot("a.b"));
}
Also used : Name(com.google.javascript.jscomp.GlobalNamespace.Name) Test(org.junit.Test)

Example 27 with Name

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

the class GlobalNamespaceTest method testNestedObjectPatternAliasInDeclaration.

@Test
public void testNestedObjectPatternAliasInDeclaration() {
    GlobalNamespace namespace = parse("const ns = {a: {b: 3}}; const {a: {b}} = ns;");
    Name bar = namespace.getSlot("ns");
    assertThat(bar.getGlobalSets()).isEqualTo(1);
    assertThat(bar.getAliasingGets()).isEqualTo(1);
    // we treat ns.a as having an 'aliasing' get since we don't traverse into the nested pattern
    Name nsA = namespace.getSlot("ns.a");
    assertThat(nsA.getGlobalSets()).isEqualTo(1);
    assertThat(nsA.getAliasingGets()).isEqualTo(1);
    Name nsAB = namespace.getSlot("ns.a.b");
    assertThat(nsAB.getGlobalSets()).isEqualTo(1);
    // we don't consider this an 'aliasing get' because it's in a nested pattern
    assertThat(nsAB.getAliasingGets()).isEqualTo(0);
    Name b = namespace.getSlot("b");
    assertThat(b.getGlobalSets()).isEqualTo(1);
    assertThat(b.getTotalGets()).isEqualTo(0);
}
Also used : Name(com.google.javascript.jscomp.GlobalNamespace.Name) Test(org.junit.Test)

Example 28 with Name

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

the class GlobalNamespaceTest method firstDeclarationJSDocAlwaysWins.

@Test
public void firstDeclarationJSDocAlwaysWins() {
    GlobalNamespace namespace = parse(lines(// 
    "const X = {};", // later assignment should win
    "/** @type {symbol} */", "X.number;", // this is the JSDoc we should use
    "/** @type {number} */", "X.number = 3;", "/** @type {string} */", "X.number = 'hi';", "/** @type {Object} */", "X.number;", ""));
    Name nameX = namespace.getOwnSlot("X.number");
    Ref declarationRef = nameX.getDeclaration();
    assertThat(declarationRef).isNotNull();
    // make sure first assignment is considered to be the declaration
    Node declarationNode = declarationRef.getNode();
    assertNode(declarationNode).matchesQualifiedName("X.number");
    Node assignNode = declarationNode.getParent();
    assertNode(assignNode).isAssign();
    Node valueNode = declarationNode.getNext();
    assertNode(valueNode).isNumber(3);
    // Make sure JSDoc on the first assignment is the JSDoc for the name
    JSDocInfo jsDocInfo = nameX.getJSDocInfo();
    assertThat(jsDocInfo).isNotNull();
    JSTypeExpression jsTypeExpression = jsDocInfo.getType();
    assertThat(jsTypeExpression).isNotNull();
    JSType jsType = jsTypeExpression.evaluate(/* scope= */
    null, lastCompiler.getTypeRegistry());
    assertType(jsType).isNumber();
}
Also used : Ref(com.google.javascript.jscomp.GlobalNamespace.Ref) JSType(com.google.javascript.rhino.jstype.JSType) Node(com.google.javascript.rhino.Node) NodeSubject.assertNode(com.google.javascript.rhino.testing.NodeSubject.assertNode) JSTypeExpression(com.google.javascript.rhino.JSTypeExpression) JSDocInfo(com.google.javascript.rhino.JSDocInfo) Name(com.google.javascript.jscomp.GlobalNamespace.Name) Test(org.junit.Test)

Example 29 with Name

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

the class GlobalNamespaceTest method googModuleLevelNamesAreCaptured.

@Test
public void googModuleLevelNamesAreCaptured() {
    GlobalNamespace namespace = parseAndGatherModuleData("goog.module('m'); const x = 0;");
    ModuleMetadata metadata = lastCompiler.getModuleMetadataMap().getModulesByGoogNamespace().get("m");
    Name x = namespace.getNameFromModule(metadata, "x");
    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 30 with Name

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

the class GlobalNamespaceTest method logicalAssignment1.

@Test
public void logicalAssignment1() {
    GlobalNamespace namespace = parse("var a = a ||= {};");
    Name a = namespace.getSlot("a");
    assertThat(a).isNotNull();
    assertThat(a.getRefs()).hasSize(2);
    assertThat(a.getLocalSets()).isEqualTo(0);
    assertThat(a.getGlobalSets()).isEqualTo(2);
}
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