Update isPrimerComponent helper to only match @primer/react or @primer/react/*
#218
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.
The
isPrimerComponenthelper 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 theno-unnecessary-componentsrule.The
no-unnecessary-componentsrule did have a unit test for this case, but the unit test was incorrectly configured to import from@primer/brandinstead 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/reactsuch as@primer/react/drafts. So we want an expression that accounts for either exactly@primer/reactor 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.