Skip to content

Commit

Permalink
WIP: Integrating SNC changes
Browse files Browse the repository at this point in the history
  • Loading branch information
andreabergia committed Nov 5, 2024
1 parent 59eb197 commit 05c0476
Show file tree
Hide file tree
Showing 12 changed files with 145 additions and 9 deletions.
1 change: 1 addition & 0 deletions .java-version
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
17.0
36 changes: 32 additions & 4 deletions rhino/src/main/java/org/mozilla/javascript/IRFactory.java
Original file line number Diff line number Diff line change
Expand Up @@ -534,7 +534,9 @@ private Node transformElementGet(ElementGet node) {

private Node transformExprStmt(ExpressionStatement node) {
Node expr = transform(node.getExpression());
return new Node(node.getType(), expr, node.getLineno());
Node exprStatementNode = new Node(node.getType(), expr, node.getLineno());
if (node.wasColumnAssigned()) exprStatementNode.setColumn(node.column());
return exprStatementNode;
}

private Node transformForInLoop(ForInLoop loop) {
Expand Down Expand Up @@ -812,7 +814,20 @@ private Node transformIf(IfStatement n) {
private Node transformInfix(InfixExpression node) {
Node left = transform(node.getLeft());
Node right = transform(node.getRight());
return createBinary(node.getType(), left, right);
Node binaryNode = createBinary(node.getType(), left, right);

// Since we are transforming InfixExpression -> Node, we need to copy over the column and
// line, but only for newly created nodes which have no column information set.
// createBinary() may return a Node reference with the correct line/column.
// Example: `true && <other_expr>` would return the `<other_expr>` reference from
// createBinary
boolean nodeCreated = (binaryNode != left) && (binaryNode != right);
if (nodeCreated && node.wasColumnAssigned()) {
binaryNode.setColumn(node.column());
binaryNode.setLineno(node.getLineno());
}

return binaryNode;
}

private Node transformLabeledStatement(LabeledStatement ls) {
Expand Down Expand Up @@ -878,6 +893,8 @@ private Node transformObjectLiteral(ObjectLiteral node) {
// stages don't need to know about object literals.
List<ObjectProperty> elems = node.getElements();
Node object = new Node(Token.OBJECTLIT);
object.setColumn(node.column());
object.setLineno(node.getLineno());
Object[] properties;
if (elems.isEmpty()) {
properties = ScriptRuntime.emptyArgs;
Expand Down Expand Up @@ -992,7 +1009,10 @@ private Node transformScript(ScriptNode node) {
}

private Node transformString(StringLiteral node) {
return Node.newString(node.getValue());
Node stringNode = Node.newString(node.getValue());
stringNode.setColumn(node.column());
stringNode.setLineno(node.getLineno());
return stringNode;
}

private Node transformSwitch(SwitchStatement node) {
Expand Down Expand Up @@ -1063,7 +1083,10 @@ private Node transformSwitch(SwitchStatement node) {

private Node transformThrow(ThrowStatement node) {
Node value = transform(node.getExpression());
return new Node(Token.THROW, value, node.getLineno());
value.setColumn(node.columnOrDefault());
Node nx = new Node(Token.THROW, value, node.getLineno());
nx.setColumn(node.columnOrDefault());
return nx;
}

private Node transformTry(TryStatement node) {
Expand Down Expand Up @@ -1753,6 +1776,11 @@ private static Node createIf(Node cond, Node ifTrue, Node ifFalse, int lineno) {
result.addChildToBack(ifNotTarget);
}

if (cond.getFirstChild() != null) {
Node conditionalChild = cond.getFirstChild();
if (conditionalChild.wasColumnAssigned()) result.setColumn(conditionalChild.column());
}

return result;
}

Expand Down
33 changes: 33 additions & 0 deletions rhino/src/main/java/org/mozilla/javascript/Node.java
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,8 @@ 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 @@ -1253,11 +1255,42 @@ 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.
* <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;
}

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;

/**
* Linked list of properties. Since vast majority of nodes would have no more then 2 properties,
Expand Down
44 changes: 39 additions & 5 deletions rhino/src/main/java/org/mozilla/javascript/Parser.java
Original file line number Diff line number Diff line change
Expand Up @@ -670,6 +670,7 @@ private AstNode parseFunctionBody(int type, FunctionNode fnNode) throws IOExcept
++nestingOfFunction;
int pos = ts.tokenBeg;
Block pn = new Block(pos); // starts at LC position
pn.setColumn(ts.getTokenColumn());

// Function code that is supplied as the arguments to the built-in
// Function, Generator, and AsyncFunction constructors is strict mode code
Expand Down Expand Up @@ -955,6 +956,7 @@ private FunctionNode function(int type) throws IOException {
}

FunctionNode fnNode = new FunctionNode(functionSourceStart, name);
fnNode.setColumn(ts.getTokenColumn(functionSourceStart));
fnNode.setFunctionType(type);
if (isGenerator) {
fnNode.setIsES6Generator();
Expand Down Expand Up @@ -1167,6 +1169,7 @@ private AstNode statements(AstNode parent) throws IOException {
int pos = ts.tokenBeg;
AstNode block = parent != null ? parent : new Block(pos);
block.setLineno(ts.lineno);
block.setColumn(ts.getTokenColumn());

int tt;
while ((tt = peekToken()) > Token.EOF && tt != Token.RC) {
Expand Down Expand Up @@ -1418,6 +1421,11 @@ private IfStatement ifStatement() throws IOException {
pn.setElsePart(ifFalse);
pn.setElsePosition(elsePos);
pn.setLineno(lineno);
if (pn.getCondition() != null) {
AstNode condition = pn.getCondition();
if (condition.wasColumnAssigned()) pn.setColumn(condition.column());
}

return pn;
}

Expand All @@ -1429,6 +1437,7 @@ private SwitchStatement switchStatement() throws IOException {
SwitchStatement pn = new SwitchStatement(pos);
if (mustMatchToken(Token.LP, "msg.no.paren.switch", true)) pn.setLp(ts.tokenBeg - pos);
pn.setLineno(ts.lineno);
pn.setColumn(ts.getTokenColumn(pos));

AstNode discriminant = expr(false);
pn.setExpression(discriminant);
Expand Down Expand Up @@ -1478,6 +1487,7 @@ private SwitchStatement switchStatement() throws IOException {
caseNode.setExpression(caseExpression);
caseNode.setLength(ts.tokenEnd - pos); // include colon
caseNode.setLineno(caseLineno);
caseNode.setColumn(ts.getTokenColumn(casePos));

while ((tt = peekToken()) != Token.RC
&& tt != Token.CASE
Expand Down Expand Up @@ -1511,6 +1521,7 @@ private WhileLoop whileLoop() throws IOException {
int pos = ts.tokenBeg;
WhileLoop pn = new WhileLoop(pos);
pn.setLineno(ts.lineno);
pn.setColumn(ts.getTokenColumn(pos));
enterLoop(pn);
try {
ConditionData data = condition();
Expand All @@ -1532,6 +1543,7 @@ private DoLoop doLoop() throws IOException {
int pos = ts.tokenBeg, end;
DoLoop pn = new DoLoop(pos);
pn.setLineno(ts.lineno);
pn.setColumn(ts.getTokenColumn(pos));
enterLoop(pn);
try {
AstNode body = getNextStatementAfterInlineComments(pn);
Expand Down Expand Up @@ -1865,6 +1877,7 @@ private ThrowStatement throwStatement() throws IOException {
AstNode expr = expr(false);
ThrowStatement pn = new ThrowStatement(pos, expr);
pn.setLineno(lineno);
pn.setColumn(ts.getTokenColumn(pos));
return pn;
}

Expand Down Expand Up @@ -2224,6 +2237,7 @@ private VariableDeclaration variables(int declType, int pos, boolean isStatement
VariableDeclaration pn = new VariableDeclaration(pos);
pn.setType(declType);
pn.setLineno(ts.lineno);
pn.setColumn(ts.getTokenColumn());
Comment varjsdocNode = getAndResetJsDoc();
if (varjsdocNode != null) {
pn.setJsDocNode(varjsdocNode);
Expand Down Expand Up @@ -2273,6 +2287,7 @@ private VariableDeclaration variables(int declType, int pos, boolean isStatement
}

VariableInitializer vi = new VariableInitializer(kidPos, end - kidPos);
vi.setColumn(ts.getTokenColumn(kidPos));
if (destructuring != null) {
if (init == null && !inForInit) {
reportError("msg.destruct.assign.no.init");
Expand Down Expand Up @@ -2442,7 +2457,9 @@ private AstNode assignExpr() throws IOException {
if (isNotValidSimpleAssignmentTarget(pn))
reportError("msg.syntax.invalid.assignment.lhs");

Node left = pn;
pn = new Assignment(tt, pn, assignExpr(), opPos);
if (left.wasColumnAssigned()) pn.setColumn(left.column());

if (jsdocNode != null) {
pn.setJsDocNode(jsdocNode);
Expand Down Expand Up @@ -2698,34 +2715,39 @@ private AstNode unaryExpr() throws IOException {
consumeToken();
node = new UnaryExpression(tt, ts.tokenBeg, unaryExpr());
node.setLineno(line);
node.setColumn(ts.getTokenColumn());
return node;

case Token.ADD:
consumeToken();
// Convert to special POS token in parse tree
node = new UnaryExpression(Token.POS, ts.tokenBeg, unaryExpr());
node.setLineno(line);
node.setColumn(ts.getTokenColumn());
return node;

case Token.SUB:
consumeToken();
// Convert to special NEG token in parse tree
node = new UnaryExpression(Token.NEG, ts.tokenBeg, unaryExpr());
node.setLineno(line);
node.setColumn(ts.getTokenColumn());
return node;

case Token.INC:
case Token.DEC:
consumeToken();
UpdateExpression expr = new UpdateExpression(tt, ts.tokenBeg, memberExpr(true));
expr.setLineno(line);
expr.setColumn(ts.getTokenColumn());
checkBadIncDec(expr);
return expr;

case Token.DELPROP:
consumeToken();
node = new UnaryExpression(tt, ts.tokenBeg, unaryExpr());
node.setLineno(line);
node.setColumn(ts.getTokenColumn());
return node;

case Token.ERROR:
Expand All @@ -2750,6 +2772,7 @@ private AstNode unaryExpr() throws IOException {
consumeToken();
UpdateExpression uexpr = new UpdateExpression(tt, ts.tokenBeg, pn, true);
uexpr.setLineno(line);
uexpr.setColumn(ts.getTokenColumn());
checkBadIncDec(uexpr);
return uexpr;
}
Expand Down Expand Up @@ -2843,6 +2866,7 @@ private AstNode memberExpr(boolean allowCallSyntax) throws IOException {
consumeToken();
int pos = ts.tokenBeg;
NewExpression nx = new NewExpression(pos);
nx.setColumn(ts.getTokenColumn());

AstNode target = memberExpr(false);
int end = getNodeEnd(target);
Expand Down Expand Up @@ -2968,6 +2992,7 @@ private FunctionCall makeFunctionCall(AstNode pn, int pos, int lineno, boolean i
// Assign the line number for the function call to where
// the paren appeared, not where the name expression started.
f.setLineno(lineno);
f.setColumn(ts.getTokenColumn());
f.setLp(ts.tokenBeg - pos);
List<AstNode> args = argumentList();
if (args != null && args.size() > ARGC_LIMIT) reportError("msg.too.many.function.args");
Expand Down Expand Up @@ -3122,6 +3147,7 @@ private ElementGet makeElemGet(AstNode pn, int lb, int lineno) throws IOExceptio
g.setElement(expr);
g.setParens(lb, rb);
g.setLineno(lineno);
g.setColumn(ts.getTokenColumn());
return g;
}

Expand Down Expand Up @@ -3303,6 +3329,7 @@ private AstNode primaryExpr() throws IOException {
RegExpLiteral re = new RegExpLiteral(pos, end - pos);
re.setValue(ts.getString());
re.setFlags(ts.readAndClearRegExpFlags());
re.setColumn(ts.getTokenColumn());
return re;

case Token.NULL:
Expand All @@ -3312,7 +3339,7 @@ private AstNode primaryExpr() throws IOException {
consumeToken();
pos = ts.tokenBeg;
end = ts.tokenEnd;
return new KeywordLiteral(pos, end - pos, tt);
return new KeywordLiteral(pos, end - pos, tt, ts.getTokenColumn(pos));

case Token.TEMPLATE_LITERAL:
consumeToken();
Expand Down Expand Up @@ -3783,6 +3810,7 @@ private ObjectLiteral objectLiteral() throws IOException {

mustMatchToken(Token.RC, "msg.no.brace.prop", true);
ObjectLiteral pn = new ObjectLiteral(pos, ts.tokenEnd - pos);
pn.setColumn(ts.getTokenColumn());
if (objectLiteralDestructuringDefault) {
pn.putIntProp(Node.OBJECT_LITERAL_DESTRUCTURING, 1);
}
Expand Down Expand Up @@ -3815,6 +3843,7 @@ private AstNode objliteralProperty() throws IOException {
if (compilerEnv.getLanguageVersion() >= Context.VERSION_ES6) {
int pos = ts.tokenBeg;
int lineno = ts.lineno;
int col = ts.tokenBeg;
nextToken();
AstNode expr = assignExpr();
if (peekToken() != Token.RB) {
Expand All @@ -3824,6 +3853,7 @@ private AstNode objliteralProperty() throws IOException {

pname = new ComputedPropertyKey(pos, ts.tokenEnd - pos);
pname.setLineno(lineno);
pname.setColumn(col);
((ComputedPropertyKey) pname).setExpression(expr);
} else {
reportError("msg.bad.prop");
Expand Down Expand Up @@ -3938,7 +3968,7 @@ private Name createNameNode(boolean checkActivation, int token) {
codeBug();
}
}
Name name = new Name(beg, s);
Name name = new Name(beg, s, ts.getTokenColumn(beg));
name.setLineno(lineno);
if (checkActivation) {
checkActivationName(s, token);
Expand All @@ -3950,6 +3980,7 @@ private StringLiteral createStringLiteral() {
int pos = ts.tokenBeg, end = ts.tokenEnd;
StringLiteral s = new StringLiteral(pos, end - pos);
s.setLineno(ts.lineno);
s.setColumn(ts.getTokenColumn());
s.setValue(ts.getString());
s.setQuoteCharacter(ts.getQuoteChar());
return s;
Expand Down Expand Up @@ -4012,9 +4043,9 @@ private AstNode createNumericLiteral(int tt, boolean isProperty) {
}
}
if (tt == Token.BIGINT) {
return new BigIntLiteral(ts.tokenBeg, s + "n", ts.getBigInt());
return new BigIntLiteral(ts.tokenBeg, s + "n", ts.getBigInt(), ts.getTokenColumn());
} else {
return new NumberLiteral(ts.tokenBeg, s, ts.getNumber());
return new NumberLiteral(ts.tokenBeg, s, ts.getNumber(), ts.getTokenColumn());
}
}

Expand Down Expand Up @@ -4076,6 +4107,7 @@ private void checkBadIncDec(UpdateExpression expr) {
private ErrorNode makeErrorNode() {
ErrorNode pn = new ErrorNode(ts.tokenBeg, ts.tokenEnd - ts.tokenBeg);
pn.setLineno(ts.lineno);
pn.setColumn(ts.getTokenColumn());
return pn;
}

Expand Down Expand Up @@ -4584,7 +4616,9 @@ protected Node simpleAssignment(Node left, Node right, Transformer transformer)
reportError("msg.bad.id.strict", name);
}
left.setType(Token.BINDNAME);
return new Node(Token.SETNAME, left, right);
Node node = new Node(Token.SETNAME, left, right);
if (left.wasColumnAssigned()) node.setColumn(left.column());
return node;

case Token.GETPROP:
case Token.GETELEM:
Expand Down
Loading

0 comments on commit 05c0476

Please sign in to comment.