Skip to content

Conversation

iansan5653
Copy link
Contributor

@iansan5653 iansan5653 commented Sep 6, 2024

The isPrimerComponent helper uses the regex /^@primer\/react/ to see if the import path is from @primer/react. At first glance this appears fine, but it's critically missing an 'end of string' token, meaning that it will match any path starting with @primer/react. This includes @primer/react-brand - one of the exact paths we are trying not to match in the no-unnecessary-components rule.

The no-unnecessary-components rule did have a unit test for this case, but the unit test was incorrectly configured to import from @primer/brand instead of @primer/react-brand. I've updated the test.

However, per the unit test in direct-slot-children.test.js (nice catch!), we do want to include imports from sub-paths of @primer/react such as @primer/react/drafts. So we want an expression that accounts for either exactly @primer/react or anything in the form @primer/react/**.

So this PR updates the regex to /^@primer\/react(?:$|\/)/. The (?:$|\/) is a non-capturing group that matches either the end of the string ($), or a forward slash (\/). If it matches a forward slash, anything is permitted after that point.

Copy link

changeset-bot bot commented Sep 6, 2024

🦋 Changeset detected

Latest commit: 20c0335

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

This PR includes changesets to release 1 package
Name Type
eslint-plugin-primer-react 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

@iansan5653 iansan5653 marked this pull request as ready for review September 6, 2024 15:26
@iansan5653 iansan5653 requested a review from a team as a code owner September 6, 2024 15:26
@iansan5653 iansan5653 requested a review from keithamus September 6, 2024 15:26
@iansan5653 iansan5653 changed the title Update isPrimerComponent helper to only match @primer/react exactly Update isPrimerComponent helper to only match @primer/react or @primer/react/* Sep 6, 2024
@iansan5653 iansan5653 enabled auto-merge (squash) September 6, 2024 15:29
@iansan5653 iansan5653 merged commit e55d3d3 into main Sep 6, 2024
8 checks passed
@iansan5653 iansan5653 deleted the fix-is-primer-component branch September 6, 2024 15:59
@primer-css primer-css mentioned this pull request Sep 6, 2024
# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants