diff options
| author | Amaury Levé | 2018-06-26 15:24:29 +0200 | 
|---|---|---|
| committer | Elena Vilchik | 2018-06-26 17:12:59 +0200 | 
| commit | 510ac865875fcd1ed4475a50f74fd3ed5f0de3d5 (patch) | |
| tree | e5211369488c3107bb706064b8faa046650f10bc /sonar-css-plugin | |
| parent | 5aaa1f182736f84f1613aa60f3f7da37103167c2 (diff) | |
| download | sonar-css-510ac865875fcd1ed4475a50f74fd3ed5f0de3d5.tar.bz2 | |
Rule S4664: Selectors of lower specificity should come before overriding selectors of higher specificity
Diffstat (limited to 'sonar-css-plugin')
5 files changed, 78 insertions, 0 deletions
| 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 fe3088b..b2f8121 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 @@ -42,6 +42,7 @@ import org.sonar.css.plugin.rules.FunctionCalcNoUnspacedOperator;  import org.sonar.css.plugin.rules.FunctionLinearGradientNoNonstandardDirection;  import org.sonar.css.plugin.rules.KeyframeDeclarationNoImportant;  import org.sonar.css.plugin.rules.MediaFeatureNameNoUnknown; +import org.sonar.css.plugin.rules.NoDescendingSpecificity;  import org.sonar.css.plugin.rules.NoDuplicateAtImportRules;  import org.sonar.css.plugin.rules.NoDuplicateSelectors;  import org.sonar.css.plugin.rules.NoEmptySource; @@ -84,6 +85,7 @@ public class CssRules {        FunctionLinearGradientNoNonstandardDirection.class,        KeyframeDeclarationNoImportant.class,        MediaFeatureNameNoUnknown.class, +      NoDescendingSpecificity.class,        NoDuplicateAtImportRules.class,        NoDuplicateSelectors.class,        NoEmptySource.class, diff --git a/sonar-css-plugin/src/main/java/org/sonar/css/plugin/rules/NoDescendingSpecificity.java b/sonar-css-plugin/src/main/java/org/sonar/css/plugin/rules/NoDescendingSpecificity.java new file mode 100644 index 0000000..72a31f6 --- /dev/null +++ b/sonar-css-plugin/src/main/java/org/sonar/css/plugin/rules/NoDescendingSpecificity.java @@ -0,0 +1,31 @@ +/* + * SonarCSS + * Copyright (C) 2018-2018 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 = "S4664") +public class NoDescendingSpecificity implements CssRule { + +  @Override +  public String stylelintKey() { +    return "no-descending-specificity"; +  } +} diff --git a/sonar-css-plugin/src/main/resources/org/sonar/l10n/css/rules/css/S4664.html b/sonar-css-plugin/src/main/resources/org/sonar/l10n/css/rules/css/S4664.html new file mode 100644 index 0000000..6f501e2 --- /dev/null +++ b/sonar-css-plugin/src/main/resources/org/sonar/l10n/css/rules/css/S4664.html @@ -0,0 +1,28 @@ +<p>Order of instructions in CSS is important: instructions that occur last in the file take the priority. When a selector with high specificity (eg +<code>#container a top: 10px; </code>) comes before the selector it overrides (eg: <code>a top: 0; </code>), the priority is given to the high +specificity definition which violates the principle of the last instruction takes the priority. </p> +<p>CSS are most legible when overriding selectors always come after the selectors they override. That way both mechanisms, source order and +specificity, work together nicely.</p> +<h2>Noncompliant Code Example</h2> +<pre> +b a {} +a {} + + +@media print { +  #c a {} +  a {} +} +</pre> +<h2>Compliant Solution</h2> +<pre> +a {} +b a {} + + +@media print { +  a {} +  #c a {} +} +</pre> + diff --git a/sonar-css-plugin/src/main/resources/org/sonar/l10n/css/rules/css/S4664.json b/sonar-css-plugin/src/main/resources/org/sonar/l10n/css/rules/css/S4664.json new file mode 100644 index 0000000..ebcea71 --- /dev/null +++ b/sonar-css-plugin/src/main/resources/org/sonar/l10n/css/rules/css/S4664.json @@ -0,0 +1,16 @@ +{ +  "title": "Selectors of lower specificity should come before overriding selectors of higher specificity", +  "type": "BUG", +  "status": "ready", +  "remediation": { +    "func": "Constant\/Issue", +    "constantCost": "5min" +  }, +  "tags": [ +     +  ], +  "defaultSeverity": "Critical", +  "ruleSpecification": "RSPEC-4664", +  "sqKey": "S4664", +  "scope": "Main" +} 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 adbe829..ce1c647 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 @@ -20,6 +20,7 @@      "S4661",      "S4662",      "S4663", +    "S4664",      "S4666",      "S4667",      "S4668", | 
