if-parser.js 881 B

1234567891011121314151617181920212223242526
  1. /** 语句:if 条件判断(解析在此,执行在 sequence-runner);须同时含 then、else 数组(可无分支写 []) */
  2. const { assertStrictKeys } = require('../action-schema.js')
  3. const types = ['if']
  4. function parse (action, parseContext) {
  5. const path = parseContext.actionPath || 'if'
  6. assertStrictKeys(action, ['type', 'condition', 'then', 'else'], path)
  7. if (!Array.isArray(action.then)) {
  8. throw new Error(`${path}: if 须包含数组 then`)
  9. }
  10. if (!Array.isArray(action.else)) {
  11. throw new Error(`${path}: if 须包含数组 else(无 else 分支写 [])`)
  12. }
  13. return {
  14. type: 'if',
  15. condition: action.condition,
  16. then: parseContext.parseActions(action.then, 'then'),
  17. else: parseContext.parseActions(action.else, 'else'),
  18. }
  19. }
  20. async function execute() {
  21. return { success: true }
  22. }
  23. module.exports = { types, parse, execute }