Skip to content

Add three new stylelint rules for if() function validation - #247

Merged
bartveneman merged 6 commits into
mainfrom
claude/stylelint-if-else-rules-3u6m4a
Aug 16, 2026
Merged

bartveneman merged 6 commits into
mainfrom
claude/stylelint-if-else-rules-3u6m4a

Conversation

@bartveneman

Copy link
Copy Markdown
Member

Summary

This PR adds three new stylelint rules to validate the structure and usage of CSS if() functions, improving code quality and preventing common mistakes.

Key Changes

  • no-missing-if-else: Requires every if() function to include an else condition, ensuring a fallback value is always available
  • no-unreachable-if-branches: Enforces that the else branch must be the last branch in an if() function, preventing unreachable dead code
  • no-useless-if: Disallows if() functions that only contain an else branch with no conditions, as these can be simplified to direct values

Each rule includes:

  • Complete implementation with proper validation and error reporting
  • Comprehensive test suite covering valid and invalid cases
  • Detailed README documentation with examples and rationale

Implementation Details

  • All three rules leverage the @projectwallace/css-parser library to parse and walk CSS value trees
  • Rules use the is_function() and is_if_branch() utilities to identify and validate if() structures
  • Proper error positioning is maintained to help developers locate issues
  • Rules are added to both correctness and recommended configurations
  • Updated @projectwallace/css-parser dependency to ~0.18.6 to support the required parsing utilities

https://claude.ai/code/session_01JSU5ue9ojbz3Zf4QV1Uq7p

claude added 6 commits August 16, 2026 14:14
- Bump @projectwallace/css-parser to ~0.18.6 to get CSS if()/else parsing support (IfBranch/IfCondition nodes)
- Add no-missing-if-else: requires every if() function to include an else condition
- Add no-unreachable-if-branches: requires the else branch of an if() function to always be the last branch
- Register both rules in the recommended and correctness config presets

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01JSU5ue9ojbz3Zf4QV1Uq7p
Break if()'s condition/else branches onto their own indented lines in
the README examples instead of cramming them on one line. Also removes
the prior-art link from no-unreachable-if-branches since it duplicated
the intro paragraph's context.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01JSU5ue9ojbz3Zf4QV1Uq7p
.children builds a fresh array on every access (it walks first_child /
next_sibling internally), so .filter()/.some() on it allocated twice
per if(). Both rules now iterate the node directly via for...of and
check has_next, which walks the same linked list without allocating.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01JSU5ue9ojbz3Zf4QV1Uq7p
Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01JSU5ue9ojbz3Zf4QV1Uq7p
Flags if() functions whose only branch is else — since else always
matches, such an if() is equivalent to writing the value directly, with
none of the (false) implication that the declaration is conditional.
Registered in the recommended and correctness config presets.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01JSU5ue9ojbz3Zf4QV1Uq7p
@github-actions

Copy link
Copy Markdown
Contributor

⚠️ Package Size Increase

📦 Package 📏 Base Size 📏 Source Size 📈 Size Change
@projectwallace/stylelint-plugin 28.6 kB 29.4 kB +864 B

This comment was marked as low quality.

@bartveneman
bartveneman merged commit 124656b into main Aug 16, 2026
13 of 14 checks passed
@bartveneman
bartveneman deleted the claude/stylelint-if-else-rules-3u6m4a branch August 16, 2026 14:42
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