Skip to content

JS-1845 Create rule S9451: no-irregular-whitespace - #8211

Merged
guillemsarda merged 8 commits into
masterfrom
JS-1845-new-rule-S9451
Oct 22, 2026
Merged

guillemsarda merged 8 commits into
masterfrom
JS-1845-new-rule-S9451

Conversation

@guillemsarda

@guillemsarda guillemsarda commented Oct 20, 2026 •

Copy link
Copy Markdown
Contributor

Summary by Gitar

  • Analyzer:
    • Lints code as written so invisible characters reach rules, replacing them with spaces only when parsing fails
  • Grid areas helper:
    • Extracted gridAreaStrings helper shared with S9442 for grid area string parsing
  • New rule S9451:
    • Added no-irregular-whitespace rule reporting irregular whitespace outside comments, URLs and strings
  • S9424 validation:
    • Validates invisible characters as spaces so S9451 reports them instead
  • Ruling expectations:
    • Added S9451 ruling expectations for custom CSS and mootools-core

This will update automatically on new commits.

@hashicorp-vault-sonar-prod

hashicorp-vault-sonar-prod Bot commented Oct 20, 2026 •

Copy link
Copy Markdown

JS-1845

@github-actions

github-actions Bot commented Oct 20, 2026 •

Copy link
Copy Markdown
Contributor

Ruling Report

New issues flagged (5 issues)

S9451

custom-css/S9451.css:1

>    1 | .menu .item { /* Noncompliant: no-break space before ".item" */
     2 |   padding: 8px 16px; /* Noncompliant: no-break space before "16px" */
     3 |   margin:​0; /* Noncompliant: zero-width space */

custom-css/S9451.css:2

     1 | .menu .item { /* Noncompliant: no-break space before ".item" */
>    2 |   padding: 8px 16px; /* Noncompliant: no-break space before "16px" */
     3 |   margin:​0; /* Noncompliant: zero-width space */
     4 |   grid-template-areas: "head head"; /* Noncompliant: grid area strings are checked */

custom-css/S9451.css:3

     1 | .menu .item { /* Noncompliant: no-break space before ".item" */
     2 |   padding: 8px 16px; /* Noncompliant: no-break space before "16px" */
>    3 |   margin:​0; /* Noncompliant: zero-width space */
     4 |   grid-template-areas: "head head"; /* Noncompliant: grid area strings are checked */
     5 | }

custom-css/S9451.css:4

     2 |   padding: 8px 16px; /* Noncompliant: no-break space before "16px" */
     3 |   margin:​0; /* Noncompliant: zero-width space */
>    4 |   grid-template-areas: "head head"; /* Noncompliant: grid area strings are checked */
     5 | }
     6 | 

mootools-core/Tests/Fx/Fx.html:174

   172 | 				width: 100px;
   173 | 				height: 100px;
>  174 | 			}​
   175 | 			</style>
   176 | 

guillemsarda and others added 3 commits October 21, 2026 09:31
Point the RSPEC refresh to SonarSource/rspec#8381 until it is merged.

Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
@guillemsarda
guillemsarda force-pushed the JS-1845-new-rule-S9451 branch from 718fabe to 0735eb8 Compare October 21, 2026 07:32
@datadog-sonarsource

This comment has been minimized.

@guillemsarda

Copy link
Copy Markdown
Contributor Author

/pvf

@hashicorp-vault-sonar-prod

Copy link
Copy Markdown

📊 Performance Validation complete
🆕 0 new · 🗑️ 0 lost issues
View dashboard

@gitar-bot

gitar-bot Bot commented Oct 21, 2026 •

Copy link
Copy Markdown

Reviewing your code

Code Review ✅ Approved 2 closed / 2 findings

🟡 Medium risk · Adding S9451 and retrying CSS parsing with invisible characters replaced could change diagnostics for affected stylesheets.

Adds the no-irregular-whitespace rule (S9451) reporting irregular whitespace outside comments, URLs and strings, with the analyzer now linting code as written and only replacing invisible characters with spaces when parsing fails. The ruling expectation now covers the zero-width space on line 3 of S9451.css, and the double-reporting with S9424 has been resolved. No open issues found.

✅ 2 closed
✅ Bug: Ruling expectation omits line 3 (zero-width space) of S9451.css

