-
Notifications
You must be signed in to change notification settings - Fork 2.5k
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): matchNewValue (#27374)
- Loading branch information
1 parent
5585818
commit 7e7124e
Showing
8 changed files
with
184 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
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,65 @@ | ||
import { NewValueMatcher } from './new-value'; | ||
|
||
describe('util/package-rules/new-value', () => { | ||
const matcher = new NewValueMatcher(); | ||
|
||
describe('match', () => { | ||
it('return null if non-regex', () => { | ||
const result = matcher.matches( | ||
{ | ||
newValue: '"~> 1.1.0"', | ||
}, | ||
{ | ||
matchNewValue: '^v', | ||
}, | ||
); | ||
expect(result).toBeFalse(); | ||
}); | ||
|
||
it('return false for regex version non match', () => { | ||
const result = matcher.matches( | ||
{ | ||
newValue: '"~> 1.1.0"', | ||
}, | ||
{ | ||
matchNewValue: '/^v/', | ||
}, | ||
); | ||
expect(result).toBeFalse(); | ||
}); | ||
|
||
it('case insensitive match', () => { | ||
const result = matcher.matches( | ||
{ | ||
newValue: '"V1.1.0"', | ||
}, | ||
{ | ||
matchNewValue: '/^"v/i', | ||
}, | ||
); | ||
expect(result).toBeTrue(); | ||
}); | ||
|
||
it('return true for regex version match', () => { | ||
const result = matcher.matches( | ||
{ | ||
newValue: '"~> 0.1.0"', | ||
}, | ||
{ | ||
matchNewValue: '/^"/', | ||
}, | ||
); | ||
expect(result).toBeTrue(); | ||
}); | ||
|
||
it('return false for now value', () => { | ||
const result = matcher.matches( | ||
{}, | ||
{ | ||
matchNewValue: '/^v?[~ -]?0/', | ||
}, | ||
); | ||
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,31 @@ | ||
import is from '@sindresorhus/is'; | ||
import type { PackageRule, PackageRuleInputConfig } from '../../config/types'; | ||
import { logger } from '../../logger'; | ||
import { getRegexPredicate } from '../string-match'; | ||
import { Matcher } from './base'; | ||
|
||
export class NewValueMatcher extends Matcher { | ||
override matches( | ||
{ newValue }: PackageRuleInputConfig, | ||
{ matchNewValue }: PackageRule, | ||
): boolean | null { | ||
if (is.undefined(matchNewValue)) { | ||
return null; | ||
} | ||
const matchNewValuePred = getRegexPredicate(matchNewValue); | ||
|
||
if (!matchNewValuePred) { | ||
logger.debug( | ||
{ matchNewValue }, | ||
'matchNewValue should be a regex, starting and ending with `/`', | ||
); | ||
return false; | ||
} | ||
|
||
if (!newValue) { | ||
return false; | ||
} | ||
|
||
return matchNewValuePred(newValue); | ||
} | ||
} |