Skip to content

Commit

Permalink
Started implementing tests for column numbers. Fixing bugs as found.
Browse files Browse the repository at this point in the history
  • Loading branch information
andreabergia committed Nov 19, 2024
1 parent 66e17f4 commit 508b2d6
Show file tree
Hide file tree
Showing 18 changed files with 1,539 additions and 480 deletions.
144 changes: 77 additions & 67 deletions rhino/src/main/java/org/mozilla/javascript/IRFactory.java

Large diffs are not rendered by default.

50 changes: 14 additions & 36 deletions rhino/src/main/java/org/mozilla/javascript/Node.java
Original file line number Diff line number Diff line change
Expand Up @@ -84,8 +84,6 @@ public class Node implements Iterable<Node> {
ATTRIBUTE_FLAG = 0x2, // x.@y or x..@y
DESCENDANTS_FLAG = 0x4; // x..y or x..@i

protected static final int DEFAULT_COLUMN = 1;

private static class PropListItem {
PropListItem next;
int type;
Expand Down Expand Up @@ -120,24 +118,24 @@ public Node(int nodeType, Node left, Node mid, Node right) {
right.next = null;
}

public Node(int nodeType, int line) {
public Node(int nodeType, int line, int column) {
type = nodeType;
lineno = line;
setLineColumnNumber(line, column);
}

public Node(int nodeType, Node child, int line) {
public Node(int nodeType, Node child, int line, int column) {
this(nodeType, child);
lineno = line;
setLineColumnNumber(line, column);
}

public Node(int nodeType, Node left, Node right, int line) {
public Node(int nodeType, Node left, Node right, int line, int column) {
this(nodeType, left, right);
lineno = line;
setLineColumnNumber(line, column);
}

public Node(int nodeType, Node left, Node mid, Node right, int line) {
public Node(int nodeType, Node left, Node mid, Node right, int line, int column) {
this(nodeType, left, mid, right);
lineno = line;
setLineColumnNumber(line, column);
}

public static Node newNumber(double number) {
Expand Down Expand Up @@ -533,8 +531,9 @@ public int getLineno() {
return lineno;
}

public void setLineno(int lineno) {
public void setLineColumnNumber(int lineno, int column) {
this.lineno = lineno;
this.column = column;
}

/** Can only be called when <code>getType() == Token.NUMBER</code> */
Expand Down Expand Up @@ -1255,42 +1254,21 @@ private static void appendPrintId(Node n, Map<Node, Integer> printIds, StringBui
}
}

protected void setColumn(int column) {
fColumn = column;
}

/**
* @return the column of where a Node is defined in source. If the column is -1, it was never
* initialized.
* initialized. One-based.
* <p>May be overridden by sub classes
*/
public int column() {
return fColumn;
}

/**
* This is a safe retrieval of the column number assigned to a node with the intent of being
* used by ISourceMapper.
*
* <p>Worst case the mapping is off by the number of lines a transpiler has compacted from
* source. In practice, this should be no more than a few lines.
*
* @return DEFAULT_COLUMN if column was never initialized, else the actual column
*/
public int columnOrDefault() {
return wasColumnAssigned() ? fColumn : DEFAULT_COLUMN;
}

public boolean wasColumnAssigned() {
return fColumn > 0;
public int getColumn() {
return column;
}

protected int type = Token.ERROR; // type of the node, e.g. Token.NAME
protected Node next; // next sibling
protected Node first; // first element of a linked list of children
protected Node last; // last element of a linked list of children
protected int lineno = -1;
private int fColumn = -1;
private int column = -1;

/**
* Linked list of properties. Since vast majority of nodes would have no more then 2 properties,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,8 @@ private void transformCompilationUnit_r(
unwind = new Node(Token.LEAVEWITH);
}
if (unwindBlock == null) {
unwindBlock = new Node(Token.BLOCK, node.getLineno());
unwindBlock = new Node(Token.BLOCK);
unwind.setLineColumnNumber(node.getLineno(), node.getColumn());
}
unwindBlock.addChildToBack(unwind);
}
Expand Down Expand Up @@ -297,7 +298,8 @@ private void transformCompilationUnit_r(
// to a LETEXPR
if (n.getType() != Token.LETEXPR) throw Kit.codeBug();
}
Node pop = new Node(Token.EXPR_VOID, n, node.getLineno());
Node pop = new Node(Token.EXPR_VOID, n);
pop.setLineColumnNumber(node.getLineno(), node.getColumn());
result.addChildToBack(pop);
}
node = replaceCurrent(parent, previous, node, result);
Expand Down
Loading

0 comments on commit 508b2d6

Please sign in to comment.