From a80779cbe47be28a4fc59b3529d29441ef2951aa Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Cl=C3=A9ment=20Dessoude?= Date: Tue, 8 Oct 2019 11:35:53 +0200 Subject: [PATCH 1/2] fix formatComment to keep stable reformating and add unit tests --- .../scripts/update-test-output.js | 25 +++++++--- .../src/printers/prettier-builder.js | 47 +++++++++++++++---- .../unit-test/comments/bug-fixes/_input.java | 46 ++++++++++++++++++ .../unit-test/comments/bug-fixes/_output.java | 35 ++++++++++++++ .../unit-test/comments/class/_output.java | 3 +- .../comments/comments-only/_output.java | 36 +++++++------- .../test/unit-test/comments/comments-spec.js | 1 + 7 files changed, 157 insertions(+), 36 deletions(-) create mode 100644 packages/prettier-plugin-java/test/unit-test/comments/bug-fixes/_input.java create mode 100644 packages/prettier-plugin-java/test/unit-test/comments/bug-fixes/_output.java diff --git a/packages/prettier-plugin-java/scripts/update-test-output.js b/packages/prettier-plugin-java/scripts/update-test-output.js index 0fe7e5d68..02446f07a 100644 --- a/packages/prettier-plugin-java/scripts/update-test-output.js +++ b/packages/prettier-plugin-java/scripts/update-test-output.js @@ -11,7 +11,10 @@ if (process.argv.indexOf("-single") > -1) { samplesDir = path.resolve(__dirname, "./single-printer-run"); } else if (process.argv.indexOf("-repository") > -1) { const testSamples = path.resolve(__dirname, "../test-samples"); - originalSamplesDir = path.resolve(__dirname, process.argv[3]); + originalSamplesDir = path.resolve( + __dirname, + process.argv[process.argv.indexOf("-repository") + 1] + ); samplesDir = path.resolve(testSamples, path.basename(originalSamplesDir)); if (fs.existsSync(samplesDir)) { fs.removeSync(samplesDir); @@ -21,6 +24,11 @@ if (process.argv.indexOf("-single") > -1) { console.log(`end copy ${originalSamplesDir} to ${samplesDir}`); } +let numberOfTime = 1; +if (process.argv.indexOf("-times") > -1) { + numberOfTime = process.argv[process.argv.indexOf("-times") + 1]; +} + const sampleFiles = klawSync(samplesDir, { nodir: true }); const javaSampleFiles = sampleFiles.filter(fileDesc => { if (fileDesc.path.includes("node_modules")) { @@ -38,12 +46,15 @@ javaSampleFiles.forEach(fileDesc => { try { console.log(`Reading <${fileDesc.path}>`); - const newExpectedText = prettier.format(javaFileText, { - parser: "java", - plugins: [path.resolve(__dirname, "../")], - tabWidth: 2, - endOfLine: "lf" - }); + let newExpectedText = javaFileText; + for (let i = 0; i < numberOfTime; i++) { + newExpectedText = prettier.format(newExpectedText, { + parser: "java", + plugins: [path.resolve(__dirname, "../")], + tabWidth: 2, + endOfLine: "lf" + }); + } let outputFilePath = fileDesc.path.replace(/input.java$/, "output.java"); if (process.argv.indexOf("-repository") > -1) { outputFilePath = fileDesc.path; diff --git a/packages/prettier-plugin-java/src/printers/prettier-builder.js b/packages/prettier-plugin-java/src/printers/prettier-builder.js index e0f417a6c..ed60349ef 100644 --- a/packages/prettier-plugin-java/src/printers/prettier-builder.js +++ b/packages/prettier-plugin-java/src/printers/prettier-builder.js @@ -61,19 +61,46 @@ function getTrailingComments(token) { return concat(arr); } +function isJavaDoc(comment, lines) { + let isJavaDoc = true; + if (comment.tokenType.name === "TraditionalComment" && lines.length > 1) { + for (let i = 1; i < lines.length; i++) { + if (lines[i].trim().charAt(0) !== "*") { + isJavaDoc = false; + break; + } + } + } else { + isJavaDoc = false; + } + + return isJavaDoc; +} + +function formatJavaDoc(lines) { + const res = [lines[0].trim()]; + + for (let i = 1; i < lines.length; i++) { + res.push(prettier.hardline); + res.push(" " + lines[i].trim()); + } + + return res; +} + function formatComment(comment) { const res = []; - comment.image.split("\n").forEach(l => { - if (l.match(/(\s+)(\*)(.*)/gm) && !l.match(/(\/)(\*)(.*)(\*)(\/)/gm)) { - res.push(" " + l.trim()); - } else { - res.push(l); - } - res.push(hardLineWithoutBreakParent); - }); - if (res[res.length - 1] === hardLineWithoutBreakParent) { - res.pop(); + const lines = comment.image.split("\n"); + + if (isJavaDoc(comment, lines)) { + return formatJavaDoc(lines); } + + lines.forEach(line => { + res.push(line); + res.push(prettier.literalline); + }); + res.pop(); return res; } diff --git a/packages/prettier-plugin-java/test/unit-test/comments/bug-fixes/_input.java b/packages/prettier-plugin-java/test/unit-test/comments/bug-fixes/_input.java new file mode 100644 index 000000000..f2d591431 --- /dev/null +++ b/packages/prettier-plugin-java/test/unit-test/comments/bug-fixes/_input.java @@ -0,0 +1,46 @@ +// Bug Fix: #279 - See also https://prettier.io/docs/en/rationale.html#comments +class T { + /* + * comment + */ + void t() { + + } + + /* + * comment +*/ + void t() { + + } + + /* + * comment + */ + void t() { + + } + + /* + * comment + */ + void t() { + + } + + /* + * line 1 + line 2 + */ + void t() { + + } + + /* + + *line 2 + */ + void t() { + + } +} diff --git a/packages/prettier-plugin-java/test/unit-test/comments/bug-fixes/_output.java b/packages/prettier-plugin-java/test/unit-test/comments/bug-fixes/_output.java new file mode 100644 index 000000000..722920457 --- /dev/null +++ b/packages/prettier-plugin-java/test/unit-test/comments/bug-fixes/_output.java @@ -0,0 +1,35 @@ +// Bug Fix: #279 - See also https://prettier.io/docs/en/rationale.html#comments +class T { + + /* + * comment + */ + void t() {} + + /* + * comment + */ + void t() {} + + /* + * comment + */ + void t() {} + + /* + * comment + */ + void t() {} + + /* + * line 1 + line 2 + */ + void t() {} + + /* + + *line 2 + */ + void t() {} +} diff --git a/packages/prettier-plugin-java/test/unit-test/comments/class/_output.java b/packages/prettier-plugin-java/test/unit-test/comments/class/_output.java index db4bed5a2..5124e0d0e 100644 --- a/packages/prettier-plugin-java/test/unit-test/comments/class/_output.java +++ b/packages/prettier-plugin-java/test/unit-test/comments/class/_output.java @@ -158,7 +158,8 @@ private ArrayTable( * elements but rowKeySet() will be empty and containsRow() won't * acknolwedge them. */ - rowKeyToIndex = Maps.indexMap(rowList); + rowKeyToIndex = + Maps.indexMap(rowList); columnKeyToIndex = Maps.indexMap(columnList); @SuppressWarnings( diff --git a/packages/prettier-plugin-java/test/unit-test/comments/comments-only/_output.java b/packages/prettier-plugin-java/test/unit-test/comments/comments-only/_output.java index adaafa66d..648a96e9b 100644 --- a/packages/prettier-plugin-java/test/unit-test/comments/comments-only/_output.java +++ b/packages/prettier-plugin-java/test/unit-test/comments/comments-only/_output.java @@ -1,23 +1,23 @@ /* -* # Copyright 2013-2019 the original author or authors from the JHipster project. -* # -* # This file is part of the JHipster project, see https://www.jhipster.tech/ -* # for more information. -* # -* # Licensed under the Apache License, Version 2.0 (the "License"); -* # you may not use this file except in compliance with the License. -* # You may obtain a copy of the License at -* # -* # http://www.apache.org/licenses/LICENSE-2.0 -* # -* # Unless required by applicable law or agreed to in writing, software -* # distributed under the License is distributed on an "AS IS" BASIS, -* # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -* # See the License for the specific language governing permissions and -* # limitations under the License. -* # -*/ + * # Copyright 2013-2019 the original author or authors from the JHipster project. + * # + * # This file is part of the JHipster project, see https://www.jhipster.tech/ + * # for more information. + * # + * # Licensed under the Apache License, Version 2.0 (the "License"); + * # you may not use this file except in compliance with the License. + * # You may obtain a copy of the License at + * # + * # http://www.apache.org/licenses/LICENSE-2.0 + * # + * # Unless required by applicable law or agreed to in writing, software + * # distributed under the License is distributed on an "AS IS" BASIS, + * # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * # See the License for the specific language governing permissions and + * # limitations under the License. + * # + */ /* package io.github.jhipster.sample.repository; diff --git a/packages/prettier-plugin-java/test/unit-test/comments/comments-spec.js b/packages/prettier-plugin-java/test/unit-test/comments/comments-spec.js index 7ac7085fd..91c1a8753 100644 --- a/packages/prettier-plugin-java/test/unit-test/comments/comments-spec.js +++ b/packages/prettier-plugin-java/test/unit-test/comments/comments-spec.js @@ -8,4 +8,5 @@ describe("prettier-java", () => { testSample(path.resolve(__dirname, "./package")); testSample(path.resolve(__dirname, "./comments-blocks-and-statements")); testSample(path.resolve(__dirname, "./comments-only")); + testSample(path.resolve(__dirname, "./bug-fixes")); }); From a8d007040c6d16b2d4395929cd632d3921cbcbdd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Cl=C3=A9ment=20Dessoude?= Date: Tue, 15 Oct 2019 15:53:18 +0200 Subject: [PATCH 2/2] update CONTRIBUTING.md with new flag --- CONTRIBUTING.md | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index 165fe7d07..befa27ba1 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -55,7 +55,7 @@ It will basically clone some java repositories and try to parse every java file. ### prettier-java-plugin -In this section, We suppose you are in the packages/prettier-plugin-java folder. +In this section, we suppose you are in the packages/prettier-plugin-java folder. If you would like to check the impact of your changes on a sample code, edit the scripts/single-printer-run/\_input.java file and run @@ -73,6 +73,12 @@ node scripts/update-test-ouput.js -repository relative/path/to/the/repository It will then be output inside test-samples/repository-name. +To check the stability of the reformating, you can run several times Prettier with the `-times` flag (e.g. 5 times): + +```bash +node scripts/update-test-output.js -single -times 5 +``` + If you run: ```bash