📄 its/ruling/src/test/resources/expected/custom-css/css-S9451.json:2-6 📄 its/sources/custom/css/S9451.css:3
Line 3 of its/sources/custom/css/S9451.css really does contain U+200B: the bytes after margin: are 342 200 213, and the line is commented /* Noncompliant: zero-width space */. The rule should report it. The tokenizer gives \u200B as a code token (delim or ident), not a comment, string or URL token, so irregularWhitespaces picks it up. The unit tests confirm that a ZWSP in code is reported (a\u200B {, and the ZWSP after a string). But css-S9451.json only lists lines 1, 2 and 4.

So either the ruling test will fail because there is an extra issue on line 3, or the rule really does miss this case and the expected file hides the false negative. Add line 3 to the expected file, or fix the rule if the expectation was produced by an actual run.

✅ Quality: Unsanitized U+200B now also triggers S9424, double-reporting S9451

📄 packages/analysis/src/css/analysis/analyzer.ts:75-83 📄 packages/analysis/src/css/analysis/analyzer.ts:157-168 📄 its/ruling/src/test/resources/expected/custom-css/css-S9424.json:1-5 📄 its/sources/custom/css/S9451.css:3
Before this commit, U+2000–U+200F were swapped for spaces before any rule ran. Now the CSS is linted as written whenever it parses, so every Stylelint rule sees the raw invisible characters, not just S9451. The new ruling file css-S9424.json shows the effect: on margin:\u200B0; (S9451.css line 3), S9424 now reports Unknown value "\u200B0" for property "margin". S9451 already flags that same character, so users get two issues for one cause, and S9424 now flags files it used to treat as clean. One way to keep S9451 seeing the raw text while other rules see sanitized input: run S9451 on the raw code and the other rules on the sanitized code. Another: have S9424 (and any other value-validating rule) drop or skip values containing these characters, so the overlap is left to S9451, the same way S9424 already leaves hex colors to S4647 and units to S4653.

Verification Rules 2/2 met Repos 4 inspected · PRs 3 inspected

Functional validation No results

Rules 2 passed 1 not applicable

✅ Internal artifact registries
✅ Repository license compliance

Broader impact Repos 4 inspected PRs 3 inspected
Cross-repo 4 repositories inspected 0 open impacts

SonarSource/sonarlint-intellij Inspected
SonarSource/sonarlint-language-server Inspected
SonarSource/sonarlint-vscode Inspected
SonarSource/sonarqube Inspected

Cross-PR 3 PRs inspected 0 open impacts

PR 8217: JS-1846 New rule S9452: media-feature-name-value-no-unknown Inspected
PR 8207: JS-1849: Implement S9450: Custom properties should not be duplicated within a declaration block Inspected
PR 8202: JS-2683: Implement S9449: Transitions should name the properties they animate Inspected

Auto-approval Not enabled

Set up

For more verification details, comment gitar display:verbose.

Options

Auto-apply is off → Gitar will not commit updates to this branch.
Display: compact → Showing compact verification results.

Comment with these commands to change the behavior for this request:

Auto-apply Compact
gitar auto-apply:on         
gitar display:verbose         

Was this helpful? React with 👍 / 👎 | Gitar

@guillemsarda
guillemsarda requested a review from nathsou October 21, 2026 09:20

@nathsou nathsou left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Image

Comment on lines +166 to +167
const sanitizedCode = replaceInvisibleCharacters(code);
return { ...(await linter.lint(filePath, sanitizedCode, ruleFileType)), code: sanitizedCode };

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[P2] Preserve S9451 findings during parser recovery

When the first parse fails, the retry runs S9451 on text where all U+2000–U+200F characters have already been replaced with spaces. This loses findings throughout the file, including characters unrelated to the parse failure. For example, using JavaScript escape notation to construct the actual CSS:

const css = 'a\u200B { color: red; } b { color: red;\u200B }';

analyzeCSS returns zero S9451 issues, whereas the first selector alone is reported. Please preserve the original source for S9451 during recovery, using the recovered AST for context and locations, and add an analyzer regression expecting both findings. Grid strings should likewise be inspected in the original text, while comments, ordinary strings, URLs, and escapes retain their exclusions.

}
const value = decl.raws.value?.raw ?? decl.value;
// invisible characters such as U+200B are validated as spaces, leaving their report to S9451
const value = replaceInvisibleCharacters(decl.raws.value?.raw ?? decl.value);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[P2] Complete S9424's delegation to S9451

replaceInvisibleCharacters only replaces U+2000–U+200F, but S9451 also owns characters such as U+00A0, U+1680, U+202F, and U+3000. With both rules enabled, the actual CSS constructed by 'a { color:\u00A0red; }' produces both S9451's whitespace issue and S9424's Unknown value issue at the same position. Replacing that one character with an ordinary space resolves both.

