Skip to content

Commit

Permalink
Fix #102 Add parameter to 'experimental-properties-usage' rules to fl…
Browse files Browse the repository at this point in the history
…ag some properties as non-experimental
  • Loading branch information
racodond committed Apr 25, 2018
1 parent cdd4e60 commit bd2fa54
Show file tree
Hide file tree
Showing 16 changed files with 143 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,21 @@
*/
package org.sonar.css.checks.common;

import com.google.common.annotations.VisibleForTesting;
import org.sonar.check.Priority;
import org.sonar.check.Rule;
import org.sonar.check.RuleProperty;
import org.sonar.css.checks.CheckList;
import org.sonar.css.checks.CheckUtils;
import org.sonar.css.checks.Tags;
import org.sonar.plugins.css.api.tree.css.PropertyTree;
import org.sonar.plugins.css.api.visitors.DoubleDispatchVisitorCheck;
import org.sonar.squidbridge.annotations.ActivatedByDefault;
import org.sonar.squidbridge.annotations.SqaleConstantRemediation;

import java.util.regex.Pattern;
import java.util.regex.PatternSyntaxException;

@Rule(
key = "experimental-property-usage",
name = "Experimental properties should not be used",
Expand All @@ -36,14 +43,45 @@
@ActivatedByDefault
public class ExperimentalPropertyCheck extends DoubleDispatchVisitorCheck {

private static final String DEFAULT_PROPERTIES_TO_EXCLUDE = "";

@RuleProperty(
key = "propertiesToExclude",
description = "A regular expression to exclude properties from being considered as experimental. Example: \"user-select|voice.*\". See " + CheckUtils.LINK_TO_JAVA_REGEX_PATTERN_DOC + " for detailed regular expression syntax.",
defaultValue = DEFAULT_PROPERTIES_TO_EXCLUDE)
private String propertiesToExclude = DEFAULT_PROPERTIES_TO_EXCLUDE;

@Override
public void visitProperty(PropertyTree tree) {
if (!tree.isScssNamespace() && (tree.isVendorPrefixed() || tree.standardProperty().isExperimental())) {
if (!tree.isScssNamespace()
&& !tree.standardProperty().getName().matches(propertiesToExclude)
&& (tree.isVendorPrefixed() || tree.standardProperty().isExperimental())) {
addPreciseIssue(
tree,
"Remove this usage of the experimental \"" + tree.standardProperty().getName() + "\" property.");
}
super.visitProperty(tree);
}

@Override
public void validateParameters() {
try {
Pattern.compile(propertiesToExclude);
} catch (PatternSyntaxException exception) {
throw new IllegalStateException(paramsErrorMessage(), exception);
}
}

@VisibleForTesting
void setPropertiesToExclude(String propertiesToExclude) {
this.propertiesToExclude = propertiesToExclude;
}

private String paramsErrorMessage() {
return CheckUtils.paramsErrorMessage(
this.getClass(),
CheckList.CSS_REPOSITORY_KEY,
"propertiesToExclude parameter \"" + propertiesToExclude + "\" is not a valid regular expression.");
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -25,23 +25,56 @@

import java.io.File;

public class ExperimentalPropertyCheckTest {
import static org.fest.assertions.Assertions.assertThat;

private ExperimentalPropertyCheck check = new ExperimentalPropertyCheck();
public class ExperimentalPropertyCheckTest {

@Test
public void test_css() {
CssCheckVerifier.verifyCssFile(check, getTestFile("experimentalPropertyUsage.css"));
CssCheckVerifier.verifyCssFile(new ExperimentalPropertyCheck(), getTestFile("experimentalPropertyUsage.css"));
}

@Test
public void test_css_exclude_properties() {
ExperimentalPropertyCheck check = new ExperimentalPropertyCheck();
check.setPropertiesToExclude("user-select|voice.*");
CssCheckVerifier.verifyCssFile(check, getTestFile("experimentalPropertyUsageExcludeProperties.css"));
}

@Test
public void test_less() {
CssCheckVerifier.verifyLessFile(check, getTestFile("experimentalPropertyUsage.less"));
CssCheckVerifier.verifyLessFile(new ExperimentalPropertyCheck(), getTestFile("experimentalPropertyUsage.less"));
}

@Test
public void test_less_exclude_properties() {
ExperimentalPropertyCheck check = new ExperimentalPropertyCheck();
check.setPropertiesToExclude("user-select|voice.*");
CssCheckVerifier.verifyCssFile(check, getTestFile("experimentalPropertyUsageExcludeProperties.less"));
}

@Test
public void test_scss() {
CssCheckVerifier.verifyScssFile(check, getTestFile("experimentalPropertyUsage.scss"));
CssCheckVerifier.verifyScssFile(new ExperimentalPropertyCheck(), getTestFile("experimentalPropertyUsage.scss"));
}

@Test
public void test_scss_exclude_properties() {
ExperimentalPropertyCheck check = new ExperimentalPropertyCheck();
check.setPropertiesToExclude("user-select|voice.*");
CssCheckVerifier.verifyCssFile(check, getTestFile("experimentalPropertyUsageExcludeProperties.scss"));
}

@Test
public void should_throw_an_illegal_state_exception_as_the_regular_expression_parameter_is_not_valid() {
try {
ExperimentalPropertyCheck check = new ExperimentalPropertyCheck();
check.setPropertiesToExclude("(");
CssCheckVerifier.issuesOnCssFile(check, getTestFile("experimentalPropertyUsage.css")).noMore();
} catch (IllegalStateException e) {
assertThat(e.getMessage()).isEqualTo("Check css:experimental-property-usage (Experimental properties should not be used): "
+ "propertiesToExclude parameter \"(\" is not a valid regular expression.");
}
}

private File getTestFile(String fileName) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
.mybox {
border-radius: 5px;
user-select: all;
voice-balance: center;
pause: 3s; /* Noncompliant ![sc=3;ec=8;el=+0]! !{Remove this usage of the experimental "pause" property.}! */
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
.mybox {
border-radius: 5px;
user-select: all;
voice-balance: center;
pause: 3s; /* Noncompliant ![sc=3;ec=8;el=+0]! !{Remove this usage of the experimental "pause" property.}! */
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
.mybox {
border-radius: 5px;
user-select: all;
voice-balance: center;
pause: 3s; /* Noncompliant ![sc=3;ec=8;el=+0]! !{Remove this usage of the experimental "pause" property.}! */
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
.mybox {
border-radius: 5px;
user-select: all;
voice-balance: center;
pause: 3s; /* Noncompliant ![sc=3;ec=8;el=+0]! !{Remove this usage of the experimental "pause" property.}! */
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
.mybox {
border-radius: 5px;
user-select: all;
voice-balance: center;
pause: 3s; /* Noncompliant ![sc=3;ec=8;el=+0]! !{Remove this usage of the experimental "pause" property.}! */
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
.mybox {
border-radius: 5px;
user-select: all;
voice-balance: center;
pause: 3s; /* Noncompliant ![sc=3;ec=8;el=+0]! !{Remove this usage of the experimental "pause" property.}! */
}
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,9 @@
'project:custom/common/emptyRule.css':[
27,
],
'project:custom/common/experimental-property-usage/experimentalPropertyUsageExcludeProperties.css':[
1,
],
'project:custom/common/experimentalAtRuleUsage.css':[
6,
19,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,11 @@
21,
22,
],
'project:custom/common/experimental-property-usage/experimentalPropertyUsageExcludeProperties.css':[
3,
4,
5,
],
'project:custom/common/experimentalAtRuleUsage.css':[
7,
8,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,9 @@
50,
57,
],
'project:custom/common/experimental-property-usage/experimentalPropertyUsageExcludeProperties.less':[
1,
],
'project:custom/common/font-family-not-ending-with-generic-font-family/fontFamilyNotEndingWithGenericFontFamily.less':[
17,
],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,11 @@
21,
22,
],
'project:custom/common/experimental-property-usage/experimentalPropertyUsageExcludeProperties.less':[
3,
4,
5,
],
'project:custom/common/known-properties/knownProperties.less':[
12,
],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,9 @@
21,
22,
],
'project:custom/common/experimental-property-usage/experimentalPropertyUsageExcludeProperties.less':[
5,
],
'project:custom/common/experimental-pseudo-usage/experimentalPseudoUsage.less':[
1,
5,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,9 @@
50,
57,
],
'project:custom/common/experimental-property-usage/experimentalPropertyUsageExcludeProperties.scss':[
1,
],
'project:custom/common/font-family-not-ending-with-generic-font-family/fontFamilyNotEndingWithGenericFontFamily.scss':[
17,
],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,11 @@
36,
37,
],
'project:custom/common/experimental-property-usage/experimentalPropertyUsageExcludeProperties.scss':[
3,
4,
5,
],
'project:custom/common/known-properties/knownProperties.scss':[
12,
],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,9 @@
36,
37,
],
'project:custom/common/experimental-property-usage/experimentalPropertyUsageExcludeProperties.scss':[
5,
],
'project:custom/common/experimental-pseudo-usage/experimentalPseudoUsage.scss':[
1,
5,
Expand Down

0 comments on commit bd2fa54

Please sign in to comment.