@projectwallace/css-layer-tree 2.0.2 → 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 p from "css-tree";
2
- class d {
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, a, u) {
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);
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 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 _(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,86 +36,90 @@ 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(), (t) => t.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 h(s) {
44
+ return s.split(".").map((t) => t.trim());
52
45
  }
53
- function h(i) {
54
- return i.name.toLowerCase() === "layer";
46
+ function u(s) {
47
+ return {
48
+ line: s.line,
49
+ column: s.column,
50
+ start: s.start,
51
+ end: s.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;
61
- function s() {
62
- return u++, `__anonymous-${u}__`;
54
+ function k(s) {
55
+ let t = [], r = new _("root"), n = 0;
56
+ function a() {
57
+ return n++, `__anonymous-${n}__`;
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(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));
80
92
  }
93
+ break;
81
94
  }
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
95
  }
92
- if (p.find(r, (l) => l.type === "Identifier" && l.name.toLowerCase() === "layer"))
93
- return a.add_child([], s(), n), this.skip;
94
96
  }
95
97
  },
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);
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);
105
112
  }
106
- }), a.to_plain_object().children;
113
+ }), r.to_plain_object().children;
107
114
  }
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
115
+ function v(s) {
116
+ let t = b(s, {
117
+ parse_selectors: !1,
118
+ parse_values: !1
115
119
  });
116
- return g(e);
120
+ return k(t);
117
121
  }
118
122
  export {
119
- k as layer_tree,
120
- g as layer_tree_from_ast
123
+ v as layer_tree,
124
+ k as layer_tree_from_ast
121
125
  };
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.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
+ }