@projectwallace/css-layer-tree 1.0.0 → 2.0.0-alpha.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.
package/README.md CHANGED
@@ -11,7 +11,7 @@ npm install @projectwallace/css-layer-tree
11
11
  ## Usage
12
12
 
13
13
  ```js
14
- import { get_tree } from '@projectwallace/css-layer-tree'
14
+ import { layer_tree } from '@projectwallace/css-layer-tree'
15
15
 
16
16
  let css = `
17
17
  @import url("test.css") layer;
@@ -25,7 +25,7 @@ let css = `
25
25
  @layer {}
26
26
  `
27
27
 
28
- let tree = get_tree(css)
28
+ let tree = layer_tree(css)
29
29
  ```
30
30
 
31
31
  This example would result in this `tree`:
@@ -2,28 +2,28 @@ import * as u from "css-tree";
2
2
  class f {
3
3
  /** @param {string} name */
4
4
  constructor(e) {
5
- this.name = e, this.children = /* @__PURE__ */ new Map(), this.locations = [];
5
+ this.name = e, this.is_anonymous = !1, this.children = /* @__PURE__ */ new Map(), this.locations = [];
6
6
  }
7
7
  /**
8
- *
9
8
  * @param {string[]} path
10
9
  * @param {string} name
11
10
  * @param {T} location
12
11
  */
13
12
  add_child(e, l, p) {
14
- let n = this;
13
+ let s = this;
15
14
  if (e.forEach((t) => {
16
- n = n.children.get(t);
17
- }), n.children.has(l))
18
- n.children.get(l).locations.push(p);
15
+ s = s.children.get(t);
16
+ }), s.children.has(l))
17
+ s.children.get(l).locations.push(p);
19
18
  else {
20
19
  const t = new f(l);
21
- t.locations.push(p), n.children.set(l, t);
20
+ t.locations.push(p), t.is_anonymous = l.startsWith("__anonymous"), s.children.set(l, t);
22
21
  }
23
22
  }
24
23
  /**
25
24
  * @typedef PlainObject
26
25
  * @property {string} name
26
+ * @property {boolean} is_anonymous
27
27
  * @property {T[]} locations
28
28
  * @property {PlainObject[]} children
29
29
  */
@@ -34,13 +34,14 @@ class f {
34
34
  to_plain_object() {
35
35
  return {
36
36
  name: this.name,
37
+ is_anonymous: this.is_anonymous,
37
38
  locations: this.locations,
38
39
  children: Array.from(this.children.values(), (e) => e.to_plain_object())
39
40
  };
40
41
  }
41
42
  }
42
- function d(i) {
43
- let e = i.loc;
43
+ function _(a) {
44
+ let e = a.loc;
44
45
  if (e)
45
46
  return {
46
47
  line: e.start.line,
@@ -49,51 +50,49 @@ function d(i) {
49
50
  end: e.end.offset
50
51
  };
51
52
  }
52
- function _(i) {
53
- return i.name.toLowerCase() === "layer";
53
+ function d(a) {
54
+ return a.name.toLowerCase() === "layer";
54
55
  }
55
- function c(i) {
56
- return u.generate(i).split(".").map((e) => e.trim());
56
+ function c(a) {
57
+ return u.generate(a).split(".").map((e) => e.trim());
57
58
  }
58
- function h(i) {
59
+ function h(a) {
59
60
  let e = [], l = new f("root"), p = 0;
60
- function n() {
61
+ function s() {
61
62
  return p++, `__anonymous-${p}__`;
62
63
  }
63
- return u.walk(i, {
64
+ return u.walk(a, {
64
65
  visit: "Atrule",
65
66
  enter(t) {
66
- if (_(t)) {
67
- let s = d(t);
67
+ if (d(t)) {
68
+ let i = _(t);
68
69
  if (t.prelude === null) {
69
- let r = n();
70
- l.add_child(e, r, s), e.push(r);
70
+ let r = s();
71
+ l.add_child(e, r, i), e.push(r);
71
72
  } else if (t.prelude.type === "AtrulePrelude")
72
73
  if (t.block === null) {
73
- let r = u.findAll(t.prelude, (o) => o.type === "Layer").map((o) => o.name);
74
- for (let o of r)
75
- l.add_child(e, o, s);
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);
76
77
  } else
77
78
  for (let r of c(t.prelude))
78
- l.add_child(e, r, s), e.push(r);
79
+ l.add_child(e, r, i), e.push(r);
79
80
  } else if (t.name.toLowerCase() === "import" && t.prelude !== null && t.prelude.type === "AtrulePrelude") {
80
- let s = d(t), r = t.prelude, o = u.find(r, (a) => a.type === "Layer");
81
- if (o) {
82
- for (let a of c(o))
83
- l.add_child(e, a, s), e.push(a);
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);
84
85
  return this.skip;
85
86
  }
86
- if (u.find(r, (a) => a.type === "Function" && a.name.toLowerCase() === "layer"))
87
- return l.add_child([], n(), s), this.skip;
88
- if (u.find(r, (a) => a.type === "Identifier" && a.name.toLowerCase() === "layer"))
89
- return l.add_child([], n(), s), this.skip;
87
+ if (u.find(r, (o) => o.type === "Identifier" && o.name.toLowerCase() === "layer"))
88
+ return l.add_child([], s(), i), this.skip;
90
89
  }
91
90
  },
92
91
  leave(t) {
93
- if (_(t))
92
+ if (d(t))
94
93
  if (t.prelude !== null && t.prelude.type === "AtrulePrelude") {
95
- let s = c(t.prelude);
96
- for (let r = 0; r < s.length; r++)
94
+ let i = c(t.prelude);
95
+ for (let r = 0; r < i.length; r++)
97
96
  e.pop();
98
97
  } else
99
98
  e.pop();
@@ -101,8 +100,8 @@ function h(i) {
101
100
  }
102
101
  }), l.to_plain_object().children;
103
102
  }
104
- function w(i) {
105
- let e = u.parse(i, {
103
+ function m(a) {
104
+ let e = u.parse(a, {
106
105
  positions: !0,
107
106
  parseAtrulePrelude: !0,
108
107
  parseValue: !1,
@@ -112,6 +111,6 @@ function w(i) {
112
111
  return h(e);
113
112
  }
114
113
  export {
115
- w as layer_tree,
114
+ m as layer_tree,
116
115
  h as layer_tree_from_ast
117
116
  };
package/index.d.ts CHANGED
@@ -9,6 +9,7 @@ export type Location = {
9
9
 
10
10
  export type TreeNode = {
11
11
  name: string;
12
+ is_anonymous: boolean;
12
13
  children: TreeNode[];
13
14
  locations: Location[];
14
15
  }
package/package.json CHANGED
@@ -1,25 +1,27 @@
1
1
  {
2
2
  "name": "@projectwallace/css-layer-tree",
3
- "version": "1.0.0",
4
- "description": "Discover the composition of your CSS @layers",
3
+ "version": "2.0.0-alpha.0",
4
+ "description": "Discover the composition of your CSS @layers in a tree-based format.",
5
5
  "repository": {
6
6
  "type": "git",
7
7
  "url": "git+https://github.com/projectwallace/css-layer-tree.git"
8
8
  },
9
+ "author": {
10
+ "name": "Bart Veneman"
11
+ },
9
12
  "homepage": "https://github.com/projectwallace/css-layer-tree",
10
13
  "issues": "https://github.com/projectwallace/css-layer-tree/issues",
11
14
  "license": "MIT",
15
+ "engines": {
16
+ "node": ">=18.0.0"
17
+ },
12
18
  "type": "module",
13
- "source": "src/index.js",
19
+ "main": "./dist/css-layer-tree.js",
20
+ "types": "./index.d.ts",
14
21
  "exports": {
15
22
  "types": "./index.d.ts",
16
- "require": "./dist/css-layer-tree.umd.cjs",
17
23
  "default": "./dist/css-layer-tree.js"
18
24
  },
19
- "types": "./index.d.ts",
20
- "main": "./dist/css-layer-tree.umd.cjs",
21
- "module": "./dist/css-layer-tree.js",
22
- "unpkg": "./dist/css-layer-tree.umd.cjs",
23
25
  "scripts": {
24
26
  "build": "vite build",
25
27
  "test": "c8 --reporter=lcov uvu",
@@ -1 +0,0 @@
1
- (function(o,p){typeof exports=="object"&&typeof module<"u"?p(exports,require("css-tree")):typeof define=="function"&&define.amd?define(["exports","css-tree"],p):(o=typeof globalThis<"u"?globalThis:o||self,p(o.cssLayerTree={},o.csstree))})(this,function(o,p){"use strict";function g(l){const e=Object.create(null,{[Symbol.toStringTag]:{value:"Module"}});if(l){for(const r in l)if(r!=="default"){const u=Object.getOwnPropertyDescriptor(l,r);Object.defineProperty(e,r,u.get?u:{enumerable:!0,get:()=>l[r]})}}return e.default=l,Object.freeze(e)}const f=g(p);class d{constructor(e){this.name=e,this.children=new Map,this.locations=[]}add_child(e,r,u){let a=this;if(e.forEach(t=>{a=a.children.get(t)}),a.children.has(r))a.children.get(r).locations.push(u);else{const t=new d(r);t.locations.push(u),a.children.set(r,t)}}to_plain_object(){return{name:this.name,locations:this.locations,children:Array.from(this.children.values(),e=>e.to_plain_object())}}}function y(l){let e=l.loc;if(e)return{line:e.start.line,column:e.start.column,start:e.start.offset,end:e.end.offset}}function h(l){return l.name.toLowerCase()==="layer"}function _(l){return f.generate(l).split(".").map(e=>e.trim())}function m(l){let e=[],r=new d("root"),u=0;function a(){return u++,`__anonymous-${u}__`}return f.walk(l,{visit:"Atrule",enter(t){if(h(t)){let s=y(t);if(t.prelude===null){let n=a();r.add_child(e,n,s),e.push(n)}else if(t.prelude.type==="AtrulePrelude")if(t.block===null){let n=f.findAll(t.prelude,c=>c.type==="Layer").map(c=>c.name);for(let c of n)r.add_child(e,c,s)}else for(let n of _(t.prelude))r.add_child(e,n,s),e.push(n)}else if(t.name.toLowerCase()==="import"&&t.prelude!==null&&t.prelude.type==="AtrulePrelude"){let s=y(t),n=t.prelude,c=f.find(n,i=>i.type==="Layer");if(c){for(let i of _(c))r.add_child(e,i,s),e.push(i);return this.skip}if(f.find(n,i=>i.type==="Function"&&i.name.toLowerCase()==="layer"))return r.add_child([],a(),s),this.skip;if(f.find(n,i=>i.type==="Identifier"&&i.name.toLowerCase()==="layer"))return r.add_child([],a(),s),this.skip}},leave(t){if(h(t))if(t.prelude!==null&&t.prelude.type==="AtrulePrelude"){let s=_(t.prelude);for(let n=0;n<s.length;n++)e.pop()}else e.pop();else t.name.toLowerCase()==="import"&&(e.length=0)}}),r.to_plain_object().children}function w(l){let e=f.parse(l,{positions:!0,parseAtrulePrelude:!0,parseValue:!1,parseRulePrelude:!1,parseCustomProperty:!1});return m(e)}o.layer_tree=w,o.layer_tree_from_ast=m,Object.defineProperty(o,Symbol.toStringTag,{value:"Module"})});