Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add selector support to require-baseline #61

Open
wants to merge 3 commits into
base: main
Choose a base branch
from

Conversation

rviscomi
Copy link

@rviscomi rviscomi commented Feb 22, 2025

Prerequisites checklist

What is the purpose of this pull request?

Add support for validating the Baseline status of selectors like :has()

What changes did you make? (Give an overview)

  • added a new Selector handler that checks the validity of all child nodes
  • added tests for pseudo-class and pseudo-element selectors
  • updated docs

Related Issues

Fixes #60

Is there anything you'd like reviewers to focus on?

Copy link

linux-foundation-easycla bot commented Feb 22, 2025

CLA Signed

The committers listed above are authorized under a signed CLA.

@rviscomi rviscomi changed the title feat: add selector support feat: add selector support to require-baseline Feb 22, 2025
@rviscomi
Copy link
Author

rviscomi commented Feb 22, 2025

I wasn't sure how to handle selectors used within @supports. It seems like CSSTree always parses them as raw text rather than FeatureFunction as suggested in the docs.

Example CSS:

@supports selector(.example) {}

AST:

{
  "type": "Function",
  "loc": {
    "source": "<unknown>",
    "start": {
      "offset": 10,
      "line": 1,
      "column": 11
    },
    "end": {
      "offset": 28,
      "line": 1,
      "column": 29
    }
  },
  "name": "selector",
  "children": [
    {
      "type": "Raw",
      "loc": {
        "source": "<unknown>",
        "start": {
          "offset": 19,
          "line": 1,
          "column": 20
        },
        "end": {
          "offset": 27,
          "line": 1,
          "column": 28
        }
      },
      "value": ".example"
    }
  ]
}

https://astexplorer.net/#/gist/244e2fb4da940df52bf0f4b94277db44/ff8f2e9e7ca35af89743f13ae98f37a69e501e9d

Copy link
Member

@nzakas nzakas left a comment

Choose a reason for hiding this comment

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

Thanks for getting this started. The basics are here, there are a couple of edge cases that need addressing.

continue;
}

const ruleLevel = selectors.get(selector);
Copy link
Member

Choose a reason for hiding this comment

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

This can be undefined if someone is using an unknown selector. In that case, we just want to exit without reporting anything.

Copy link
Author

Choose a reason for hiding this comment

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

Is this addressed by the check above?

if (!selectors.has(selector)) {
  continue;
}

@@ -625,6 +628,48 @@ export default {
});
}
},

Selector(node) {
Copy link
Member

Choose a reason for hiding this comment

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

This matches all selectors, including those in @supports:

@supports selector(:nth-child(1 of .foo)) {
  /* Styles */
}

When in @supports, we should not warn because they are checking for browser support.

Also related to @supports: If someone is checking if a selector is supported then we should not warn on any selectors used inside of that @supports. We already do this for property names and values, so we should do the same for selectors.

Copy link
Author

Choose a reason for hiding this comment

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

Based on #61 (comment) it doesn't seem like CSSTree parses selectors within @supports preludes. So if I understand correctly, while we should be able to tell whether the selector is used somewhere within a @support rule, we won't necessarily know if the selector was used in the @support condition itself.

For example, if an unrelated property is used in the condition, the non-Baseline feature could still be used in the declaration block:

@supports (color: red) {
	h1:has(+ h2) {
		color: red;
	}
}

Is that an acceptable false negative? If so I can update the code and add a note in the docs.

Copy link
Author

Choose a reason for hiding this comment

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

Ah I just realized that the astexplorer.net tool I was using to check the AST is stuck on v2.0.1 and the selector support we need was added to v3.0.0. I'll take another look at this.

Copy link
Author

Choose a reason for hiding this comment

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

Updated

@rviscomi rviscomi requested a review from nzakas February 24, 2025 19:04
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Bug: :has() not flagged with Baseline Widely available config
2 participants