From bd2fa543c1fc708f54d982ab85983ffb82fb322a Mon Sep 17 00:00:00 2001 From: David RACODON Date: Wed, 25 Apr 2018 12:23:36 +0200 Subject: [PATCH] Fix #102 Add parameter to 'experimental-properties-usage' rules to flag some properties as non-experimental --- .../common/ExperimentalPropertyCheck.java | 40 ++++++++++++++++- .../common/ExperimentalPropertyCheckTest.java | 43 ++++++++++++++++--- ...rimentalPropertyUsageExcludeProperties.css | 6 +++ ...imentalPropertyUsageExcludeProperties.less | 6 +++ ...imentalPropertyUsageExcludeProperties.scss | 6 +++ ...rimentalPropertyUsageExcludeProperties.css | 6 +++ ...imentalPropertyUsageExcludeProperties.less | 6 +++ ...imentalPropertyUsageExcludeProperties.scss | 6 +++ .../css-alphabetize-declarations.json | 3 ++ .../css-experimental-property-usage.json | 5 +++ .../less-alphabetize-declarations.json | 3 ++ .../less-experimental-property-usage.json | 5 +++ .../less-prefer-single-line-comments.json | 3 ++ .../scss-alphabetize-declarations.json | 3 ++ .../scss-experimental-property-usage.json | 5 +++ .../scss-prefer-single-line-comments.json | 3 ++ 16 files changed, 143 insertions(+), 6 deletions(-) create mode 100644 css-checks/src/test/resources/checks/common/experimental-property-usage/experimentalPropertyUsageExcludeProperties.css create mode 100644 css-checks/src/test/resources/checks/common/experimental-property-usage/experimentalPropertyUsageExcludeProperties.less create mode 100644 css-checks/src/test/resources/checks/common/experimental-property-usage/experimentalPropertyUsageExcludeProperties.scss create mode 100644 its/ruling/projects/custom/common/experimental-property-usage/experimentalPropertyUsageExcludeProperties.css create mode 100644 its/ruling/projects/custom/common/experimental-property-usage/experimentalPropertyUsageExcludeProperties.less create mode 100644 its/ruling/projects/custom/common/experimental-property-usage/experimentalPropertyUsageExcludeProperties.scss diff --git a/css-checks/src/main/java/org/sonar/css/checks/common/ExperimentalPropertyCheck.java b/css-checks/src/main/java/org/sonar/css/checks/common/ExperimentalPropertyCheck.java index 140d6559..32182ad5 100644 --- a/css-checks/src/main/java/org/sonar/css/checks/common/ExperimentalPropertyCheck.java +++ b/css-checks/src/main/java/org/sonar/css/checks/common/ExperimentalPropertyCheck.java @@ -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", @@ -36,9 +43,19 @@ @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."); @@ -46,4 +63,25 @@ public void visitProperty(PropertyTree tree) { 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."); + } + } diff --git a/css-checks/src/test/java/org/sonar/css/checks/common/ExperimentalPropertyCheckTest.java b/css-checks/src/test/java/org/sonar/css/checks/common/ExperimentalPropertyCheckTest.java index c5646885..9d02c90d 100644 --- a/css-checks/src/test/java/org/sonar/css/checks/common/ExperimentalPropertyCheckTest.java +++ b/css-checks/src/test/java/org/sonar/css/checks/common/ExperimentalPropertyCheckTest.java @@ -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) { diff --git a/css-checks/src/test/resources/checks/common/experimental-property-usage/experimentalPropertyUsageExcludeProperties.css b/css-checks/src/test/resources/checks/common/experimental-property-usage/experimentalPropertyUsageExcludeProperties.css new file mode 100644 index 00000000..b217b09f --- /dev/null +++ b/css-checks/src/test/resources/checks/common/experimental-property-usage/experimentalPropertyUsageExcludeProperties.css @@ -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.}! */ +} diff --git a/css-checks/src/test/resources/checks/common/experimental-property-usage/experimentalPropertyUsageExcludeProperties.less b/css-checks/src/test/resources/checks/common/experimental-property-usage/experimentalPropertyUsageExcludeProperties.less new file mode 100644 index 00000000..b217b09f --- /dev/null +++ b/css-checks/src/test/resources/checks/common/experimental-property-usage/experimentalPropertyUsageExcludeProperties.less @@ -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.}! */ +} diff --git a/css-checks/src/test/resources/checks/common/experimental-property-usage/experimentalPropertyUsageExcludeProperties.scss b/css-checks/src/test/resources/checks/common/experimental-property-usage/experimentalPropertyUsageExcludeProperties.scss new file mode 100644 index 00000000..b217b09f --- /dev/null +++ b/css-checks/src/test/resources/checks/common/experimental-property-usage/experimentalPropertyUsageExcludeProperties.scss @@ -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.}! */ +} diff --git a/its/ruling/projects/custom/common/experimental-property-usage/experimentalPropertyUsageExcludeProperties.css b/its/ruling/projects/custom/common/experimental-property-usage/experimentalPropertyUsageExcludeProperties.css new file mode 100644 index 00000000..b217b09f --- /dev/null +++ b/its/ruling/projects/custom/common/experimental-property-usage/experimentalPropertyUsageExcludeProperties.css @@ -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.}! */ +} diff --git a/its/ruling/projects/custom/common/experimental-property-usage/experimentalPropertyUsageExcludeProperties.less b/its/ruling/projects/custom/common/experimental-property-usage/experimentalPropertyUsageExcludeProperties.less new file mode 100644 index 00000000..b217b09f --- /dev/null +++ b/its/ruling/projects/custom/common/experimental-property-usage/experimentalPropertyUsageExcludeProperties.less @@ -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.}! */ +} diff --git a/its/ruling/projects/custom/common/experimental-property-usage/experimentalPropertyUsageExcludeProperties.scss b/its/ruling/projects/custom/common/experimental-property-usage/experimentalPropertyUsageExcludeProperties.scss new file mode 100644 index 00000000..b217b09f --- /dev/null +++ b/its/ruling/projects/custom/common/experimental-property-usage/experimentalPropertyUsageExcludeProperties.scss @@ -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.}! */ +} diff --git a/its/ruling/tests/src/test/expected/css-alphabetize-declarations.json b/its/ruling/tests/src/test/expected/css-alphabetize-declarations.json index 9596c13a..a17aedff 100644 --- a/its/ruling/tests/src/test/expected/css-alphabetize-declarations.json +++ b/its/ruling/tests/src/test/expected/css-alphabetize-declarations.json @@ -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, diff --git a/its/ruling/tests/src/test/expected/css-experimental-property-usage.json b/its/ruling/tests/src/test/expected/css-experimental-property-usage.json index d8eb05d6..9327b25b 100644 --- a/its/ruling/tests/src/test/expected/css-experimental-property-usage.json +++ b/its/ruling/tests/src/test/expected/css-experimental-property-usage.json @@ -34,6 +34,11 @@ 21, 22, ], +'project:custom/common/experimental-property-usage/experimentalPropertyUsageExcludeProperties.css':[ +3, +4, +5, +], 'project:custom/common/experimentalAtRuleUsage.css':[ 7, 8, diff --git a/its/ruling/tests/src/test/expected/less-alphabetize-declarations.json b/its/ruling/tests/src/test/expected/less-alphabetize-declarations.json index fcbadd2e..1b8e7972 100644 --- a/its/ruling/tests/src/test/expected/less-alphabetize-declarations.json +++ b/its/ruling/tests/src/test/expected/less-alphabetize-declarations.json @@ -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, ], diff --git a/its/ruling/tests/src/test/expected/less-experimental-property-usage.json b/its/ruling/tests/src/test/expected/less-experimental-property-usage.json index 03e2ac51..34626256 100644 --- a/its/ruling/tests/src/test/expected/less-experimental-property-usage.json +++ b/its/ruling/tests/src/test/expected/less-experimental-property-usage.json @@ -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, ], diff --git a/its/ruling/tests/src/test/expected/less-prefer-single-line-comments.json b/its/ruling/tests/src/test/expected/less-prefer-single-line-comments.json index 61b4762f..2938dcfb 100644 --- a/its/ruling/tests/src/test/expected/less-prefer-single-line-comments.json +++ b/its/ruling/tests/src/test/expected/less-prefer-single-line-comments.json @@ -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, diff --git a/its/ruling/tests/src/test/expected/scss-alphabetize-declarations.json b/its/ruling/tests/src/test/expected/scss-alphabetize-declarations.json index ea724f2f..c18ffe42 100644 --- a/its/ruling/tests/src/test/expected/scss-alphabetize-declarations.json +++ b/its/ruling/tests/src/test/expected/scss-alphabetize-declarations.json @@ -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, ], diff --git a/its/ruling/tests/src/test/expected/scss-experimental-property-usage.json b/its/ruling/tests/src/test/expected/scss-experimental-property-usage.json index e558734f..7fda17e6 100644 --- a/its/ruling/tests/src/test/expected/scss-experimental-property-usage.json +++ b/its/ruling/tests/src/test/expected/scss-experimental-property-usage.json @@ -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, ], diff --git a/its/ruling/tests/src/test/expected/scss-prefer-single-line-comments.json b/its/ruling/tests/src/test/expected/scss-prefer-single-line-comments.json index ca2b6295..45d1cbf0 100644 --- a/its/ruling/tests/src/test/expected/scss-prefer-single-line-comments.json +++ b/its/ruling/tests/src/test/expected/scss-prefer-single-line-comments.json @@ -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,