diff --git a/rhino/src/main/java/org/mozilla/javascript/IRFactory.java b/rhino/src/main/java/org/mozilla/javascript/IRFactory.java index 845d760f4e..8434dbecd0 100644 --- a/rhino/src/main/java/org/mozilla/javascript/IRFactory.java +++ b/rhino/src/main/java/org/mozilla/javascript/IRFactory.java @@ -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())); } diff --git a/rhino/src/main/java/org/mozilla/javascript/Parser.java b/rhino/src/main/java/org/mozilla/javascript/Parser.java index b4900bbcce..04c9b38256 100644 --- a/rhino/src/main/java/org/mozilla/javascript/Parser.java +++ b/rhino/src/main/java/org/mozilla/javascript/Parser.java @@ -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(); } diff --git a/rhino/src/main/resources/org/mozilla/javascript/resources/Messages.properties b/rhino/src/main/resources/org/mozilla/javascript/resources/Messages.properties index 96d2dd50b1..273ede32c1 100644 --- a/rhino/src/main/resources/org/mozilla/javascript/resources/Messages.properties +++ b/rhino/src/main/resources/org/mozilla/javascript/resources/Messages.properties @@ -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}". diff --git a/tests/src/test/java/org/mozilla/javascript/tests/ComputedPropertiesTest.java b/tests/src/test/java/org/mozilla/javascript/tests/ComputedPropertiesTest.java index 9556c7e70d..9ff27a9923 100644 --- a/tests/src/test/java/org/mozilla/javascript/tests/ComputedPropertiesTest.java +++ b/tests/src/test/java/org/mozilla/javascript/tests/ComputedPropertiesTest.java @@ -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()); + } + } }