|
| 1 | +import { Children, isValidElement } from "react"; |
| 2 | +import { LogicErrors } from "../fixtures"; |
| 3 | + |
| 4 | +/** |
| 5 | + * Validate if the children of the If directive are valid. |
| 6 | + * @param children |
| 7 | + * @returns |
| 8 | + */ |
| 9 | +const validateSwitchBlocks: ValidatorFn = (children) => { |
| 10 | + const _children = Children.toArray(children); |
| 11 | + const errors: LogicErrors[] = []; |
| 12 | + |
| 13 | + if (_children.length === 0) { |
| 14 | + errors.push(LogicErrors.ChildrenExpected); |
| 15 | + return errors; |
| 16 | + } |
| 17 | + |
| 18 | + _children.forEach((child) => { |
| 19 | + // @ts-expect-error |
| 20 | + const typeName = isValidElement(child) ? child.type.name : "unknown"; |
| 21 | + |
| 22 | + // If can't have a direct child of If, ElseIf, Else |
| 23 | + if (typeName === "If" || typeName === "ElseIf" || typeName === "Else") { |
| 24 | + errors.push(LogicErrors.SwitchBlockExpected); |
| 25 | + } |
| 26 | + }); |
| 27 | + |
| 28 | + return errors; |
| 29 | +}; |
| 30 | + |
| 31 | +const validateSwitch: ValidatorFn = (children) => { |
| 32 | + const _children = Children.toArray(children); |
| 33 | + const errors: LogicErrors[] = []; |
| 34 | + const elements: Record<string, number> = {}; |
| 35 | + |
| 36 | + if (_children.length === 0) { |
| 37 | + errors.push(LogicErrors.IfBlockExpected); |
| 38 | + return errors; |
| 39 | + } |
| 40 | + |
| 41 | + _children.forEach((child, index) => { |
| 42 | + // @ts-expect-error |
| 43 | + const typeName = isValidElement(child) ? child.type.name : "unknown"; |
| 44 | + |
| 45 | + const count = elements[typeName] ?? 0; |
| 46 | + elements[typeName] = count + 1; |
| 47 | + |
| 48 | + validateIfBlock(typeName, index, elements, errors); |
| 49 | + validateElseBlock(typeName, index, _children.length, elements, errors); |
| 50 | + validateElseIfBlock(typeName, index, errors); |
| 51 | + validateInvalidElement(typeName, errors); |
| 52 | + }); |
| 53 | + |
| 54 | + if (!elements["If"]) { |
| 55 | + errors.push(LogicErrors.IfBlockExpected); |
| 56 | + } |
| 57 | + |
| 58 | + return errors; |
| 59 | +}; |
| 60 | + |
| 61 | +const validateIfBlock = (typeName: string, index: number, elements: Record<string, number>, errors: LogicErrors[]) => { |
| 62 | + if (typeName === "If") { |
| 63 | + if (index !== 0) { |
| 64 | + errors.push(LogicErrors.InvalidIfBlockOrdinal); |
| 65 | + } |
| 66 | + |
| 67 | + if (elements["If"] > 1) { |
| 68 | + errors.push(LogicErrors.OnlyOneIfBlockExpected); |
| 69 | + } |
| 70 | + } |
| 71 | +}; |
| 72 | + |
| 73 | +const validateElseBlock = ( |
| 74 | + typeName: string, |
| 75 | + index: number, |
| 76 | + length: number, |
| 77 | + elements: Record<string, number>, |
| 78 | + errors: LogicErrors[] |
| 79 | +) => { |
| 80 | + if (typeName === "Else") { |
| 81 | + if (elements["Else"] > 1) { |
| 82 | + errors.push(LogicErrors.OnlyOneElseBlockExpected); |
| 83 | + } |
| 84 | + if (index === 0 || index !== length - 1) { |
| 85 | + errors.push(LogicErrors.InvalidElseBlockOrdinal); |
| 86 | + } |
| 87 | + } |
| 88 | +}; |
| 89 | + |
| 90 | +const validateElseIfBlock = (typeName: string, index: number, errors: LogicErrors[]) => { |
| 91 | + if (typeName === "ElseIf" && index === 0) { |
| 92 | + errors.push(LogicErrors.InvalidElseIfBlockOrdinal); |
| 93 | + } |
| 94 | +}; |
| 95 | + |
| 96 | +const validateInvalidElement = (typeName: string, errors: LogicErrors[]) => { |
| 97 | + if (typeName !== "If" && typeName !== "ElseIf" && typeName !== "Else") { |
| 98 | + errors.push(LogicErrors.InvalidElement); |
| 99 | + } |
| 100 | +}; |
| 101 | + |
| 102 | +export class ValidationFactory { |
| 103 | + static get(validator: string) { |
| 104 | + switch (validator) { |
| 105 | + case "Switch": |
| 106 | + return validateSwitch; |
| 107 | + case "If": |
| 108 | + case "ElseIf": |
| 109 | + case "Else": |
| 110 | + return validateSwitchBlocks; |
| 111 | + default: |
| 112 | + return () => []; |
| 113 | + } |
| 114 | + } |
| 115 | +} |
0 commit comments