* Split an AST condition into early and late parts based on which variables are referenced. * Early conditions only reference variables in the allowedVariables list. * Late conditions reference other variables.
( condition: Condition, allowedVariables: string[], )
| 1839 | * Late conditions reference other variables. |
| 1840 | */ |
| 1841 | function splitASTConditionByVariables( |
| 1842 | condition: Condition, |
| 1843 | allowedVariables: string[], |
| 1844 | ): { early?: Condition; late?: Condition } { |
| 1845 | // Handle logical operators (and, or, xor) |
| 1846 | if ( |
| 1847 | condition.type === "AndCondition" || |
| 1848 | condition.type === "OrCondition" || |
| 1849 | condition.type === "XorCondition" |
| 1850 | ) { |
| 1851 | const leftSplit = splitASTConditionByVariables( |
| 1852 | (condition as AndCondition | OrCondition | XorCondition).left, |
| 1853 | allowedVariables, |
| 1854 | ); |
| 1855 | const rightSplit = splitASTConditionByVariables( |
| 1856 | (condition as AndCondition | OrCondition | XorCondition).right, |
| 1857 | allowedVariables, |
| 1858 | ); |
| 1859 | |
| 1860 | const earlyParts = [leftSplit.early, rightSplit.early].filter( |
| 1861 | (c): c is Condition => c !== undefined, |
| 1862 | ); |
| 1863 | const lateParts = [leftSplit.late, rightSplit.late].filter( |
| 1864 | (c): c is Condition => c !== undefined, |
| 1865 | ); |
| 1866 | |
| 1867 | const buildCondition = (parts: Condition[], type: string): Condition => { |
| 1868 | if (type === "AndCondition") { |
| 1869 | return { |
| 1870 | type: "AndCondition", |
| 1871 | left: parts[0]!, |
| 1872 | right: parts[1]!, |
| 1873 | } as AndCondition; |
| 1874 | } else if (type === "OrCondition") { |
| 1875 | return { |
| 1876 | type: "OrCondition", |
| 1877 | left: parts[0]!, |
| 1878 | right: parts[1]!, |
| 1879 | } as OrCondition; |
| 1880 | } else { |
| 1881 | return { |
| 1882 | type: "XorCondition", |
| 1883 | left: parts[0]!, |
| 1884 | right: parts[1]!, |
| 1885 | } as XorCondition; |
| 1886 | } |
| 1887 | }; |
| 1888 | |
| 1889 | const early = |
| 1890 | earlyParts.length === 0 |
| 1891 | ? undefined |
| 1892 | : earlyParts.length === 1 |
| 1893 | ? earlyParts[0] |
| 1894 | : buildCondition(earlyParts, condition.type); |
| 1895 | |
| 1896 | const late = |
| 1897 | lateParts.length === 0 |
| 1898 | ? undefined |
no test coverage detected