Java Code Examples for com.google.javascript.rhino.Node#getSourceFileName()

The following examples show how to use com.google.javascript.rhino.Node#getSourceFileName() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: CleanupPasses.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
@Override
public void hotSwapScript(Node scriptRoot, Node originalRoot) {
  ScopeCreator creator = compiler.getTypedScopeCreator();
  if (creator instanceof MemoizedScopeCreator) {
    MemoizedScopeCreator scopeCreator = (MemoizedScopeCreator) creator;
    String newSrc = scriptRoot.getSourceFileName();
    for (Var var : scopeCreator.getAllSymbols()) {
      JSType type = var.getType();
      if (type != null) {
        FunctionType fnType = type.toMaybeFunctionType();
        if (fnType != null
            && newSrc.equals(NodeUtil.getSourceName(fnType.getSource()))) {
          fnType.setSource(null);
        }
      }
    }
    scopeCreator.removeScopesForScript(originalRoot.getSourceFileName());
  }
}
 
Example 2
Source File: NodeModulePass.java    From js-dossier with Apache License 2.0 6 votes vote down vote up
@Override
public boolean shouldTraverse(NodeTraversal t, Node n, Node parent) {
  if (n.isScript()) {
    checkState(currentModule == null);

    String sourceName = n.getSourceFileName();
    Path path = inputFs.getPath(n.getSourceFileName());
    if (!nodeLibrary.isModulePath(sourceName) && !modulePaths.contains(path)) {
      return false;
    }

    if (nodeLibrary.isModulePath(sourceName)) {
      currentModule = nodeLibrary.getIdFromPath(sourceName);
    } else {
      Module module = globalSymbolTable.getModule(path);
      checkNotNull(module, "module not found: %s", path);
      if (module.isEs6()) {
        return false;
      }
      currentModule = module.getId().getOriginalName();
    }

    traverse(t.getCompiler(), n, new SplitRequireDeclarations());
  }
  return true;
}
 
Example 3
Source File: CodePrinter.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Starts the source mapping for the given
 * node at the current position.
 */
@Override
void startSourceMapping(Node node) {
  Preconditions.checkState(sourceMapDetailLevel != null);
  Preconditions.checkState(node != null);
  if (createSrcMap
      && node.getSourceFileName() != null
      && node.getLineno() > 0
      && sourceMapDetailLevel.apply(node)) {
    int line = getCurrentLineIndex();
    int index = getCurrentCharIndex();
    Preconditions.checkState(line >= 0);
    Mapping mapping = new Mapping();
    mapping.node = node;
    mapping.start = new FilePosition(line, index);
    mappings.push(mapping);
    allMappings.add(mapping);
  }
}
 
Example 4
Source File: SymbolTable.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
@Override
public String toString() {
  Node n = getRootNode();
  if (n != null) {
    return "Scope@" + n.getSourceFileName() + ":" + n.getLineno();
  } else {
    return "PropertyScope@" + getSymbolForScope();
  }
}
 
Example 5
Source File: ReplaceIdGenerators.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
String getIdForGeneratorNode(boolean consistent, Node n) {
  Preconditions.checkState(n.isString());
  if (consistent) {
    return n.getString();
  } else {
    return n.getSourceFileName() + ':' + n.getLineno() + ":" + n.getCharno();
  }
}
 
Example 6
Source File: Closure_10_NodeUtil_t.java    From coming with MIT License 5 votes vote down vote up
/**
 * @param n The node.
 * @return The source name property on the node or its ancestors.
 */
public static String getSourceName(Node n) {
  String sourceName = null;
  while (sourceName == null && n != null) {
    sourceName = n.getSourceFileName();
    n = n.getParent();
  }
  return sourceName;
}
 
Example 7
Source File: Closure_125_TypeCheck_s.java    From coming with MIT License 5 votes vote down vote up
/**
 * After a struct object is created, we can't add new properties to it, with
 * one exception. We allow creation of "static" properties like
 * Foo.prototype.bar = baz;
 * where Foo.prototype is a struct, if the assignment happens at the top level
 * and the constructor Foo is defined in the same file.
 */
