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

The following examples show how to use com.google.javascript.rhino.Node#getLineno() . 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: Closure_34_CodePrinter_t.java    From coming with MIT License 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 2
Source File: Closure_79_Normalize_t.java    From coming with MIT License 6 votes vote down vote up
/**
 * Rewrite the function declaration from:
 *   function x() {}
 *   FUNCTION
 *     NAME
 *     LP
 *     BLOCK
 * to:
 *   var x = function() {};
 *   VAR
 *     NAME
 *       FUNCTION
 *         NAME (w/ empty string)
 *         LP
 *         BLOCK
 */
private void rewriteFunctionDeclaration(Node n) {
  // Prepare a spot for the function.
  Node oldNameNode = n.getFirstChild();
  Node fnNameNode = oldNameNode.cloneNode();
  Node var = new Node(Token.VAR, fnNameNode, n.getLineno(), n.getCharno());
  var.copyInformationFrom(n);

  // Prepare the function
  oldNameNode.setString("");

  // Move the function
  Node parent = n.getParent();
  parent.replaceChild(n, var);
  fnNameNode.addChildToFront(n);

  reportCodeChange("Function declaration");
}
 
Example 3
Source File: Closure_84_IRFactory_s.java    From coming with MIT License 6 votes vote down vote up
private Node transform(AstNode node) {
  JSDocInfo jsDocInfo = handleJsDoc(node);
  Node irNode = justTransform(node);
  if (jsDocInfo != null) {
    irNode.setJSDocInfo(jsDocInfo);
  }

  // If we have a named function, set the position to that of the name.
  if (irNode.getType() == Token.FUNCTION &&
      irNode.getFirstChild().getLineno() != -1) {
    irNode.setLineno(irNode.getFirstChild().getLineno());
    irNode.setCharno(irNode.getFirstChild().getCharno());
  } else {
    if (irNode.getLineno() == -1) {
      // If we didn't already set the line, then set it now.  This avoids
      // cases like ParenthesizedExpression where we just return a previous
      // node, but don't want the new node to get its parent's line number.
      int lineno = node.getLineno();
      irNode.setLineno(lineno);
      int charno = position2charno(node.getAbsolutePosition());
      irNode.setCharno(charno);
    }
  }
  return irNode;
}
 
Example 4
Source File: Closure_79_Normalize_s.java    From coming with MIT License 6 votes vote down vote up
/**
 * Split a var node such as:
 *   var a, b;
 * into individual statements:
 *   var a;
 *   var b;
 * @param n The whose children we should inspect.
 */
private void splitVarDeclarations(Node n) {
  for (Node next, c = n.getFirstChild(); c != null; c = next) {
    next = c.getNext();
    if (c.getType() == Token.VAR) {
      if (assertOnChange && !c.hasChildren()) {
        throw new IllegalStateException("Empty VAR node.");
      }

      while (c.getFirstChild() != c.getLastChild()) {
        Node name = c.getFirstChild();
        c.removeChild(name);
        Node newVar = new Node(
            Token.VAR, name, n.getLineno(), n.getCharno());
        n.addChildBefore(newVar, c);
        reportCodeChange("VAR with multiple children");
      }
    }
  }
}
 
Example 5
Source File: Closure_108_ScopedAliases_t.java    From coming with MIT License 6 votes vote down vote up
private SourcePosition<AliasTransformation> getSourceRegion(Node n) {
  Node testNode = n;
  Node next = null;
  for (; next != null || testNode.isScript();) {
    next = testNode.getNext();
    testNode = testNode.getParent();
  }

  int endLine = next == null ? Integer.MAX_VALUE : next.getLineno();
  int endChar = next == null ? Integer.MAX_VALUE : next.getCharno();
  SourcePosition<AliasTransformation> pos =
      new SourcePosition<AliasTransformation>() {};
  pos.setPositionInformation(
      n.getLineno(), n.getCharno(), endLine, endChar);
  return pos;
}
 
Example 6
Source File: Closure_108_ScopedAliases_s.java    From coming with MIT License 6 votes vote down vote up
private SourcePosition<AliasTransformation> getSourceRegion(Node n) {
  Node testNode = n;
  Node next = null;
  for (; next != null || testNode.isScript();) {
    next = testNode.getNext();
    testNode = testNode.getParent();
  }

  int endLine = next == null ? Integer.MAX_VALUE : next.getLineno();
  int endChar = next == null ? Integer.MAX_VALUE : next.getCharno();
  SourcePosition<AliasTransformation> pos =
      new SourcePosition<AliasTransformation>() {};
  pos.setPositionInformation(
      n.getLineno(), n.getCharno(), endLine, endChar);
  return pos;
}
 
