Skip to content

Commit

Permalink
Only accept supported web languages for CSS analysis (and activate SA…
Browse files Browse the repository at this point in the history
…SS by default) (#4308)
  • Loading branch information
ilia-kebets-sonarsource committed Oct 25, 2023
1 parent bf877cd commit 2472b76
Show file tree
Hide file tree
Showing 24 changed files with 964 additions and 29,231 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ This SonarSource project is a [static code analyzer](https://en.wikipedia.org/wi
- [25 CSS rules](https://rules.sonarsource.com/css)
- Compatible with ECMAScript 2015-2020
- React JSX, Flow, Vue, and AWS lambda functions support for JavaScript and TypeScript
- CSS, SCSS, Less, also 'style' inside PHP, HTML, and VueJS files
- CSS, SCSS, SASS, Less, also 'style' inside HTML and VueJS files
- Metrics (complexity, number of lines, etc.)
- Import of test coverage reports
- Import of ESLint, TSLint, and Stylelint issues
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,6 @@
</style>
</head>
<body>
<?= "Hello World!" ?>
"Hello World!
</body>
</html>
12 changes: 12 additions & 0 deletions its/plugin/projects/css-metrics-project/src/file4.sass
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
$firstValue: 62.5%

$firstValue: 24px !default

body
font-size: $firstValue
foo: "some text"


// body font size = 62.5%
/* some comment */
6 changes: 6 additions & 0 deletions its/plugin/projects/css-metrics-project/src/file6.htm
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<!doctype html>
<title>Example</title>
<style>
p { }
</style>
<p>Hello World!</p>
15 changes: 15 additions & 0 deletions its/plugin/projects/css-metrics-project/src/file7.xhtml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?xml version="1.0"?>
<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Example</title>
<style>
p {
font-size: 5vw;
padding: 0 5vw;
line-height: 1.8em;
}
</style>
</head>
</html>
9 changes: 9 additions & 0 deletions its/plugin/projects/css-metrics-project/src/ignored/file8.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<!DOCTYPE html>
<html>
<head>
<title>PHP Test</title>
</head>
<body>
<?php echo '<p>Hello World</p>'; ?>
</body>
</html>
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,6 @@ public static void prepare() {

SonarScanner scanner = CssTestsUtils.createScanner(PROJECT_KEY);
scanner.setProperty("sonar.exclusions", "**/file-with-parsing-error-excluded.css");
scanner.setProperty("sonar.html.file.suffixes", ".htm");
buildResult = orchestrator.executeBuild(scanner);
}

Expand Down Expand Up @@ -168,7 +167,8 @@ void issue_list() {
tuple("css:S1116", "css-issues-project:src/file6.vue"),
tuple("css:S5362", "css-issues-project:src/file1.css"),
tuple("css:S5362", "css-issues-project:src/file2.less"),
tuple("css:S5362", "css-issues-project:src/file3.scss")
tuple("css:S5362", "css-issues-project:src/file3.scss"),
tuple("css:S1116", "css-issues-project:src/file5-1.html")
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -42,11 +42,11 @@ public static void prepare() {

@Test
void test() {
assertThat(getMeasureAsDouble(PROJECT_KEY, "lines")).isEqualTo(43);
assertThat(getMeasureAsDouble(PROJECT_KEY, "ncloc")).isEqualTo(32);
assertThat(getMeasureAsDouble(PROJECT_KEY, "lines")).isEqualTo(89);
assertThat(getMeasureAsDouble(PROJECT_KEY, "ncloc")).isEqualTo(52);
assertThat(getMeasure(PROJECT_KEY, "ncloc_language_distribution").getValue())
.isEqualTo("css=22;web=10");
assertThat(getMeasureAsDouble(PROJECT_KEY, "comment_lines")).isEqualTo(4);
.isEqualTo("css=27;php=1;web=24");
assertThat(getMeasureAsDouble(PROJECT_KEY, "comment_lines")).isEqualTo(6);

assertThat(getMeasure(PROJECT_KEY + ":src/file1.css", "ncloc_data").getValue())
.contains("1=1;", "2=1;", "3=1;", "4=1;", "5=1;", "6=1;", "7=1");
Expand All @@ -56,5 +56,32 @@ void test() {

assertThat(getMeasure(PROJECT_KEY + ":src/file3.scss", "ncloc_data").getValue())
.contains("1=1;", "3=1;", "5=1;", "6=1;", "7=1;", "8=1");

assertThat(getMeasure(PROJECT_KEY + ":src/file4.sass", "ncloc_data").getValue())
.contains("1=1", "3=1", "5=1", "6=1", "7=1");

assertThat(getMeasure(PROJECT_KEY + ":src/file5.html", "ncloc_data").getValue())
.contains("1=1", "2=1", "3=1", "4=1", "5=1", "6=1", "7=1", "8=1", "9=1", "10=1");

// .htm is not part of sonar-HTML languages. When it is, an assertion should be added for file6.htm
// https://sonarsource.atlassian.net/jira/software/c/projects/SONARHTML/issues/SONARHTML-180

assertThat(getMeasure(PROJECT_KEY + ":src/file7.xhtml", "ncloc_data").getValue())
.contains(
"1=1",
"2=1",
"4=1",
"5=1",
"6=1",
"7=1",
"8=1",
"9=1",
"10=1",
"11=1",
"12=1",
"13=1",
"14=1",
"15=1"
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@
@ExtendWith(OrchestratorStarter.class)
class CssNoCssFileProjectTest {

private static final String PROJECT_KEY = "css-php-project";
private static final String PROJECT_KEY = "css-html-project";

private static final Orchestrator orchestrator = OrchestratorStarter.ORCHESTRATOR;

Expand All @@ -62,6 +62,6 @@ void test() {

assertThat(issuesList)
.extracting(Issues.Issue::getRule, Issues.Issue::getLine, Issues.Issue::getComponent)
.containsExactlyInAnyOrder(tuple("css:S4658", 7, "css-php-project:src/index.php"));
.containsExactlyInAnyOrder(tuple("css:S4658", 7, PROJECT_KEY + ":src/index.html"));
}
}

0 comments on commit 2472b76

Please sign in to comment.