From 87dde3c11cee80e8cae6300816f856da26f386cb Mon Sep 17 00:00:00 2001 From: David RACODON Date: Wed, 6 Dec 2017 13:35:40 +0100 Subject: [PATCH] Fix #75 FP on rule multiple-variable-declarations-same-scope within mixin calls when parameters are the same variables --- ...multipleVariableDeclarationsSameScope.less | 10 ++++--- .../sonar/css/tree/symbol/SymbolVisitor.java | 30 +++++++++---------- ...multipleVariableDeclarationsSameScope.less | 10 ++++--- .../less-experimental-property-usage.json | 2 -- .../src/test/expected/less-line-length.json | 2 -- ...iple-variable-declarations-same-scope.json | 2 -- .../less-prefer-single-line-comments.json | 2 -- .../less-variable-declaration-first.json | 2 ++ 8 files changed, 29 insertions(+), 31 deletions(-) diff --git a/css-checks/src/test/resources/checks/less/multipleVariableDeclarationsSameScope.less b/css-checks/src/test/resources/checks/less/multipleVariableDeclarationsSameScope.less index d571d039..117725dd 100644 --- a/css-checks/src/test/resources/checks/less/multipleVariableDeclarationsSameScope.less +++ b/css-checks/src/test/resources/checks/less/multipleVariableDeclarationsSameScope.less @@ -17,14 +17,16 @@ border-radius: @radius; } -.border-radius2(@radius, @radius) { /* Noncompliant ![sc=18;ec=24;secondary=+0]! !{Merge those multiple variable declarations within the same scope.}! */ +.border-radius2(@radius) { -webkit-border-radius: @radius; -moz-border-radius: @radius; border-radius: @radius; + @radius: 10; } -.border-radius3(@radius: 3px, @radius: 4px) { /* Noncompliant ![sc=18;ec=24;secondary=+0]! !{Merge those multiple variable declarations within the same scope.}! */ - -webkit-border-radius: @radius; - -moz-border-radius: @radius; +@radius: 10; + +.myclass { + @radius: 10; border-radius: @radius; } diff --git a/css-frontend/src/main/java/org/sonar/css/tree/symbol/SymbolVisitor.java b/css-frontend/src/main/java/org/sonar/css/tree/symbol/SymbolVisitor.java index 30bd7443..18bd9948 100644 --- a/css-frontend/src/main/java/org/sonar/css/tree/symbol/SymbolVisitor.java +++ b/css-frontend/src/main/java/org/sonar/css/tree/symbol/SymbolVisitor.java @@ -73,24 +73,10 @@ public void visitLessMixinParameters(LessMixinParametersTree tree) { leaveScope(); } - private void leaveScope() { - if (currentScope != null) { - currentScope = currentScope.outer(); - } - } - - private void enterScope(Tree tree) { - currentScope = treeScopeMap.get(tree); - if (currentScope == null) { - throw new IllegalStateException("No scope found for the tree"); - } - } - @Override public void visitLessVariable(LessVariableTree tree) { Usage.Kind usage; - - if (tree.parent().is(Tree.Kind.LESS_VARIABLE_DECLARATION, Tree.Kind.LESS_MIXIN_PARAMETER)) { + if (tree.parent().is(Tree.Kind.LESS_VARIABLE_DECLARATION)) { usage = Usage.Kind.DECLARATION; } else { usage = Usage.Kind.READ; @@ -108,4 +94,18 @@ private void initScopes() { currentScope = null; } + + private void leaveScope() { + if (currentScope != null) { + currentScope = currentScope.outer(); + } + } + + private void enterScope(Tree tree) { + currentScope = treeScopeMap.get(tree); + if (currentScope == null) { + throw new IllegalStateException("No scope found for the tree"); + } + } + } diff --git a/its/ruling/projects/custom/less/multipleVariableDeclarationsSameScope.less b/its/ruling/projects/custom/less/multipleVariableDeclarationsSameScope.less index d571d039..117725dd 100644 --- a/its/ruling/projects/custom/less/multipleVariableDeclarationsSameScope.less +++ b/its/ruling/projects/custom/less/multipleVariableDeclarationsSameScope.less @@ -17,14 +17,16 @@ border-radius: @radius; } -.border-radius2(@radius, @radius) { /* Noncompliant ![sc=18;ec=24;secondary=+0]! !{Merge those multiple variable declarations within the same scope.}! */ +.border-radius2(@radius) { -webkit-border-radius: @radius; -moz-border-radius: @radius; border-radius: @radius; + @radius: 10; } -.border-radius3(@radius: 3px, @radius: 4px) { /* Noncompliant ![sc=18;ec=24;secondary=+0]! !{Merge those multiple variable declarations within the same scope.}! */ - -webkit-border-radius: @radius; - -moz-border-radius: @radius; +@radius: 10; + +.myclass { + @radius: 10; border-radius: @radius; } 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 b05be515..87df182a 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 @@ -1886,8 +1886,6 @@ 16, 21, 22, -27, -28, ], 'project:custom/less/obsoleteFunctions.less':[ 6, diff --git a/its/ruling/tests/src/test/expected/less-line-length.json b/its/ruling/tests/src/test/expected/less-line-length.json index bfec4761..53319eba 100644 --- a/its/ruling/tests/src/test/expected/less-line-length.json +++ b/its/ruling/tests/src/test/expected/less-line-length.json @@ -1045,8 +1045,6 @@ 'project:custom/less/multipleVariableDeclarationsSameScope.less':[ 1, 5, -20, -26, ], 'project:custom/less/nestedRulesets/nestedRulesets.less':[ 18, diff --git a/its/ruling/tests/src/test/expected/less-multiple-variable-declarations-same-scope.json b/its/ruling/tests/src/test/expected/less-multiple-variable-declarations-same-scope.json index 6aa7bde3..aa189cab 100644 --- a/its/ruling/tests/src/test/expected/less-multiple-variable-declarations-same-scope.json +++ b/its/ruling/tests/src/test/expected/less-multiple-variable-declarations-same-scope.json @@ -8,7 +8,5 @@ 'project:custom/less/multipleVariableDeclarationsSameScope.less':[ 1, 5, -20, -26, ], } 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 f8c23085..bbac3ba1 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 @@ -2116,8 +2116,6 @@ 'project:custom/less/multipleVariableDeclarationsSameScope.less':[ 1, 5, -20, -26, ], 'project:custom/less/nestedRulesets/nestedRulesets.less':[ 1, diff --git a/its/ruling/tests/src/test/expected/less-variable-declaration-first.json b/its/ruling/tests/src/test/expected/less-variable-declaration-first.json index c5c8304f..0669b77d 100644 --- a/its/ruling/tests/src/test/expected/less-variable-declaration-first.json +++ b/its/ruling/tests/src/test/expected/less-variable-declaration-first.json @@ -15,6 +15,8 @@ 8, 9, 12, +24, +27, ], 'project:custom/less/variableDeclarationFirst.less':[ 19,