Please align this delegation with S9451's character policy so that the whitespace defect is reported once, while preserving S9424 findings for unrelated invalid parts of the value. Add a combined-rule regression for NBSP and representative characters outside U+2000–U+200F.

github-actions Bot and others added 2 commits October 22, 2026 08:59
Co-authored-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com>
Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
@gitar-bot

gitar-bot Bot commented Oct 22, 2026

Copy link
Copy Markdown
Code Review ✅ Approved 2 closed / 2 findings

🟡 Medium risk · Changing CSS analysis to retry parsing with sanitized invisible characters could alter rule findings or source locations for malformed stylesheets.

Adds the no-irregular-whitespace rule (S9451) reporting irregular whitespace outside comments, URLs and strings, with the analyzer now linting code as written and only replacing invisible characters with spaces when parsing fails. The ruling expectation now covers the zero-width space on line 3 of S9451.css, and the double-reporting with S9424 has been resolved. No open issues found.

✅ 2 closed
✅ Bug: Ruling expectation omits line 3 (zero-width space) of S9451.css

📄 its/ruling/src/test/resources/expected/custom-css/css-S9451.json:2-6 📄 its/sources/custom/css/S9451.css:3
Line 3 of its/sources/custom/css/S9451.css really does contain U+200B: the bytes after margin: are 342 200 213, and the line is commented /* Noncompliant: zero-width space */. The rule should report it. The tokenizer gives \u200B as a code token (delim or ident), not a comment, string or URL token, so irregularWhitespaces picks it up. The unit tests confirm that a ZWSP in code is reported (a\u200B {, and the ZWSP after a string). But css-S9451.json only lists lines 1, 2 and 4.

So either the ruling test will fail because there is an extra issue on line 3, or the rule really does miss this case and the expected file hides the false negative. Add line 3 to the expected file, or fix the rule if the expectation was produced by an actual run.

✅ Quality: Unsanitized U+200B now also triggers S9424, double-reporting S9451

📄 packages/analysis/src/css/analysis/analyzer.ts:75-83 📄 packages/analysis/src/css/analysis/analyzer.ts:157-168 📄 its/ruling/src/test/resources/expected/custom-css/css-S9424.json:1-5 📄 its/sources/custom/css/S9451.css:3
Before this commit, U+2000–U+200F were swapped for spaces before any rule ran. Now the CSS is linted as written whenever it parses, so every Stylelint rule sees the raw invisible characters, not just S9451. The new ruling file css-S9424.json shows the effect: on margin:\u200B0; (S9451.css line 3), S9424 now reports Unknown value "\u200B0" for property "margin". S9451 already flags that same character, so users get two issues for one cause, and S9424 now flags files it used to treat as clean. One way to keep S9451 seeing the raw text while other rules see sanitized input: run S9451 on the raw code and the other rules on the sanitized code. Another: have S9424 (and any other value-validating rule) drop or skip values containing these characters, so the overlap is left to S9451, the same way S9424 already leaves hex colors to S4647 and units to S4653.

Verification Rules 2/2 met Repos 4 inspected · PRs 3 inspected

Functional validation No results

Rules 2 passed 1 not applicable

✅ Internal artifact registries
✅ Repository license compliance

Broader impact Repos 4 inspected PRs 3 inspected
Cross-repo 4 repositories inspected 0 open impacts

SonarSource/sonarlint-intellij Inspected
SonarSource/sonarlint-language-server Inspected
SonarSource/sonarlint-vscode Inspected
SonarSource/sonarqube Inspected

Cross-PR 3 PRs inspected 0 open impacts

PR 8217: JS-1846 New rule S9452: media-feature-name-value-no-unknown Inspected
PR 8207: JS-1849: Implement S9450: Custom properties should not be duplicated within a declaration block Inspected
PR 8202: JS-2683: Implement S9449: Transitions should name the properties they animate Inspected

Auto-approval Not enabled

Set up

For more verification details, comment gitar display:verbose.

Options

Auto-apply is off → Gitar will not commit updates to this branch.
Display: compact → Showing compact verification results.

Comment with these commands to change the behavior for this request:

Auto-apply Compact
gitar auto-apply:on         
gitar display:verbose         

Was this helpful? React with 👍 / 👎 | Gitar

@sonarqube-next

Copy link
Copy Markdown

@guillemsarda
guillemsarda merged commit ba3d612 into master Oct 22, 2026
49 checks passed
@guillemsarda
guillemsarda deleted the JS-1845-new-rule-S9451 branch October 22, 2026 12:11
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants