MCPcopy Create free account
hub / github.com/angular/dev-infra / convertConditionToFunction

Function convertConditionToFunction

ng-dev/pullapprove/condition_evaluator.ts:49–76  ·  view source on GitHub ↗
(
  expr: string,
)

Source from the content-addressed store, hash-verified

47 * function can be called to check if the set of files matches the condition.
48 */
49export function convertConditionToFunction(
50 expr: string,
51): (files: string[], groups: PullApproveGroup[]) => boolean {
52 const jsExpressionStr = transformExpressionToJs(expr);
53
54 const sourceFile = ts.createSourceFile(
55 'expr.ts',
56 jsExpressionStr,
57 ts.ScriptTarget.Latest,
58 true,
59 ts.ScriptKind.TS,
60 );
61
62 return (files, groups) => {
63 const evaluationContext = Object.create(conditionEvaluationContext);
64 evaluationContext['files'] = new PullApproveStringArray(...files);
65 evaluationContext['groups'] = new PullApproveGroupArray(...groups);
66
67 const result = evaluateNode(sourceFile, evaluationContext);
68
69 // If an array is returned, we consider the condition as active if the array is not
70 // empty. This matches PullApprove's condition evaluation that is based on Python.
71 if (Array.isArray(result)) {
72 return result.length !== 0;
73 }
74 return !!result;
75 };
76}
77
78function evaluateNode(node: ts.Node, context: any): any {
79 if (ts.isSourceFile(node)) {

Callers 1

_captureConditionsMethod · 0.85

Calls 3

transformExpressionToJsFunction · 0.85
evaluateNodeFunction · 0.85
createMethod · 0.45

Tested by

no test coverage detected