Skip to content

[bug][stylelint] rule fails on multiline token fallback values #184

Description

@zeroedin

Problem

The rhds/token-values stylelint rule reports a false positive when a token's fallback value is formatted across multiple lines. The rule compares the authored value (with whitespace preserved) against the canonical single-line value from tokens.get(name), so any multiline formatting causes a mismatch.

For example, this is flagged as an error:

color:
  var(--rh-color-text-primary,
    light-dark(
      var(--rh-color-text-primary-on-light, #151515),
      var(--rh-color-text-primary-on-dark, #ffffff)
    )
  );

Even though the value is semantically identical to the single-line form:

color: var(--rh-color-text-primary, light-dark(var(--rh-color-text-primary-on-light, #151515), var(--rh-color-text-primary-on-dark, #ffffff)));

Root cause

In token-values.ts, the comparison at line ~47 uses parser.stringify(values) which preserves authored whitespace (newlines, indentation). The expected value from tokens.get(name) is always single-line. The two never match when the authored CSS is multiline.

Suggested fix

Normalize whitespace before comparing:

function normalizeWhitespace(s: string) {
  return s.replace(/\s+/g, ' ').trim();
}

Then update the comparison:

- if ((expected as string)?.toString() !== actual) {
+ if (normalizeWhitespace((expected as string)?.toString()) !== normalizeWhitespace(actual)) {

The fix() function does not need changes since it already writes the canonical single-line value. With the normalized comparison, fix() will only fire when the actual token value is wrong -- not when only the whitespace formatting differs. This avoids a conflict where fix() collapses multiline values to a single line, which could then trigger a line-length violation.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions