-
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(packageRules): matchJsonata (#31826)
Co-authored-by: Sebastian Poxhofer <[email protected]>
- Loading branch information
Showing
8 changed files
with
178 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
import { JsonataMatcher } from './jsonata'; | ||
|
||
describe('util/package-rules/jsonata', () => { | ||
const matcher = new JsonataMatcher(); | ||
|
||
it('should return true for a matching JSONata expression', async () => { | ||
const result = await matcher.matches( | ||
{ depName: 'lodash' }, | ||
{ matchJsonata: ['depName = "lodash"'] }, | ||
); | ||
expect(result).toBeTrue(); | ||
}); | ||
|
||
it('should return false for a non-matching JSONata expression', async () => { | ||
const result = await matcher.matches( | ||
{ depName: 'lodash' }, | ||
{ matchJsonata: ['depName = "react"'] }, | ||
); | ||
expect(result).toBeFalse(); | ||
}); | ||
|
||
it('should return false for an invalid JSONata expression', async () => { | ||
const result = await matcher.matches( | ||
{ depName: 'lodash' }, | ||
{ matchJsonata: ['depName = '] }, | ||
); | ||
expect(result).toBeFalse(); | ||
}); | ||
|
||
it('should return null if matchJsonata is not defined', async () => { | ||
const result = await matcher.matches({ depName: 'lodash' }, {}); | ||
expect(result).toBeNull(); | ||
}); | ||
|
||
it('should return true for a complex JSONata expression', async () => { | ||
const result = await matcher.matches( | ||
{ depName: 'lodash', version: '4.17.21' }, | ||
{ matchJsonata: ['depName = "lodash" and version = "4.17.21"'] }, | ||
); | ||
expect(result).toBeTrue(); | ||
}); | ||
|
||
it('should return false for a complex JSONata expression with non-matching version', async () => { | ||
const result = await matcher.matches( | ||
{ depName: 'lodash', version: '4.17.20' }, | ||
{ matchJsonata: ['depName = "lodash" and version = "4.17.21"'] }, | ||
); | ||
expect(result).toBeFalse(); | ||
}); | ||
|
||
it('should return true for a JSONata expression with nested properties', async () => { | ||
const result = await matcher.matches( | ||
{ dep: { name: 'lodash', version: '4.17.21' } }, | ||
{ matchJsonata: ['dep.name = "lodash" and dep.version = "4.17.21"'] }, | ||
); | ||
expect(result).toBeTrue(); | ||
}); | ||
|
||
it('should return false for a JSONata expression with nested properties and non-matching version', async () => { | ||
const result = await matcher.matches( | ||
{ dep: { name: 'lodash', version: '4.17.20' } }, | ||
{ matchJsonata: ['dep.name = "lodash" and dep.version = "4.17.21"'] }, | ||
); | ||
expect(result).toBeFalse(); | ||
}); | ||
|
||
it('should return true if any JSONata expression matches', async () => { | ||
const result = await matcher.matches( | ||
{ depName: 'lodash' }, | ||
{ matchJsonata: ['depName = "react"', 'depName = "lodash"'] }, | ||
); | ||
expect(result).toBeTrue(); | ||
}); | ||
|
||
it('should catch evaluate errors', async () => { | ||
const result = await matcher.matches( | ||
{ depName: 'lodash' }, | ||
{ matchJsonata: ['$notafunction()'] }, | ||
); | ||
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,37 @@ | ||
import type { PackageRule, PackageRuleInputConfig } from '../../config/types'; | ||
import { logger } from '../../logger'; | ||
import { getExpression } from '../jsonata'; | ||
import { Matcher } from './base'; | ||
|
||
export class JsonataMatcher extends Matcher { | ||
override async matches( | ||
inputConfig: PackageRuleInputConfig, | ||
{ matchJsonata }: PackageRule, | ||
): Promise<boolean | null> { | ||
if (!matchJsonata) { | ||
return null; | ||
} | ||
|
||
for (const expressionStr of matchJsonata) { | ||
const expression = getExpression(expressionStr); | ||
if (expression instanceof Error) { | ||
logger.warn( | ||
{ errorMessage: expression.message }, | ||
'Invalid JSONata expression', | ||
); | ||
} else { | ||
try { | ||
const result = await expression.evaluate(inputConfig); | ||
if (result) { | ||
// Only one needs to match, so return early | ||
return true; | ||
} | ||
} catch (err) { | ||
logger.warn({ err }, 'Error evaluating JSONata expression'); | ||
} | ||
} | ||
} | ||
// None matched, so return false | ||
return false; | ||
} | ||
} |
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