Skip to content

Commit

Permalink
Fix #10 Parsing Less Functions containing default values gets interpr…
Browse files Browse the repository at this point in the history
…eted as Pseudo-Elements
  • Loading branch information
racodond committed May 10, 2017
1 parent 5634029 commit b321188
Show file tree
Hide file tree
Showing 34 changed files with 405 additions and 39 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -27,19 +27,21 @@

public class DeprecatedSystemColorCheckTest {

private DeprecatedSystemColorCheck check = new DeprecatedSystemColorCheck();

@Test
public void test_css() {
CssCheckVerifier.verifyCssFile(new DeprecatedSystemColorCheck(), getTestFile("deprecatedSystemColor.css"));
CssCheckVerifier.verifyCssFile(check, getTestFile("deprecatedSystemColor.css"));
}

@Test
public void test_less() {
CssCheckVerifier.verifyLessFile(new DeprecatedSystemColorCheck(), getTestFile("deprecatedSystemColor.less"));
CssCheckVerifier.verifyLessFile(check, getTestFile("deprecatedSystemColor.less"));
}

@Test
public void test_scss() {
CssCheckVerifier.verifyScssFile(new DeprecatedSystemColorCheck(), getTestFile("deprecatedSystemColor.scss"));
CssCheckVerifier.verifyScssFile(check, getTestFile("deprecatedSystemColor.scss"));
}

private File getTestFile(String fileName) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,29 @@
import org.sonar.css.checks.CheckTestUtils;
import org.sonar.css.checks.verifier.CssCheckVerifier;

import java.io.File;

public class ExperimentalPseudoCheckTest {

private ExperimentalPseudoCheck check = new ExperimentalPseudoCheck();

@Test
public void test_css() {
CssCheckVerifier.verifyCssFile(check, getTestFile("experimentalPseudoUsage.css"));
}

@Test
public void test() {
CssCheckVerifier.verifyCssFile(new ExperimentalPseudoCheck(), CheckTestUtils.getCommonTestFile("experimentalPseudoUsage.css"));
public void test_less() {
CssCheckVerifier.verifyLessFile(check, getTestFile("experimentalPseudoUsage.less"));
}

@Test
public void test_scss() {
CssCheckVerifier.verifyScssFile(check, getTestFile("experimentalPseudoUsage.scss"));
}

private File getTestFile(String fileName) {
return CheckTestUtils.getCommonTestFile("experimental-pseudo-usage/" + fileName);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
#fullscreen:fullscreen > h1 { /* Noncompliant ![sc=13;ec=23;el=+0]! !{Remove this usage of the experimental "fullscreen" pseudo.}! */
color: red;
}

#id:-moz-abc { /* Noncompliant ![sc=5;ec=13;el=+0]! !{Remove this usage of the experimental "abc" pseudo.}! */
color: red;
}

p:dir(ltr) { /* Noncompliant ![sc=3;ec=6;el=+0]! !{Remove this usage of the experimental "dir" pseudo.}! */
color: red;
}

p:-moz-abc(ltr) { /* Noncompliant ![sc=3;ec=11;el=+0]! !{Remove this usage of the experimental "abc" pseudo.}! */
color: red;
}

:host { /* Noncompliant ![sc=2;ec=6;el=+0]! !{Remove this usage of the experimental "host" pseudo.}! */
color: red;
}

p:empty {
color: red;
}

p:not(.mybox) {
color: red;
}

.animation (@animation-name, @animation-count: infinte, @timing-function:linear) {
color: #000000;
}
Original file line number Diff line number Diff line change
Expand Up @@ -326,6 +326,7 @@ public enum LexicalGrammar implements GrammarRuleKey {
LESS_MIXIN_GUARD_OR,
LESS_MIXIN_PARAMETERS,
LESS_MIXIN_PARAMETER,
LESS_MIXIN_PARAMETER_DEFAULT_VALUE,
LESS_IDENTIFIER_NO_WS_NOR_WHEN,
LESS_IDENT_IDENTIFIER_NO_WS_NOR_WHEN,
LESS_MERGE,
Expand Down
10 changes: 7 additions & 3 deletions css-frontend/src/main/java/org/sonar/css/parser/TreeFactory.java
Original file line number Diff line number Diff line change
Expand Up @@ -729,12 +729,16 @@ public SeparatedList<LessMixinParameterTree, DelimiterTree> lessMixinParameterLi
return new SeparatedList<>(parameters, separators);
}

public LessMixinParameterTree lessMixinParameter(LessVariableTree variable, Optional<SyntaxToken> colon, Optional<ValueTree> value) {
return new LessMixinParameterTreeImpl(variable, colon.orNull(), value.orNull());
public LessMixinParameterTree lessMixinParameter(LessVariableTree variable, Optional<LessMixinParameterDefaultValueTree> defaultValue) {
return new LessMixinParameterTreeImpl(variable, null, defaultValue.orNull());
}

public LessMixinParameterTree lessMixinParameter(ValueTree value) {
return new LessMixinParameterTreeImpl(null, null, value);
return new LessMixinParameterTreeImpl(null, value, null);
}

public LessMixinParameterDefaultValueTree lessMixinParameterDefaultValue(SyntaxToken colon, ValueTree value) {
return new LessMixinParameterDefaultValueTreeImpl(colon, value);
}

public LessEscapingTree lessEscaping(SyntaxToken escapingSymbol, StringTree string) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -348,11 +348,17 @@ public LessMixinParameterTree LESS_MIXIN_PARAMETER() {
b.firstOf(
f.lessMixinParameter(
LESS_VARIABLE(),
b.optional(b.token(LexicalGrammar.COLON)),
b.optional(DECLARATION_VALUE())),
b.optional(LESS_MIXIN_PARAMETER_DEFAULT_VALUE())),
f.lessMixinParameter(DECLARATION_VALUE())));
}

public LessMixinParameterDefaultValueTree LESS_MIXIN_PARAMETER_DEFAULT_VALUE() {
return b.<LessMixinParameterDefaultValueTree>nonterminal(LexicalGrammar.LESS_MIXIN_PARAMETER_DEFAULT_VALUE).is(
f.lessMixinParameterDefaultValue(
b.token(LexicalGrammar.COLON),
DECLARATION_VALUE()));
}

public IdentifierTree LESS_INTERPOLATED_IDENTIFIER() {
return b.<IdentifierTree>nonterminal(LexicalGrammar.LESS_INTERPOLATED_IDENTIFIER).is(
f.lessInterpolatedIdentifier(b.token(LexicalGrammar.LESS_IDENT_INTERPOLATED_IDENTIFIER)));
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
/*
* SonarQube CSS / SCSS / Less Analyzer
* Copyright (C) 2013-2017 David RACODON
* mailto: [email protected]
*
* 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.tree.impl.less;

import com.google.common.collect.Iterators;
import org.sonar.css.tree.impl.TreeImpl;
import org.sonar.plugins.css.api.tree.Tree;
import org.sonar.plugins.css.api.tree.css.SyntaxToken;
import org.sonar.plugins.css.api.tree.css.ValueTree;
import org.sonar.plugins.css.api.tree.less.LessMixinParameterDefaultValueTree;
import org.sonar.plugins.css.api.visitors.DoubleDispatchVisitor;

import java.util.Iterator;

public class LessMixinParameterDefaultValueTreeImpl extends TreeImpl implements LessMixinParameterDefaultValueTree {

private final SyntaxToken colon;
private final ValueTree value;

public LessMixinParameterDefaultValueTreeImpl(SyntaxToken colon, ValueTree value) {
this.colon = colon;
this.value = value;
}

@Override
public Kind getKind() {
return Kind.LESS_MIXIN_PARAMETER_DEFAULT_VALUE;
}

@Override
public Iterator<Tree> childrenIterator() {
return Iterators.forArray(colon, value);
}

@Override
public void accept(DoubleDispatchVisitor visitor) {
visitor.visitLessMixinParameterDefaultValue(this);
}

@Override
public SyntaxToken colon() {
return colon;
}

@Override
public ValueTree value() {
return value;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@
import com.google.common.collect.Iterators;
import org.sonar.css.tree.impl.TreeImpl;
import org.sonar.plugins.css.api.tree.Tree;
import org.sonar.plugins.css.api.tree.css.SyntaxToken;
import org.sonar.plugins.css.api.tree.css.ValueTree;
import org.sonar.plugins.css.api.tree.less.LessMixinParameterDefaultValueTree;
import org.sonar.plugins.css.api.tree.less.LessMixinParameterTree;
import org.sonar.plugins.css.api.tree.less.LessVariableTree;
import org.sonar.plugins.css.api.visitors.DoubleDispatchVisitor;
Expand All @@ -34,13 +34,13 @@
public class LessMixinParameterTreeImpl extends TreeImpl implements LessMixinParameterTree {

private final LessVariableTree variable;
private final SyntaxToken colon;
private final ValueTree value;
private final LessMixinParameterDefaultValueTree defaultValue;

public LessMixinParameterTreeImpl(LessVariableTree variable, @Nullable SyntaxToken colon, @Nullable ValueTree value) {
public LessMixinParameterTreeImpl(@Nullable LessVariableTree variable, @Nullable ValueTree value, @Nullable LessMixinParameterDefaultValueTree defaultValue) {
this.variable = variable;
this.colon = colon;
this.value = value;
this.defaultValue = defaultValue;
}

@Override
Expand All @@ -50,7 +50,7 @@ public Kind getKind() {

@Override
public Iterator<Tree> childrenIterator() {
return Iterators.forArray(variable, colon, value);
return Iterators.forArray(variable, value, defaultValue);
}

@Override
Expand All @@ -66,14 +66,14 @@ public LessVariableTree variable() {

@Override
@Nullable
public SyntaxToken colon() {
return colon;
public ValueTree value() {
return value;
}

@Override
@Nullable
public ValueTree value() {
return value;
public LessMixinParameterDefaultValueTree defaultValue() {
return defaultValue;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,7 @@ enum Kind implements GrammarRuleKey {
LESS_MIXIN_GUARD(LessMixinGuardTree.class),
LESS_MIXIN_PARAMETERS(LessMixinParametersTree.class),
LESS_MIXIN_PARAMETER(LessMixinParameterTree.class),
LESS_MIXIN_PARAMETER_DEFAULT_VALUE(LessMixinParameterDefaultValueTree.class),
LESS_ESCAPING(LessEscapingTree.class),
LESS_OPERATOR(LessOperatorTree.class);

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/*
* SonarQube CSS / SCSS / Less Analyzer
* Copyright (C) 2013-2017 David RACODON
* mailto: [email protected]
*
* 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.plugins.css.api.tree.less;

import org.sonar.plugins.css.api.tree.Tree;
import org.sonar.plugins.css.api.tree.css.SyntaxToken;
import org.sonar.plugins.css.api.tree.css.ValueTree;

public interface LessMixinParameterDefaultValueTree extends Tree {

SyntaxToken colon();

ValueTree value();

}
Original file line number Diff line number Diff line change
Expand Up @@ -19,21 +19,20 @@
*/
package org.sonar.plugins.css.api.tree.less;

import javax.annotation.Nullable;

import org.sonar.plugins.css.api.tree.css.SyntaxToken;
import org.sonar.plugins.css.api.tree.Tree;
import org.sonar.plugins.css.api.tree.css.ValueTree;

import javax.annotation.Nullable;

public interface LessMixinParameterTree extends Tree {

@Nullable
LessVariableTree variable();

@Nullable
SyntaxToken colon();
ValueTree value();

@Nullable
ValueTree value();
LessMixinParameterDefaultValueTree defaultValue();

}
Original file line number Diff line number Diff line change
Expand Up @@ -457,6 +457,10 @@ public void visitLessMixinParameter(LessMixinParameterTree tree) {
scanChildren(tree);
}

public void visitLessMixinParameterDefaultValue(LessMixinParameterDefaultValueTree tree) {
scanChildren(tree);
}

public void visitLessEscaping(LessEscapingTree tree) {
scanChildren(tree);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
/*
* SonarQube CSS / SCSS / Less Analyzer
* Copyright (C) 2013-2017 David RACODON
* mailto: [email protected]
*
* 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.parser.less;

import org.junit.Test;
import org.sonar.css.parser.LexicalGrammar;
import org.sonar.plugins.css.api.tree.less.LessMixinParameterDefaultValueTree;

import static org.fest.assertions.Assertions.assertThat;

public class LessMixinParameterDefaultValueTreeTest extends LessTreeTest {

public LessMixinParameterDefaultValueTreeTest() {
super(LexicalGrammar.LESS_MIXIN_PARAMETER_DEFAULT_VALUE);
}

@Test
public void lessMixinParameterDefaultValue() {
checkParsed(":abc");
checkParsed(": abc");
checkParsed(" : abc");
}

private LessMixinParameterDefaultValueTree checkParsed(String toParse) {
LessMixinParameterDefaultValueTree tree = (LessMixinParameterDefaultValueTree) parser().parse(toParse);
assertThat(tree.colon()).isNotNull();
assertThat(tree.value()).isNotNull();
return tree;
}

}
Loading

0 comments on commit b321188

Please sign in to comment.