From 364780c6760b452f19f3a52cd4be3a675abacccf Mon Sep 17 00:00:00 2001 From: Amaury Levé Date: Wed, 27 Jun 2018 11:38:45 +0200 Subject: Update S4656: Ignore consecutive duplications with different values --- .../src/main/java/org/sonar/css/plugin/CssRules.java | 4 ++-- .../java/org/sonar/css/plugin/rules/CssRule.java | 3 +++ .../rules/DeclarationBlockNoDuplicateProperties.java | 12 ++++++++++++ .../java/org/sonar/css/plugin/CssRuleSensorTest.java | 20 ++++++++++---------- 4 files changed, 27 insertions(+), 12 deletions(-) (limited to 'sonar-css-plugin') 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 b2f8121..926fff0 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 @@ -67,7 +67,7 @@ public class CssRules { stylelintKeyToRuleKey = new HashMap<>(); for (CssRule rule : enabledRules) { stylelintKeyToRuleKey.put(rule.stylelintKey(), checks.ruleKey(rule)); - config.rules.put(rule.stylelintKey(), true); + config.rules.put(rule.stylelintKey(), rule.stylelintOptions()); } } @@ -114,6 +114,6 @@ public class CssRules { } public static class StylelintConfig { - Map rules = new HashMap<>(); + Map rules = new HashMap<>(); } } diff --git a/sonar-css-plugin/src/main/java/org/sonar/css/plugin/rules/CssRule.java b/sonar-css-plugin/src/main/java/org/sonar/css/plugin/rules/CssRule.java index 237228b..fdc577b 100644 --- a/sonar-css-plugin/src/main/java/org/sonar/css/plugin/rules/CssRule.java +++ b/sonar-css-plugin/src/main/java/org/sonar/css/plugin/rules/CssRule.java @@ -23,4 +23,7 @@ public interface CssRule { String stylelintKey(); + default Object stylelintOptions() { + return true; + } } diff --git a/sonar-css-plugin/src/main/java/org/sonar/css/plugin/rules/DeclarationBlockNoDuplicateProperties.java b/sonar-css-plugin/src/main/java/org/sonar/css/plugin/rules/DeclarationBlockNoDuplicateProperties.java index 75382de..5c4b00a 100644 --- a/sonar-css-plugin/src/main/java/org/sonar/css/plugin/rules/DeclarationBlockNoDuplicateProperties.java +++ b/sonar-css-plugin/src/main/java/org/sonar/css/plugin/rules/DeclarationBlockNoDuplicateProperties.java @@ -19,6 +19,9 @@ */ package org.sonar.css.plugin.rules; +import java.util.Arrays; +import java.util.Collections; +import java.util.List; import org.sonar.check.Rule; @Rule(key = "S4656") @@ -28,4 +31,13 @@ public class DeclarationBlockNoDuplicateProperties implements CssRule { public String stylelintKey() { return "declaration-block-no-duplicate-properties"; } + + @Override + public Object stylelintOptions() { + return Arrays.asList(true, new StylelintIgnoreOption()); + } + + private static class StylelintIgnoreOption { + private final List ignore = Collections.singletonList("consecutive-duplicates-with-different-values"); + } } diff --git a/sonar-css-plugin/src/test/java/org/sonar/css/plugin/CssRuleSensorTest.java b/sonar-css-plugin/src/test/java/org/sonar/css/plugin/CssRuleSensorTest.java index dfe7ff2..f44781b 100644 --- a/sonar-css-plugin/src/test/java/org/sonar/css/plugin/CssRuleSensorTest.java +++ b/sonar-css-plugin/src/test/java/org/sonar/css/plugin/CssRuleSensorTest.java @@ -58,7 +58,7 @@ public class CssRuleSensorTest { @Rule public ExpectedException thrown= ExpectedException.none(); - private static CheckFactory checkFactory = new CheckFactory(new TestActiveRules("S4647")); + private static CheckFactory checkFactory = new CheckFactory(new TestActiveRules("S4647", "S4656")); private static final File BASE_DIR = new File("src/test/resources").getAbsoluteFile(); @@ -66,7 +66,7 @@ public class CssRuleSensorTest { private DefaultInputFile inputFile = createInputFile(context, "some css content\n on 2 lines", "dir/file.css"); @Before - public void setUp() throws Exception { + public void setUp() { context.fileSystem().setWorkDir(tmpDir.getRoot().toPath()); } @@ -90,11 +90,11 @@ public class CssRuleSensorTest { assertThat(issue.primaryLocation().message()).isEqualTo("some message"); Path configPath = Paths.get(context.fileSystem().workDir().getAbsolutePath(), "testconfig.json"); - assertThat(Files.readAllLines(configPath)).containsOnly("{\"rules\":{\"color-no-invalid-hex\":true}}"); + assertThat(Files.readAllLines(configPath)).containsOnly("{\"rules\":{\"color-no-invalid-hex\":true,\"declaration-block-no-duplicate-properties\":[true,{\"ignore\":[\"consecutive-duplicates-with-different-values\"]}]}}"); } @Test - public void test_invalid_node() throws IOException { + public void test_invalid_node() { TestLinterCommandProvider commandProvider = getCommandProvider(); commandProvider.nodeExecutable += " " + TestLinterCommandProvider.resourceScript("/executables/invalidNodeVersion.js"); CssRuleSensor sensor = new CssRuleSensor(new TestBundleHandler(), checkFactory, commandProvider); @@ -105,7 +105,7 @@ public class CssRuleSensorTest { } @Test - public void test_no_node() throws IOException { + public void test_no_node() { TestLinterCommandProvider commandProvider = getCommandProvider(); commandProvider.nodeExecutable = TestLinterCommandProvider.resourceScript("/executables/invalidNodeVersion.js"); CssRuleSensor sensor = new CssRuleSensor(new TestBundleHandler(), checkFactory, commandProvider); @@ -116,7 +116,7 @@ public class CssRuleSensorTest { } @Test - public void test_old_node() throws IOException { + public void test_old_node() { TestLinterCommandProvider commandProvider = getCommandProvider(); commandProvider.nodeExecutable += " " + TestLinterCommandProvider.resourceScript("/executables/oldNodeVersion.js"); CssRuleSensor sensor = new CssRuleSensor(new TestBundleHandler(), checkFactory, commandProvider); @@ -127,7 +127,7 @@ public class CssRuleSensorTest { } @Test - public void test_error() throws IOException { + public void test_error() { thrown.expect(IllegalStateException.class); thrown.expectMessage("Failed to parse json result of external process execution"); @@ -137,7 +137,7 @@ public class CssRuleSensorTest { } @Test - public void test_not_execute_rules_if_nothing_enabled() throws IOException { + public void test_not_execute_rules_if_nothing_enabled() { TestLinterCommandProvider commandProvider = new TestLinterCommandProvider().nodeScript("/executables/mockError.js", inputFile.absolutePath()); CssRuleSensor sensor = new CssRuleSensor(new TestBundleHandler(), new CheckFactory(new TestActiveRules()), commandProvider); sensor.execute(context); @@ -146,7 +146,7 @@ public class CssRuleSensorTest { } @Test - public void test_syntax_error() throws IOException { + public void test_syntax_error() { SensorContextTester context = SensorContextTester.create(BASE_DIR); context.fileSystem().setWorkDir(tmpDir.getRoot().toPath()); DefaultInputFile inputFile = createInputFile(context, "some css content\n on 2 lines", "dir/file.css"); @@ -158,7 +158,7 @@ public class CssRuleSensorTest { } @Test - public void test_unknown_rule() throws IOException { + public void test_unknown_rule() { SensorContextTester context = SensorContextTester.create(BASE_DIR); context.fileSystem().setWorkDir(tmpDir.getRoot().toPath()); DefaultInputFile inputFile = createInputFile(context, "some css content\n on 2 lines", "dir/file.css"); -- cgit v1.2.3