|
1 | | -import { parseAtRule } from './parse' |
2 | 1 | import type { SourceLocation } from './source' |
3 | | -import type { VisitContext } from '../util/walk' |
| 2 | +import { parseAtRule } from './parse' |
4 | 3 |
|
5 | 4 | const AT_SIGN = 0x40 |
6 | 5 |
|
@@ -117,95 +116,3 @@ export function atRoot(nodes: AstNode[]): AtRoot { |
117 | 116 | nodes, |
118 | 117 | } |
119 | 118 | } |
120 | | - |
121 | | -export function cloneAstNode<T extends AstNode>(node: T): T { |
122 | | - switch (node.kind) { |
123 | | - case 'rule': |
124 | | - return { |
125 | | - kind: node.kind, |
126 | | - selector: node.selector, |
127 | | - nodes: node.nodes.map(cloneAstNode), |
128 | | - src: node.src, |
129 | | - dst: node.dst, |
130 | | - } satisfies StyleRule as T |
131 | | - |
132 | | - case 'at-rule': |
133 | | - return { |
134 | | - kind: node.kind, |
135 | | - name: node.name, |
136 | | - params: node.params, |
137 | | - nodes: node.nodes.map(cloneAstNode), |
138 | | - src: node.src, |
139 | | - dst: node.dst, |
140 | | - } satisfies AtRule as T |
141 | | - |
142 | | - case 'at-root': |
143 | | - return { |
144 | | - kind: node.kind, |
145 | | - nodes: node.nodes.map(cloneAstNode), |
146 | | - src: node.src, |
147 | | - dst: node.dst, |
148 | | - } satisfies AtRoot as T |
149 | | - |
150 | | - case 'context': |
151 | | - return { |
152 | | - kind: node.kind, |
153 | | - context: { ...node.context }, |
154 | | - nodes: node.nodes.map(cloneAstNode), |
155 | | - src: node.src, |
156 | | - dst: node.dst, |
157 | | - } satisfies Context as T |
158 | | - |
159 | | - case 'declaration': |
160 | | - return { |
161 | | - kind: node.kind, |
162 | | - property: node.property, |
163 | | - value: node.value, |
164 | | - important: node.important, |
165 | | - src: node.src, |
166 | | - dst: node.dst, |
167 | | - } satisfies Declaration as T |
168 | | - |
169 | | - case 'comment': |
170 | | - return { |
171 | | - kind: node.kind, |
172 | | - value: node.value, |
173 | | - src: node.src, |
174 | | - dst: node.dst, |
175 | | - } satisfies Comment as T |
176 | | - |
177 | | - default: |
178 | | - node satisfies never |
179 | | - throw new Error(`Unknown node kind: ${(node as any).kind}`) |
180 | | - } |
181 | | -} |
182 | | - |
183 | | -export function cssContext( |
184 | | - ctx: VisitContext<AstNode>, |
185 | | -): VisitContext<AstNode> & { context: Record<string, string | boolean> } { |
186 | | - return { |
187 | | - depth: ctx.depth, |
188 | | - get context() { |
189 | | - let context: Record<string, string | boolean> = {} |
190 | | - for (let child of ctx.path()) { |
191 | | - if (child.kind === 'context') { |
192 | | - Object.assign(context, child.context) |
193 | | - } |
194 | | - } |
195 | | - |
196 | | - // Once computed, we never need to compute this again |
197 | | - Object.defineProperty(this, 'context', { value: context }) |
198 | | - return context |
199 | | - }, |
200 | | - get parent() { |
201 | | - let parent = (this.path().pop() as Extract<AstNode, { nodes: AstNode[] }>) ?? null |
202 | | - |
203 | | - // Once computed, we never need to compute this again |
204 | | - Object.defineProperty(this, 'parent', { value: parent }) |
205 | | - return parent |
206 | | - }, |
207 | | - path() { |
208 | | - return ctx.path().filter((n) => n.kind !== 'context') |
209 | | - }, |
210 | | - } |
211 | | -} |
0 commit comments