Skip to content

Commit

Permalink
Fix issue #1095. Fix some potential NPEs in Parser.java.
Browse files Browse the repository at this point in the history
  • Loading branch information
MaxisTekfield committed Nov 27, 2021
1 parent cb8d5f0 commit 1860d6e
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 3 deletions.
12 changes: 9 additions & 3 deletions src/org/mozilla/javascript/Parser.java
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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) {
Expand Down
36 changes: 36 additions & 0 deletions testsrc/jstests/es6/desctructuring-assignment.js
Original file line number Diff line number Diff line change
@@ -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";
Original file line number Diff line number Diff line change
@@ -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 {}

0 comments on commit 1860d6e

Please sign in to comment.