Skip to content

Commit a03392f

Browse files
committed
Fix #107 MissingNewLineAtEndOfFileCheck should skip empty files
1 parent cf926be commit a03392f

File tree

14 files changed

+29
-22
lines changed

14 files changed

+29
-22
lines changed

css-checks/src/main/java/org/sonar/css/checks/common/MissingNewLineAtEndOfFileCheck.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ public class MissingNewLineAtEndOfFileCheck extends DoubleDispatchVisitorCheck {
4242
@Override
4343
public void visitStyleSheet(StyleSheetTree tree) {
4444
try (RandomAccessFile randomAccessFile = new RandomAccessFile(getContext().getFile(), "r")) {
45-
if (!endsWithNewline(randomAccessFile)) {
45+
if (!endsWithNewline(randomAccessFile) && !tree.all().isEmpty()) {
4646
addFileIssue("Add an empty new line at the end of this file.");
4747
}
4848
} catch (IOException e) {

css-checks/src/main/resources/org/sonar/css/checks/l10n/common/empty-line-end-of-file.html

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99
</pre>
1010
<p>if the empty line is missing at the end of the file.</p>
1111

12+
<p>Note that empty files are not checked against this rule.</p>
13+
1214
<h2>stylelint Related Rules</h2>
1315
<ul>
1416
<li><a href="https://stylelint.io/user-guide/rules/no-missing-end-of-source-newline/" target="_blank">no-missing-end-of-source-newline</a></li>

css-checks/src/test/java/org/sonar/css/checks/common/MissingNewLineAtEndOfFileCheckTest.java

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,19 +23,33 @@
2323
import org.sonar.css.checks.CheckTestUtils;
2424
import org.sonar.css.checks.verifier.CssCheckVerifier;
2525

26+
import java.io.File;
27+
2628
public class MissingNewLineAtEndOfFileCheckTest {
2729

30+
private MissingNewLineAtEndOfFileCheck check = new MissingNewLineAtEndOfFileCheck();
31+
2832
@Test
2933
public void should_have_an_empty_new_line_at_the_end_of_the_file_and_not_raise_any_issue() {
30-
CssCheckVerifier.issuesOnCssFile(new MissingNewLineAtEndOfFileCheck(), CheckTestUtils.getCommonTestFile("newLineEndOfFile.css"))
34+
CssCheckVerifier.issuesOnCssFile(check, getTestFile("newLineEndOfFile.css"))
3135
.noMore();
3236
}
3337

3438
@Test
3539
public void should_not_have_an_empty_new_line_at_the_end_of_the_file_and_raise_an_issue() {
36-
CssCheckVerifier.issuesOnCssFile(new MissingNewLineAtEndOfFileCheck(), CheckTestUtils.getCommonTestFile("noNewLineEndOfFile.css"))
40+
CssCheckVerifier.issuesOnCssFile(check, getTestFile("noNewLineEndOfFile.css"))
3741
.next().withMessage("Add an empty new line at the end of this file.")
3842
.noMore();
3943
}
4044

45+
@Test
46+
public void should_not_raise_any_issue_on_empty_file() {
47+
CssCheckVerifier.issuesOnCssFile(check, getTestFile("noNewLineEndOfEmptyFile.css"))
48+
.noMore();
49+
}
50+
51+
private File getTestFile(String fileName) {
52+
return CheckTestUtils.getCommonTestFile("empty-line-end-of-file/" + fileName);
53+
}
54+
4155
}

css-checks/src/test/resources/checks/common/empty-line-end-of-file/noNewLineEndOfEmptyFile.css

Whitespace-only changes.

its/ruling/projects/custom/common/empty-line-end-of-file/noNewLineEndOfEmptyFile.css

Whitespace-only changes.

its/ruling/tests/src/test/expected/css-empty-line-end-of-file.json

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,7 @@
22
'project:custom/common/comment-regular-expression/commentRegularExpression.css':[
33
0,
44
],
5-
'project:custom/common/empty-stylesheet/emptyStylesheet.css':[
6-
0,
7-
],
8-
'project:custom/common/empty-stylesheet/emptyStylesheetNoComment.css':[
5+
'project:custom/common/empty-line-end-of-file/noNewLineEndOfFile.css':[
96
0,
107
],
118
'project:custom/common/lineLength.css':[
@@ -20,9 +17,6 @@
2017
'project:custom/common/noDuplication.css':[
2118
0,
2219
],
23-
'project:custom/common/noNewLineEndOfFile.css':[
24-
0,
25-
],
2620
'project:custom/common/properties/css/alignment-baseline.css':[
2721
0,
2822
],

0 commit comments

Comments
 (0)