forked from kalidasya/sonar-css
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #148 from SonarCommunity/feature/Add_rule_BOM_in_U…
…TF8_files Add rule: use of BOM for UTF-8 files
- Loading branch information
Showing
12 changed files
with
157 additions
and
3 deletions.
There are no files selected for viewing
66 changes: 66 additions & 0 deletions
66
css-checks/src/main/java/org/sonar/css/checks/BOMCheck.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
/* | ||
* SonarQube CSS Plugin | ||
* Copyright (C) 2013 Tamas Kende and David RACODON | ||
* kende.tamas@gmail.com | ||
* | ||
* This program is free software; you can redistribute it and/or | ||
* modify it under the terms of the GNU Lesser General Public | ||
* License as published by the Free Software Foundation; either | ||
* version 3 of the License, or (at your option) any later version. | ||
* | ||
* This program is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||
* Lesser General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU Lesser General Public | ||
* License along with this program; if not, write to the Free Software | ||
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02 | ||
*/ | ||
package org.sonar.css.checks; | ||
|
||
import com.google.common.base.Charsets; | ||
import com.sonar.sslr.api.AstNode; | ||
|
||
import java.nio.charset.Charset; | ||
|
||
import org.sonar.api.server.rule.RulesDefinition; | ||
import org.sonar.check.Priority; | ||
import org.sonar.check.Rule; | ||
import org.sonar.css.CharsetAwareVisitor; | ||
import org.sonar.css.parser.CssGrammar; | ||
import org.sonar.squidbridge.annotations.ActivatedByDefault; | ||
import org.sonar.squidbridge.annotations.SqaleConstantRemediation; | ||
import org.sonar.squidbridge.annotations.SqaleSubCharacteristic; | ||
import org.sonar.squidbridge.checks.SquidCheck; | ||
|
||
@Rule( | ||
key = "bom-utf8-files", | ||
name = "BOM should not be used for UTF-8 files", | ||
priority = Priority.MAJOR, | ||
tags = {Tags.PITFALL}) | ||
@SqaleSubCharacteristic(RulesDefinition.SubCharacteristics.SOFTWARE_RELATED_PORTABILITY) | ||
@SqaleConstantRemediation("5min") | ||
@ActivatedByDefault | ||
public class BOMCheck extends SquidCheck implements CharsetAwareVisitor { | ||
|
||
private Charset charset; | ||
|
||
@Override | ||
public void setCharset(Charset charset) { | ||
this.charset = charset; | ||
} | ||
|
||
@Override | ||
public void init() { | ||
if (charset.equals(Charsets.UTF_8)) { | ||
subscribeTo(CssGrammar.BOM); | ||
} | ||
} | ||
|
||
@Override | ||
public void visitNode(AstNode node) { | ||
getContext().createFileViolation(this, "Remove the BOM."); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
5 changes: 5 additions & 0 deletions
5
css-checks/src/main/resources/org/sonar/l10n/css/rules/css/bom-utf8-files.html
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
<p>As stated in the Unicode specifications, use of a BOM is neither required nor recommended for UTF-8 files.</p> | ||
<h2>See</h2> | ||
<ul> | ||
<li><a href="http://www.unicode.org/versions/Unicode8.0.0/ch02.pdf">Unicode Specifications (2.6 Encoding Schemes)</a></li> | ||
</ul> |
71 changes: 71 additions & 0 deletions
71
css-checks/src/test/java/org/sonar/css/checks/BOMCheckTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
/* | ||
* SonarQube CSS Plugin | ||
* Copyright (C) 2013 Tamas Kende and David RACODON | ||
* kende.tamas@gmail.com | ||
* | ||
* This program is free software; you can redistribute it and/or | ||
* modify it under the terms of the GNU Lesser General Public | ||
* License as published by the Free Software Foundation; either | ||
* version 3 of the License, or (at your option) any later version. | ||
* | ||
* This program is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||
* Lesser General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU Lesser General Public | ||
* License along with this program; if not, write to the Free Software | ||
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02 | ||
*/ | ||
package org.sonar.css.checks; | ||
|
||
import com.google.common.base.Charsets; | ||
|
||
import java.io.File; | ||
|
||
import org.junit.Test; | ||
import org.sonar.css.CssAstScanner; | ||
import org.sonar.css.CssConfiguration; | ||
import org.sonar.squidbridge.api.SourceFile; | ||
import org.sonar.squidbridge.checks.CheckMessagesVerifier; | ||
|
||
public class BOMCheckTest { | ||
|
||
@Test | ||
public void should_find_that_the_UTF8_file_starts_with_a_BOM_and_raise_an_issue() { | ||
SourceFile file = CssAstScanner.scanSingleFile( | ||
new File("src/test/resources/checks/utf8WithBom.css"), | ||
new BOMCheck()); | ||
|
||
CheckMessagesVerifier.verify(file.getCheckMessages()).next().withMessage("Remove the BOM.").noMore(); | ||
} | ||
|
||
@Test | ||
public void should_find_that_the_UTF8_file_does_not_start_with_a_BOM_and_not_raise_any_issue() { | ||
SourceFile file = CssAstScanner.scanSingleFile( | ||
new File("src/test/resources/checks/unknownFunctions.css"), | ||
new BOMCheck()); | ||
|
||
CheckMessagesVerifier.verify(file.getCheckMessages()).noMore(); | ||
} | ||
|
||
@Test | ||
public void should_find_that_the_UTF16_files_start_with_a_BOM_but_not_raise_any_issue() { | ||
SourceFile file; | ||
|
||
file = CssAstScanner.scanSingleFileWithCustomConfiguration( | ||
new File("src/test/resources/checks/utf16BE.css"), | ||
new CssConfiguration(Charsets.UTF_16BE), | ||
new BOMCheck()); | ||
|
||
CheckMessagesVerifier.verify(file.getCheckMessages()).noMore(); | ||
|
||
file = CssAstScanner.scanSingleFileWithCustomConfiguration( | ||
new File("src/test/resources/checks/utf16LE.css"), | ||
new CssConfiguration(Charsets.UTF_16LE), | ||
new BOMCheck()); | ||
|
||
CheckMessagesVerifier.verify(file.getCheckMessages()).noMore(); | ||
} | ||
|
||
} |
Binary file not shown.
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
.mybox { | ||
margin-left: 10px; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Binary file added
BIN
+70 Bytes
sonar-css-plugin/src/test/resources/org/sonar/plugins/css/cssProject/css/utf16BE.css
Binary file not shown.
Binary file added
BIN
+70 Bytes
sonar-css-plugin/src/test/resources/org/sonar/plugins/css/cssProject/css/utf16LE.css
Binary file not shown.
3 changes: 3 additions & 0 deletions
3
sonar-css-plugin/src/test/resources/org/sonar/plugins/css/cssProject/css/utf8WithBom.css
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
.mybox { | ||
margin-left: 10px; | ||
} |