Skip to content
This repository has been archived by the owner on Feb 21, 2020. It is now read-only.

Commit

Permalink
Merge pull request #6 from racodond/issue_5_do_not_rely_anymore_on_pu…
Browse files Browse the repository at this point in the history
…ppet_lint_to_track_trailing_whitespaces

Issue 5 - Do not rely anymore on Puppet Lint to track trailing whitespaces
  • Loading branch information
iwarapter committed Jul 18, 2015
2 parents 97982ac + 2a5df74 commit f72938a
Show file tree
Hide file tree
Showing 6 changed files with 136 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ public static List<Class> getChecks() {
LineLengthCheck.class,
ParsingErrorCheck.class,
QuotedBooleanCheck.class,
TrailingWhitespaceCheck.class,
UserResourceLiteralNameCheck.class,
UserResourcePasswordNotSetCheck.class,
XPathCheck.class
Expand Down
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);
}
}
}
}
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>
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 puppet-checks/src/test/resources/checks/trailingWhitespace.pp
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
notice('hello')
notice('hello')
notice('hello')
notice('hello')
/* Comments */
/* Comments */
Original file line number Diff line number Diff line change
Expand Up @@ -51,9 +51,12 @@ file { '/tmp/foo':
<name><![CDATA[Trailing Whitespace]]></name>
<configKey>trailing_whitespace</configKey>
<description>
<![CDATA[Your manifests must not contain any trailing whitespace on any line.]]>
<![CDATA[Your manifests must not contain any trailing whitespace on any line.
<p>This rule is deprecated, use <a href="coding_rules#rule_key=puppet:S1131">S1131</a> instead.</p>
]]>
</description>
<priority>MINOR</priority>
<status>DEPRECATED</status>
</rule>
<rule>
<key>80chars</key>
Expand Down

0 comments on commit f72938a

Please sign in to comment.