private void checkPropCreation(NodeTraversal t, Node lvalue) {
  if (lvalue.isGetProp()) {
    Node obj = lvalue.getFirstChild();
    Node prop = lvalue.getLastChild();
    JSType objType = getJSType(obj);
    String pname = prop.getString();

    if (!objType.isStruct() || objType.hasProperty(pname)) {
      return;
    }
    Scope s = t.getScope();
    if (obj.isThis() && getJSType(s.getRootNode()).isConstructor()) {
      return;
    }
    // Prop created outside ctor, check that it's a static prop
    Node assgnExp = lvalue.getParent();
    Node assgnStm = assgnExp.getParent();
    if (objType instanceof ObjectType &&
        s.isGlobal() &&
        NodeUtil.isPrototypePropertyDeclaration(assgnStm)) {
      ObjectType instance =
          objType.toObjectType().getOwnerFunction().getInstanceType();
      String file = lvalue.getSourceFileName();
      Node ctor = instance.getConstructor().getSource();
      if (ctor != null && ctor.getSourceFileName().equals(file)) {
        JSType rvalueType = assgnExp.getLastChild().getJSType();
        instance.defineInferredProperty(pname, rvalueType, lvalue);
        return;
      }
    }
    report(t, prop, ILLEGAL_PROPERTY_CREATION);
  }
}
 
Example 8
Source File: Closure_125_TypeCheck_t.java    From coming with MIT License 5 votes vote down vote up
/**
 * After a struct object is created, we can't add new properties to it, with
 * one exception. We allow creation of "static" properties like
 * Foo.prototype.bar = baz;
 * where Foo.prototype is a struct, if the assignment happens at the top level
 * and the constructor Foo is defined in the same file.
 */
private void checkPropCreation(NodeTraversal t, Node lvalue) {
  if (lvalue.isGetProp()) {
    Node obj = lvalue.getFirstChild();
    Node prop = lvalue.getLastChild();
    JSType objType = getJSType(obj);
    String pname = prop.getString();

    if (!objType.isStruct() || objType.hasProperty(pname)) {
      return;
    }
    Scope s = t.getScope();
    if (obj.isThis() && getJSType(s.getRootNode()).isConstructor()) {
      return;
    }
    // Prop created outside ctor, check that it's a static prop
    Node assgnExp = lvalue.getParent();
    Node assgnStm = assgnExp.getParent();
    if (objType instanceof ObjectType &&
        s.isGlobal() &&
        NodeUtil.isPrototypePropertyDeclaration(assgnStm)) {
      ObjectType instance =
          objType.toObjectType().getOwnerFunction().getInstanceType();
      String file = lvalue.getSourceFileName();
      Node ctor = instance.getConstructor().getSource();
      if (ctor != null && ctor.getSourceFileName().equals(file)) {
        JSType rvalueType = assgnExp.getLastChild().getJSType();
        instance.defineInferredProperty(pname, rvalueType, lvalue);
        return;
      }
    }
    report(t, prop, ILLEGAL_PROPERTY_CREATION);
  }
}
 
Example 9
Source File: SourceExtractor.java    From clutz with MIT License 5 votes vote down vote up
/**
 * Gets the source file name for the given node or <code>null</code> if the node doesn't have a
 * source filename.
 *
 * @param node The node to analyze. This value can be <code>null</code>.
 * @return The node's source filename or <code>null</code> the source filename could not be
 *     determined or if <code>node</code> is <code>null</code>.
 */
@Nullable
private static String getSourceFileName(Node node) {
  if (node == null) {
    return null;
  }

  String name = node.getSourceFileName();
  if (name != null) {
    return name;
  }

  return getSourceFileName(node.getParent());
}
 
Example 10
Source File: jKali_003_t.java    From coming with MIT License 5 votes vote down vote up
/**
 * @param n The node.
 * @return The source name property on the node or its ancestors.
 */
public static String getSourceName(Node n) {
  String sourceName = null;
  while (sourceName == null && n != null) {
    sourceName = n.getSourceFileName();
    n = n.getParent();
  }
  return sourceName;
}
 
Example 11
Source File: NameReferenceGraphReport.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Given a node, find the name of the containing source file.
 *
 * @param node Parse tree node whose filename is requested
 * @return String containing name of source file, or empty string if name
 *     cannot be identified.
 */
private String getSourceFile(Node node) {
  String filename = node.getSourceFileName();
  if (filename == null) {
    return "";
  }
  return filename;
}
 
