feat(groups): handle negation for packages option #232
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.
Description
This modifies
matchesPackages
to make use of the pre-existingmatchesKnowList
helper function, thus allowingmatchesPackages
the ability to handlenegated
patterns.Justification
I ran into an issue with
isBanned
and a misunderstanding with the documentation surrounding the optionalpackages
setting.From the documentation, it stated that
packages
will utilizeminimatch
to find matches. In addition, I noted thatdependencyTypes
andspecifierTypes
supported an array ofnegated
values. I mistakenly conflated this support to includepackages
. Thus, I was planning to have an array ofnegated
package names to act as a blacklist from the ban rule, but it did not work as expected.After taking a look at the source code, I noted that unlike
matchesSpecifierTypes
ormatchesDependencyTypes
,matchesPackages
does not handlenegated
patterns well. Even thoughminimatch
is capable of handlingnegated
patterns, its implementation withinmatchesPackages
limits its capabilities.syncpack/src/guards/can-add-to-group.ts
Lines 41 to 45 in e85b234
With the current implementation, the only method to exempt packages from any rule is to use a single
negated
pattern in the array or treat thepackages
array as a whitelist. For example, either["!packageA"]
or["packageB", "packageC", "packageD"]
The former is difficult if there is no common pattern that could match all packages you'd want to exempt. The latter would be hard to maintain as you would need to constantly add to it if you create a new package in your monorepo.
As such, it seems reasonable to use the same helper function as
matchesSpecifierTypes
andmatchesDependencyTypes
to allowmatchesPackages
the ability to handle multiplenegated
patterns.I further modified
matchesKnownList
to useminimatch
instead ofincludes
for matching against the values. This extends the options forspecifierTypes
anddependencyTypes
while retaining the same behavior forpackages
of allowing scoped package patterns -- i.e.@my-scope/**
.How Can This Be Tested?
I tried writing a unit test specifically for
can-add-to-group.ts
but I was having a hard time creating the arguments forcanAddToGroup
.syncpack/src/guards/can-add-to-group.ts
Lines 8 to 11 in e85b234
I was further confused on how, if at all, I could make use of the
create-scenario
helper function:https://github.com/JamieMason/syncpack/blob/main/test/lib/create-scenario.ts
I did extend the
banned.spec.ts
test but I was unsure about adding it since the underlying changes affect allGroup
types not justBannedVersionGroup
banned.spec.ts
diffThat said, that tests still continued to pass even after the changes.