@projectwallace/format-css 2.1.0 → 2.1.2

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,193 +1,188 @@
1
- import d from "css-tree/parser";
2
- const a = " ", c = "", $ = ":", x = ";", g = '"', I = "(", v = ")", ee = "[", te = "]", V = "{", W = "}", re = "{}", Z = ",", j = "Atrule", F = "Rule", G = "Block", Q = "SelectorList", Y = "Selector", w = "Declaration", B = "Operator";
1
+ import { parse as te } from "css-tree";
2
+ const o = " ", p = "", $ = ":", x = ";", L = '"', Y = "(", g = ")", re = "[", ne = "]", V = "{", W = "}", le = "{}", Z = ",", j = "Atrule", F = "Rule", G = "Block", Q = "SelectorList", I = "Selector", q = "PseudoElementSelector", w = "Declaration", B = "Operator";
3
3
  function C(y) {
4
4
  return /[A-Z]/.test(y) ? y.toLowerCase() : y;
5
5
  }
6
- function ne(y, {
7
- minify: O = !1,
8
- tab_size: T = void 0
9
- } = /* @__PURE__ */ Object.create(null)) {
10
- if (T !== void 0 && Number(T) < 1)
6
+ function fe(y, { minify: T = !1, tab_size: P = void 0 } = /* @__PURE__ */ Object.create(null)) {
7
+ if (P !== void 0 && Number(P) < 1)
11
8
  throw new TypeError("tab_size must be a number greater than 0");
12
- let A = [], R = d(y, {
9
+ let S = [];
10
+ function J(n, e) {
11
+ S.push(e.start.offset, e.end.offset);
12
+ }
13
+ let N = te(y, {
13
14
  positions: !0,
14
15
  parseAtrulePrelude: !1,
15
16
  parseCustomProperty: !0,
16
17
  parseValue: !0,
17
- onComment: (n, e) => {
18
- A.push(e.start.offset, e.end.offset);
19
- }
18
+ onComment: J
20
19
  });
21
- const u = O ? c : `
22
- `, p = O ? c : a, h = O ? c : x;
20
+ const u = T ? p : `
21
+ `, c = T ? p : o, X = T ? p : x;
23
22
  let s = 0;
24
- function i(n) {
25
- return O ? c : T ? a.repeat(T * n) : " ".repeat(n);
23
+ function a(n) {
24
+ return T === !0 ? p : P !== void 0 ? o.repeat(P * n) : " ".repeat(n);
26
25
  }
27
- function P(n) {
26
+ function O(n) {
28
27
  let e = n.loc;
29
- return e ? y.slice(e.start.offset, e.end.offset) : c;
28
+ return e == null ? p : y.slice(e.start.offset, e.end.offset);
30
29
  }
31
30
  function b(n) {
32
- return /** @type {import('css-tree').CssLocation} */ n.loc.start.offset;
31
+ var e;
32
+ return (e = n.loc) == null ? void 0 : e.start.offset;
33
33
  }
34
34
  function E(n) {
35
- return /** @type {import('css-tree').CssLocation} */ n.loc.end.offset;
35
+ var e;
36
+ return (e = n.loc) == null ? void 0 : e.end.offset;
36
37
  }
37
38
  function m(n, e) {
38
- if (O || n === void 0 || e === void 0)
39
- return c;
40
- let t = "";
41
- for (let r = 0; r < A.length; r += 2) {
42
- let l = A[r];
39
+ if (T === !0 || n === void 0 || e === void 0)
40
+ return p;
41
+ let t = p;
42
+ for (let r = 0; r < S.length; r += 2) {
43
+ let l = S[r];
43
44
  if (l === void 0 || l < n) continue;
44
- let f = A[r + 1];
45
+ let f = S[r + 1];
45
46
  if (f === void 0 || f > e) break;
46
- t.length > 0 && (t += u + i(s)), t += y.slice(l, f);
47
+ t.length > 0 && (t += u + a(s)), t += y.slice(l, f);
47
48
  }
48
49
  return t;
49
50
  }
50
51
  function M(n) {
51
- let e, t = n.prelude, r = n.block;
52
- t.type === Q && (e = q(t));
52
+ let e = "", t = n.prelude, r = n.block;
53
+ t.type === Q && (e = z(t));
53
54
  let l = m(E(t), b(r));
54
- return l && (e += u + i(s) + l), r.type === G && (e += K(r)), e;
55
+ return l && (e += u + a(s) + l), r.type === G && (e += U(r)), e;
55
56
  }
56
- function q(n) {
57
- let e = c;
57
+ function z(n) {
58
+ let e = p;
58
59
  return n.children.forEach((t, r) => {
59
- t.type === Y && (e += i(s) + S(t)), r.next !== null && (e += Z + u);
60
+ t.type === I && (e += a(s) + v(t)), r.next !== null && (e += Z + u);
60
61
  let l = r.next !== null ? b(r.next.data) : E(n), f = m(E(t), l);
61
- f && (e += i(s) + f + u);
62
+ f && (e += a(s) + f + u);
62
63
  }), e;
63
64
  }
64
- function S(n) {
65
- let e = c;
66
- return (n.children || []).forEach((r) => {
65
+ function v(n) {
66
+ let e = p, t = n.children;
67
+ return t == null || t.forEach((r) => {
67
68
  switch (r.type) {
68
69
  case "TypeSelector": {
69
70
  e += C(r.name);
70
71
  break;
71
72
  }
72
73
  case "Combinator": {
73
- e += a, r.name !== " " && (e += r.name + a);
74
+ r.name === " " ? e += o : e += c + r.name + c;
74
75
  break;
75
76
  }
76
77
  case "PseudoClassSelector":
77
- case "PseudoElementSelector": {
78
+ case q: {
78
79
  e += $;
79
80
  let l = C(r.name);
80
- (l === "before" || l === "after" || r.type === "PseudoElementSelector") && (e += $), e += l, r.children && (e += I + S(r) + v);
81
+ (l === "before" || l === "after" || r.type === q) && (e += $), e += l, r.children !== null && (e += Y + v(r) + g);
81
82
  break;
82
83
  }
83
84
  case Q: {
84
85
  r.children.forEach((l, f) => {
85
- l.type === Y && (e += S(l)), f.next && f.next.data.type === Y && (e += Z + p);
86
+ l.type === I && (e += v(l)), f.next !== null && f.next.data.type === I && (e += Z + c);
86
87
  });
87
88
  break;
88
89
  }
89
90
  case "Nth": {
90
91
  let l = r.nth;
91
- if (l)
92
- if (l.type === "AnPlusB") {
93
- let f = l.a, o = l.b;
94
- f !== null && (e += f + "n"), f !== null && o !== null && (e += a), o !== null && (f !== null && !o.startsWith("-") && (e += "+" + a), e += o);
95
- } else
96
- e += P(l);
97
- r.selector !== null && (e += a + "of" + a + S(r.selector));
92
+ if (l.type === "AnPlusB") {
93
+ let f = l.a, i = l.b;
94
+ f !== null && (e += f + "n"), f !== null && i !== null && (e += o), i !== null && (f !== null && !i.startsWith("-") && (e += "+" + o), e += i);
95
+ } else
96
+ e += O(l);
97
+ r.selector !== null && (e += o + "of" + o + v(r.selector));
98
98
  break;
99
99
  }
100
100
  case "AttributeSelector": {
101
- e += ee, e += r.name.name, r.matcher && r.value && (e += r.matcher, e += g, r.value.type === "String" ? e += r.value.value : r.value.type === "Identifier" && (e += r.value.name), e += g), r.flags && (e += a + r.flags), e += te;
101
+ e += re, e += r.name.name, r.matcher !== null && r.value !== null && (e += r.matcher, e += L, r.value.type === "String" ? e += r.value.value : r.value.type === "Identifier" && (e += r.value.name), e += L), r.flags !== null && (e += o + r.flags), e += ne;
102
+ break;
103
+ }
104
+ case "NestingSelector": {
105
+ e += "&";
102
106
  break;
103
107
  }
104
108
  default: {
105
- e += P(r);
109
+ e += O(r);
106
110
  break;
107
111
  }
108
112
  }
109
113
  }), e;
110
114
  }
111
- function K(n) {
112
- let e = n.children, t = p;
115
+ function U(n) {
116
+ let e = n.children, t = c;
113
117
  if (e.isEmpty) {
114
118
  let f = m(b(n), E(n));
115
- return f ? (t += V + u, t += i(s + 1) + f, t += u + i(s) + W, t) : t + re;
119
+ return f ? (t += V + u, t += a(s + 1) + f, t += u + a(s) + W, t) : t + le;
116
120
  }
117
121
  t += V + u, s++;
118
- let r = m(b(n), b(
119
- /** @type {import('css-tree').CssNode} */
120
- e.first
121
- ));
122
- r && (t += i(s) + r + u), e.forEach((f, o) => {
123
- if (o.prev !== null) {
124
- let H = m(E(o.prev.data), b(f));
125
- H && (t += i(s) + H + u);
122
+ let r = m(b(n), b(e.first));
123
+ r && (t += a(s) + r + u), e.forEach((f, i) => {
124
+ if (i.prev !== null) {
125
+ let H = m(E(i.prev.data), b(f));
126
+ H && (t += a(s) + H + u);
126
127
  }
127
- f.type === w ? (t += X(f), o.next === null ? t += h : t += x) : (o.prev !== null && o.prev.data.type === w && (t += u), f.type === F ? t += M(f) : f.type === j ? t += U(f) : t += N(f, s)), o.next !== null && (t += u, f.type !== w && (t += u));
128
+ f.type === w ? (t += d(f), i.next === null ? t += X : t += x) : (i.prev !== null && i.prev.data.type === w && (t += u), f.type === F ? t += M(f) : f.type === j ? t += D(f) : t += k(f, s)), i.next !== null && (t += u, f.type !== w && (t += u));
128
129
  });
129
- let l = m(E(
130
- /** @type {import('css-tree').CssNode} */
131
- e.last
132
- ), E(n));
133
- return l && (t += u + i(s) + l), s--, t += u + i(s) + W, t;
130
+ let l = m(E(e.last), E(n));
131
+ return l && (t += u + a(s) + l), s--, t += u + a(s) + W, t;
134
132
  }
135
- function U(n) {
136
- let e = i(s) + "@", t = n.prelude, r = n.block;
137
- return e += C(n.name), t !== null && (e += a + J(t)), r === null ? e += x : r.type === G && (e += K(r)), e;
133
+ function D(n) {
134
+ let e = a(s) + "@", t = n.prelude, r = n.block;
135
+ return e += C(n.name), t !== null && (e += o + h(t)), r === null ? e += x : r.type === G && (e += U(r)), e;
138
136
  }
139
- function J(n) {
140
- let e = P(n);
141
- return e.replace(/\s*([:,])/g, e.toLowerCase().includes("selector(") ? "$1" : "$1 ").replace(/\)([a-zA-Z])/g, ") $1").replace(/\s*(=>|<=)\s*/g, " $1 ").replace(/([^<>=\s])([<>])([^<>=\s])/g, `$1${p}$2${p}$3`).replace(/\s+/g, p).replace(/calc\(\s*([^()+\-*/]+)\s*([*/+-])\s*([^()+\-*/]+)\s*\)/g, (t, r, l, f) => {
142
- let o = l === "+" || l === "-" ? a : p;
143
- return `calc(${r.trim()}${o}${l}${o}${f.trim()})`;
144
- }).replace(/selector|url|supports|layer\(/ig, (t) => C(t));
137
+ function h(n) {
138
+ let e = O(n);
139
+ return e.replace(/\s*([:,])/g, e.toLowerCase().includes("selector(") ? "$1" : "$1 ").replace(/\)([a-zA-Z])/g, ") $1").replace(/\s*(=>|<=)\s*/g, " $1 ").replace(/([^<>=\s])([<>])([^<>=\s])/g, `$1${c}$2${c}$3`).replace(/\s+/g, c).replace(/calc\(\s*([^()+\-*/]+)\s*([*/+-])\s*([^()+\-*/]+)\s*\)/g, (t, r, l, f) => {
140
+ let i = l === "+" || l === "-" ? o : c;
141
+ return `calc(${r.trim()}${i}${l}${i}${f.trim()})`;
142
+ }).replace(/selector|url|supports|layer\(/gi, (t) => C(t));
145
143
  }
146
- function X(n) {
144
+ function d(n) {
147
145
  let e = n.property;
148
146
  e.charCodeAt(0) === 45 && e.charCodeAt(1) === 45 || (e = C(e));
149
- let t = D(n.value);
150
- return e === "font" && (t = t.replace(/\s*\/\s*/, "/")), t === c && O && (t += a), n.important === !0 ? t += p + "!important" : typeof n.important == "string" && (t += p + "!" + C(n.important)), i(s) + e + $ + p + t;
147
+ let t = K(n.value);
148
+ return e === "font" && (t = t.replace(/\s*\/\s*/, "/")), t === p && T === !0 && (t += o), n.important === !0 ? t += c + "!important" : typeof n.important == "string" && (t += c + "!" + C(n.important)), a(s) + e + $ + c + t;
151
149
  }
152
- function k(n) {
153
- let e = c;
150
+ function R(n) {
151
+ let e = p;
154
152
  return n.forEach((t, r) => {
155
- t.type === "Identifier" ? e += t.name : t.type === "Function" ? e += C(t.name) + I + k(t.children) + v : t.type === "Dimension" ? e += t.value + C(t.unit) : t.type === "Value" ? e += D(t) : t.type === B ? e += z(t) : t.type === "Parentheses" ? e += I + k(t.children) + v : t.type === "Url" ? e += "url(" + g + t.value + g + v : e += P(t), t.type !== B && r.next !== null && r.next.data.type !== B && (e += a);
153
+ t.type === "Identifier" ? e += t.name : t.type === "Function" ? e += C(t.name) + Y + R(t.children) + g : t.type === "Dimension" ? e += t.value + C(t.unit) : t.type === "Value" ? e += K(t) : t.type === B ? e += ee(t) : t.type === "Parentheses" ? e += Y + R(t.children) + g : t.type === "Url" ? e += "url(" + L + t.value + L + g : e += O(t), t.type !== B && r.next !== null && r.next.data.type !== B && (e += o);
156
154
  }), e;
157
155
  }
158
- function z(n) {
159
- let e = c, t = n.value.trim(), r = t.charCodeAt(0);
160
- return r === 43 || r === 45 ? e += a : r !== 44 && (e += p), e += t, r === 43 || r === 45 ? e += a : e += p, e;
156
+ function ee(n) {
157
+ let e = p, t = n.value.trim(), r = t.charCodeAt(0);
158
+ return r === 43 || r === 45 ? e += o : r !== 44 && (e += c), e += t, r === 43 || r === 45 ? e += o : e += c, e;
161
159
  }
162
- function D(n) {
163
- return n.type === "Raw" ? N(n, 0) : k(n.children);
160
+ function K(n) {
161
+ return n.type === "Raw" ? k(n, 0) : R(n.children);
164
162
  }
165
- function N(n, e) {
166
- return i(e) + P(n).trim();
163
+ function k(n, e) {
164
+ return a(e) + O(n).trim();
167
165
  }
168
- let L = R.children, _ = c;
169
- if (L.first) {
170
- let n = m(0, b(L.first));
171
- n && (_ += n + u), L.forEach((t, r) => {
172
- if (t.type === F ? _ += M(t) : t.type === j ? _ += U(t) : _ += N(t, s), r.next !== null) {
166
+ let A = N.children, _ = p;
167
+ if (A.first !== null) {
168
+ let n = m(0, b(A.first));
169
+ n && (_ += n + u), A.forEach((t, r) => {
170
+ if (t.type === F ? _ += M(t) : t.type === j ? _ += D(t) : _ += k(t, s), r.next !== null) {
173
171
  _ += u;
174
172
  let l = m(E(t), b(r.next.data));
175
- l && (_ += i(s) + l), _ += u;
173
+ l && (_ += a(s) + l), _ += u;
176
174
  }
177
175
  });
178
- let e = m(E(
179
- /** @type {import('css-tree').CssNode} */
180
- L.last
181
- ), E(R));
176
+ let e = m(E(A.last), E(N));
182
177
  e && (_ += u + e);
183
178
  } else
184
- _ += m(0, E(R));
179
+ _ += m(0, E(N));
185
180
  return _;
186
181
  }
187
- function fe(y) {
188
- return ne(y, { minify: !0 });
182
+ function se(y) {
183
+ return fe(y, { minify: !0 });
189
184
  }
190
185
  export {
191
- ne as format,
192
- fe as minify
186
+ fe as format,
187
+ se as minify
193
188
  };
package/dist/index.d.ts CHANGED
@@ -1,29 +1,16 @@
1
+ export type FormatOptions = {
2
+ /** Whether to minify the CSS or keep it formatted */
3
+ minify?: boolean;
4
+ /** Tell the formatter to use N spaces instead of tabs */
5
+ tab_size?: number;
6
+ };
1
7
  /**
2
- * @typedef {Object} Options
3
- * @property {boolean} [minify] Whether to minify the CSS or keep it formatted
4
- * @property {number} [tab_size] Tell the formatter to use N spaces instead of tabs
5
- *
6
8
  * Format a string of CSS using some simple rules
7
- * @param {string} css The original CSS
8
- * @param {Options} options
9
- * @returns {string} The formatted CSS
10
9
  */
11
- export function format(css: string, { minify, tab_size, }?: Options): string;
10
+ export declare function format(css: string, { minify, tab_size }?: FormatOptions): string;
12
11
  /**
13
12
  * Minify a string of CSS
14
13
  * @param {string} css The original CSS
15
14
  * @returns {string} The minified CSS
16
15
  */
17
- export function minify(css: string): string;
18
- export type Options = {
19
- /**
20
- * Whether to minify the CSS or keep it formatted
21
- */
22
- minify?: boolean | undefined;
23
- /**
24
- * Tell the formatter to use N spaces instead of tabs
25
- *
26
- * Format a string of CSS using some simple rules
27
- */
28
- tab_size?: number | undefined;
29
- };
16
+ export declare function minify(css: string): string;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@projectwallace/format-css",
3
- "version": "2.1.0",
3
+ "version": "2.1.2",
4
4
  "description": "Fast, small, zero-config library to format or minify CSS with basic rules.",
5
5
  "repository": {
6
6
  "type": "git",
@@ -9,6 +9,7 @@
9
9
  "homepage": "https://github.com/projectwallace/format-css",
10
10
  "issues": "https://github.com/projectwallace/format-css/issues",
11
11
  "license": "MIT",
12
+ "author": "Bart Veneman <bart@projectwallace.com>",
12
13
  "engines": {
13
14
  "node": ">=18.0.0"
14
15
  },
@@ -21,22 +22,27 @@
21
22
  "types": "./dist/index.d.ts",
22
23
  "scripts": {
23
24
  "build": "vite build",
24
- "test": "c8 --reporter=lcov uvu",
25
+ "test": "vitest --coverage",
25
26
  "check": "tsc",
26
- "prettier": "prettier --check index.js test/**/*.js"
27
+ "prettier": "prettier --check index.ts test/**/*.ts",
28
+ "lint": "oxlint",
29
+ "lint-package": "publint"
27
30
  },
28
31
  "devDependencies": {
29
- "@codecov/vite-plugin": "^1.9.0",
30
- "@types/css-tree": "^2.3.10",
32
+ "@codecov/vite-plugin": "^1.9.1",
33
+ "@types/css-tree": "^2.3.11",
34
+ "@vitest/coverage-v8": "^4.0.3",
31
35
  "c8": "^10.1.3",
32
- "prettier": "^3.5.3",
33
- "typescript": "^5.8.3",
34
- "uvu": "^0.5.6",
36
+ "oxlint": "^1.24.0",
37
+ "prettier": "^3.6.2",
38
+ "publint": "^0.3.15",
39
+ "typescript": "^5.9.3",
35
40
  "vite": "^6.2.6",
36
- "vite-plugin-dts": "^4.5.3"
41
+ "vite-plugin-dts": "^4.5.4",
42
+ "vitest": "^4.0.3"
37
43
  },
38
44
  "dependencies": {
39
- "css-tree": "^3.0.1"
45
+ "css-tree": "^3.1.0"
40
46
  },
41
47
  "files": [
42
48
  "dist"
@@ -57,4 +63,4 @@
57
63
  "printWidth": 140,
58
64
  "singleQuote": true
59
65
  }
60
- }
66
+ }