Skip to content

Commit

Permalink
Feature: Organize module imports according to Google style (#281)
Browse files Browse the repository at this point in the history
Following PR #273 

* Feature: Organize module imports according to Google style

* add empty newline at the end of modules files
  • Loading branch information
clementdessoude committed Oct 11, 2019
1 parent d239d91 commit 1939add
Show file tree
Hide file tree
Showing 12 changed files with 80 additions and 6 deletions.
16 changes: 12 additions & 4 deletions packages/prettier-plugin-java/src/printers/packages-and-modules.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,12 +43,20 @@ class PackagesAndModulesPrettierVisitor {
}

modularCompilationUnit(ctx) {
// TODO: Should imports be sorted? Can imports in Java be safely sorted?
// TODO2: should the imports be grouped in some manner?
const importsDecl = this.mapVisit(ctx.importDeclaration);
const sortedImportsDecl = sortImports(ctx.importDeclaration);
const nonStaticImports = this.mapVisit(sortedImportsDecl.nonStaticImports);
const staticImports = this.mapVisit(sortedImportsDecl.staticImports);

const moduleDeclaration = this.visit(ctx.moduleDeclaration);

return join(concat(line, line), [importsDecl, moduleDeclaration]);
return rejectAndConcat([
rejectAndJoin(concat([hardline, hardline]), [
rejectAndJoin(hardline, staticImports),
rejectAndJoin(hardline, nonStaticImports),
moduleDeclaration
]),
line
]);
}

packageDeclaration(ctx) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,4 @@

// uses
/*a*/uses /*b*/fr.soat.vendinga/*a*/./*b*/machine.services.DrinksService/*a*/;/*b*/
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -59,4 +59,4 @@
another,
again,
averyveryveryveryveryveryveryveryveryveryverylongname;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import something.Different;
import java.utils.*;;;
import abc.def.Something;
import abc.def.Another;;;
import abc.def;
import static abc.def;
import static something.Different;
import static java.utils.*;;;
import static abc.def.Something;
import static abc.def.Another;;;
import one.last;;;

module my.module {}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import static abc.def;
import static abc.def.Another;
import static abc.def.Something;
import static java.utils.*;
import static something.Different;

import abc.def;
import abc.def.Another;
import abc.def.Something;
import java.utils.*;
import one.last;
import something.Different;

module my.module {}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@


module my.module {}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
module my.module {}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import something.Different;
import java.utils.*;;;
import abc.def.Something;
import abc.def.Another;;;
import abc.def;
import one.last;;;

module my.module {}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import abc.def;
import abc.def.Another;
import abc.def.Something;
import java.utils.*;
import one.last;
import something.Different;

module my.module {}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@

import static abc.def;
import static something.Different;
import static java.utils.*;;;
import static abc.def.Something;
import static abc.def.Another;;;

module my.module {}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import static abc.def;
import static abc.def.Another;
import static abc.def.Something;
import static java.utils.*;
import static something.Different;

module my.module {}
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,8 @@ describe("prettier-java", () => {
testSample(path.resolve(__dirname, "./classWithNoImports"));
testSample(path.resolve(__dirname, "./classWithOnlyStaticImports"));
testSample(path.resolve(__dirname, "./classWithOnlyNonStaticImports"));
testSample(path.resolve(__dirname, "./moduleWithMixedImports"));
testSample(path.resolve(__dirname, "./moduleWithNoImports"));
testSample(path.resolve(__dirname, "./moduleWithOnlyStaticImports"));
testSample(path.resolve(__dirname, "./moduleWithOnlyNonStaticImports"));
});

0 comments on commit 1939add

Please sign in to comment.