Example 7
Source File: Closure_79_Normalize_s.java    From coming with MIT License 6 votes vote down vote up
/**
 * Rewrite the function declaration from:
 *   function x() {}
 *   FUNCTION
 *     NAME
 *     LP
 *     BLOCK
 * to:
 *   var x = function() {};
 *   VAR
 *     NAME
 *       FUNCTION
 *         NAME (w/ empty string)
 *         LP
 *         BLOCK
 */
private void rewriteFunctionDeclaration(Node n) {
  // Prepare a spot for the function.
  Node oldNameNode = n.getFirstChild();
  Node fnNameNode = oldNameNode.cloneNode();
  Node var = new Node(Token.VAR, fnNameNode, n.getLineno(), n.getCharno());
  var.copyInformationFrom(n);

  // Prepare the function
  oldNameNode.setString("");

  // Move the function
  Node parent = n.getParent();
  parent.replaceChild(n, var);
  fnNameNode.addChildToFront(n);

  reportCodeChange("Function declaration");
}
 
Example 8
Source File: Closure_24_ScopedAliases_s.java    From coming with MIT License 6 votes vote down vote up
private SourcePosition<AliasTransformation> getSourceRegion(Node n) {
  Node testNode = n;
  Node next = null;
  for (; next != null || testNode.isScript();) {
    next = testNode.getNext();
    testNode = testNode.getParent();
  }

  int endLine = next == null ? Integer.MAX_VALUE : next.getLineno();
  int endChar = next == null ? Integer.MAX_VALUE : next.getCharno();
  SourcePosition<AliasTransformation> pos =
      new SourcePosition<AliasTransformation>() {};
  pos.setPositionInformation(
      n.getLineno(), n.getCharno(), endLine, endChar);
  return pos;
}
 
Example 9
Source File: Closure_96_TypeCheck_t.java    From coming with MIT License 6 votes vote down vote up
/**
 * Visits a NEW node.
 */
private void visitNew(NodeTraversal t, Node n) {
  Node constructor = n.getFirstChild();
  FunctionType type = getFunctionType(constructor);
  if (type != null && type.isConstructor()) {
    visitParameterList(t, n, type);
    ensureTyped(t, n, type.getInstanceType());
  } else {
    // TODO(user): add support for namespaced objects.
    if (constructor.getType() != Token.GETPROP) {
      // TODO(user): make the constructor node have lineno/charno
      // and use constructor for a more precise error indication.
      // It seems that GETPROP nodes are missing this information.
      Node line;
      if (constructor.getLineno() < 0 || constructor.getCharno() < 0) {
        line = n;
      } else {
        line = constructor;
      }
      report(t, line, NOT_A_CONSTRUCTOR);
    }
    ensureTyped(t, n);
  }
}
 
Example 10
Source File: Nopol2017_0051_t.java    From coming with MIT License 6 votes vote down vote up
/**
 * Visits a NEW node.
 */
private void visitNew(NodeTraversal t, Node n) {
  Node constructor = n.getFirstChild();
  FunctionType type = getFunctionType(constructor);
  if (type != null && type.isConstructor()) {
    visitParameterList(t, n, type);
    ensureTyped(t, n, type.getInstanceType());
  } else {
    // TODO(user): add support for namespaced objects.
    if (constructor.getType() != Token.GETPROP) {
      // TODO(user): make the constructor node have lineno/charno
      // and use constructor for a more precise error indication.
      // It seems that GETPROP nodes are missing this information.
      Node line;
      if (constructor.getLineno() < 0 || constructor.getCharno() < 0) {
        line = n;
      } else {
        line = constructor;
      }
      report(t, line, NOT_A_CONSTRUCTOR);
    }
    ensureTyped(t, n);
  }
}
 
Example 11
Source File: NodeTraversal.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
private String formatNodePosition(Node n) {
  String sourceFileName = getBestSourceFileName(n);
  if (sourceFileName == null) {
    return MISSING_SOURCE + "\n";
  }

  int lineNumber = n.getLineno();
  int columnNumber = n.getCharno();
  String src = compiler.getSourceLine(sourceFileName, lineNumber);
  if (src == null) {
    src = MISSING_SOURCE;
  }
  return sourceFileName + ":" + lineNumber + ":" + columnNumber + "\n"
      + src + "\n";
}
 