Example 12
Source File: AstParallelizer.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
public static AstParallelizer createNewFileLevelAstParallelizer(Node root) {

    // Split at every node that has a file name prop.
    Predicate<Node> shouldSplit = new Predicate<Node>() {
      @Override
      public boolean apply(Node input) {
        return input.getSourceFileName() != null;
      }
    };

    // Use a string as place holder.
    Supplier<Node> placeHolders = new Supplier<Node>() {
      @Override
      public Node get() {
        return NodeUtil.newExpr(IR.string(TEMP_NAME));
      }
    };

    // Only traverse blocks.
    Predicate<Node> shouldTraverse = new Predicate<Node>() {
      @Override
      public boolean apply(Node n) {
        return n.isBlock();
      }
    };
    return new AstParallelizer(
        shouldSplit, shouldTraverse, placeHolders, root, false);
  }
 
Example 13
Source File: Cardumen_0014_s.java    From coming with MIT License 5 votes vote down vote up
/**
 * @param n The node.
 * @return The source name property on the node or its ancestors.
 */
public static String getSourceName(Node n) {
  String sourceName = null;
  while (sourceName == null && n != null) {
    sourceName = n.getSourceFileName();
    n = n.getParent();
  }
  return sourceName;
}
 
Example 14
Source File: Cardumen_0014_t.java    From coming with MIT License 5 votes vote down vote up
/**
 * @param n The node.
 * @return The source name property on the node or its ancestors.
 */
public static String getSourceName(Node n) {
  String sourceName = null;
  while (sourceName == null && n != null) {
    sourceName = n.getSourceFileName();
    n = n.getParent();
  }
  return sourceName;
}
 
Example 15
Source File: NodeUtil.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
/**
 * @param n The node.
 * @return The source name property on the node or its ancestors.
 */
public static String getSourceName(Node n) {
  String sourceName = null;
  while (sourceName == null && n != null) {
    sourceName = n.getSourceFileName();
    n = n.getParent();
  }
  return sourceName;
}
 
Example 16
Source File: Cardumen_00200_t.java    From coming with MIT License 5 votes vote down vote up
/**
 * @param n The node.
 * @return The source name property on the node or its ancestors.
 */
public static String getSourceName(Node n) {
  String sourceName = null;
  while (sourceName == null && n != null) {
    sourceName = n.getSourceFileName();
    n = n.getParent();
  }
  return sourceName;
}
 
Example 17
Source File: NodeTraversal.java    From astor with GNU General Public License v2.0 4 votes vote down vote up
private String getBestSourceFileName(Node n) {
  return n == null ? sourceName : n.getSourceFileName();
}
 
Example 18
Source File: Closure_112_TypeInference_t.java    From coming with MIT License 4 votes vote down vote up
/**
 * Defines a property if the property has not been defined yet.
 */
private void ensurePropertyDefined(Node getprop, JSType rightType) {
  String propName = getprop.getLastChild().getString();
  Node obj = getprop.getFirstChild();
  JSType nodeType = getJSType(obj);
  ObjectType objectType = ObjectType.cast(
      nodeType.restrictByNotNullOrUndefined());
  boolean propCreationInConstructor = obj.isThis() &&
      getJSType(syntacticScope.getRootNode()).isConstructor();

  if (objectType == null) {
    registry.registerPropertyOnType(propName, nodeType);
  } else {
    if (nodeType.isStruct() && !objectType.hasProperty(propName)) {
      // In general, we don't want to define a property on a struct object,
      // b/c TypeCheck will later check for improper property creation on
      // structs. There are two exceptions.
      // 1) If it's a property created inside the constructor, on the newly
      //    created instance, allow it.
      // 2) If it's a prototype property, allow it. For example:
      //    Foo.prototype.bar = baz;
      //    where Foo.prototype is a struct and the assignment happens at the
      //    top level and the constructor Foo is defined in the same file.
      boolean staticPropCreation = false;
      Node maybeAssignStm = getprop.getParent().getParent();
      if (syntacticScope.isGlobal() &&
          NodeUtil.isPrototypePropertyDeclaration(maybeAssignStm)) {
        String propCreationFilename = maybeAssignStm.getSourceFileName();
        Node ctor = objectType.getOwnerFunction().getSource();
        if (ctor != null &&
            ctor.getSourceFileName().equals(propCreationFilename)) {
          staticPropCreation = true;
        }
      }
      if (!propCreationInConstructor && !staticPropCreation) {
        return; // Early return to avoid creating the property below.
      }
    }

    if (ensurePropertyDeclaredHelper(getprop, objectType)) {
      return;
    }

    if (!objectType.isPropertyTypeDeclared(propName)) {
      // We do not want a "stray" assign to define an inferred property
      // for every object of this type in the program. So we use a heuristic
      // approach to determine whether to infer the property.
      //
      // 1) If the property is already defined, join it with the previously
      //    inferred type.
      // 2) If this isn't an instance object, define it.
      // 3) If the property of an object is being assigned in the constructor,
      //    define it.
      // 4) If this is a stub, define it.
      // 5) Otherwise, do not define the type, but declare it in the registry
      //    so that we can use it for missing property checks.
      if (objectType.hasProperty(propName) || !objectType.isInstanceType()) {
        if ("prototype".equals(propName)) {
          objectType.defineDeclaredProperty(propName, rightType, getprop);
        } else {
          objectType.defineInferredProperty(propName, rightType, getprop);
        }
      } else if (propCreationInConstructor) {
        objectType.defineInferredProperty(propName, rightType, getprop);
      } else {
        registry.registerPropertyOnType(propName, objectType);
      }
    }
  }
}
 
