-
Notifications
You must be signed in to change notification settings - Fork 2.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(package-rules): add DepNamePrefix matcher (#28542)
Co-authored-by: Rhys Arkins <[email protected]>
- Loading branch information
Showing
8 changed files
with
226 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,105 @@ | ||
import { DepPrefixesMatcher } from './dep-prefixes'; | ||
|
||
describe('util/package-rules/dep-prefixes', () => { | ||
const depPrefixesMatcher = new DepPrefixesMatcher(); | ||
|
||
describe('match', () => { | ||
it('should return null if matchDepPrefixes is not defined', () => { | ||
const result = depPrefixesMatcher.matches( | ||
{ | ||
depName: 'abc1', | ||
}, | ||
{ | ||
matchDepPrefixes: undefined, | ||
}, | ||
); | ||
expect(result).toBeNull(); | ||
}); | ||
|
||
it('should return false if depName is not defined', () => { | ||
const result = depPrefixesMatcher.matches( | ||
{ | ||
depName: undefined, | ||
}, | ||
{ | ||
matchDepPrefixes: ['@opentelemetry'], | ||
}, | ||
); | ||
expect(result).toBeFalse(); | ||
}); | ||
|
||
it('should return true if depName matched', () => { | ||
const result = depPrefixesMatcher.matches( | ||
{ | ||
depName: 'abc1', | ||
}, | ||
{ | ||
matchDepPrefixes: ['abc'], | ||
}, | ||
); | ||
expect(result).toBeTrue(); | ||
}); | ||
|
||
it('should return false if depName does not match', () => { | ||
const result = depPrefixesMatcher.matches( | ||
{ | ||
depName: 'abc1', | ||
}, | ||
{ | ||
matchDepPrefixes: ['def'], | ||
}, | ||
); | ||
expect(result).toBeFalse(); | ||
}); | ||
}); | ||
|
||
describe('exclude', () => { | ||
it('should return null if excludeDepPrefixes is not defined', () => { | ||
const result = depPrefixesMatcher.excludes( | ||
{ | ||
depName: 'abc1', | ||
}, | ||
{ | ||
excludeDepPrefixes: undefined, | ||
}, | ||
); | ||
expect(result).toBeNull(); | ||
}); | ||
|
||
it('should return false if depName is not defined', () => { | ||
const result = depPrefixesMatcher.excludes( | ||
{ | ||
depName: undefined, | ||
}, | ||
{ | ||
excludeDepPrefixes: ['@opentelemetry'], | ||
}, | ||
); | ||
expect(result).toBeFalse(); | ||
}); | ||
|
||
it('should return true if depName matched', () => { | ||
const result = depPrefixesMatcher.excludes( | ||
{ | ||
depName: 'abc1', | ||
}, | ||
{ | ||
excludeDepPrefixes: ['abc'], | ||
}, | ||
); | ||
expect(result).toBeTrue(); | ||
}); | ||
|
||
it('should return false if depName does not match', () => { | ||
const result = depPrefixesMatcher.excludes( | ||
{ | ||
depName: 'abc1', | ||
}, | ||
{ | ||
excludeDepPrefixes: ['def'], | ||
}, | ||
); | ||
expect(result).toBeFalse(); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
import is from '@sindresorhus/is'; | ||
import type { PackageRule, PackageRuleInputConfig } from '../../config/types'; | ||
import { Matcher } from './base'; | ||
|
||
export class DepPrefixesMatcher extends Matcher { | ||
override matches( | ||
{ depName }: PackageRuleInputConfig, | ||
{ matchDepPrefixes }: PackageRule, | ||
): boolean | null { | ||
if (is.undefined(matchDepPrefixes)) { | ||
return null; | ||
} | ||
|
||
if (is.undefined(depName)) { | ||
return false; | ||
} | ||
|
||
return matchDepPrefixes.some((prefix) => depName.startsWith(prefix)); | ||
} | ||
|
||
override excludes( | ||
{ depName }: PackageRuleInputConfig, | ||
{ excludeDepPrefixes }: PackageRule, | ||
): boolean | null { | ||
if (is.undefined(excludeDepPrefixes)) { | ||
return null; | ||
} | ||
|
||
if (is.undefined(depName)) { | ||
return false; | ||
} | ||
|
||
return excludeDepPrefixes.some((prefix) => depName.startsWith(prefix)); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters