Skip to content

Fix: CSS - Work comma selector with parens#199

Merged
github-actions[bot] merged 1 commit intomainfrom
nested-comma-selector
Apr 24, 2025
Merged

Fix: CSS - Work comma selector with parens#199
github-actions[bot] merged 1 commit intomainfrom
nested-comma-selector

Conversation

@black7375
Copy link
Contributor

@black7375 black7375 commented Apr 24, 2025

Description

Fix at paren case.

Related Issue

Summary by CodeRabbit

  • Bug Fixes

    • Resolved an issue where nested CSS selectors containing commas and parentheses were incorrectly processed, ensuring proper handling of complex selectors.
  • Tests

    • Added new tests to verify correct behavior with complex nested selectors.

Additional context

Checklist

@changeset-bot
Copy link

changeset-bot bot commented Apr 24, 2025

🦋 Changeset detected

Latest commit: 1c89ca9

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 5 packages
Name Type
@mincho-js/transform-to-vanilla Patch
@mincho-js/css Patch
@mincho-js/vite Patch
@mincho-js/react Patch
react-babel Patch

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

@black7375 black7375 added the bug Something isn't working label Apr 24, 2025
@coderabbitai
Copy link

coderabbitai bot commented Apr 24, 2025

Walkthrough

This update introduces a bug fix for the @mincho-js/transform-to-vanilla package, specifically targeting the handling of nested CSS selectors that include commas and parentheses. The core change involves replacing a naive comma-based split with a new function that properly parses selectors, ensuring that commas inside parentheses or brackets do not cause incorrect splitting. Additional tests have been added to verify the improved handling of complex selectors.

Changes

Files/Paths Change Summary
.changeset/twenty-laws-create.md Adds a changeset documenting a patch update for a bug related to nested CSS selectors with commas and parentheses.
packages/transform-to-vanilla/src/transform-keys/complex-selectors.ts Replaces simple comma split with splitSelector to handle nested selectors correctly; adds corresponding tests.

Sequence Diagram(s)

sequenceDiagram
    participant Caller
    participant complexSelectors
    Caller->>complexSelectors: nestedSelectorKey(selector)
    complexSelectors->>complexSelectors: splitSelector(selector)
    complexSelectors-->>Caller: Correctly split selectors (respecting parentheses/brackets)
Loading

Possibly related PRs

  • Fix: CSS - Work comma selector with nested selectors #195: Both PRs modify the handling of nested CSS selectors with commas in the same file and function (nestedSelectorKey in complex-selectors.ts), improving how selectors are split and expanded to correctly process complex nested selectors.

Suggested labels

bug

Poem

🐇
In tangled selectors, commas would hide,
Parentheses nested, errors implied.
With careful new splitting, the code is now wise—
No more confusion, no more surprise!
CSS flows smoothly, selectors in line,
This patch makes the styles work just fine.

Warning

There were issues while running some tools. Please review the errors and either fix the tool's configuration or disable the tool if it's a critical failure.

🔧 ESLint

If the error stems from missing dependencies, add them to the package.json file. For unrecoverable errors (e.g., due to private dependencies), disable the tool in the CodeRabbit configuration.

yarn install v1.22.22
[1/4] Resolving packages...
error Couldn't find any versions for "eslint-config-custom" that matches "workspace:^"
info Visit https://yarnpkg.com/en/docs/cli/install for documentation about this command.

