postcss 8.3.9 → 8.4.31

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.
Files changed (44) hide show
  1. package/README.md +7 -23
  2. package/lib/at-rule.d.ts +58 -49
  3. package/lib/comment.d.ts +43 -32
  4. package/lib/container.d.ts +233 -223
  5. package/lib/container.js +251 -244
  6. package/lib/css-syntax-error.d.ts +117 -61
  7. package/lib/css-syntax-error.js +10 -3
  8. package/lib/declaration.d.ts +85 -61
  9. package/lib/document.d.ts +27 -16
  10. package/lib/fromJSON.d.ts +6 -2
  11. package/lib/input.d.ts +118 -54
  12. package/lib/input.js +87 -55
  13. package/lib/lazy-result.d.ts +82 -67
  14. package/lib/lazy-result.js +234 -232
  15. package/lib/list.d.ts +53 -47
  16. package/lib/list.js +16 -14
  17. package/lib/map-generator.js +231 -172
  18. package/lib/no-work-result.d.ts +46 -0
  19. package/lib/no-work-result.js +135 -0
  20. package/lib/node.d.ts +345 -253
  21. package/lib/node.js +200 -139
  22. package/lib/parse.d.ts +6 -2
  23. package/lib/parser.js +354 -309
  24. package/lib/postcss.d.mts +72 -0
  25. package/lib/postcss.d.ts +288 -319
  26. package/lib/postcss.js +18 -12
  27. package/lib/postcss.mjs +1 -0
  28. package/lib/previous-map.d.ts +23 -14
  29. package/lib/previous-map.js +37 -40
  30. package/lib/processor.d.ts +55 -41
  31. package/lib/processor.js +20 -27
  32. package/lib/result.d.ts +87 -76
  33. package/lib/root.d.ts +49 -36
  34. package/lib/root.js +12 -10
  35. package/lib/rule.d.ts +54 -45
  36. package/lib/stringifier.d.ts +46 -0
  37. package/lib/stringifier.js +140 -138
  38. package/lib/stringify.d.ts +6 -2
  39. package/lib/terminal-highlight.js +11 -11
  40. package/lib/tokenize.js +2 -2
  41. package/lib/warn-once.js +1 -0
  42. package/lib/warning.d.ts +79 -36
  43. package/lib/warning.js +6 -4
  44. package/package.json +20 -10
