Skip to content

Commit

Permalink
Fixed more cases and added more tests
Browse files Browse the repository at this point in the history
  • Loading branch information
andreabergia committed Nov 15, 2024
1 parent 492ab45 commit 7c7c811
Show file tree
Hide file tree
Showing 3 changed files with 141 additions and 12 deletions.
13 changes: 9 additions & 4 deletions rhino/src/main/java/org/mozilla/javascript/Parser.java
Original file line number Diff line number Diff line change
Expand Up @@ -3751,7 +3751,7 @@ private GeneratorExpressionLoop generatorExpressionLoop() throws IOException {
private static final int METHOD_ENTRY = 8;

private ObjectLiteral objectLiteral() throws IOException {
int pos = ts.tokenBeg, lineno = lineNumber();
int pos = ts.tokenBeg, lineno = lineNumber(), column = columnNumber();
int afterComma = -1;
List<ObjectProperty> elems = new ArrayList<>();
Set<String> getterNames = null;
Expand Down Expand Up @@ -3783,6 +3783,11 @@ private ObjectLiteral objectLiteral() throws IOException {
propertyName = ts.getString();
int ppos = ts.tokenBeg;
consumeToken();
if (pname instanceof Name) {
// For complicated reasons, parsing a name does not advance the token
pname.setLineno(lineNumber());
pname.setColumn(columnNumber());
}

// This code path needs to handle both destructuring object
// literals like:
Expand Down Expand Up @@ -3877,7 +3882,6 @@ private ObjectLiteral objectLiteral() throws IOException {

mustMatchToken(Token.RC, "msg.no.brace.prop", true);
ObjectLiteral pn = new ObjectLiteral(pos, ts.tokenEnd - pos);
pn.setColumn(columnNumber());
if (objectLiteralDestructuringDefault) {
pn.putIntProp(Node.OBJECT_LITERAL_DESTRUCTURING, 1);
}
Expand All @@ -3886,6 +3890,7 @@ private ObjectLiteral objectLiteral() throws IOException {
}
pn.setElements(elems);
pn.setLineno(lineno);
pn.setColumn(column);
return pn;
}

Expand All @@ -3909,9 +3914,9 @@ private AstNode objliteralProperty() throws IOException {
case Token.LB:
if (compilerEnv.getLanguageVersion() >= Context.VERSION_ES6) {
int pos = ts.tokenBeg;
int lineno = lineNumber();
int col = ts.tokenBeg;
nextToken();
int lineno = lineNumber();
int col = columnNumber();
AstNode expr = assignExpr();
if (peekToken() != Token.RB) {
reportError("msg.bad.prop");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@ public void setLeft(AstNode left) {
this.left = left;
// line number should agree with source position
setLineno(left.getLineno());
setColumn(left.getColumn());
left.setParent(this);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import org.mozilla.javascript.ast.Block;
import org.mozilla.javascript.ast.BreakStatement;
import org.mozilla.javascript.ast.CatchClause;
import org.mozilla.javascript.ast.ComputedPropertyKey;
import org.mozilla.javascript.ast.ConditionalExpression;
import org.mozilla.javascript.ast.ContinueStatement;
import org.mozilla.javascript.ast.DoLoop;
Expand All @@ -33,7 +34,10 @@
import org.mozilla.javascript.ast.KeywordLiteral;
import org.mozilla.javascript.ast.LabeledStatement;
import org.mozilla.javascript.ast.Name;
import org.mozilla.javascript.ast.NewExpression;
import org.mozilla.javascript.ast.NumberLiteral;
import org.mozilla.javascript.ast.ObjectLiteral;
import org.mozilla.javascript.ast.ObjectProperty;
import org.mozilla.javascript.ast.ParenthesizedExpression;
import org.mozilla.javascript.ast.PropertyGet;
import org.mozilla.javascript.ast.ReturnStatement;
Expand Down Expand Up @@ -656,14 +660,133 @@ void expressionHook() {
assertLineColumnAre(falseExpression, 0, 10);
}

// TODO expressions:
// parenthesis
// square brackets
// object literals
// obj literal with function syntax
// comma
// new
// destructuring
@Test
void expressionParenthesis() {
AstRoot root = myParse(" (3)");
ExpressionStatement firstExpr =
assertInstanceOf(ExpressionStatement.class, root.getFirstChild());
assertLineColumnAre(firstExpr, 0, 2);
ParenthesizedExpression parenthesizedExpression =
assertInstanceOf(ParenthesizedExpression.class, firstExpr.getExpression());
assertLineColumnAre(parenthesizedExpression, 0, 2);
NumberLiteral nestedExpression =
assertInstanceOf(NumberLiteral.class, parenthesizedExpression.getExpression());
assertLineColumnAre(nestedExpression, 0, 3);
}

@Test
void expressionComma() {
AstRoot root = myParse(" a, /*comment*/ 3");
ExpressionStatement firstExpr =
assertInstanceOf(ExpressionStatement.class, root.getFirstChild());
assertLineColumnAre(firstExpr, 0, 2);
InfixExpression infixExpression =
assertInstanceOf(InfixExpression.class, firstExpr.getExpression());
assertLineColumnAre(infixExpression, 0, 2);
Name left = assertInstanceOf(Name.class, infixExpression.getLeft());
assertLineColumnAre(left, 0, 2);
NumberLiteral right = assertInstanceOf(NumberLiteral.class, infixExpression.getRight());
assertLineColumnAre(right, 0, 17);
}

@Test
void expressionNew() {
AstRoot root = myParse(" new Foo( a)");
ExpressionStatement firstExpr =
assertInstanceOf(ExpressionStatement.class, root.getFirstChild());
assertLineColumnAre(firstExpr, 0, 2);
NewExpression newExpression =
assertInstanceOf(NewExpression.class, firstExpr.getExpression());
assertLineColumnAre(newExpression, 0, 2);
Name target = assertInstanceOf(Name.class, newExpression.getTarget());
assertLineColumnAre(target, 0, 7);
assertEquals(1, newExpression.getArguments().size());
Name arg = assertInstanceOf(Name.class, newExpression.getArguments().get(0));
assertLineColumnAre(arg, 0, 12);
}

@Test
void expressionObjectLiteral() {
AstRoot root =
myParse(
" o = {a: 1,\n"
+ " [b]: 2,\n"
+ " c() { return 3; },\n"
+ " d: function() { return 4; },\n"
+ " get e() { return 5; },\n"
+ " set f(value) { this._f = value; }\n"
+ "}");
ExpressionStatement expressionStatement =
assertInstanceOf(ExpressionStatement.class, root.getFirstChild());
assertLineColumnAre(expressionStatement, 0, 2);
Assignment assignment =
assertInstanceOf(Assignment.class, expressionStatement.getExpression());
assertLineColumnAre(assignment, 0, 2);
ObjectLiteral objectLiteral = assertInstanceOf(ObjectLiteral.class, assignment.getRight());
assertLineColumnAre(objectLiteral, 0, 6);
assertEquals(6, objectLiteral.getElements().size());

ObjectProperty propA =
assertInstanceOf(ObjectProperty.class, objectLiteral.getElements().get(0));
assertLineColumnAre(propA, 0, 7);
assertLineColumnAre(propA.getLeft(), 0, 7);
assertLineColumnAre(propA.getRight(), 0, 10);

ObjectProperty propB =
assertInstanceOf(ObjectProperty.class, objectLiteral.getElements().get(1));
assertLineColumnAre(propB, 1, 2);
ComputedPropertyKey propBKey = assertInstanceOf(ComputedPropertyKey.class, propB.getLeft());
assertLineColumnAre(propBKey, 1, 2);
assertLineColumnAre(propBKey.getExpression(), 1, 3);
assertLineColumnAre(propB.getRight(), 1, 7);

ObjectProperty propC =
assertInstanceOf(ObjectProperty.class, objectLiteral.getElements().get(2));
assertLineColumnAre(propC, 2, 2);
assertLineColumnAre(propC.getLeft(), 2, 2);
assertLineColumnAre(propC.getRight(), 2, 2);

ObjectProperty propD =
assertInstanceOf(ObjectProperty.class, objectLiteral.getElements().get(3));
assertLineColumnAre(propD, 3, 2);
assertLineColumnAre(propD.getLeft(), 3, 2);
assertLineColumnAre(propD.getRight(), 3, 5);

ObjectProperty propE =
assertInstanceOf(ObjectProperty.class, objectLiteral.getElements().get(4));
assertLineColumnAre(propE, 4, 2);
assertLineColumnAre(propE.getLeft(), 4, 2);
assertLineColumnAre(propE.getRight(), 4, 6);

ObjectProperty propF =
assertInstanceOf(ObjectProperty.class, objectLiteral.getElements().get(5));
assertLineColumnAre(propF, 5, 2);
assertLineColumnAre(propF.getLeft(), 5, 2);
assertLineColumnAre(propF.getRight(), 5, 6);
}

@Test
void expressionDestructuring() {
AstRoot root = myParse(" let {a, b} = c();");
VariableDeclaration variableDeclaration =
assertInstanceOf(VariableDeclaration.class, root.getFirstChild());
assertLineColumnAre(variableDeclaration, 0, 2);
assertEquals(1, variableDeclaration.getVariables().size());

VariableInitializer firstVar = variableDeclaration.getVariables().get(0);
ObjectLiteral left = assertInstanceOf(ObjectLiteral.class, firstVar.getTarget());
assertLineColumnAre(left, 0, 6);
assertEquals(2, left.getElements().size());
ObjectProperty firstProp =
assertInstanceOf(ObjectProperty.class, left.getElements().get(0));
assertLineColumnAre(firstProp, 0, 7);
ObjectProperty secondProp =
assertInstanceOf(ObjectProperty.class, left.getElements().get(1));
assertLineColumnAre(secondProp, 0, 10);

FunctionCall right = assertInstanceOf(FunctionCall.class, firstVar.getInitializer());
assertLineColumnAre(right, 0, 15);
}

private AstRoot myParse(String source) {
environment.setLanguageVersion(Context.VERSION_ES6);
Expand Down

0 comments on commit 7c7c811

Please sign in to comment.