Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit bd2fa54

Browse files
committedApr 25, 2018
Fix #102 Add parameter to 'experimental-properties-usage' rules to flag some properties as non-experimental
1 parent cdd4e60 commit bd2fa54

16 files changed

+143
-6
lines changed
 

‎css-checks/src/main/java/org/sonar/css/checks/common/ExperimentalPropertyCheck.java

Lines changed: 39 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,14 +19,21 @@
1919
*/
2020
package org.sonar.css.checks.common;
2121

22+
import com.google.common.annotations.VisibleForTesting;
2223
import org.sonar.check.Priority;
2324
import org.sonar.check.Rule;
25+
import org.sonar.check.RuleProperty;
26+
import org.sonar.css.checks.CheckList;
27+
import org.sonar.css.checks.CheckUtils;
2428
import org.sonar.css.checks.Tags;
2529
import org.sonar.plugins.css.api.tree.css.PropertyTree;
2630
import org.sonar.plugins.css.api.visitors.DoubleDispatchVisitorCheck;
2731
import org.sonar.squidbridge.annotations.ActivatedByDefault;
2832
import org.sonar.squidbridge.annotations.SqaleConstantRemediation;
2933

34+
import java.util.regex.Pattern;
35+
import java.util.regex.PatternSyntaxException;
36+
3037
@Rule(
3138
key = "experimental-property-usage",
3239
name = "Experimental properties should not be used",
@@ -36,14 +43,45 @@
3643
@ActivatedByDefault
3744
public class ExperimentalPropertyCheck extends DoubleDispatchVisitorCheck {
3845

46+
private static final String DEFAULT_PROPERTIES_TO_EXCLUDE = "";
47+
48+
@RuleProperty(
49+
key = "propertiesToExclude",
50+
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.",
51+
defaultValue = DEFAULT_PROPERTIES_TO_EXCLUDE)
52+
private String propertiesToExclude = DEFAULT_PROPERTIES_TO_EXCLUDE;
53+
3954
@Override
4055
public void visitProperty(PropertyTree tree) {
41-
if (!tree.isScssNamespace() && (tree.isVendorPrefixed() || tree.standardProperty().isExperimental())) {
56+
if (!tree.isScssNamespace()
57+
&& !tree.standardProperty().getName().matches(propertiesToExclude)
58+
&& (tree.isVendorPrefixed() || tree.standardProperty().isExperimental())) {
4259
addPreciseIssue(
4360
tree,
4461
"Remove this usage of the experimental \"" + tree.standardProperty().getName() + "\" property.");
4562
}
4663
super.visitProperty(tree);
4764
}
4865

66+
@Override
67+
public void validateParameters() {
68+
try {
69+
Pattern.compile(propertiesToExclude);
70+
} catch (PatternSyntaxException exception) {
71+
throw new IllegalStateException(paramsErrorMessage(), exception);
72+
}
73+
}
74+
75+
@VisibleForTesting
76+
void setPropertiesToExclude(String propertiesToExclude) {
77+
this.propertiesToExclude = propertiesToExclude;
78+
}
79+
80+
private String paramsErrorMessage() {
81+
return CheckUtils.paramsErrorMessage(
82+
this.getClass(),
83+
CheckList.CSS_REPOSITORY_KEY,
84+
"propertiesToExclude parameter \"" + propertiesToExclude + "\" is not a valid regular expression.");
85+
}
86+
4987
}

‎css-checks/src/test/java/org/sonar/css/checks/common/ExperimentalPropertyCheckTest.java

Lines changed: 38 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,23 +25,56 @@
2525

2626
import java.io.File;
2727

28-
public class ExperimentalPropertyCheckTest {
28+
import static org.fest.assertions.Assertions.assertThat;
2929

30-
private ExperimentalPropertyCheck check = new ExperimentalPropertyCheck();
30+
public class ExperimentalPropertyCheckTest {
3131

3232
@Test
3333
public void test_css() {
34-
CssCheckVerifier.verifyCssFile(check, getTestFile("experimentalPropertyUsage.css"));
34+
CssCheckVerifier.verifyCssFile(new ExperimentalPropertyCheck(), getTestFile("experimentalPropertyUsage.css"));
35+
}
36+
37+
@Test
38+
public void test_css_exclude_properties() {
39+
ExperimentalPropertyCheck check = new ExperimentalPropertyCheck();
40+
check.setPropertiesToExclude("user-select|voice.*");
41+
CssCheckVerifier.verifyCssFile(check, getTestFile("experimentalPropertyUsageExcludeProperties.css"));
3542
}
3643

3744
@Test
3845
public void test_less() {
39-
CssCheckVerifier.verifyLessFile(check, getTestFile("experimentalPropertyUsage.less"));
46+
CssCheckVerifier.verifyLessFile(new ExperimentalPropertyCheck(), getTestFile("experimentalPropertyUsage.less"));
47+
}
48+
49+
@Test
50+
public void test_less_exclude_properties() {
51+
ExperimentalPropertyCheck check = new ExperimentalPropertyCheck();
52+
check.setPropertiesToExclude("user-select|voice.*");
53+
CssCheckVerifier.verifyCssFile(check, getTestFile("experimentalPropertyUsageExcludeProperties.less"));
4054
}
4155

4256
@Test
4357
public void test_scss() {
44-
CssCheckVerifier.verifyScssFile(check, getTestFile("experimentalPropertyUsage.scss"));
58+
CssCheckVerifier.verifyScssFile(new ExperimentalPropertyCheck(), getTestFile("experimentalPropertyUsage.scss"));
59+
}
60+
61+
@Test
62+
public void test_scss_exclude_properties() {
63+
ExperimentalPropertyCheck check = new ExperimentalPropertyCheck();
64+
check.setPropertiesToExclude("user-select|voice.*");
65+
CssCheckVerifier.verifyCssFile(check, getTestFile("experimentalPropertyUsageExcludeProperties.scss"));
66+
}
67+
68+
@Test
69+
public void should_throw_an_illegal_state_exception_as_the_regular_expression_parameter_is_not_valid() {
70+
try {
71+
ExperimentalPropertyCheck check = new ExperimentalPropertyCheck();
72+
check.setPropertiesToExclude("(");
73+
CssCheckVerifier.issuesOnCssFile(check, getTestFile("experimentalPropertyUsage.css")).noMore();
74+
} catch (IllegalStateException e) {
75+
assertThat(e.getMessage()).isEqualTo("Check css:experimental-property-usage (Experimental properties should not be used): "
76+
+ "propertiesToExclude parameter \"(\" is not a valid regular expression.");
77+
}
4578
}
4679

4780
private File getTestFile(String fileName) {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
.mybox {
2+
border-radius: 5px;
3+
user-select: all;
4+
voice-balance: center;
5+
pause: 3s; /* Noncompliant ![sc=3;ec=8;el=+0]! !{Remove this usage of the experimental "pause" property.}! */
6+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
.mybox {
2+
border-radius: 5px;
3+
user-select: all;
4+
voice-balance: center;
5+
pause: 3s; /* Noncompliant ![sc=3;ec=8;el=+0]! !{Remove this usage of the experimental "pause" property.}! */
6+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
.mybox {
2+
border-radius: 5px;
3+
user-select: all;
4+
voice-balance: center;
5+
pause: 3s; /* Noncompliant ![sc=3;ec=8;el=+0]! !{Remove this usage of the experimental "pause" property.}! */
6+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
.mybox {
2+
border-radius: 5px;
3+
user-select: all;
4+
voice-balance: center;
5+
pause: 3s; /* Noncompliant ![sc=3;ec=8;el=+0]! !{Remove this usage of the experimental "pause" property.}! */
6+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
.mybox {
2+
border-radius: 5px;
3+
user-select: all;
4+
voice-balance: center;
5+
pause: 3s; /* Noncompliant ![sc=3;ec=8;el=+0]! !{Remove this usage of the experimental "pause" property.}! */
6+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
.mybox {
2+
border-radius: 5px;
3+
user-select: all;
4+
voice-balance: center;
5+
pause: 3s; /* Noncompliant ![sc=3;ec=8;el=+0]! !{Remove this usage of the experimental "pause" property.}! */
6+
}

‎its/ruling/tests/src/test/expected/css-alphabetize-declarations.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,9 @@
3838
'project:custom/common/emptyRule.css':[
3939
27,
4040
],
41+
'project:custom/common/experimental-property-usage/experimentalPropertyUsageExcludeProperties.css':[
42+
1,
43+
],
4144
'project:custom/common/experimentalAtRuleUsage.css':[
4245
6,
4346
19,

‎its/ruling/tests/src/test/expected/css-experimental-property-usage.json

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,11 @@
3434
21,
3535
22,
3636
],
37+
'project:custom/common/experimental-property-usage/experimentalPropertyUsageExcludeProperties.css':[
38+
3,
39+
4,
40+
5,
41+
],
3742
'project:custom/common/experimentalAtRuleUsage.css':[
3843
7,
3944
8,

‎its/ruling/tests/src/test/expected/less-alphabetize-declarations.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,9 @@
2929
50,
3030
57,
3131
],
32+
'project:custom/common/experimental-property-usage/experimentalPropertyUsageExcludeProperties.less':[
33+
1,
34+
],
3235
'project:custom/common/font-family-not-ending-with-generic-font-family/fontFamilyNotEndingWithGenericFontFamily.less':[
3336
17,
3437
],

‎its/ruling/tests/src/test/expected/less-experimental-property-usage.json

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,11 @@
3434
21,
3535
22,
3636
],
37+
'project:custom/common/experimental-property-usage/experimentalPropertyUsageExcludeProperties.less':[
38+
3,
39+
4,
40+
5,
41+
],
3742
'project:custom/common/known-properties/knownProperties.less':[
3843
12,
3944
],

‎its/ruling/tests/src/test/expected/less-prefer-single-line-comments.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,9 @@
125125
21,
126126
22,
127127
],
128+
'project:custom/common/experimental-property-usage/experimentalPropertyUsageExcludeProperties.less':[
129+
5,
130+
],
128131
'project:custom/common/experimental-pseudo-usage/experimentalPseudoUsage.less':[
129132
1,
130133
5,

‎its/ruling/tests/src/test/expected/scss-alphabetize-declarations.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,9 @@
3737
50,
3838
57,
3939
],
40+
'project:custom/common/experimental-property-usage/experimentalPropertyUsageExcludeProperties.scss':[
41+
1,
42+
],
4043
'project:custom/common/font-family-not-ending-with-generic-font-family/fontFamilyNotEndingWithGenericFontFamily.scss':[
4144
17,
4245
],

‎its/ruling/tests/src/test/expected/scss-experimental-property-usage.json

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,11 @@
3838
36,
3939
37,
4040
],
41+
'project:custom/common/experimental-property-usage/experimentalPropertyUsageExcludeProperties.scss':[
42+
3,
43+
4,
44+
5,
45+
],
4146
'project:custom/common/known-properties/knownProperties.scss':[
4247
12,
4348
],

‎its/ruling/tests/src/test/expected/scss-prefer-single-line-comments.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,9 @@
160160
36,
161161
37,
162162
],
163+
'project:custom/common/experimental-property-usage/experimentalPropertyUsageExcludeProperties.scss':[
164+
5,
165+
],
163166
'project:custom/common/experimental-pseudo-usage/experimentalPseudoUsage.scss':[
164167
1,
165168
5,

0 commit comments

Comments
 (0)
Please sign in to comment.