Search in sources :

Example 46 with Name

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

the class CheckGlobalNames method process.

@Override
public void process(Node externs, Node root) {
    if (namespace == null) {
        namespace = new GlobalNamespace(compiler, externs, root);
    }
    // Find prototype properties that will affect our analysis.
    checkState(namespace.hasExternsRoot());
    findPrototypeProps("Object", objectPrototypeProps);
    findPrototypeProps("Function", functionPrototypeProps);
    objectPrototypeProps.addAll(convention.getIndirectlyDeclaredProperties());
    for (Name name : namespace.getNameForest()) {
        // which this check forbids.
        if (name.inExterns) {
            continue;
        }
        checkDescendantNames(name, name.globalSets + name.localSets > 0);
    }
}
Also used : Name(com.google.javascript.jscomp.GlobalNamespace.Name)

Example 47 with Name

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

the class ProcessDefinesTest method testReassignAfterNonGlobalRef.

public void testReassignAfterNonGlobalRef() {
    test(lines("/** @fileoverview @suppress {newCheckTypes} */", "/** @define {boolean} */ var DEF=true;", "var x=function(){var y=DEF};", "DEF=false"), lines("/** @fileoverview @suppress {newCheckTypes} */", "/** @define {boolean} */ var DEF = false;", "var x = function(){var y = DEF; };", "false"));
    Name def = namespace.getNameIndex().get("DEF");
    assertThat(def.getRefs()).hasSize(2);
    assertEquals(1, def.globalSets);
    assertNotNull(def.getDeclaration());
}
Also used : Name(com.google.javascript.jscomp.GlobalNamespace.Name)

Example 48 with Name

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

the class ProcessDefinesTest method testSimpleReassign2.

public void testSimpleReassign2() {
    test(lines("/** @fileoverview @suppress {newCheckTypes} */", "/** @define {number|boolean} */ var DEF=false;", "DEF=true;", "DEF=3"), lines("/** @fileoverview @suppress {newCheckTypes} */", "/** @define {number|boolean} */ var DEF=3;", "true;3"));
    Name def = namespace.getNameIndex().get("DEF");
    assertThat(def.getRefs()).hasSize(1);
    assertEquals(1, def.globalSets);
    assertNotNull(def.getDeclaration());
}
Also used : Name(com.google.javascript.jscomp.GlobalNamespace.Name)

Example 49 with Name

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

the class ProcessDefinesTest method testNamespacedDefine1.

public void testNamespacedDefine1() {
    test("var a = {}; /** @define {boolean} */ a.B = false; a.B = true;", "var a = {}; /** @define {boolean} */ a.B = true; true;");
    Name aDotB = namespace.getNameIndex().get("a.B");
    assertThat(aDotB.getRefs()).hasSize(1);
    assertEquals(1, aDotB.globalSets);
    assertNotNull(aDotB.getDeclaration());
}
Also used : Name(com.google.javascript.jscomp.GlobalNamespace.Name)

Example 50 with Name

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

the class AggressiveInlineAliases 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 GlobalNamespace.Name#canCollapse} for what can/cannot be collapsed.
 */
private boolean referencesCollapsibleProperty(ReferenceCollection aliasRefs, Name aliasedName, GlobalNamespace namespace) {
    for (Reference ref : aliasRefs.references) {
        if (ref.getParent() == null) {
            continue;
        }
        if (ref.getParent().isGetProp()) {
            Node propertyNode = ref.getNode().getNext();
            // e.g. if the reference is "alias.b.someProp", this will be "b".
            String propertyName = propertyNode.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 : Node(com.google.javascript.rhino.Node) Name(com.google.javascript.jscomp.GlobalNamespace.Name)

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