@projectwallace/css-layer-tree 2.0.2 → 2.1.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,23 +1,23 @@
1
- import * as p from "css-tree";
2
- class d {
1
+ import { traverse as g, AT_RULE as m, LAYER_NAME as _, parse as b } from "@projectwallace/css-parser";
2
+ class p {
3
3
  /** @param {string} name */
4
- constructor(e) {
5
- this.name = e, this.is_anonymous = !1, this.children = /* @__PURE__ */ new Map(), this.locations = [];
4
+ constructor(l) {
5
+ this.name = l, this.is_anonymous = !1, this.children = /* @__PURE__ */ new Map(), this.locations = [];
6
6
  }
7
7
  /**
8
8
  * @param {string[]} path
9
9
  * @param {string} name
10
10
  * @param {T} location
11
11
  */
12
- add_child(e, a, u) {
12
+ add_child(l, i, c) {
13
13
  let s = this;
14
- if (e.forEach((t) => {
15
- s = s.children.get(t);
16
- }), s.children.has(a))
17
- u !== void 0 && s.children.get(a).locations.push(u);
14
+ if (l.forEach((e) => {
15
+ s = s.children.get(e);
16
+ }), s.children.has(i))
17
+ c !== void 0 && s.children.get(i).locations.push(c);
18
18
  else {
19
- const t = new d(a);
20
- u !== void 0 && t.locations.push(u), t.is_anonymous = a.startsWith("__anonymous"), s.children.set(a, t);
19
+ const e = new p(i);
20
+ c !== void 0 && e.locations.push(c), e.is_anonymous = i.startsWith("__anonymous"), s.children.set(i, e);
21
21
  }
22
22
  }
23
23
  /**
@@ -36,86 +36,96 @@ class d {
36
36
  name: this.name,
37
37
  is_anonymous: this.is_anonymous,
38
38
  locations: this.locations,
39
- children: Array.from(this.children.values(), (e) => e.to_plain_object())
39
+ children: Array.from(this.children.values(), (l) => l.to_plain_object())
40
40
  };
41
41
  }
42
42
  }
43
- function _(i) {
44
- let e = i.loc;
45
- if (e)
46
- return {
47
- line: e.start.line,
48
- column: e.start.column,
49
- start: e.start.offset,
50
- end: e.end.offset
51
- };
43
+ function d(a) {
44
+ return a.split(".").map((l) => l.trim());
52
45
  }
53
- function h(i) {
54
- return i.name.toLowerCase() === "layer";
46
+ function u(a) {
47
+ return {
48
+ line: a.line,
49
+ column: a.column,
50
+ start: a.start,
51
+ end: a.end
52
+ };
55
53
  }
56
- function c(i) {
57
- return p.generate(i).split(".").map((e) => e.trim());
58
- }
59
- function g(i) {
60
- let e = [], a = new d("root"), u = 0;
54
+ function k(a) {
55
+ let l = [], i = new p("root"), c = 0;
61
56
  function s() {
62
- return u++, `__anonymous-${u}__`;
57
+ return c++, `__anonymous-${c}__`;
63
58
  }
64
- return p.walk(i, {
65
- visit: "Atrule",
66
- enter(t) {
67
- if (h(t)) {
68
- let n = _(t);
69
- if (t.prelude === null) {
70
- let r = s();
71
- a.add_child(e, r, n), e.push(r);
72
- } else if (t.prelude.type === "AtrulePrelude")
73
- if (t.block === null) {
74
- let r = p.findAll(t.prelude, (o) => o.type === "Layer").map((o) => o.name);
75
- for (let o of r) {
76
- let f = o.split(".").map((l) => l.trim());
77
- for (let l = 0; l < f.length; l++) {
78
- let y = f.slice(0, l), m = f[l], w = l === f.length - 1 ? { ...n } : void 0;
79
- a.add_child(y, m, w);
59
+ return g(a, {
60
+ enter(e) {
61
+ if (e.type === m) {
62
+ if (e.name === "layer")
63
+ if (e.prelude) {
64
+ let n = e.prelude.text.split(",").map((t) => t.trim());
65
+ if (e.has_block) {
66
+ let t = [];
67
+ for (let r of e.prelude.children)
68
+ r.type === _ && t.push(r.text);
69
+ for (let r = 0; r < t.length; r++) {
70
+ let o = t[r];
71
+ if (!o) continue;
72
+ let h = t.slice(0, r), f = r === t.length - 1 ? u(e) : void 0;
73
+ i.add_child(l.concat(h), o, f);
74
+ }
75
+ l.push(...t);
76
+ } else
77
+ for (let t of n) {
78
+ let r = d(t);
79
+ for (let o = 0; o < r.length; o++) {
80
+ let h = r.slice(0, o), f = r[o];
81
+ if (f) {
82
+ let y = o === r.length - 1 ? u(e) : void 0;
83
+ i.add_child(h, f, y);
84
+ }
85
+ }
80
86
  }
87
+ } else {
88
+ let n = s();
89
+ i.add_child(l, n, u(e)), l.push(n);
90
+ }
91
+ else if (e.name === "import" && e.prelude) {
92
+ for (let n of e.prelude.children)
93
+ if (n.type === _) {
94
+ if (n.name.trim())
95
+ for (let t of d(n.name))
96
+ i.add_child(l, t, u(e)), l.push(t);
97
+ else {
98
+ let t = s();
99
+ i.add_child([], t, u(e));
100
+ }
101
+ break;
81
102
  }
82
- } else
83
- for (let r of c(t.prelude))
84
- a.add_child(e, r, n), e.push(r);
85
- } else if (t.name.toLowerCase() === "import" && t.prelude !== null && t.prelude.type === "AtrulePrelude") {
86
- let n = _(t), r = t.prelude, o = p.find(r, (l) => l.type === "Layer");
87
- if (o) {
88
- for (let l of c(o))
89
- a.add_child(e, l, n), e.push(l);
90
- return this.skip;
91
103
  }
92
- if (p.find(r, (l) => l.type === "Identifier" && l.name.toLowerCase() === "layer"))
93
- return a.add_child([], s(), n), this.skip;
94
104
  }
95
105
  },
96
- leave(t) {
97
- if (h(t))
98
- if (t.prelude !== null && t.prelude.type === "AtrulePrelude") {
99
- let n = c(t.prelude);
100
- for (let r = 0; r < n.length; r++)
101
- e.pop();
102
- } else
103
- e.pop();
104
- else t.name.toLowerCase() === "import" && (e.length = 0);
106
+ leave(e) {
107
+ if (e.type === m)
108
+ if (e.name === "layer")
109
+ if (e.prelude && e.has_block) {
110
+ let n = 0;
111
+ for (let t of e.prelude.children)
112
+ t.type === _ && n++;
113
+ for (let t = 0; t < n; t++)
114
+ l.pop();
115
+ } else
116
+ l.pop();
117
+ else e.name === "import" && (l.length = 0);
105
118
  }
106
- }), a.to_plain_object().children;
119
+ }), i.to_plain_object().children;
107
120
  }
108
- function k(i) {
109
- let e = p.parse(i, {
110
- positions: !0,
111
- parseAtrulePrelude: !0,
112
- parseValue: !1,
113
- parseRulePrelude: !1,
114
- parseCustomProperty: !1
121
+ function w(a) {
122
+ let l = b(a, {
123
+ parse_selectors: !1,
124
+ parse_values: !1
115
125
  });
116
- return g(e);
126
+ return k(l);
117
127
  }
118
128
  export {
119
- k as layer_tree,
120
- g as layer_tree_from_ast
129
+ w as layer_tree,
130
+ k as layer_tree_from_ast
121
131
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@projectwallace/css-layer-tree",
3
- "version": "2.0.2",
3
+ "version": "2.1.1",
4
4
  "description": "Discover the composition of your CSS @layers in a tree-based format.",
5
5
  "repository": {
6
6
  "type": "git",
@@ -25,21 +25,17 @@
25
25
  "scripts": {
26
26
  "build": "vite build",
27
27
  "test": "c8 --reporter=lcov uvu",
28
- "check": "tsc",
28
+ "check": "tsc --noEmit",
29
29
  "prettier": "prettier --check src/**/*.js test/**/*.js"
30
30
  },
31
31
  "devDependencies": {
32
32
  "@codecov/vite-plugin": "^1.2.1",
33
- "@types/css-tree": "^2.3.8",
34
33
  "c8": "^10.1.2",
35
34
  "prettier": "^3.3.3",
36
35
  "typescript": "5.4.2",
37
36
  "uvu": "^0.5.6",
38
37
  "vite": "^5.4.10"
39
38
  },
40
- "dependencies": {
41
- "css-tree": "^3.0.0"
42
- },
43
39
  "files": [
44
40
  "dist",
45
41
  "index.d.ts"
@@ -58,5 +54,8 @@
58
54
  "useTabs": true,
59
55
  "printWidth": 140,
60
56
  "singleQuote": true
57
+ },
58
+ "dependencies": {
59
+ "@projectwallace/css-parser": "^0.12.2"
61
60
  }
62
- }
61
+ }