Skip to content

Commit 9aa2b12

Browse files
committed
Fix #102 Add parameter to 'experimental-function-usage' rules to flag some functions as non-experimental
1 parent 5b5dbd0 commit 9aa2b12

File tree

11 files changed

+91
-3
lines changed

11 files changed

+91
-3
lines changed

css-checks/src/main/java/org/sonar/css/checks/css/ExperimentalCssFunctionCheck.java

Lines changed: 40 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,14 +19,21 @@
1919
*/
2020
package org.sonar.css.checks.css;
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.FunctionTree;
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-function-usage",
3239
name = "Experimental functions should not be used",
@@ -36,14 +43,46 @@
3643
@ActivatedByDefault
3744
public class ExperimentalCssFunctionCheck extends DoubleDispatchVisitorCheck {
3845

46+
private static final String DEFAULT_FUNCTIONS_TO_EXCLUDE = "";
47+
48+
@RuleProperty(
49+
key = "functionsToExclude",
50+
description = "A regular expression to exclude functions from being considered as experimental. Example: \"conic-gradient|count.*\". See " + CheckUtils.LINK_TO_JAVA_REGEX_PATTERN_DOC + " for detailed regular expression syntax.",
51+
defaultValue = DEFAULT_FUNCTIONS_TO_EXCLUDE)
52+
private String functionsToExclude = DEFAULT_FUNCTIONS_TO_EXCLUDE;
53+
3954
@Override
4055
public void visitFunction(FunctionTree tree) {
41-
if (tree.standardFunction().isCss() && (tree.isVendorPrefixed() || tree.standardFunction().isExperimental())) {
56+
if (tree.standardFunction().isCss()
57+
&& !tree.standardFunction().getName().matches(functionsToExclude)
58+
&& (tree.isVendorPrefixed() || tree.standardFunction().isExperimental())) {
4259
addPreciseIssue(
4360
tree.function(),
4461
"Remove this usage of the experimental \"" + tree.standardFunction().getName() + "\" function.");
4562
}
4663
super.visitFunction(tree);
4764
}
4865

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

css-checks/src/test/java/org/sonar/css/checks/css/ExperimentalCssFunctionCheckTest.java

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,13 +21,35 @@
2121

2222
import org.junit.Test;
2323
import org.sonar.css.checks.CheckTestUtils;
24+
import org.sonar.css.checks.common.ExperimentalPseudoCheck;
2425
import org.sonar.css.checks.verifier.CssCheckVerifier;
2526

27+
import static org.fest.assertions.Assertions.assertThat;
28+
2629
public class ExperimentalCssFunctionCheckTest {
2730

2831
@Test
2932
public void test() {
3033
CssCheckVerifier.verifyCssFile(new ExperimentalCssFunctionCheck(), CheckTestUtils.getCssTestFile("experimentalFunctionUsage.css"));
3134
}
3235

36+
@Test
37+
public void test_exclude_functions() {
38+
ExperimentalCssFunctionCheck check = new ExperimentalCssFunctionCheck();
39+
check.setFunctionsToExclude("conic-gradient|count.*");
40+
CssCheckVerifier.verifyCssFile(check, CheckTestUtils.getCssTestFile("experimentalFunctionUsageExcludeFunctions.css"));
41+
}
42+
43+
@Test
44+
public void should_throw_an_illegal_state_exception_as_the_functionsToExclude_parameter_is_not_valid() {
45+
try {
46+
ExperimentalCssFunctionCheck check = new ExperimentalCssFunctionCheck();
47+
check.setFunctionsToExclude("(");
48+
CssCheckVerifier.issuesOnCssFile(check, CheckTestUtils.getCssTestFile("experimentalFunctionUsage.css")).noMore();
49+
} catch (IllegalStateException e) {
50+
assertThat(e.getMessage()).isEqualTo("Check css:experimental-function-usage (Experimental functions should not be used): "
51+
+ "functionsToExclude parameter \"(\" is not a valid regular expression.");
52+
}
53+
}
54+
3355
}

css-checks/src/test/resources/checks/css/experimentalFunctionUsage.css

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,4 +18,3 @@
1818
@document url-prefix("http://www.w3.org/Style/CSS/") { /* Noncompliant */
1919
#summary { background: yellow; color: black}
2020
}
21-
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
.mybox {
2+
background: conic-gradient(#f06, rgba(0,0,0,.5));
3+
width: counter(1);
4+
abc: supports(); /* Noncompliant */
5+
}

its/ruling/projects/custom/css/experimentalFunctionUsage.css

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,4 +18,3 @@
1818
@document url-prefix("http://www.w3.org/Style/CSS/") { /* Noncompliant */
1919
#summary { background: yellow; color: black}
2020
}
21-
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
.mybox {
2+
background: conic-gradient(#f06, rgba(0,0,0,.5));
3+
width: counter(1);
4+
abc: supports(); /* Noncompliant */
5+
}

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,9 @@
143143
'project:custom/css/experimentalFunctionUsage.css':[
144144
1,
145145
],
146+
'project:custom/css/experimentalFunctionUsageExcludeFunctions.css':[
147+
1,
148+
],
146149
'project:custom/css/unknownFunctions.css':[
147150
1,
148151
],

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,11 @@
7272
14,
7373
18,
7474
],
75+
'project:custom/css/experimentalFunctionUsageExcludeFunctions.css':[
76+
2,
77+
3,
78+
4,
79+
],
7580
'project:custom/css/obsoleteFunctions.css':[
7681
7,
7782
8,

its/ruling/tests/src/test/expected/css-formatting.json

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -383,6 +383,11 @@
383383
19,
384384
19,
385385
],
386+
'project:custom/css/experimentalFunctionUsageExcludeFunctions.css':[
387+
2,
388+
2,
389+
2,
390+
],
386391
'project:custom/css/obsoleteFunctions.css':[
387392
8,
388393
],

its/ruling/tests/src/test/expected/css-known-properties.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,4 +96,7 @@
9696
'project:custom/css/experimentalFunctionUsage.css':[
9797
7,
9898
],
99+
'project:custom/css/experimentalFunctionUsageExcludeFunctions.css':[
100+
4,
101+
],
99102
}

0 commit comments

Comments
 (0)