This repository has been archived by the owner on Feb 21, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 99
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 #6 from racodond/issue_5_do_not_rely_anymore_on_pu…
…ppet_lint_to_track_trailing_whitespaces Issue 5 - Do not rely anymore on Puppet Lint to track trailing whitespaces
- Loading branch information
Showing
6 changed files
with
136 additions
and
1 deletion.
There are no files selected for viewing
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
76 changes: 76 additions & 0 deletions
76
puppet-checks/src/main/java/com/iadams/sonarqube/puppet/checks/TrailingWhitespaceCheck.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,76 @@ | ||
/** | ||
* Sonar Puppet Plugin | ||
* The MIT License (MIT) | ||
* | ||
* Copyright (c) 2015 Iain Adams | ||
* | ||
* Permission is hereby granted, free of charge, to any person obtaining a copy | ||
* of this software and associated documentation files (the "Software"), to deal | ||
* in the Software without restriction, including without limitation the rights | ||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
* copies of the Software, and to permit persons to whom the Software is | ||
* furnished to do so, subject to the following conditions: | ||
* | ||
* The above copyright notice and this permission notice shall be included in all | ||
* copies or substantial portions of the Software. | ||
* | ||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
* SOFTWARE. | ||
*/ | ||
package com.iadams.sonarqube.puppet.checks; | ||
|
||
import com.google.common.io.Files; | ||
import com.iadams.sonarqube.puppet.CharsetAwareVisitor; | ||
import com.sonar.sslr.api.AstNode; | ||
import org.sonar.api.server.rule.RulesDefinition; | ||
import org.sonar.api.utils.SonarException; | ||
import org.sonar.check.Priority; | ||
import org.sonar.check.Rule; | ||
import org.sonar.squidbridge.annotations.SqaleConstantRemediation; | ||
import org.sonar.squidbridge.annotations.SqaleSubCharacteristic; | ||
import org.sonar.squidbridge.checks.SquidCheck; | ||
import org.sonar.sslr.parser.LexerlessGrammar; | ||
|
||
import java.io.IOException; | ||
import java.nio.charset.Charset; | ||
import java.util.List; | ||
import java.util.regex.Pattern; | ||
|
||
@Rule( | ||
key = "S1131", | ||
name = "Lines should not end with trailing whitespaces", | ||
priority = Priority.MINOR, | ||
tags = {Tags.CONVENTION}) | ||
@SqaleSubCharacteristic(RulesDefinition.SubCharacteristics.READABILITY) | ||
@SqaleConstantRemediation("1min") | ||
public class TrailingWhitespaceCheck extends SquidCheck<LexerlessGrammar> implements CharsetAwareVisitor { | ||
|
||
private static final String WHITESPACE = "\\t\\u000B\\f\\u0020\\u00A0\\uFEFF\\p{Zs}"; | ||
private Charset charset; | ||
|
||
@Override | ||
public void setCharset(Charset charset) { | ||
this.charset = charset; | ||
} | ||
|
||
@Override | ||
public void visitFile(AstNode astNode) { | ||
List<String> lines; | ||
try { | ||
lines = Files.readLines(getContext().getFile(), charset); | ||
} catch (IOException e) { | ||
throw new SonarException(e); | ||
} | ||
for (int i = 0; i < lines.size(); i++) { | ||
String line = lines.get(i); | ||
if (line.length() > 0 && Pattern.matches("[" + WHITESPACE + "]", line.subSequence(line.length() - 1, line.length()))) { | ||
getContext().createLineViolation(this, "Remove the useless trailing whitespaces at the end of this line.", i + 1); | ||
} | ||
} | ||
} | ||
} |
1 change: 1 addition & 0 deletions
1
puppet-checks/src/main/resources/org/sonar/l10n/pp/rules/puppet/S1131.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 @@ | ||
<p>Trailing whitespaces are simply useless and should not stay in code. They may generate noise when comparing different versions of the same file. If you encounter issues from this rule, this probably means that you are not using an automated code formatter - which you should if you have the opportunity to do so.</p> |
48 changes: 48 additions & 0 deletions
48
...cks/src/test/groovy/com/iadams/sonarqube/puppet/checks/TrailingWhitespaceCheckSpec.groovy
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,48 @@ | ||
/* | ||
* Sonar Puppet Plugin | ||
* The MIT License (MIT) | ||
* | ||
* Copyright (c) 2015 Iain Adams | ||
* | ||
* Permission is hereby granted, free of charge, to any person obtaining a copy | ||
* of this software and associated documentation files (the "Software"), to deal | ||
* in the Software without restriction, including without limitation the rights | ||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
* copies of the Software, and to permit persons to whom the Software is | ||
* furnished to do so, subject to the following conditions: | ||
* | ||
* The above copyright notice and this permission notice shall be included in all | ||
* copies or substantial portions of the Software. | ||
* | ||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
* SOFTWARE. | ||
*/ | ||
package com.iadams.sonarqube.puppet.checks | ||
|
||
import com.iadams.sonarqube.puppet.PuppetAstScanner | ||
import org.sonar.squidbridge.api.SourceFile | ||
import org.sonar.squidbridge.checks.CheckMessagesVerifier | ||
import spock.lang.Specification | ||
|
||
class TrailingWhitespaceCheckSpec extends Specification { | ||
|
||
def "validate rule"() { | ||
given: | ||
TrailingWhitespaceCheck check = new TrailingWhitespaceCheck(); | ||
|
||
SourceFile file = PuppetAstScanner.scanSingleFile(new File("src/test/resources/checks/trailingWhitespace.pp"), check); | ||
|
||
expect: | ||
CheckMessagesVerifier.verify(file.getCheckMessages()) | ||
.next().atLine(2).withMessage("Remove the useless trailing whitespaces at the end of this line.") | ||
.next().atLine(3).withMessage("Remove the useless trailing whitespaces at the end of this line.") | ||
.next().atLine(4).withMessage("Remove the useless trailing whitespaces at the end of this line.") | ||
.next().atLine(6).withMessage("Remove the useless trailing whitespaces at the end of this line.") | ||
.noMore(); | ||
} | ||
} |
6 changes: 6 additions & 0 deletions
6
puppet-checks/src/test/resources/checks/trailingWhitespace.pp
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,6 @@ | ||
notice('hello') | ||
notice('hello') | ||
notice('hello') | ||
notice('hello') | ||
/* Comments */ | ||
/* Comments */ |
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