✨ Finishing Touches
  • 📝 Generate Docstrings

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share
🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Generate unit testing code for this file.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai generate unit testing code for this file.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read src/utils.ts and generate unit testing code.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (Invoked using PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai generate docstrings to generate docstrings for this PR.
  • @coderabbitai generate sequence diagram to generate a sequence diagram of the changes in this PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

@github-actions
Copy link
Contributor

Triggered from #199 by @​black7375.

Checking if we can fast forward main (cdbc5a3) to nested-comma-selector (1c89ca9).

Target branch (main):

commit cdbc5a3e25b2cc857c36d9814e6c7584e93ff5ab (HEAD -> main, tag: @mincho-js/transform-to-vanilla@0.2.1, tag: @mincho-js/react@0.1.1, tag: @mincho-js/css@0.2.1, origin/main)
Author: alstjr7375 <alstjr7375@daum.net>
Date:   Wed Apr 23 03:25:29 2025 +0900

    Chore: Add cspell allow words

Pull request (nested-comma-selector):

commit 1c89ca943c9d1495230145d47cf810d820aeddbb (pull_request/nested-comma-selector)
Author: alstjr7375 <alstjr7375@daum.net>
Date:   Fri Apr 25 02:03:20 2025 +0900

    Fix: CSS - Work comma selector with parens

It is possible to fast forward main (cdbc5a3) to nested-comma-selector (1c89ca9). If you have write access to the target repository, you can add a comment with /fast-forward to fast forward main to nested-comma-selector.

Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 0

🧹 Nitpick comments (3)
packages/transform-to-vanilla/src/transform-keys/complex-selectors.ts (2)

32-82: Well-implemented selector parsing logic

The splitSelector function correctly tracks nesting levels of parentheses and brackets to only split on commas at the root level. This approach properly handles complex CSS selectors containing commas within pseudo-classes like :not(:active, :disabled) or attribute selectors.

I have a couple of minor suggestions to improve readability and robustness:

 function splitSelector(selector: string): string[] {
   if (!selector.includes(",")) {
     return [selector];
   }

   const result = [];
   let currentSelector = "";
+  // Track nesting level of parentheses and brackets
   let parenLevel = 0;
   let bracketLevel = 0;

   const selectorLength = selector.length;
   for (let i = 0; i < selectorLength; i++) {
     const char = selector[i];

     switch (char) {
       case "(":
         parenLevel++;
         currentSelector += char;
         break;
       case ")":
         parenLevel--;
+        // Optional: Handle unbalanced parentheses
+        if (parenLevel < 0) parenLevel = 0;
         currentSelector += char;
         break;
       case "[":
         bracketLevel++;
         currentSelector += char;
         break;
       case "]":
         bracketLevel--;
+        // Optional: Handle unbalanced brackets
+        if (bracketLevel < 0) bracketLevel = 0;
         currentSelector += char;
         break;
       case ",":
         if (parenLevel === 0 && bracketLevel === 0) {
           result.push(currentSelector);
           currentSelector = "";
         } else {
           currentSelector += char;
         }
         break;
       default:
         currentSelector += char;
         break;
     }
   }

   if (currentSelector.trim() !== "") {
     result.push(currentSelector);
   }

   return result;
 }

144-155: Excellent test coverage for complex selectors

The added test thoroughly verifies the handling of complex nested selectors with commas inside parentheses and brackets. This is a good addition that helps ensure the code is working correctly.

You might consider adding one more test case for edge cases like empty selectors or selectors with escaped commas, but this is comprehensive enough for the current fix.

.changeset/twenty-laws-create.md (1)

1-6: Good changeset documentation

The changeset correctly documents the patch with a clear description of the bug being fixed.

Consider using a proper Markdown heading instead of emphasis for better formatting:

---
"@mincho-js/transform-to-vanilla": patch
---

-**Nested Selector with commas and parens**
+## Nested Selector with commas and parens
- Fixes an error that occurs when parentheses are present.
🧰 Tools
🪛 markdownlint-cli2 (0.17.2)

5-5: Emphasis used instead of a heading
null

(MD036, no-emphasis-as-heading)

📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between cdbc5a3 and 1c89ca9.

📒 Files selected for processing (2)
  • .changeset/twenty-laws-create.md (1 hunks)
  • packages/transform-to-vanilla/src/transform-keys/complex-selectors.ts (3 hunks)
🧰 Additional context used
🧬 Code Graph Analysis (1)
packages/transform-to-vanilla/src/transform-keys/complex-selectors.ts (2)
packages/transform-to-vanilla/src/transform-object/index.ts (2)
  • TransformContext (58-68)
  • initTransformContext (69-82)
packages/transform-to-vanilla/src/index.ts (2)
  • TransformContext (4-4)
  • initTransformContext (3-3)
🪛 markdownlint-cli2 (0.17.2)
.changeset/twenty-laws-create.md

5-5: Emphasis used instead of a heading
null

(MD036, no-emphasis-as-heading)

⏰ Context from checks skipped due to timeout of 90000ms (1)
  • GitHub Check: Analyze (javascript-typescript)
🔇 Additional comments (1)
packages/transform-to-vanilla/src/transform-keys/complex-selectors.ts (1)

19-19: Good improvement to handle complex selectors

Replacing the naive comma split with a specialized splitSelector function will properly handle commas within parentheses and brackets, fixing the issue mentioned in the PR description.

@black7375
Copy link
Contributor Author

/fast-forward

@github-actions
Copy link
Contributor

Triggered from #199 (comment) by @​black7375.

Trying to fast forward main (cdbc5a3) to nested-comma-selector (1c89ca9).

Target branch (main):

commit cdbc5a3e25b2cc857c36d9814e6c7584e93ff5ab (HEAD -> main, tag: @mincho-js/transform-to-vanilla@0.2.1, tag: @mincho-js/react@0.1.1, tag: @mincho-js/css@0.2.1, origin/main)
Author: alstjr7375 <alstjr7375@daum.net>
Date:   Wed Apr 23 03:25:29 2025 +0900

    Chore: Add cspell allow words

Pull request (nested-comma-selector):

commit 1c89ca943c9d1495230145d47cf810d820aeddbb (pull_request/nested-comma-selector)
Author: alstjr7375 <alstjr7375@daum.net>
Date:   Fri Apr 25 02:03:20 2025 +0900

    Fix: CSS - Work comma selector with parens

Fast forwarding main (cdbc5a3) to nested-comma-selector (1c89ca9).

$ git push origin 1c89ca943c9d1495230145d47cf810d820aeddbb:main
To https://github.com/mincho-js/mincho.git
   cdbc5a3..1c89ca9  1c89ca943c9d1495230145d47cf810d820aeddbb -> main

@github-actions github-actions bot merged commit 1c89ca9 into main Apr 24, 2025
10 checks passed
@github-actions github-actions bot deleted the nested-comma-selector branch April 24, 2025 17:09
@github-actions github-actions bot mentioned this pull request Apr 24, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working

Projects

Status: Done

Development

Successfully merging this pull request may close these issues.

1 participant