本文整理汇总了Java中com.google.javascript.jscomp.GlobalNamespace.Ref类的典型用法代码示例。如果您正苦于以下问题:Java Ref类的具体用法?Java Ref怎么用?Java Ref使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
Ref类属于com.google.javascript.jscomp.GlobalNamespace包,在下文中一共展示了Ref类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: CollectDefines
import com.google.javascript.jscomp.GlobalNamespace.Ref; //导入依赖的package包/类
CollectDefines(AbstractCompiler compiler, List<Name> listOfDefines) {
this.compiler = compiler;
this.allDefines = Maps.newHashMap();
assignableDefines = Maps.newHashMap();
assignAllowed = new ArrayDeque<Integer>();
assignAllowed.push(1);
// Create a map of references to defines keyed by node for easy lookup
allRefInfo = Maps.newHashMap();
for (Name name : listOfDefines) {
if (name.declaration != null) {
allRefInfo.put(name.declaration.node,
new RefInfo(name.declaration, name));
}
if (name.refs != null) {
for (Ref ref : name.refs) {
// If there's a TWIN def, only put one of the twins in.
if (ref.getTwin() == null || !ref.getTwin().isSet()) {
allRefInfo.put(ref.node, new RefInfo(ref, name));
}
}
}
}
}
示例2: checkNamespaces
import com.google.javascript.jscomp.GlobalNamespace.Ref; //导入依赖的package包/类
/**
* Runs through all namespaces (prefixes of classes and enums), and checks if
* any of them have been used in an unsafe way.
*/
private void checkNamespaces() {
for (Name name : nameMap.values()) {
if (name.isNamespace() && name.refs != null &&
(name.aliasingGets > 0 || name.localSets + name.globalSets > 1)) {
boolean initialized = name.declaration != null;
for (Ref ref : name.refs) {
if (ref.type == Ref.Type.SET_FROM_GLOBAL ||
ref.type == Ref.Type.SET_FROM_LOCAL) {
if (initialized) {
warnAboutNamespaceRedefinition(name, ref);
}
initialized = true;
} else if (ref.type == Ref.Type.ALIASING_GET) {
warnAboutNamespaceAliasing(name, ref);
}
}
}
}
}
示例3: validateName
import com.google.javascript.jscomp.GlobalNamespace.Ref; //导入依赖的package包/类
private void validateName(Name name, boolean isDefined) {
// If the name is not defined, emit warnings for each reference. While
// we're looking through each reference, check all the module dependencies.
Ref declaration = name.declaration;
if (!isDefined) {
if (declaration != null) {
reportRefToUndefinedName(name, declaration);
}
}
if (name.refs != null) {
JSModuleGraph moduleGraph = compiler.getModuleGraph();
for (Ref ref : name.refs) {
if (!isDefined) {
reportRefToUndefinedName(name, ref);
} else {
if (declaration != null &&
ref.module != declaration.module &&
!moduleGraph.dependsOn(ref.module, declaration.module)) {
reportBadModuleReference(name, ref);
}
}
}
}
}
示例4: testRemoveDeclaration1
import com.google.javascript.jscomp.GlobalNamespace.Ref; //导入依赖的package包/类
public void testRemoveDeclaration1() {
Name n = new Name("a", null, false);
Ref set1 = createNodelessRef(Ref.Type.SET_FROM_GLOBAL);
Ref set2 = createNodelessRef(Ref.Type.SET_FROM_GLOBAL);
n.addRef(set1);
n.addRef(set2);
assertEquals(set1, n.declaration);
assertEquals(2, n.globalSets);
assertEquals(1, n.refs.size());
n.removeRef(set1);
assertEquals(set2, n.declaration);
assertEquals(1, n.globalSets);
assertEquals(0, n.refs.size());
}
示例5: testRemoveDeclaration2
import com.google.javascript.jscomp.GlobalNamespace.Ref; //导入依赖的package包/类
public void testRemoveDeclaration2() {
Name n = new Name("a", null, false);
Ref set1 = createNodelessRef(Ref.Type.SET_FROM_GLOBAL);
Ref set2 = createNodelessRef(Ref.Type.SET_FROM_LOCAL);
n.addRef(set1);
n.addRef(set2);
assertEquals(set1, n.declaration);
assertEquals(1, n.globalSets);
assertEquals(1, n.localSets);
assertEquals(1, n.refs.size());
n.removeRef(set1);
assertEquals(null, n.declaration);
assertEquals(0, n.globalSets);
}
示例6: testRemoveDeclaration1
import com.google.javascript.jscomp.GlobalNamespace.Ref; //导入依赖的package包/类
public void testRemoveDeclaration1() {
Name n = new Name("a", null, false);
Ref set1 = createNodelessRef(Ref.Type.SET_FROM_GLOBAL);
Ref set2 = createNodelessRef(Ref.Type.SET_FROM_GLOBAL);
n.addRef(set1);
n.addRef(set2);
assertEquals(set1, n.getDeclaration());
assertEquals(2, n.globalSets);
assertEquals(2, n.getRefs().size());
n.removeRef(set1);
assertEquals(set2, n.getDeclaration());
assertEquals(1, n.globalSets);
assertEquals(1, n.getRefs().size());
}
示例7: testRemoveDeclaration2
import com.google.javascript.jscomp.GlobalNamespace.Ref; //导入依赖的package包/类
public void testRemoveDeclaration2() {
Name n = new Name("a", null, false);
Ref set1 = createNodelessRef(Ref.Type.SET_FROM_GLOBAL);
Ref set2 = createNodelessRef(Ref.Type.SET_FROM_LOCAL);
n.addRef(set1);
n.addRef(set2);
assertEquals(set1, n.getDeclaration());
assertEquals(1, n.globalSets);
assertEquals(1, n.localSets);
assertEquals(2, n.getRefs().size());
n.removeRef(set1);
assertEquals(null, n.getDeclaration());
assertEquals(0, n.globalSets);
}
示例8: mayBeGlobalAlias
import com.google.javascript.jscomp.GlobalNamespace.Ref; //导入依赖的package包/类
/**
* Returns true if the alias is possibly defined in the global scope, which we handle with more
* caution than with locally scoped variables. May return false positives.
*
* @param alias An aliasing get.
* @return If the alias is possibly defined in the global scope.
*/
private boolean mayBeGlobalAlias(Ref alias) {
// Note: alias.scope is the closest scope in which the aliasing assignment occurred.
// So for "if (true) { var alias = aliasedVar; }", the alias.scope would be the IF block scope.
if (alias.scope.isGlobal()) {
return true;
}
// If the scope in which the alias is assigned is not global, look up the LHS of the assignment.
Node aliasParent = alias.node.getParent();
if (!aliasParent.isAssign() && !aliasParent.isName()) {
// Only handle variable assignments and initializing declarations.
return true;
}
Node aliasLhsNode = aliasParent.isName() ? aliasParent : aliasParent.getFirstChild();
if (!aliasLhsNode.isName()) {
// Only handle assignments to simple names, not qualified names or GETPROPs.
return true;
}
String aliasVarName = aliasLhsNode.getString();
Var aliasVar = alias.scope.getVar(aliasVarName);
if (aliasVar != null) {
return aliasVar.isGlobal();
}
return true;
}
示例9: testRemoveDeclaration1
import com.google.javascript.jscomp.GlobalNamespace.Ref; //导入依赖的package包/类
public void testRemoveDeclaration1() {
Name n = new Name("a", null, false);
Ref set1 = createNodelessRef(Ref.Type.SET_FROM_GLOBAL);
Ref set2 = createNodelessRef(Ref.Type.SET_FROM_GLOBAL);
n.addRef(set1);
n.addRef(set2);
assertEquals(set1, n.getDeclaration());
assertEquals(2, n.globalSets);
assertThat(n.getRefs()).hasSize(2);
n.removeRef(set1);
assertEquals(set2, n.getDeclaration());
assertEquals(1, n.globalSets);
assertThat(n.getRefs()).hasSize(1);
}
示例10: testRemoveDeclaration2
import com.google.javascript.jscomp.GlobalNamespace.Ref; //导入依赖的package包/类
public void testRemoveDeclaration2() {
Name n = new Name("a", null, false);
Ref set1 = createNodelessRef(Ref.Type.SET_FROM_GLOBAL);
Ref set2 = createNodelessRef(Ref.Type.SET_FROM_LOCAL);
n.addRef(set1);
n.addRef(set2);
assertEquals(set1, n.getDeclaration());
assertEquals(1, n.globalSets);
assertEquals(1, n.localSets);
assertThat(n.getRefs()).hasSize(2);
n.removeRef(set1);
assertNull(n.getDeclaration());
assertEquals(0, n.globalSets);
}
示例11: collectDefines
import com.google.javascript.jscomp.GlobalNamespace.Ref; //导入依赖的package包/类
/**
* Finds all defines, and creates a {@link DefineInfo} data structure for
* each one.
* @return A map of {@link DefineInfo} structures, keyed by name.
*/
private Map<String, DefineInfo> collectDefines(Node root,
GlobalNamespace namespace) {
// Find all the global names with a @define annotation
List<Name> allDefines = Lists.newArrayList();
for (Name name : namespace.getNameIndex().values()) {
if (name.docInfo != null && name.docInfo.isDefine()) {
allDefines.add(name);
} else if (name.refs != null) {
for (Ref ref : name.refs) {
Node n = ref.node;
Node parent = ref.node.getParent();
JSDocInfo info = n.getJSDocInfo();
if (info == null &&
parent.getType() == Token.VAR && parent.hasOneChild()) {
info = parent.getJSDocInfo();
}
if (info != null && info.isDefine()) {
allDefines.add(name);
break;
}
}
}
}
CollectDefines pass = new CollectDefines(compiler, allDefines);
NodeTraversal.traverse(compiler, root, pass);
return pass.getAllDefines();
}
示例12: getValueParent
import com.google.javascript.jscomp.GlobalNamespace.Ref; //导入依赖的package包/类
/**
* Gets the parent node of the value for any assignment to a Name.
* For example, in the assignment
* {@code var x = 3;}
* the parent would be the NAME node.
*/
private static Node getValueParent(Ref ref) {
// there are two types of declarations: VARs and ASSIGNs
return ref.node.getParent() != null &&
ref.node.getParent().getType() == Token.VAR ?
ref.node : ref.node.getParent();
}
示例13: inlineAliases
import com.google.javascript.jscomp.GlobalNamespace.Ref; //导入依赖的package包/类
/**
* For each qualified name N in the global scope, we check if:
* (a) No ancestor of N is ever aliased or assigned an unknown value type.
* (If N = "a.b.c", "a" and "a.b" are never aliased).
* (b) N has exactly one write, and it lives in the global scope.
* (c) N is aliased in a local scope.
*
* If (a) is true, then GlobalNamespace must know all the writes to N.
* If (a) and (b) are true, then N cannot change during the execution of
* a local scope.
* If (a) and (b) and (c) are true, then the alias can be inlined if the
* alias obeys the usual rules for how we decide whether a variable is
* inlineable.
* @see InlineVariables
*/
private void inlineAliases(GlobalNamespace namespace) {
// Invariant: All the names in the worklist meet condition (a).
Deque<Name> workList = new ArrayDeque<Name>(namespace.getNameForest());
while (!workList.isEmpty()) {
Name name = workList.pop();
if (name.globalSets == 1 && name.localSets == 0 &&
name.aliasingGets > 0) {
// {@code name} meets condition (b). Find all of its local aliases
// and try to inline them.
List<Ref> refs = Lists.newArrayList(name.refs);
for (Ref ref : refs) {
if (ref.type == Type.ALIASING_GET && ref.scope.isLocal()) {
// {@code name} meets condition (c). Try to inline it.
if (inlineAliasIfPossible(ref, namespace)) {
name.removeRef(ref);
}
}
}
}
// Check if {@code name} has any aliases left after the
// local-alias-inlining above.
if ((name.type == Name.Type.OBJECTLIT ||
name.type == Name.Type.FUNCTION) &&
name.aliasingGets == 0 && name.props != null) {
// All of {@code name}'s children meet condition (a), so they can be
// added to the worklist.
workList.addAll(name.props);
}
}
}
示例14: flattenReferencesTo
import com.google.javascript.jscomp.GlobalNamespace.Ref; //导入依赖的package包/类
/**
* Flattens all references to a collapsible property of a global name except
* its initial definition.
*
* @param n A global property name (e.g. "a.b" or "a.b.c.d")
* @param alias The flattened name (e.g. "a$b" or "a$b$c$d")
*/
private void flattenReferencesTo(Name n, String alias) {
if (n.refs != null) {
String originalName = n.fullName();
for (Ref r : n.refs) {
Node rParent = r.node.getParent();
// There are two cases when we shouldn't flatten a reference:
// 1) Object literal keys, because duplicate keys show up as refs.
// 2) References inside a complex assign. (a = x.y = 0). These are
// called TWIN references, because they show up twice in the
// reference list. Only collapse the set, not the alias.
if (!NodeUtil.isObjectLitKey(r.node, rParent) &&
(r.getTwin() == null || r.isSet())) {
flattenNameRef(alias, r.node, rParent, originalName);
}
}
}
// Flatten all occurrences of a name as a prefix of its subnames. For
// example, if {@code n} corresponds to the name "a.b", then "a.b" will be
// replaced with "a$b" in all occurrences of "a.b.c", "a.b.c.d", etc.
if (n.props != null) {
for (Name p : n.props) {
flattenPrefixes(alias, p, 1);
}
}
}
示例15: flattenPrefixes
import com.google.javascript.jscomp.GlobalNamespace.Ref; //导入依赖的package包/类
/**
* Flattens all occurrences of a name as a prefix of subnames beginning
* with a particular subname.
*
* @param n A global property name (e.g. "a.b.c.d")
* @param alias A flattened prefix name (e.g. "a$b")
* @param depth The difference in depth between the property name and
* the prefix name (e.g. 2)
*/
private void flattenPrefixes(String alias, Name n, int depth) {
// Only flatten the prefix of a name declaration if the name being
// initialized is fully qualified (i.e. not an object literal key).
String originalName = n.fullName();
if (n.declaration != null && n.declaration.node != null &&
n.declaration.node.getType() == Token.GETPROP) {
flattenNameRefAtDepth(alias, n.declaration.node, depth, originalName);
}
if (n.refs != null) {
for (Ref r : n.refs) {
// References inside a complex assign (a = x.y = 0)
// have twins. We should only flatten one of the twins.
if (r.getTwin() == null || r.isSet()) {
flattenNameRefAtDepth(alias, r.node, depth, originalName);
}
}
}
if (n.props != null) {
for (Name p : n.props) {
flattenPrefixes(alias, p, depth + 1);
}
}
}