Skip to content

Report hex colors of invalid length with hexColorLength - #580

Open
Usman Tahir Qureshi (UsmanQT) wants to merge 1 commit into
microsoft:mainfrom
UsmanQT:fix/81808-hex-color-length
Open

Usman Tahir Qureshi (UsmanQT) wants to merge 1 commit into
microsoft:mainfrom
UsmanQT:fix/81808-hex-color-length

Conversation

@UsmanQT

Copy link
Copy Markdown
Contributor

Fixes microsoft/vscode#81808

The css.lint.hexColorLength rule (and its SCSS/LESS counterparts) never fires, because a hex color with an invalid number of digits never reaches the linter.

a { color: #12; }
  • Before: CSS and SCSS report at-rule or selector expected and property value expected. LESS reports nothing.
  • After: all three report Hex colors must consist of three, four, six or eight hex numbers (hexColorLength) on #12.

Cause

_parseHexColor only accepted hash tokens with exactly 3, 4, 6 or 8 hex digits. Any other length was not parsed as a HexColorValue, so LintVisitor.visitHexColorValue never saw it.

Change

_parseHexColor now accepts any # followed by hex digits, as suggested in the issue, and the existing lint rule reports the invalid lengths. Hashes with non-hex characters (e.g. #12Y) are still not hex colors.

I checked the other consumers of HexColorValue: colorFromHex already returns null for lengths other than 4/5/7/9, so findDocumentColors shows no color decorator for invalid values, and the completion collectors only reuse the node text.

Tests

  • parser.test.ts (hexcolor): hex colors with 1, 2, 5 and 9 digits parse as hex colors, #FFG does not, and a { color: #12; } parses without errors.
  • lint.test.ts (new hex color length): valid lengths produce no entry, and invalid lengths produce hexColorLength for the CSS, SCSS and LESS parsers.
  • Both tests fail without the change to cssParser.ts and pass with it.
  • Full npm test (compile, lint and tests): 423 passing, 0 failing.

🤖 Generated with Claude Code

`_parseHexColor` only accepted hashes with 3, 4, 6 or 8 hex digits, so a
value like `microsoft#12` was never a HexColorValue. CSS and SCSS reported generic
parse errors instead, LESS reported nothing, and the hexColorLength lint
rule could never fire.

Accept any `#` followed by hex digits as a hex color and let the lint rule
report the invalid lengths. `colorFromHex` already returns null for those,
so no color decorator is shown for them.

Fixes microsoft/vscode#81808

Co-Authored-By: Claude <noreply@anthropic.com>
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.

[css] css.lint.hexColorLength seems to be now irrelevant

1 participant