for-parser.js 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738
  1. /** 语句:for 循环(解析在此,执行在 sequence-runner) */
  2. const { assertStrictKeys } = require('../action-schema.js')
  3. const types = ['for']
  4. function parse (action, parseContext) {
  5. const path = parseContext.actionPath || 'for'
  6. assertStrictKeys(action, ['type', 'variable', 'indexVariable', 'times', 'array', 'body'], path)
  7. if (!Array.isArray(action.body)) {
  8. throw new Error(`${path}: for 须包含数组 body`)
  9. }
  10. const hasTimes = action.times != null && action.times !== ''
  11. const hasArray = action.array != null && action.array !== ''
  12. if (hasTimes === hasArray) {
  13. throw new Error(`${path}: for 须二选一:填写 times+variable,或填写 array+indexVariable+variable`)
  14. }
  15. if (hasTimes && (action.variable === undefined || action.variable === null || action.variable === '')) {
  16. throw new Error(`${path}: for 使用 times 时须提供 variable`)
  17. }
  18. if (hasArray) {
  19. if (!action.indexVariable) {
  20. throw new Error(`${path}: for 使用 array 时须提供 indexVariable`)
  21. }
  22. }
  23. return {
  24. type: 'for',
  25. variable: action.variable,
  26. indexVariable: action.indexVariable,
  27. times: hasTimes ? action.times : null,
  28. array: hasArray ? action.array : null,
  29. body: parseContext.parseActions(action.body, 'body'),
  30. }
  31. }
  32. async function execute() {
  33. return { success: true }
  34. }
  35. module.exports = { types, parse, execute }