Fix a parser bug with missing semicolon warnings. There was no test coverage for this feature so add some, as well as simple tests for the trailing comma warning.
diff --git a/src/org/mozilla/javascript/Parser.java b/src/org/mozilla/javascript/Parser.java index 899361d..5fe743a 100644 --- a/src/org/mozilla/javascript/Parser.java +++ b/src/org/mozilla/javascript/Parser.java
@@ -1290,7 +1290,9 @@ case Token.EOF: case Token.RC: // Autoinsert ; - warnMissingSemi(pos, nodeEnd(pn)); + // Token.EOF can have negative length and negative nodeEnd(pn). + // So, make the end position at least pos+1. + warnMissingSemi(pos, Math.max(pos + 1, nodeEnd(pn))); break; default: if ((ttFlagged & TI_AFTER_EOL) == 0) {
diff --git a/testsrc/org/mozilla/javascript/tests/ParserIDETest.java b/testsrc/org/mozilla/javascript/tests/ParserIDETest.java new file mode 100644 index 0000000..c7ed203 --- /dev/null +++ b/testsrc/org/mozilla/javascript/tests/ParserIDETest.java
@@ -0,0 +1,85 @@ +package org.mozilla.javascript.tests; + +import org.junit.BeforeClass; +import org.junit.Test; +import org.mozilla.javascript.CompilerEnvirons; +import org.mozilla.javascript.ast.AstRoot; + +/** + * Tests for specific parser features targeted at IDE environments, namely the ability + * to warn about missing semicolons for JavaScript programmers who follow that style. + */ +public class ParserIDETest { + + private static CompilerEnvirons environment; + + @BeforeClass + public static void init() { + environment = CompilerEnvirons.ideEnvirons(); + } + + private AstRoot parse(String script, String[] errors, String[] warnings) { + return ParserTest.parse(script, errors, warnings, false, environment); + } + + @Test + public void testUnterminatedRE() { + String[] errors = {"unterminated regular expression literal"}; + String[] warnings = {"missing ; after statement"}; + + parse("/", errors, warnings); + } + + @Test + public void testMissingSemiBeforeComment() { + String[] errors = {}; + String[] warnings = { + "missing ; after statement", + "missing ; after statement" + }; + + parse("var a = 1\n/** a */ var b = 2", errors, warnings); + } + + @Test + public void testMissingSemiBeforeComment2() { + String[] errors = {}; + String[] warnings = { + "missing ; after statement", + "missing ; after statement" + }; + + parse("var a = 1\n/** a */\n var b = 2", errors, warnings); + } + + @Test + public void testMissingSemiBeforeComment3() { + String[] errors = {}; + String[] warnings = { + "missing ; after statement", + "missing ; after statement" + }; + + parse("var a = 1\n/** a */\n /** b */ var b = 2", errors, warnings); + } + + @Test + public void testWarnTrailingComma() { + String[] errors = {}; + String[] warnings = { + "Trailing comma is not legal in an ECMA-262 object initializer" + }; + + parse("var o = {a: 'foo', b: 'bar',};", errors, warnings); + } + + @Test + public void testWarnTrailingArrayComma() { + String[] errors = {}; + String[] warnings = { + "Trailing comma is not legal in an ECMA-262 object initializer" + }; + + parse("var a = [1, 2, 3,];", errors, warnings); + } +}
diff --git a/testsrc/org/mozilla/javascript/tests/ParserTest.java b/testsrc/org/mozilla/javascript/tests/ParserTest.java index 997bf1e1..9c7ad9d 100644 --- a/testsrc/org/mozilla/javascript/tests/ParserTest.java +++ b/testsrc/org/mozilla/javascript/tests/ParserTest.java
@@ -1328,34 +1328,39 @@ private AstRoot parse( String string, final String [] errors, final String [] warnings, boolean jsdoc) { + return parse(string, errors, warnings, jsdoc, environment); + } + + static AstRoot parse(String string, final String [] errors, final String [] warnings, + boolean jsdoc, CompilerEnvirons env) { TestErrorReporter testErrorReporter = new TestErrorReporter(errors, warnings) { - @Override - public EvaluatorException runtimeError( - String message, String sourceName, int line, String lineSource, - int lineOffset) { - if (errors == null) { - throw new UnsupportedOperationException(); - } - return new EvaluatorException( - message, sourceName, line, lineSource, lineOffset); - } - }; - environment.setErrorReporter(testErrorReporter); + @Override + public EvaluatorException runtimeError( + String message, String sourceName, int line, String lineSource, + int lineOffset) { + if (errors == null) { + throw new UnsupportedOperationException(); + } + return new EvaluatorException( + message, sourceName, line, lineSource, lineOffset); + } + }; + env.setErrorReporter(testErrorReporter); - environment.setRecordingComments(true); - environment.setRecordingLocalJsDocComments(jsdoc); + env.setRecordingComments(true); + env.setRecordingLocalJsDocComments(jsdoc); - Parser p = new Parser(environment, testErrorReporter); + Parser p = new Parser(env, testErrorReporter); AstRoot script = null; try { - script = p.parse(string, null, 0); + script = p.parse(string, null, 0); } catch (EvaluatorException e) { - if (errors == null) { - // EvaluationExceptions should not occur when we aren't expecting - // errors. - throw e; - } + if (errors == null) { + // EvaluationExceptions should not occur when we aren't expecting + // errors. + throw e; + } } assertTrue(testErrorReporter.hasEncounteredAllErrors());