Example 19
Source File: SymbolTable.java    From astor with GNU General Public License v2.0 4 votes vote down vote up
public String getSourceFileName() {
  Node n = getDeclarationNode();
  return n == null ? null : n.getSourceFileName();
}
 
Example 20
Source File: Closure_112_TypeInference_s.java    From coming with MIT License 4 votes vote down vote up
/**
 * Defines a property if the property has not been defined yet.
 */
private void ensurePropertyDefined(Node getprop, JSType rightType) {
  String propName = getprop.getLastChild().getString();
  Node obj = getprop.getFirstChild();
  JSType nodeType = getJSType(obj);
  ObjectType objectType = ObjectType.cast(
      nodeType.restrictByNotNullOrUndefined());
  boolean propCreationInConstructor = obj.isThis() &&
      getJSType(syntacticScope.getRootNode()).isConstructor();

  if (objectType == null) {
    registry.registerPropertyOnType(propName, nodeType);
  } else {
    if (nodeType.isStruct() && !objectType.hasProperty(propName)) {
      // In general, we don't want to define a property on a struct object,
      // b/c TypeCheck will later check for improper property creation on
      // structs. There are two exceptions.
      // 1) If it's a property created inside the constructor, on the newly
      //    created instance, allow it.
      // 2) If it's a prototype property, allow it. For example:
      //    Foo.prototype.bar = baz;
      //    where Foo.prototype is a struct and the assignment happens at the
      //    top level and the constructor Foo is defined in the same file.
      boolean staticPropCreation = false;
      Node maybeAssignStm = getprop.getParent().getParent();
      if (syntacticScope.isGlobal() &&
          NodeUtil.isPrototypePropertyDeclaration(maybeAssignStm)) {
        String propCreationFilename = maybeAssignStm.getSourceFileName();
        Node ctor = objectType.getOwnerFunction().getSource();
        if (ctor != null &&
            ctor.getSourceFileName().equals(propCreationFilename)) {
          staticPropCreation = true;
        }
      }
      if (!propCreationInConstructor && !staticPropCreation) {
        return; // Early return to avoid creating the property below.
      }
    }

    if (ensurePropertyDeclaredHelper(getprop, objectType)) {
      return;
    }

    if (!objectType.isPropertyTypeDeclared(propName)) {
      // We do not want a "stray" assign to define an inferred property
      // for every object of this type in the program. So we use a heuristic
      // approach to determine whether to infer the property.
      //
      // 1) If the property is already defined, join it with the previously
      //    inferred type.
      // 2) If this isn't an instance object, define it.
      // 3) If the property of an object is being assigned in the constructor,
      //    define it.
      // 4) If this is a stub, define it.
      // 5) Otherwise, do not define the type, but declare it in the registry
      //    so that we can use it for missing property checks.
      if (objectType.hasProperty(propName) || !objectType.isInstanceType()) {
        if ("prototype".equals(propName)) {
          objectType.defineDeclaredProperty(propName, rightType, getprop);
        } else {
          objectType.defineInferredProperty(propName, rightType, getprop);
        }
      } else if (propCreationInConstructor) {
        objectType.defineInferredProperty(propName, rightType, getprop);
      } else {
        registry.registerPropertyOnType(propName, objectType);
      }
    }
  }
}