Skip to content

Commit

Permalink
Proper error message when computed properties are used in destructuring
Browse files Browse the repository at this point in the history
We don't support it, but we want to give back a proper error message
rather than a generic assertion failed.
  • Loading branch information
andreabergia authored and gbrail committed Jul 3, 2024
1 parent c2bc023 commit 94aa470
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 0 deletions.
3 changes: 3 additions & 0 deletions rhino/src/main/java/org/mozilla/javascript/IRFactory.java
Original file line number Diff line number Diff line change
Expand Up @@ -2524,6 +2524,9 @@ void decompile(AstNode node) {
case Token.THIS:
decompiler.addToken(node.getType());
break;
case Token.COMPUTED_PROPERTY:
parser.reportError("msg.bad.computed.property.in.destruct");
break;
default:
Kit.codeBug("unexpected token: " + Token.typeToName(node.getType()));
}
Expand Down
3 changes: 3 additions & 0 deletions rhino/src/main/java/org/mozilla/javascript/Parser.java
Original file line number Diff line number Diff line change
Expand Up @@ -4154,6 +4154,9 @@ boolean destructuringObject(
} else if (id instanceof NumberLiteral) {
Node s = createNumber((int) ((NumberLiteral) id).getNumber());
rightElem = new Node(Token.GETELEM, createName(tempName), s);
} else if (id instanceof ComputedPropertyKey) {
reportError("msg.bad.computed.property.in.destruct");
return false;
} else {
throw codeBug();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,9 @@ msg.mult.index =\
msg.bad.for.in.destruct =\
Left hand side of for..in loop must be an array of length 2 to accept \
key/value pair.

msg.bad.computed.property.in.destruct =\
Unsupported computed property in destructuring.

msg.cant.convert =\
Can''t convert to type "{0}".
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -246,4 +246,30 @@ public void notSupportedUnderVersionLesserThanEsLatest() {
assertEquals("invalid property id (test#1)", ex.getMessage());
}
}

@Test
public void unsupportedInDestructuringInFunctionArguments() {
String script = "function({ [a]: b }) {};";
assertComputedPropertiesAreUnsupportedInDestructuring(
script, "Unsupported computed property in destructuring. (test#1)");
}

@Test
public void unsupportedInDestructuringInVariableDeclaration() {
String script = "var { [a]: b } = {};";
assertComputedPropertiesAreUnsupportedInDestructuring(
script, "Unsupported computed property in destructuring.");
}

private void assertComputedPropertiesAreUnsupportedInDestructuring(
String script, String message) {
try (Context cx = Context.enter()) {
cx.setLanguageVersion(Context.VERSION_ES6);
EvaluatorException ex =
assertThrows(
EvaluatorException.class,
() -> cx.compileString(script, "test", 1, null));
assertEquals(message, ex.getMessage());
}
}
}

0 comments on commit 94aa470

Please sign in to comment.