Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix #117 Parent selector (&) not supported in variable declaration #118

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ env:
- secure: "HJzMrBMjJ9+H1UroodSLo0tiUrBytZQz1EqGykWqU/PcgwVjVSTPqvLmFkfPbIZm3on9GElblUQqXV3/J9X2UTu4a/ZmWL5v09Tk2O12jKGbFV71LtlDFvvIoy8DtIIfnE0r642wyu6ii3VVt1J6FI/oixNmF25S6DdWDaA+q3NWlA+MZEae3n1YuQmhkpgoMkWe6xC3H6Oxdo0XgTn0SfqfBES8IM+3hG4FCU6tYfOVoffVEUd1eq3J9W7aGfMQ4zTEAorwroyWuFRDbG+W+PK8uMUJpSDDzjq3271yJAFPtLlGL2f+JPZO5GUui1IW+VM4Ju9YhQ5MiYzQXpdIUKFtBQZr480PrmTL9hP0zZFJA/QkBYr6otyBvaDOEkM1Bv/I9v2EjiutfVKVP09FKIEMNASdCCvtJ6p+BS3r2a74MZNcFtqcjXPd8ROxrh49n6hrkt99jvwsybgTpsgayTxT0N6Pdd1ni6sAiQ3LtXo6vFCi2apJ1v1hBoOXRfNvIdq00inyQI/P3LNcQzXLTou63oh2Z+I7UJcQHnl0pc4pndQMQm4A+22fFUrXRByPJK2dZpd7l/PSIEl0reirzk3UIUw1qmJlZyabAmPJPzPjvodjbH3pH5mK0S050UNDXi6G1u+F91td5gMrXxq3sA4/GIp1RpyoovSrOzIZEf4="
- secure: "P3gRgSFJNUfku81Yl61Fh5QbkH5E4ccFSv9zbwNwQQkTVNTXd3Zv5TVTjt/eXI/wM7PZnDrhWxz8y7D4J1vZjqvbIGnmXyjsm/jX83HTDt70aE/4N4oKCZcEr783H2xlXcUnrxPXW87vV4Bw/JU7EVF26jC1tAIrnfVUMfNxYMpxCi19S7Qg72nOGcNgDJbQj/Unl7UEmbEPV4POiytwF6oVDWLp+Ys8JjujeS9rCtd6vzKxBR08WLAlqG07SSLb98UxCZA+p+D7uFfQMOAkp91L9S+vXXn0FIDczmEEYaAnPRJYszDfaEr6aQSvIQVyaWVqLYBKuSFZpnXPqyczgtXgNVk1H01gRtXPjF18kAD8e/UT/n4x9YvsN7FRDr5mXO0wd/XPmbZ9D2ZrOqDNKNzFv3LdnzpWL1BJsgAbI/nhUqjz1WJ+WVUXDq6jNMegJOXGRia04D05XKBgW71ULmMZrKNufab1l9Hy2gEVpZ0AgUlSxRLNmvYLh1TZTS9Tp2ONHEBkVEFo3tbt7hP8MeTM/6sqwhhY27CGv9yfmEMBLNURDodaXAyUQEBBbCF6KLIkX+RoQsvho2+TZE1jxheY1MffL+MlWtmbVCpK46W7yeB1Gk+AptY7vJ5JsRa+RWrrwFEChfgymWJfIwPiFVRm947XAv6QirsWNJV9fqw="
matrix:
- SQ_VERSION=LTS
- SQ_VERSION=LATEST_RELEASE
- SQ_VERSION=LATEST_RELEASE[6.7]
- SQ_VERSION=DEV

cache:
directories:
Expand Down
2 changes: 1 addition & 1 deletion CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,5 @@ You feel like:

