Skip to content

Commit

Permalink
fix imports accept double semi-colon (#158)
Browse files Browse the repository at this point in the history
* fix imports accept empty statements in between

* Moving comments and adding link to the discussion for semicolons in between imports
  • Loading branch information
Shaolans authored and bd82 committed Mar 15, 2019
1 parent d06f710 commit d27e6cd
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 10 deletions.
33 changes: 23 additions & 10 deletions packages/java-parser/src/productions/packages-and-modules.js
Expand Up @@ -64,16 +64,29 @@ function defineRules($, t) {
// distinguishing between the alternatives due to unbound common prefix.
// TODO: A post parsing step is required to align with the official specs.
// The Identifier "var" is not allowed in all positions and variations of the importDeclaration
$.CONSUME(t.Import);
$.OPTION(() => {
$.CONSUME(t.Static);
});
$.SUBRULE($.packageOrTypeName);
$.OPTION2(() => {
$.CONSUME(t.Dot);
$.CONSUME(t.Star);
});
$.CONSUME(t.Semicolon);
$.OR([
{
ALT: () => {
$.CONSUME(t.Import);
$.OPTION(() => {
$.CONSUME(t.Static);
});
$.SUBRULE($.packageOrTypeName);
$.OPTION2(() => {
$.CONSUME(t.Dot);
$.CONSUME(t.Star);
});
$.CONSUME(t.Semicolon);
}
},
// Spec Deviation: The spec do not allow empty statement in between imports.
// However Java compiler consider empty statements valid, we chose
// to support that case, thus deviate from the spec.
// See here: https://github.com/jhipster/prettier-java/pull/158
{
ALT: () => $.SUBRULE($.emptyStatement)
}
]);
});

// https://docs.oracle.com/javase/specs/jls/se11/html/jls-7.html#jls-TypeDeclaration
Expand Down
9 changes: 9 additions & 0 deletions packages/java-parser/test/bugs-spec.js
Expand Up @@ -23,3 +23,12 @@ describe("The Java Parser fixed bugs", () => {
expect(() => javaParser.parse(input)).to.not.throw();
});
});

describe("The Java Parser fixed bugs", () => {
it("issue #158 - semicolons inside imports", () => {
const input = "import Foo;;import Bar;";
expect(() =>
javaParser.parse(input, "ordinaryCompilationUnit")
).to.not.throw();
});
});

0 comments on commit d27e6cd

Please sign in to comment.