package/lib/node.js CHANGED
@@ -10,7 +10,7 @@ function cloneNode(obj, parent) {
10
10
 
11
11
  for (let i in obj) {
12
12
  if (!Object.prototype.hasOwnProperty.call(obj, i)) {
13
- // istanbul ignore next
13
+ /* c8 ignore next 2 */
14
14
  continue
15
15
  }
16
16
  if (i === 'proxyCache') continue
@@ -54,37 +54,23 @@ class Node {
54
54
  }
55
55
  }
56
56
 
57
- error(message, opts = {}) {
58
- if (this.source) {
59
- let pos = this.positionBy(opts)
60
- return this.source.input.error(message, pos.line, pos.column, opts)
57
+ addToError(error) {
58
+ error.postcssNode = this
59
+ if (error.stack && this.source && /\n\s{4}at /.test(error.stack)) {
60
+ let s = this.source
61
+ error.stack = error.stack.replace(
62
+ /\n\s{4}at /,
63
+ `$&${s.input.from}:${s.start.line}:${s.start.column}$&`
64
+ )
61
65
  }
62
- return new CssSyntaxError(message)
63
- }
64
-
65
- warn(result, text, opts) {
66
- let data = { node: this }
67
- for (let i in opts) data[i] = opts[i]
68
- return result.warn(text, data)
66
+ return error
69
67
  }
70
68
 
71
- remove() {
72
- if (this.parent) {
73
- this.parent.removeChild(this)
74
- }
75
- this.parent = undefined
69
+ after(add) {
70
+ this.parent.insertAfter(this, add)
76
71
  return this
77
72
  }
78
73
 
79
- toString(stringifier = stringify) {
80
- if (stringifier.stringify) stringifier = stringifier.stringify
81
- let result = ''
82
- stringifier(this, i => {
83
- result += i
84
- })
85
- return result
86
- }
87
-
88
74
  assign(overrides = {}) {
89
75
  for (let name in overrides) {
90
76
  this[name] = overrides[name]
@@ -92,6 +78,17 @@ class Node {
92
78
  return this
93
79
  }
94
80
 
81
+ before(add) {
82
+ this.parent.insertBefore(this, add)
83
+ return this
84
+ }
85
+
86
+ cleanRaws(keepBetween) {
87
+ delete this.raws.before
88
+ delete this.raws.after
89
+ if (!keepBetween) delete this.raws.between
90
+ }
91
+
95
92
  clone(overrides = {}) {
96
93
  let cloned = cloneNode(this)
97
94
  for (let name in overrides) {
@@ -100,39 +97,70 @@ class Node {
100
97
  return cloned
101
98
  }
102
99
 
103
- cloneBefore(overrides = {}) {
100
+ cloneAfter(overrides = {}) {
104
101
  let cloned = this.clone(overrides)
105
- this.parent.insertBefore(this, cloned)
102
+ this.parent.insertAfter(this, cloned)
106
103
  return cloned
107
104
  }
108
105
 
109
- cloneAfter(overrides = {}) {
106
+ cloneBefore(overrides = {}) {
110
107
  let cloned = this.clone(overrides)
111
- this.parent.insertAfter(this, cloned)
108
+ this.parent.insertBefore(this, cloned)
112
109
  return cloned
113
110
  }
114
111
 
115
- replaceWith(...nodes) {
116
- if (this.parent) {
117
- let bookmark = this
118
- let foundSelf = false
119
- for (let node of nodes) {
120
- if (node === this) {
121
- foundSelf = true
122
- } else if (foundSelf) {
123
- this.parent.insertAfter(bookmark, node)
124
- bookmark = node
112
+ error(message, opts = {}) {
113
+ if (this.source) {
114
+ let { end, start } = this.rangeBy(opts)
115
+ return this.source.input.error(
116
+ message,
117
+ { column: start.column, line: start.line },
118
+ { column: end.column, line: end.line },
119
+ opts
120
+ )
121
+ }
122
+ return new CssSyntaxError(message)
123
+ }
124
+
125
+ getProxyProcessor() {
126
+ return {
127
+ get(node, prop) {
128
+ if (prop === 'proxyOf') {
129
+ return node
130
+ } else if (prop === 'root') {
131
+ return () => node.root().toProxy()
125
132
  } else {
126
- this.parent.insertBefore(bookmark, node)
133
+ return node[prop]
127
134
  }
128
- }
135
+ },
129
136
 
130
- if (!foundSelf) {
131
- this.remove()
137
+ set(node, prop, value) {
138
+ if (node[prop] === value) return true
139
+ node[prop] = value
140
+ if (
141
+ prop === 'prop' ||
142
+ prop === 'value' ||
143
+ prop === 'name' ||
144
+ prop === 'params' ||
145
+ prop === 'important' ||
146
+ /* c8 ignore next */
147
+ prop === 'text'
148
+ ) {
149
+ node.markDirty()
150
+ }
151
+ return true
132
152
  }
133
153
  }
154
+ }
134
155
 
135
- return this
156
+ markDirty() {
157
+ if (this[isClean]) {
158
+ this[isClean] = false
159
+ let next = this
160
+ while ((next = next.parent)) {
161
+ next[isClean] = false
162
+ }
163
+ }
136
164
  }
137
165
 
138
166
  next() {
@@ -141,19 +169,128 @@ class Node {
141
169
  return this.parent.nodes[index + 1]
142
170
  }
143
171
 
172
+ positionBy(opts, stringRepresentation) {
173
+ let pos = this.source.start
174
+ if (opts.index) {
175
+ pos = this.positionInside(opts.index, stringRepresentation)
176
+ } else if (opts.word) {
177
+ stringRepresentation = this.toString()
178
+ let index = stringRepresentation.indexOf(opts.word)
179
+ if (index !== -1) pos = this.positionInside(index, stringRepresentation)
180
+ }
181
+ return pos
182
+ }
183
+
184
+ positionInside(index, stringRepresentation) {
185
+ let string = stringRepresentation || this.toString()
186
+ let column = this.source.start.column
187
+ let line = this.source.start.line
188
+
189
+ for (let i = 0; i < index; i++) {
190
+ if (string[i] === '\n') {
191
+ column = 1
192
+ line += 1
193
+ } else {
194
+ column += 1
195
+ }
196
+ }
197
+
198
+ return { column, line }
199
+ }
200
+
144
201
  prev() {
145
202
  if (!this.parent) return undefined
146
203
  let index = this.parent.index(this)
147
204
  return this.parent.nodes[index - 1]
148
205
  }
149
206
 
150
- before(add) {
151
- this.parent.insertBefore(this, add)
207
+ rangeBy(opts) {
208
+ let start = {
209
+ column: this.source.start.column,
210
+ line: this.source.start.line
211
+ }
212
+ let end = this.source.end
213
+ ? {
214
+ column: this.source.end.column + 1,
215
+ line: this.source.end.line
216
+ }
217
+ : {
218
+ column: start.column + 1,
219
+ line: start.line
220
+ }
221
+
222
+ if (opts.word) {
223
+ let stringRepresentation = this.toString()
224
+ let index = stringRepresentation.indexOf(opts.word)
225
+ if (index !== -1) {
226
+ start = this.positionInside(index, stringRepresentation)
227
+ end = this.positionInside(index + opts.word.length, stringRepresentation)
228
+ }
229
+ } else {
230
+ if (opts.start) {
231
+ start = {
232
+ column: opts.start.column,
233
+ line: opts.start.line
234
+ }
235
+ } else if (opts.index) {
236
+ start = this.positionInside(opts.index)
237
+ }
238
+
239
+ if (opts.end) {
240
+ end = {
241
+ column: opts.end.column,
242
+ line: opts.end.line
243
+ }
244
+ } else if (opts.endIndex) {
245
+ end = this.positionInside(opts.endIndex)
246
+ } else if (opts.index) {
247
+ end = this.positionInside(opts.index + 1)
248
+ }
249
+ }
250
+
251
+ if (
252
+ end.line < start.line ||
253
+ (end.line === start.line && end.column <= start.column)
254
+ ) {
255
+ end = { column: start.column + 1, line: start.line }
256
+ }
257
+
258
+ return { end, start }
259
+ }
260
+
261
+ raw(prop, defaultType) {
262
+ let str = new Stringifier()
263
+ return str.raw(this, prop, defaultType)
264
+ }
265
+
266
+ remove() {
267
+ if (this.parent) {
268
+ this.parent.removeChild(this)
269
+ }
270
+ this.parent = undefined
152
271
  return this
153
272
  }
154
273
 
155
- after(add) {
156
- this.parent.insertAfter(this, add)
274
+ replaceWith(...nodes) {
275
+ if (this.parent) {
276
+ let bookmark = this
277
+ let foundSelf = false
278
+ for (let node of nodes) {
279
+ if (node === this) {
280
+ foundSelf = true
281
+ } else if (foundSelf) {
282
+ this.parent.insertAfter(bookmark, node)
283
+ bookmark = node
284
+ } else {
285
+ this.parent.insertBefore(bookmark, node)
286
+ }
287
+ }
288
+
289
+ if (!foundSelf) {
290
+ this.remove()
291
+ }
292
+ }
293
+
157
294
  return this
158
295
  }
159
296
 
@@ -165,17 +302,6 @@ class Node {
165
302
  return result
166
303
  }
167
304
 
168
- raw(prop, defaultType) {
169
- let str = new Stringifier()
170
- return str.raw(this, prop, defaultType)
171
- }
172
-
173
- cleanRaws(keepBetween) {
174
- delete this.raws.before
175
- delete this.raws.after
176
- if (!keepBetween) delete this.raws.between
177
- }
178
-
179
305
  toJSON(_, inputs) {
180
306
  let fixed = {}
181
307
  let emitInputs = inputs == null
@@ -184,7 +310,7 @@ class Node {
184
310
 
185
311
  for (let name in this) {
186
312
  if (!Object.prototype.hasOwnProperty.call(this, name)) {
187
- // istanbul ignore next
313
+ /* c8 ignore next 2 */
188
314
  continue
189
315
  }
190
316
  if (name === 'parent' || name === 'proxyCache') continue
@@ -208,9 +334,9 @@ class Node {
208
334
  inputsNextIndex++
209
335
  }
210
336
  fixed[name] = {
337
+ end: value.end,
211
338
  inputId,
212
- start: value.start,
213
- end: value.end
339
+ start: value.start
214
340
  }
215
341
  } else {
216
342
  fixed[name] = value
@@ -224,64 +350,6 @@ class Node {
224
350
  return fixed
225
351
  }
226
352
 
227
- positionInside(index) {
228
- let string = this.toString()
229
- let column = this.source.start.column
230
- let line = this.source.start.line
231
-
232
- for (let i = 0; i < index; i++) {
233
- if (string[i] === '\n') {
234
- column = 1
235
- line += 1
236
- } else {
237
- column += 1
238
- }
239
- }
240
-
241
- return { line, column }
242
- }
243
-
244
- positionBy(opts) {
245
- let pos = this.source.start
246
- if (opts.index) {
247
- pos = this.positionInside(opts.index)
248
- } else if (opts.word) {
249
- let index = this.toString().indexOf(opts.word)
250
- if (index !== -1) pos = this.positionInside(index)
251
- }
252
- return pos
253
- }
254
-
255
- getProxyProcessor() {
256
- return {
257
- set(node, prop, value) {
258
- if (node[prop] === value) return true
259
- node[prop] = value
260
- if (
261
- prop === 'prop' ||
262
- prop === 'value' ||
263
- prop === 'name' ||
264
- prop === 'params' ||
265
- prop === 'important' ||
266
- prop === 'text'
267
- ) {
268
- node.markDirty()
269
- }
270
- return true
271
- },
272
-
273
- get(node, prop) {
274
- if (prop === 'proxyOf') {
275
- return node
276
- } else if (prop === 'root') {
277
- return () => node.root().toProxy()
278
- } else {
279
- return node[prop]
280
- }
281
- }
282
- }
283
- }
284
-
285
353
  toProxy() {
286
354
  if (!this.proxyCache) {
287
355
  this.proxyCache = new Proxy(this, this.getProxyProcessor())
@@ -289,26 +357,19 @@ class Node {
289
357
  return this.proxyCache
290
358
  }
291
359
 
292
- addToError(error) {
293
- error.postcssNode = this
294
- if (error.stack && this.source && /\n\s{4}at /.test(error.stack)) {
295
- let s = this.source
296
- error.stack = error.stack.replace(
297
- /\n\s{4}at /,
298
- `$&${s.input.from}:${s.start.line}:${s.start.column}$&`
299
- )
300
- }
301
- return error
360
+ toString(stringifier = stringify) {
361
+ if (stringifier.stringify) stringifier = stringifier.stringify
362
+ let result = ''
363
+ stringifier(this, i => {
364
+ result += i
365
+ })
366
+ return result
302
367
  }
303
368
 
304
- markDirty() {
305
- if (this[isClean]) {
306
- this[isClean] = false
307
- let next = this
308
- while ((next = next.parent)) {
309
- next[isClean] = false
310
- }
311
- }
369
+ warn(result, text, opts) {
370
+ let data = { node: this }
371
+ for (let i in opts) data[i] = opts[i]
372
+ return result.warn(text, data)
312
373
  }
313
374
 
314
375
  get proxyOf() {
package/lib/parse.d.ts CHANGED
@@ -1,5 +1,9 @@
1
1
  import { Parser } from './postcss.js'
2
2
 
3
- declare const parse: Parser
3
+ interface Parse extends Parser {
4
+ default: Parse
5
+ }
4
6
 
5
- export default parse
7
+ declare const parse: Parse
8
+
9
+ export = parse