## Building / Testing / Releasing
* Building and running unit tests: `mvn clean install`
* Building and running unit tests and running integration tests: `mvn clean install -Pits -Dsonar.runtimeVersion=$VERSION` ($VERSION = 'LTS' or 'LATEST_RELEASE'). Behind a proxy, add `-Dhttps.proxyHost=localhost -Dhttps.proxyPort=3128 -Dhttp.proxyHost=localhost -Dhttp.proxyPort=3128`.
* Building and running unit tests and running integration tests: `mvn clean install -Pits -Dsonar.runtimeVersion=$VERSION` ($VERSION = 'LATEST_RELEASE[X.X]' or 'DEV'). Behind a proxy, add `-Dhttps.proxyHost=localhost -Dhttps.proxyPort=3128 -Dhttp.proxyHost=localhost -Dhttp.proxyPort=3128`.
* Releasing on Maven Central: `mvn clean deploy -Possrh`
4 changes: 2 additions & 2 deletions appveyor.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ install:

environment:
matrix:
- SQ_VERSION: "LTS"
- SQ_VERSION: "LATEST_RELEASE"
- SQ_VERSION: "LATEST_RELEASE[6.7]"
- SQ_VERSION: "DEV"

platform:
- x64
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,4 +34,7 @@
@ActivatedByDefault
@SqaleConstantRemediation("30min")
public class ParsingErrorCheck extends DoubleDispatchVisitorCheck {

public static final String MESSAGE = "Parse error";

}
Original file line number Diff line number Diff line change
Expand Up @@ -34,4 +34,7 @@
@ActivatedByDefault
@SqaleConstantRemediation("30min")
public class ParsingErrorCheck extends DoubleDispatchVisitorCheck {

public static final String MESSAGE = "Parse error";

}
Original file line number Diff line number Diff line change
Expand Up @@ -34,4 +34,7 @@
@ActivatedByDefault
@SqaleConstantRemediation("30min")
public class ParsingErrorCheck extends DoubleDispatchVisitorCheck {

public static final String MESSAGE = "Parse error";

}
Original file line number Diff line number Diff line change
Expand Up @@ -197,10 +197,13 @@ public enum LexicalGrammar implements GrammarRuleKey {

SCSS_MIXIN_BLOCK,

SCSS_PARENT_SELECTOR,
SCSS_PARENT_SELECTOR_KEYWORD,
SCSS_PARENT_SELECTOR_KEYWORD_WITH_SPACING,
SCSS_PARENT_SELECTOR,
SCSS_PARENT_SELECTOR_WITH_SPACING,
SCSS_PARENT_SELECTOR_COMBINATOR,
SCSS_PARENT_REFERENCING_SELECTOR,
SCSS_PARENT_REFERENCING_SELECTOR_WITH_SPACING,
SCSS_PLACEHOLDER_SELECTOR,

SCSS_EXTEND,
Expand Down Expand Up @@ -594,6 +597,7 @@ private static void scss(LexerlessGrammarBuilder b) {
b.rule(SCSS_DOLLAR).is(b.token(GenericTokenType.LITERAL, "$"));
b.rule(SCSS_ELLIPSIS).is(b.token(GenericTokenType.LITERAL, "..."));
b.rule(SCSS_PARENT_SELECTOR_KEYWORD).is("&");
b.rule(SCSS_PARENT_SELECTOR_KEYWORD_WITH_SPACING).is(SPACING, "&");

b.rule(SCSS_VARIABLE_PREFIX).is(SPACING, SCSS_DOLLAR);
b.rule(SCSS_DEFAULT_KEYWORD).is(SPACING, b.token(GenericTokenType.LITERAL, "!default"));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,23 @@
import org.sonar.css.tree.impl.SeparatedList;
import org.sonar.css.tree.impl.css.InternalSyntaxToken;
import org.sonar.plugins.css.api.tree.Tree;
import org.sonar.plugins.css.api.tree.css.*;
import org.sonar.plugins.css.api.tree.css.AttributeMatcherExpressionTree;
import org.sonar.plugins.css.api.tree.css.AttributeSelectorTree;
import org.sonar.plugins.css.api.tree.css.ClassSelectorTree;
import org.sonar.plugins.css.api.tree.css.CompoundSelectorTree;
import org.sonar.plugins.css.api.tree.css.DeclarationTree;
import org.sonar.plugins.css.api.tree.css.DelimiterTree;
import org.sonar.plugins.css.api.tree.css.IdSelectorTree;
import org.sonar.plugins.css.api.tree.css.IdentifierTree;
import org.sonar.plugins.css.api.tree.css.PropertyTree;
import org.sonar.plugins.css.api.tree.css.PseudoIdentifierTree;
import org.sonar.plugins.css.api.tree.css.SelectorCombinatorTree;
import org.sonar.plugins.css.api.tree.css.SelectorTree;
import org.sonar.plugins.css.api.tree.css.StatementBlockTree;
import org.sonar.plugins.css.api.tree.css.StyleSheetTree;
import org.sonar.plugins.css.api.tree.css.TypeSelectorTree;
import org.sonar.plugins.css.api.tree.css.ValueCommaSeparatedListTree;
import org.sonar.plugins.css.api.tree.css.ValueTree;
import org.sonar.plugins.css.api.tree.scss.*;

/**
Expand Down Expand Up @@ -129,6 +145,8 @@ public Tree ANY_WITHOUT_COMMA_SEPARATED_LIST() {
public Tree ANY_SASS_SCRIPT_EXPRESSION() {
return b.<Tree>nonterminal().is(
b.firstOf(
SCSS_PARENT_REFERENCING_SELECTOR_WITH_SPACING(),
SCSS_PARENT_SELECTOR_WITH_SPACING(),
VALUE_COMMA_SEPARATED_LIST(),
IMPORTANT_FLAG(),
SCSS_GLOBAL_FLAG(),
Expand All @@ -154,6 +172,8 @@ public Tree ANY_SASS_SCRIPT_EXPRESSION() {
public Tree ANY_SASS_SCRIPT_EXPRESSION_WITHOUT_COMMA_SEPARATED_LIST() {
return b.<Tree>nonterminal().is(
b.firstOf(
SCSS_PARENT_REFERENCING_SELECTOR_WITH_SPACING(),
SCSS_PARENT_SELECTOR_WITH_SPACING(),
IMPORTANT_FLAG(),
SCSS_GLOBAL_FLAG(),
SCSS_DEFAULT_FLAG(),
Expand Down Expand Up @@ -353,6 +373,11 @@ public ScssParentSelectorTree SCSS_PARENT_SELECTOR() {
f.scssParentSelector(b.token(LexicalGrammar.SCSS_PARENT_SELECTOR_KEYWORD)));
}

public ScssParentSelectorTree SCSS_PARENT_SELECTOR_WITH_SPACING() {
return b.<ScssParentSelectorTree>nonterminal(LexicalGrammar.SCSS_PARENT_SELECTOR_WITH_SPACING).is(
f.scssParentSelector(b.token(LexicalGrammar.SCSS_PARENT_SELECTOR_KEYWORD_WITH_SPACING)));
}

public ScssParentReferencingSelectorTree SCSS_PARENT_REFERENCING_SELECTOR() {
return b.<ScssParentReferencingSelectorTree>nonterminal(LexicalGrammar.SCSS_PARENT_REFERENCING_SELECTOR).is(
f.scssParentReferencingSelector(
Expand All @@ -362,6 +387,15 @@ public ScssParentReferencingSelectorTree SCSS_PARENT_REFERENCING_SELECTOR() {
IDENTIFIER_NO_WS())));
}

public ScssParentReferencingSelectorTree SCSS_PARENT_REFERENCING_SELECTOR_WITH_SPACING() {
return b.<ScssParentReferencingSelectorTree>nonterminal(LexicalGrammar.SCSS_PARENT_REFERENCING_SELECTOR_WITH_SPACING).is(
f.scssParentReferencingSelector(
SCSS_PARENT_SELECTOR_WITH_SPACING(),
b.firstOf(
SCSS_INTERPOLATED_IDENTIFIER_NO_WS(),
IDENTIFIER_NO_WS())));
}

public ScssPlaceholderSelectorTree SCSS_PLACEHOLDER_SELECTOR() {
return b.<ScssPlaceholderSelectorTree>nonterminal(LexicalGrammar.SCSS_PLACEHOLDER_SELECTOR).is(
f.scssPlaceholderSelector(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,14 +82,14 @@ public ValueTree value() {
@Nullable
public ScssDefaultFlagTree defaultFlag() {
Optional<ScssDefaultFlagTree> defaultFlag = value.firstValueElementOfType(ScssDefaultFlagTree.class);
return defaultFlag.isPresent() ? defaultFlag.get() : null;
return defaultFlag.orElse(null);
}

@Override
@Nullable
public ScssGlobalFlagTree globalFlag() {
Optional<ScssGlobalFlagTree> globalFlag = value.firstValueElementOfType(ScssGlobalFlagTree.class);
return globalFlag.isPresent() ? globalFlag.get() : null;
return globalFlag.orElse(null);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
/*
* 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.scss;

import org.junit.Test;
import org.sonar.css.parser.LexicalGrammar;
import org.sonar.plugins.css.api.tree.scss.ScssParentReferencingSelectorTree;

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

public class ScssParentReferencingSelectorWithSpacingTreeTest extends ScssTreeTest {

public ScssParentReferencingSelectorWithSpacingTreeTest() {
super(LexicalGrammar.SCSS_PARENT_REFERENCING_SELECTOR_WITH_SPACING);
}

@Test
public void scssParentReferencingSelector() {
checkParsed("&-bar", "-bar");
checkParsed(" &-bar", "-bar");
checkParsed("&bar", "bar");
checkParsed(" &bar", "bar");
}

@Test
public void notScssParentReferencingSelector() {
checkNotParsed("&");
checkNotParsed("& bar");
}

private void checkParsed(String toParse, String expectedIdentifier) {
ScssParentReferencingSelectorTree tree = (ScssParentReferencingSelectorTree) parser().parse(toParse);
assertThat(tree).isNotNull();
assertThat(tree.parent()).isNotNull();
assertThat(tree.append()).isNotNull();
assertThat(tree.text()).isEqualTo(expectedIdentifier);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
/*
* 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.scss;

import org.junit.Test;
import org.sonar.css.parser.LexicalGrammar;
import org.sonar.plugins.css.api.tree.scss.ScssParentSelectorTree;

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

public class ScssParentSelectorWithSpacingTreeTest extends ScssTreeTest {

public ScssParentSelectorWithSpacingTreeTest() {
super(LexicalGrammar.SCSS_PARENT_SELECTOR_WITH_SPACING);
}

@Test
public void sscsParentSelector() {
checkParsed("&");
checkParsed(" &");
}

private void checkParsed(String toParse) {
ScssParentSelectorTree tree = (ScssParentSelectorTree) parser().parse(toParse);
assertThat(tree).isNotNull();
assertThat(tree.parentSelector()).isNotNull();
assertThat(tree.parentSelector().text()).isEqualTo("&");
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,9 @@ public void scssSassScriptExpression() {
checkParsed("1 + 2");
checkParsed("(a: 2, b: 3)");
checkParsed("abc((a: 2, b: 3))");
checkParsed("&");
checkParsed("&-name");
checkParsed("&-name abc");
}

private ValueTree checkParsed(String toParse) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,11 @@ public void scssVariableDeclaration() {
checkParsed(" $abc : 123");
checkParsed(" $abc : 123;");
checkParsed(" $abc: 10, $def");
checkParsed(" $abc:&");
checkParsed(" $abc:&-name");
checkParsed(" $abc: &");
checkParsed(" $abc: &-name");
checkParsed(" $abc: &-name abc");

checkParsed("$grid-gutter-widths: (\n" +
" xs: $grid-gutter-width-base,\n" +
Expand Down
12 changes: 6 additions & 6 deletions its/plugin/tests/src/test/java/org/sonar/css/Tests.java
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,8 @@ public class Tests {

@ClassRule
public static final Orchestrator ORCHESTRATOR = Orchestrator.builderEnv()
.addPlugin(FileLocation.byWildcardFilename(new File("../../../sonar-css-plugin/target"), "sonar-css-plugin-*-SNAPSHOT.jar"))
.setSonarVersion(System.getProperty("sonar.runtimeVersion", "DEV"))
.addPlugin(FileLocation.byWildcardMavenFilename(new File("../../../sonar-css-plugin/target"), "sonar-css-plugin-*-SNAPSHOT.jar"))
.addPlugin(FileLocation.byWildcardFilename(new File("../plugins/css-custom-rules-plugin/target"), "css-custom-rules-plugin-*-SNAPSHOT.jar"))
.restoreProfileAtStartup(FileLocation.ofClasspath("/org/sonar/css/profiles/css-zero-units-only-profile.xml"))
.restoreProfileAtStartup(FileLocation.ofClasspath("/org/sonar/css/profiles/less-zero-units-only-profile.xml"))
Expand All @@ -68,11 +69,10 @@ public class Tests {
.build();

public static SonarScanner createSonarScannerBuild() {
SonarScanner build = SonarScanner.create();
build.setProjectVersion("1.0");
build.setSourceEncoding("UTF-8");
build.setSourceDirs("src");
return build;
return SonarScanner.create()
.setProjectVersion("1.0")
.setSourceEncoding("UTF-8")
.setSourceDirs("src");
}

public static void setCssProfile(String profileName, String projectKey) {
Expand Down
9 changes: 4 additions & 5 deletions its/ruling/tests/src/test/java/org/sonar/css/its/CssTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import com.sonar.orchestrator.Orchestrator;
import com.sonar.orchestrator.build.SonarScanner;
import com.sonar.orchestrator.locator.FileLocation;
import com.sonar.orchestrator.locator.MavenLocation;
import org.junit.Before;
import org.junit.ClassRule;
import org.junit.Test;
Expand All @@ -39,9 +40,9 @@ public class CssTest {

@ClassRule
public static Orchestrator orchestrator = Orchestrator.builderEnv()
.setSonarVersion(System.getProperty("sonar.runtimeVersion", "DEV"))
.addPlugin(FileLocation.byWildcardMavenFilename(new File("../../../sonar-css-plugin/target"), "sonar-css-plugin-*-SNAPSHOT.jar"))
.setOrchestratorProperty("litsVersion", "0.6")
.addPlugin("lits")
.addPlugin(MavenLocation.of("org.sonarsource.sonar-lits-plugin", "sonar-lits-plugin", "0.6"))
.build();

@Before
Expand Down Expand Up @@ -70,13 +71,11 @@ public void test() throws Exception {
.setProjectKey("project")
.setSourceDirs("./")
.setSourceEncoding("UTF-8")
.setProperty("sonar.import_unknown_files", "true")
.setProperty("sonar.analysis.mode", "preview")
.setProperty("sonar.issuesReport.html.enable", "true")
.setProperty("dump.old", FileLocation.of("src/test/expected").getFile().getAbsolutePath())
.setProperty("dump.new", FileLocation.of("target/actual").getFile().getAbsolutePath())
.setProperty("lits.differences", litsDifferencesFile.getAbsolutePath())
.setProperty("sonar.cpd.skip", "true");
.setProperty("sonar.cpd.exclusions", "**/*");
orchestrator.executeBuild(build);

assertThat(Files.asCharSource(litsDifferencesFile, StandardCharsets.UTF_8).read()).isEmpty();
Expand Down
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@
<dependency>
<groupId>org.sonarsource.orchestrator</groupId>
<artifactId>sonar-orchestrator</artifactId>
<version>3.15.2.1322</version>
<version>3.22.0.1791</version>
<exclusions>
<exclusion>
<groupId>com.oracle</groupId>
Expand Down
2 changes: 1 addition & 1 deletion travis.sh
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ git fetch --unshallow

mvn -B clean install -Pits -Dsonar.runtimeVersion=$SQ_VERSION

if [ "$SQ_VERSION" == "LTS" ]; then
if [ "$SQ_VERSION" == "DEV" ]; then

mvnCommand='mvn -B sonar:sonar'
commonArgs="-Dsonar.host.url=https://sonarcloud.io -Dsonar.login=$SONAR_TOKEN -Dsonar.organization=racodond-github"
Expand Down