Skip to content

Commit 87d6d00

Browse files
committed
unify location creation
1 parent 9d7e313 commit 87d6d00

File tree

1 file changed

+17
-24
lines changed

1 file changed

+17
-24
lines changed

src/index.js

Lines changed: 17 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,14 @@ function get_layer_names(name) {
66
return name.split('.').map((s) => s.trim())
77
}
88

9+
/** @param {import('@projectwallace/css-parser').CSSNode} node */
10+
function create_location(node) {
11+
return {
12+
line: node.line,
13+
start: node.offset,
14+
}
15+
}
16+
917
/** @param {import('@projectwallace/css-parser').CSSNode} ast */
1018
export function layer_tree_from_ast(ast) {
1119
/** @type {string[]} */
@@ -24,35 +32,26 @@ export function layer_tree_from_ast(ast) {
2432
if (node.type !== NODE_AT_RULE) return
2533

2634
if (node.name === 'layer') {
27-
if (!node.has_prelude) {
28-
let name = get_anonymous_id()
29-
root.add_child(current_stack, name, {
30-
line: node.line,
31-
start: node.offset,
32-
})
33-
current_stack.push(name)
34-
} else {
35+
if (node.has_prelude) {
3536
let has_block = node.has_children && node.children.some((c) => c.type !== NODE_PRELUDE_LAYER_NAME)
3637
if (!has_block) {
3738
for (let child of node.children) {
3839
if (child.type === NODE_PRELUDE_LAYER_NAME) {
39-
root.add_child(current_stack, child.text, {
40-
line: node.line,
41-
start: node.offset,
42-
})
40+
root.add_child(current_stack, child.text, create_location(node))
4341
}
4442
}
4543
} else {
4644
for (let child of node.children) {
4745
if (child.type === NODE_PRELUDE_LAYER_NAME) {
48-
root.add_child(current_stack, child.text, {
49-
line: node.line,
50-
start: node.offset,
51-
})
46+
root.add_child(current_stack, child.text, create_location(node))
5247
current_stack.push(child.text)
5348
}
5449
}
5550
}
51+
} else {
52+
let name = get_anonymous_id()
53+
root.add_child(current_stack, name, create_location(node))
54+
current_stack.push(name)
5655
}
5756
} else if (node.name === 'import') {
5857
// @import url("foo.css") layer(test);
@@ -62,19 +61,13 @@ export function layer_tree_from_ast(ast) {
6261
if (layerNode) {
6362
if (layerNode.name.trim()) {
6463
for (let layer_name of get_layer_names(layerNode.name)) {
65-
root.add_child(current_stack, layer_name, {
66-
line: node.line,
67-
start: node.offset,
68-
})
64+
root.add_child(current_stack, layer_name, create_location(node))
6965
current_stack.push(layer_name)
7066
}
7167
} else {
7268
// @import url("foo.css") layer;
7369
let name = get_anonymous_id()
74-
root.add_child([], name, {
75-
line: node.line,
76-
start: node.offset,
77-
})
70+
root.add_child([], name, create_location(node))
7871
}
7972
}
8073
}

0 commit comments

Comments
 (0)