@projectwallace/css-layer-tree 2.0.0 → 2.1.0

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 u from "css-tree";
2
- class f {
1
+ import { traverse as g, AT_RULE as m, LAYER_NAME as f, parse as b } from "@projectwallace/css-parser";
2
+ class _ {
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(t) {
5
+ this.name = t, 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, l, p) {
13
- let s = this;
14
- if (e.forEach((t) => {
15
- s = s.children.get(t);
16
- }), s.children.has(l))
17
- s.children.get(l).locations.push(p);
12
+ add_child(t, r, n) {
13
+ let a = this;
14
+ if (t.forEach((e) => {
15
+ a = a.children.get(e);
16
+ }), a.children.has(r))
17
+ n !== void 0 && a.children.get(r).locations.push(n);
18
18
  else {
19
- const t = new f(l);
20
- t.locations.push(p), t.is_anonymous = l.startsWith("__anonymous"), s.children.set(l, t);
19
+ const e = new _(r);
20
+ n !== void 0 && e.locations.push(n), e.is_anonymous = r.startsWith("__anonymous"), a.children.set(r, e);
21
21
  }
22
22
  }
23
23
  /**
@@ -36,81 +36,90 @@ class f {
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(), (t) => t.to_plain_object())
40
40
  };
41
41
  }
42
42
  }
43
- function _(a) {
44
- let e = a.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
- };
52
- }
53
- function d(a) {
54
- return a.name.toLowerCase() === "layer";
43
+ function h(s) {
44
+ return s.split(".").map((t) => t.trim());
55
45
  }
56
- function c(a) {
57
- return u.generate(a).split(".").map((e) => e.trim());
46
+ function u(s) {
47
+ return {
48
+ line: s.line,
49
+ column: s.column,
50
+ start: s.start,
51
+ end: s.end
52
+ };
58
53
  }
59
- function h(a) {
60
- let e = [], l = new f("root"), p = 0;
61
- function s() {
62
- return p++, `__anonymous-${p}__`;
54
+ function k(s) {
55
+ let t = [], r = new _("root"), n = 0;
56
+ function a() {
57
+ return n++, `__anonymous-${n}__`;
63
58
  }
64
- return u.walk(a, {
65
- visit: "Atrule",
66
- enter(t) {
67
- if (d(t)) {
68
- let i = _(t);
69
- if (t.prelude === null) {
70
- let r = s();
71
- l.add_child(e, r, i), e.push(r);
72
- } else if (t.prelude.type === "AtrulePrelude")
73
- if (t.block === null) {
74
- let r = u.findAll(t.prelude, (n) => n.type === "Layer").map((n) => n.name);
75
- for (let n of r)
76
- l.add_child(e, n, i);
77
- } else
78
- for (let r of c(t.prelude))
79
- l.add_child(e, r, i), e.push(r);
80
- } else if (t.name.toLowerCase() === "import" && t.prelude !== null && t.prelude.type === "AtrulePrelude") {
81
- let i = _(t), r = t.prelude, n = u.find(r, (o) => o.type === "Layer");
82
- if (n) {
83
- for (let o of c(n))
84
- l.add_child(e, o, i), e.push(o);
85
- return this.skip;
59
+ return g(s, {
60
+ enter(e) {
61
+ if (e.type === m) {
62
+ if (e.name === "layer")
63
+ if (e.prelude) {
64
+ let i = e.prelude.text.split(",").map((l) => l.trim());
65
+ if (e.has_block)
66
+ for (let l of e.prelude.children)
67
+ l.type === f && (r.add_child(t, l.text, u(e)), t.push(l.text));
68
+ else
69
+ for (let l of i) {
70
+ let o = h(l);
71
+ for (let c = 0; c < o.length; c++) {
72
+ let d = o.slice(0, c), p = o[c];
73
+ if (p) {
74
+ let y = c === o.length - 1 ? u(e) : void 0;
75
+ r.add_child(d, p, y);
76
+ }
77
+ }
78
+ }
79
+ } else {
80
+ let i = a();
81
+ r.add_child(t, i, u(e)), t.push(i);
82
+ }
83
+ else if (e.name === "import" && e.prelude) {
84
+ for (let i of e.prelude.children)
85
+ if (i.type === f) {
86
+ if (i.name.trim())
87
+ for (let l of h(i.name))
88
+ r.add_child(t, l, u(e)), t.push(l);
89
+ else {
90
+ let l = a();
91
+ r.add_child([], l, u(e));
92
+ }
93
+ break;
94
+ }
86
95
  }
87
- if (u.find(r, (o) => o.type === "Identifier" && o.name.toLowerCase() === "layer"))
88
- return l.add_child([], s(), i), this.skip;
89
96
  }
90
97
  },
91
- leave(t) {
92
- if (d(t))
93
- if (t.prelude !== null && t.prelude.type === "AtrulePrelude") {
94
- let i = c(t.prelude);
95
- for (let r = 0; r < i.length; r++)
96
- e.pop();
97
- } else
98
- e.pop();
99
- else t.name.toLowerCase() === "import" && (e.length = 0);
98
+ leave(e) {
99
+ if (e.type === m)
100
+ if (e.name === "layer")
101
+ if (e.prelude && e.has_block) {
102
+ for (let i of e.prelude.children)
103
+ if (i.type === f) {
104
+ let l = h(i.text);
105
+ for (let o = 0; o < l.length; o++)
106
+ t.pop();
107
+ break;
108
+ }
109
+ } else
110
+ t.pop();
111
+ else e.name === "import" && (t.length = 0);
100
112
  }
101
- }), l.to_plain_object().children;
113
+ }), r.to_plain_object().children;
102
114
  }
103
- function m(a) {
104
- let e = u.parse(a, {
105
- positions: !0,
106
- parseAtrulePrelude: !0,
107
- parseValue: !1,
108
- parseRulePrelude: !1,
109
- parseCustomProperty: !1
115
+ function v(s) {
116
+ let t = b(s, {
117
+ parse_selectors: !1,
118
+ parse_values: !1
110
119
  });
111
- return h(e);
120
+ return k(t);
112
121
  }
113
122
  export {
114
- m as layer_tree,
115
- h as layer_tree_from_ast
123
+ v as layer_tree,
124
+ k as layer_tree_from_ast
116
125
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@projectwallace/css-layer-tree",
3
- "version": "2.0.0",
3
+ "version": "2.1.0",
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
+ }