Skip to content

Commit 2d1218d

Browse files
committed
Cleanup
1 parent b368b40 commit 2d1218d

File tree

2 files changed

+33
-94
lines changed

2 files changed

+33
-94
lines changed

packages/tailwindcss-language-service/src/css/ast.ts

Lines changed: 1 addition & 94 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
1-
import { parseAtRule } from './parse'
21
import type { SourceLocation } from './source'
3-
import type { VisitContext } from '../util/walk'
2+
import { parseAtRule } from './parse'
43

54
const AT_SIGN = 0x40
65

@@ -117,95 +116,3 @@ export function atRoot(nodes: AstNode[]): AtRoot {
117116
nodes,
118117
}
119118
}
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-
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
import type { VisitContext } from '../util/walk'
2+
import type { AstNode } from './ast'
3+
4+
export function cssContext(
5+
ctx: VisitContext<AstNode>,
6+
): VisitContext<AstNode> & { context: Record<string, string | boolean> } {
7+
return {
8+
depth: ctx.depth,
9+
get context() {
10+
let context: Record<string, string | boolean> = {}
11+
for (let child of ctx.path()) {
12+
if (child.kind === 'context') {
13+
Object.assign(context, child.context)
14+
}
15+
}
16+
17+
// Once computed, we never need to compute this again
18+
Object.defineProperty(this, 'context', { value: context })
19+
return context
20+
},
21+
get parent() {
22+
let parent = (this.path().pop() as Extract<AstNode, { nodes: AstNode[] }>) ?? null
23+
24+
// Once computed, we never need to compute this again
25+
Object.defineProperty(this, 'parent', { value: parent })
26+
return parent
27+
},
28+
path() {
29+
return ctx.path().filter((n) => n.kind !== 'context')
30+
},
31+
}
32+
}

0 commit comments

Comments
 (0)