| 84 | let wip = new Set<AstNode>() |
| 85 | |
| 86 | function visit(node: AstNode, path: AstNode[] = []) { |
| 87 | if (seen.has(node)) { |
| 88 | return |
| 89 | } |
| 90 | |
| 91 | // Circular dependency detected |
| 92 | if (wip.has(node)) { |
| 93 | // Next node in the path is the one that caused the circular dependency |
| 94 | let next = path[(path.indexOf(node) + 1) % path.length] |
| 95 | |
| 96 | if ( |
| 97 | node.kind === 'at-rule' && |
| 98 | node.name === '@utility' && |
| 99 | next.kind === 'at-rule' && |
| 100 | next.name === '@utility' |
| 101 | ) { |
| 102 | walk(node.nodes, (child) => { |
| 103 | if (child.kind !== 'at-rule' || child.name !== '@apply') return |
| 104 | |
| 105 | let candidates = child.params.split(/\s+/g) |
| 106 | for (let candidate of candidates) { |
| 107 | for (let candidateAstNode of designSystem.parseCandidate(candidate)) { |
| 108 | switch (candidateAstNode.kind) { |
| 109 | case 'arbitrary': |
| 110 | break |
| 111 | |
| 112 | case 'static': |
| 113 | case 'functional': |
| 114 | if (next.params.replace(/-\*$/, '') === candidateAstNode.root) { |
| 115 | throw new Error( |
| 116 | `You cannot \`@apply\` the \`${candidate}\` utility here because it creates a circular dependency.`, |
| 117 | ) |
| 118 | } |
| 119 | break |
| 120 | |
| 121 | default: |
| 122 | candidateAstNode satisfies never |
| 123 | } |
| 124 | } |
| 125 | } |
| 126 | }) |
| 127 | } |
| 128 | |
| 129 | // Generic fallback error in case we cannot properly detect the origin of |
| 130 | // the circular dependency. |
| 131 | throw new Error( |
| 132 | `Circular dependency detected:\n\n${toCss([node])}\nRelies on:\n\n${toCss([next])}`, |
| 133 | ) |
| 134 | } |
| 135 | |
| 136 | wip.add(node) |
| 137 | |
| 138 | for (let dependencyId of dependencies.get(node)) { |
| 139 | for (let dependency of definitions.get(dependencyId)) { |
| 140 | path.push(node) |
| 141 | visit(dependency, path) |
| 142 | path.pop() |
| 143 | } |