diff options
| author | Elena Vilchik | 2019-06-13 18:09:27 +0200 | 
|---|---|---|
| committer | Tibor Blenessy | 2019-06-13 18:09:27 +0200 | 
| commit | 479b21eb67bd2f5f7170cb4e14b19561ed59ddcf (patch) | |
| tree | 1e5d4c8de4d87d6add3f324f51bd21e153aeaa1e | |
| parent | 6581b69a11c27d11ba5e1f4c37546485dc12b1d4 (diff) | |
| download | sonar-css-479b21eb67bd2f5f7170cb4e14b19561ed59ddcf.tar.bz2 | |
Add rule S5362: Expressions within "calc" should be valid (#180)
Removes S4650 from the default profile, because of overlap.
10 files changed, 80 insertions, 3 deletions
| diff --git a/its/plugin/projects/issues-project/src/file1.css b/its/plugin/projects/issues-project/src/file1.css index d53007e..a6de7ce 100644 --- a/its/plugin/projects/issues-project/src/file1.css +++ b/its/plugin/projects/issues-project/src/file1.css @@ -27,6 +27,7 @@ a:unknown {                                                   /* S4659 | selecto    padding-left: 10px;    padding: 20px;                                              /* S4657 | declaration-block-no-shorthand-property-overrides */    top: calc(1px+2px);                                         /* S4650 | function-calc-no-unspaced-operator */ /* S4653 | unit-no-unknown */ +  width: calc(100% 80px);                                     /* S5362 | function-calc-no-invalid */  }  //color: pink;                                               /* S4668 | no-invalid-double-slash-comments */ /* S4654 | property-no-unknown */ diff --git a/its/plugin/projects/issues-project/src/file2.less b/its/plugin/projects/issues-project/src/file2.less index 1519d88..a838bcd 100644 --- a/its/plugin/projects/issues-project/src/file2.less +++ b/its/plugin/projects/issues-project/src/file2.less @@ -27,6 +27,7 @@ a:unknown {                                                   /* S4659 | selecto    padding-left: 10px;    padding: 20px;                                              /* S4657 | declaration-block-no-shorthand-property-overrides */    top: calc(1px+2px);                                         /* S4650 | function-calc-no-unspaced-operator */ /* S4653 | unit-no-unknown */ +  width: calc(100% 80px);                                     /* S5362 | function-calc-no-invalid */  }  // color: pink;                                               /* S4668 | no-invalid-double-slash-comments | Doesn't raise for LESS */ diff --git a/its/plugin/projects/issues-project/src/file3.scss b/its/plugin/projects/issues-project/src/file3.scss index 6f19ae1..1d10479 100644 --- a/its/plugin/projects/issues-project/src/file3.scss +++ b/its/plugin/projects/issues-project/src/file3.scss @@ -27,6 +27,7 @@ a:unknown {                                                   /* S4659 | selecto    padding-left: 10px;    padding: 20px;                                              /* S4657 | declaration-block-no-shorthand-property-overrides */    top: calc(1px+2px);                                         /* S4650 | function-calc-no-unspaced-operator */ /* S4653 | unit-no-unknown */ +  width: calc(100% 80px);                                     /* S5362 | function-calc-no-invalid */  }  // color: pink;                                               /* S4668 | no-invalid-double-slash-comments | Doesn't raise for SCSS */ diff --git a/its/plugin/src/test/java/org/sonar/css/its/IssuesTest.java b/its/plugin/src/test/java/org/sonar/css/its/IssuesTest.java index 9294357..ce57e15 100644 --- a/its/plugin/src/test/java/org/sonar/css/its/IssuesTest.java +++ b/its/plugin/src/test/java/org/sonar/css/its/IssuesTest.java @@ -63,6 +63,7 @@ public class IssuesTest {        CssRules.getRuleClasses().size() * 3 /* issues are raised against .css, .less and .scss */        + 1 /* issue S4654 is raised for comment */        + 1 /* issue S4662 is raised for cssModules.css */ +      + 6 /* S5362 raises at the same place as S4650 */        - 2 /* issue S4668 not raised on .less nor .scss */);    } diff --git a/sonar-css-plugin/src/main/java/org/sonar/css/plugin/CssRules.java b/sonar-css-plugin/src/main/java/org/sonar/css/plugin/CssRules.java index ec7a27c..a4a47b4 100644 --- a/sonar-css-plugin/src/main/java/org/sonar/css/plugin/CssRules.java +++ b/sonar-css-plugin/src/main/java/org/sonar/css/plugin/CssRules.java @@ -44,6 +44,7 @@ import org.sonar.css.plugin.rules.DeclarationBlockNoDuplicateProperties;  import org.sonar.css.plugin.rules.DeclarationBlockNoShorthandPropertyOverrides;  import org.sonar.css.plugin.rules.FontFamilyNoDuplicateNames;  import org.sonar.css.plugin.rules.FontFamilyNoMissingGenericFamilyKeyword; +import org.sonar.css.plugin.rules.FunctionCalcNoInvalid;  import org.sonar.css.plugin.rules.FunctionCalcNoUnspacedOperator;  import org.sonar.css.plugin.rules.FunctionLinearGradientNoNonstandardDirection;  import org.sonar.css.plugin.rules.KeyframeDeclarationNoImportant; @@ -88,6 +89,7 @@ public class CssRules {        FontFamilyNoDuplicateNames.class,        FontFamilyNoMissingGenericFamilyKeyword.class,        FunctionCalcNoUnspacedOperator.class, +      FunctionCalcNoInvalid.class,        FunctionLinearGradientNoNonstandardDirection.class,        KeyframeDeclarationNoImportant.class,        MediaFeatureNameNoUnknown.class, diff --git a/sonar-css-plugin/src/main/java/org/sonar/css/plugin/rules/FunctionCalcNoInvalid.java b/sonar-css-plugin/src/main/java/org/sonar/css/plugin/rules/FunctionCalcNoInvalid.java new file mode 100644 index 0000000..e22a79c --- /dev/null +++ b/sonar-css-plugin/src/main/java/org/sonar/css/plugin/rules/FunctionCalcNoInvalid.java @@ -0,0 +1,31 @@ +/* + * SonarCSS + * Copyright (C) 2018-2019 SonarSource SA + * mailto:info AT sonarsource DOT com + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 3 of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this program; if not, write to the Free Software Foundation, + * Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA. + */ +package org.sonar.css.plugin.rules; + +import org.sonar.check.Rule; + +@Rule(key = "S5362") +public class FunctionCalcNoInvalid implements CssRule { + +  @Override +  public String stylelintKey() { +    return "function-calc-no-invalid"; +  } +} diff --git a/sonar-css-plugin/src/main/resources/org/sonar/l10n/css/rules/css/S5362.html b/sonar-css-plugin/src/main/resources/org/sonar/l10n/css/rules/css/S5362.html new file mode 100644 index 0000000..66fa5cd --- /dev/null +++ b/sonar-css-plugin/src/main/resources/org/sonar/l10n/css/rules/css/S5362.html @@ -0,0 +1,24 @@ +<p>To perform calculations when specifying a CSS property <code>calc()</code> function can be used. This function takes single expression as +parameter. When writing this expression some rules must be respected:</p> +<ul> +  <li> no empty <code>calc()</code> </li> +  <li> there should be an operator between the arguments, spacing should be respected </li> +  <li> there should not be any division by zero </li> +  <li> the resolved type should be valid for where the expression is placed </li> +</ul> +<p>Otherwise <code>calc()</code> function will be invalid and the entire rule using it will be ignored.</p> +<h2>Noncompliant Code Example</h2> +<pre> +.btn { +  border: solid black 1px; +  width: calc(100% 80px);  /* Noncompliant */ +} +</pre> +<h2>Compliant Solution</h2> +<pre> +.btn { +  border: solid black 1px; +  width: calc(100% - 80px); +} +</pre> + diff --git a/sonar-css-plugin/src/main/resources/org/sonar/l10n/css/rules/css/S5362.json b/sonar-css-plugin/src/main/resources/org/sonar/l10n/css/rules/css/S5362.json new file mode 100644 index 0000000..bde6f63 --- /dev/null +++ b/sonar-css-plugin/src/main/resources/org/sonar/l10n/css/rules/css/S5362.json @@ -0,0 +1,16 @@ +{ +  "title": "Expressions within \"calc\" should be valid", +  "type": "BUG", +  "status": "ready", +  "remediation": { +    "func": "Constant\/Issue", +    "constantCost": "5min" +  }, +  "tags": [ +     +  ], +  "defaultSeverity": "Critical", +  "ruleSpecification": "RSPEC-5362", +  "sqKey": "S5362", +  "scope": "All" +} diff --git a/sonar-css-plugin/src/main/resources/org/sonar/l10n/css/rules/css/Sonar_way_profile.json b/sonar-css-plugin/src/main/resources/org/sonar/l10n/css/rules/css/Sonar_way_profile.json index 9b98de0..7834cba 100644 --- a/sonar-css-plugin/src/main/resources/org/sonar/l10n/css/rules/css/Sonar_way_profile.json +++ b/sonar-css-plugin/src/main/resources/org/sonar/l10n/css/rules/css/Sonar_way_profile.json @@ -6,7 +6,6 @@      "S4647",      "S4648",      "S4649", -    "S4650",      "S4651",      "S4652",      "S4653", @@ -23,6 +22,7 @@      "S4666",      "S4667",      "S4668", -    "S4670" +    "S4670", +    "S5362"    ]  } diff --git a/sonar-css-plugin/src/test/java/org/sonar/css/plugin/SonarWayProfileTest.java b/sonar-css-plugin/src/test/java/org/sonar/css/plugin/SonarWayProfileTest.java index d622b79..0717e95 100644 --- a/sonar-css-plugin/src/test/java/org/sonar/css/plugin/SonarWayProfileTest.java +++ b/sonar-css-plugin/src/test/java/org/sonar/css/plugin/SonarWayProfileTest.java @@ -38,7 +38,7 @@ public class SonarWayProfileTest {      assertThat(profile.language()).isEqualTo(CssLanguage.KEY);      assertThat(profile.name()).isEqualTo(SonarWayProfile.PROFILE_NAME);      assertThat(profile.rules()).extracting("repoKey").containsOnly(CssRulesDefinition.REPOSITORY_KEY); -    assertThat(profile.rules()).extracting("ruleKey").hasSize(CssRules.getRuleClasses().size() - 1); +    assertThat(profile.rules()).extracting("ruleKey").hasSize(CssRules.getRuleClasses().size() - 2);    }  } | 