Example 12
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 13
Source File: Closure_47_SourceMap_t.java    From coming with MIT License 5 votes vote down vote up
public void addMapping(
    Node node,
    FilePosition outputStartPosition,
    FilePosition outputEndPosition) {
  String sourceFile = node.getSourceFileName();

  // If the node does not have an associated source file or
  // its line number is -1, then the node does not have sufficient
  // information for a mapping to be useful.
  if (sourceFile == null || node.getLineno() < 0) {
    return;
  }

  sourceFile = fixupSourceLocation(sourceFile);

  String originalName = (String) node.getProp(Node.ORIGINALNAME_PROP);

  // Strangely, Rhino source lines are one based but columns are
  // zero based.
  // We don't change this for the v1 or v2 source maps but for
  // v3 we make them both 0 based.
  int lineBaseOffset = 1;
  if (generator instanceof SourceMapGeneratorV1
      || generator instanceof SourceMapGeneratorV2) {
    lineBaseOffset = 0;
  }

  generator.addMapping(
      sourceFile, originalName,
      new FilePosition(node.getLineno() - lineBaseOffset, node.getCharno()),
      outputStartPosition, outputEndPosition);
}
 
Example 14
Source File: LineNumberCheck.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
@Override
public void visit(NodeTraversal t, Node n, Node parent) {
  if (n.isScript()) {
    requiresLineNumbers = false;
  } else if (requiresLineNumbers) {
    if (n.getLineno() == -1) {
      // The tree version of the node is really the best diagnostic
      // info we have to offer here.
      compiler.report(
          t.makeError(n, MISSING_LINE_INFO,
              n.toStringTree()));
    }
  }
}
 
Example 15
Source File: Closure_122_IRFactory_t.java    From coming with MIT License 5 votes vote down vote up
private void setSourceInfo(Node irNode, AstNode node) {
  if (irNode.getLineno() == -1) {
    // If we didn't already set the line, then set it now. This avoids
    // cases like ParenthesizedExpression where we just return a previous
    // node, but don't want the new node to get its parent's line number.
    int lineno = node.getLineno();
    irNode.setLineno(lineno);
    int charno = position2charno(node.getAbsolutePosition());
    irNode.setCharno(charno);
    maybeSetLengthFrom(irNode, node);
  }
}
 
Example 16
Source File: Closure_122_IRFactory_s.java    From coming with MIT License 5 votes vote down vote up
private void setSourceInfo(Node irNode, AstNode node) {
  if (irNode.getLineno() == -1) {
    // If we didn't already set the line, then set it now. This avoids
    // cases like ParenthesizedExpression where we just return a previous
    // node, but don't want the new node to get its parent's line number.
    int lineno = node.getLineno();
    irNode.setLineno(lineno);
    int charno = position2charno(node.getAbsolutePosition());
    irNode.setCharno(charno);
    maybeSetLengthFrom(irNode, node);
  }
}
 
Example 17
Source File: JSError.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Creates a JSError for a source file location.  Private to avoid
 * any entanglement with code outside of the compiler.
 */
private JSError(String sourceName, @Nullable Node node,
                DiagnosticType type, String... arguments) {
  this(sourceName,
       node,
       (node != null) ? node.getLineno() : -1,
       (node != null) ? node.getCharno() : -1,
       type, null, arguments);
}
 
Example 18
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 19
Source File: SourceMap.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
public void addMapping(
    Node node,
    FilePosition outputStartPosition,
    FilePosition outputEndPosition) {
  String sourceFile = node.getSourceFileName();

  // If the node does not have an associated source file or
  // its line number is -1, then the node does not have sufficient
  // information for a mapping to be useful.
  if (sourceFile == null || node.getLineno() < 0) {
    return;
  }

  sourceFile = fixupSourceLocation(sourceFile);

  String originalName = (String) node.getProp(Node.ORIGINALNAME_PROP);

  // Strangely, Rhino source lines are one based but columns are
  // zero based.
  // We don't change this for the v1 or v2 source maps but for
  // v3 we make them both 0 based.
  int lineBaseOffset = 1;
  if (generator instanceof SourceMapGeneratorV1
      || generator instanceof SourceMapGeneratorV2) {
    lineBaseOffset = 0;
  }

  generator.addMapping(
      sourceFile, originalName,
      new FilePosition(node.getLineno() - lineBaseOffset, node.getCharno()),
      outputStartPosition, outputEndPosition);
}
 
Example 20
Source File: Closure_37_NodeTraversal_s.java    From coming with MIT License 5 votes vote down vote up
/**
 * Gets the current line number, or zero if it cannot be determined. The line
 * number is retrieved lazily as a running time optimization.
 */
public int getLineNumber() {
  Node cur = curNode;
  while (cur != null) {
    int line = cur.getLineno();
    if (line >=0) {
      return line;
    }
    cur = cur.getParent();
  }
  return 0;
}