Add three new stylelint rules for if() function validation - #247
Merged
Merged
Conversation
- 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
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
Contributor
|
| 📦 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.
This comment was marked as low quality.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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 everyif()function to include anelsecondition, ensuring a fallback value is always availableno-unreachable-if-branches: Enforces that theelsebranch must be the last branch in anif()function, preventing unreachable dead codeno-useless-if: Disallowsif()functions that only contain anelsebranch with no conditions, as these can be simplified to direct valuesEach rule includes:
Implementation Details
@projectwallace/css-parserlibrary to parse and walk CSS value treesis_function()andis_if_branch()utilities to identify and validateif()structurescorrectnessandrecommendedconfigurations@projectwallace/css-parserdependency to~0.18.6to support the required parsing utilitieshttps://claude.ai/code/session_01JSU5ue9ojbz3Zf4QV1Uq7p