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

The following examples show how to use com.google.javascript.rhino.Node#getCharno() . 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: Nopol2017_0051_s.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 2
Source File: Closure_16_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 3
Source File: Closure_16_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 4
Source File: Closure_79_Normalize_t.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_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 6
Source File: Closure_96_TypeCheck_s.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 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_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 9
Source File: Closure_66_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: 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 11
Source File: Closure_102_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 12
Source File: Closure_102_Normalize_t.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 13
Source File: Closure_24_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 14
Source File: Closure_69_TypeCheck_s.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 15
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 16
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 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: Closure_37_NodeTraversal_t.java    From coming with MIT License 5 votes vote down vote up
private String formatNodePosition(Node n) {
  if (n == null) {
    return MISSING_SOURCE + "\n";
  }

  int lineNumber = n.getLineno();
  int columnNumber = n.getCharno();
  String src = compiler.getSourceLine(sourceName, lineNumber);
  if (src == null) {
    src = MISSING_SOURCE;
  }
  return sourceName + ":" + lineNumber + ":" + columnNumber + "\n"
      + src + "\n";
}
 
Example 19
Source File: LinkCommentsForOneFile.java    From clutz with MIT License 4 votes vote down vote up
@Override
public final boolean shouldTraverse(NodeTraversal nodeTraversal, Node n, Node parent) {
  if (isDone()) {
    return false;
  }
  // Ignore top level block
  if (n.isScript() || n.isModuleBody()) {
    return true;
  }

  int line = n.getLineno();
  // Comment is AFTER this line
  if (getLastLineOfCurrentComment() > line) {
    return true;
  }

  boolean outputCommentAfterLine = false;
  while (hasRemainingComments() && !isCommentAdjacentToLine(line)) {
    // Comment is AFTER this line
    if (getLastLineOfCurrentComment() > line) {
      return true;
    }

    // If the new comment is separated from the current one by at least a line,
    // output the current group of comments.
    if (getFirstLineOfNextComment() - getLastLineOfCurrentComment() > 1) {
      outputFloatingCommentFromBuffer(n);
    }
    addNextCommentToBuffer();
    outputCommentAfterLine = true;
  }

  if (getLastLineOfCurrentComment() == line) {
    // Comment on same line as code -- we have to make sure this is the node we should attach
    // it to.
    if (parent.isCall()) {
      // We're inside a function call, we have to be careful about which node to attach to,
      // since comments
      // can go before or after an argument.
      if (linkFunctionArgs(n, line)) return true;
    } else if (getCurrentComment().location.end.column < n.getCharno()) {
      // comment is before this node, so attach it
      linkCommentBufferToNode(n);
    } else {
      return true;
    }
  } else if (getLastLineOfCurrentComment() == line - 1) {
    // Comment ends just before code
    linkCommentBufferToNode(n);
  } else if (!hasRemainingComments() && !outputCommentAfterLine) {
    // Exhausted all comments, output floating comment.
    outputFloatingCommentFromBuffer(n);
  }

  if (!outputCommentAfterLine) {
    addNextCommentToBuffer();
  }
  return true;
}
 
Example 20
Source File: JSError.java    From astor with GNU General Public License v2.0 3 votes vote down vote up
/**
 * Creates a JSError from a file and Node position.
 *
 * @param sourceName The source file name
 * @param n Determines the line and char position within the source file name
 * @param type The DiagnosticType
 * @param arguments Arguments to be incorporated into the message
 */
public static JSError make(String sourceName, Node n, CheckLevel level,
    DiagnosticType type, String... arguments) {

  return new JSError(sourceName, n, n.getLineno(), n.getCharno(), type, level,
      arguments);
}