diff --git a/src/org/mozilla/javascript/Parser.java b/src/org/mozilla/javascript/Parser.java index 170d7b1d2f..30a4837e2f 100644 --- a/src/org/mozilla/javascript/Parser.java +++ b/src/org/mozilla/javascript/Parser.java @@ -209,13 +209,19 @@ void addWarning(String messageId, String messageArg, int position, int length) { addError(messageId, messageArg, position, length); } else if (errorCollector != null) { errorCollector.warning(message, sourceURI, position, length); - } else { + } else if (ts != null) { errorReporter.warning(message, sourceURI, ts.getLineno(), ts.getLine(), ts.getOffset()); + } else { + errorReporter.warning(message, sourceURI, 1, "", 1); } } void addError(String messageId) { - addError(messageId, ts.tokenBeg, ts.tokenEnd - ts.tokenBeg); + if (ts == null) { + addError(messageId, 0, 0); + } else { + addError(messageId, ts.tokenBeg, ts.tokenEnd - ts.tokenBeg); + } } void addError(String messageId, int position, int length) { @@ -232,7 +238,7 @@ void addError(String messageId, String messageArg) { void addError(String messageId, int c) { String messageArg = Character.toString((char) c); - addError(messageId, messageArg, ts.tokenBeg, ts.tokenEnd - ts.tokenBeg); + addError(messageId, messageArg); } void addError(String messageId, String messageArg, int position, int length) { diff --git a/testsrc/jstests/es6/desctructuring-assignment.js b/testsrc/jstests/es6/desctructuring-assignment.js new file mode 100644 index 0000000000..d75ac21719 --- /dev/null +++ b/testsrc/jstests/es6/desctructuring-assignment.js @@ -0,0 +1,36 @@ +// This Source Code Form is subject to the terms of the Mozilla Public +// License, v. 2.0. If a copy of the MPL was not distributed with this +// file, You can obtain one at http://mozilla.org/MPL/2.0/. + +load("testsrc/assert.js"); + +(function TestSuccessDestructuring() { + // var + var [a, b] = [1, 2]; + assertEquals(1, a); + assertEquals(2, b); + + // let + let [c, d] = [3, 4]; + assertEquals(3, c); + assertEquals(4, d); + + // const + const [e, f] = [5, 6]; + assertEquals(5, e); + assertEquals(6, f); +})(); + +(function TestDuplicateNameDestructuring() { + // var + var [a, a] = [1, 2]; + assertEquals(2, a); + + // let + assertThrows("let [b, b] = [3, 4];", TypeError); + + // const + assertThrows("const [c, c] = [5, 6];", TypeError); +})(); + +"success"; diff --git a/testsrc/org/mozilla/javascript/tests/es6/DestructuringAssignmentTest.java b/testsrc/org/mozilla/javascript/tests/es6/DestructuringAssignmentTest.java new file mode 100644 index 0000000000..fec705a750 --- /dev/null +++ b/testsrc/org/mozilla/javascript/tests/es6/DestructuringAssignmentTest.java @@ -0,0 +1,14 @@ +/* This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ + +package org.mozilla.javascript.tests.es6; + +import org.mozilla.javascript.Context; +import org.mozilla.javascript.drivers.LanguageVersion; +import org.mozilla.javascript.drivers.RhinoTest; +import org.mozilla.javascript.drivers.ScriptTestsBase; + +@RhinoTest("testsrc/jstests/es6/desctructuring-assignment.js") +@LanguageVersion(Context.VERSION_ES6) +public class DestructuringAssignmentTest extends ScriptTestsBase {}