( expr: string, )
| 47 | * function can be called to check if the set of files matches the condition. |
| 48 | */ |
| 49 | export 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 | |
| 78 | function evaluateNode(node: ts.Node, context: any): any { |
| 79 | if (ts.isSourceFile(node)) { |
no test coverage detected