postcss 8.5.3 → 8.5.5

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
@@ -9,11 +9,12 @@ These plugins can lint your CSS, support variables and mixins,
9
9
  transpile future CSS syntax, inline images, and more.
10
10
 
11
11
  PostCSS is used by industry leaders including Wikipedia, Twitter, Alibaba,
12
- and JetBrains. The [Autoprefixer] and [Stylelint] PostCSS plugins is one of the most popular CSS tools.
12
+ and JetBrains. The [Autoprefixer] and [Stylelint] PostCSS plugins are some of the most popular CSS tools.
13
13
 
14
14
  ---
15
15
 
16
- <img src="https://cdn.evilmartians.com/badges/logo-no-label.svg" alt="" width="22" height="16" />  Made at <b><a href="https://evilmartians.com/devtools?utm_source=postcss&utm_campaign=devtools-button&utm_medium=github">Evil Martians</a></b>, product consulting for <b>developer tools</b>.
16
+ <img src="https://cdn.evilmartians.com/badges/logo-no-label.svg" alt="" width="22" height="16" />  Built by
17
+ <b><a href="https://evilmartians.com/devtools?utm_source=postcss&utm_campaign=devtools-button&utm_medium=github">Evil Martians</a></b>, go-to agency for <b>developer tools</b>.
17
18
 
18
19
  ---
19
20
 
package/lib/input.d.ts CHANGED
@@ -18,6 +18,11 @@ declare namespace Input {
18
18
  */
19
19
  endLine?: number
20
20
 
21
+ /**
22
+ * Offset of exclusive end position in source file.
23
+ */
24
+ endOffset?: number
25
+
21
26
  /**
22
27
  * Absolute path to the source file.
23
28
  */
@@ -28,6 +33,11 @@ declare namespace Input {
28
33
  */
29
34
  line: number
30
35
 
36
+ /**
37
+ * Offset of inclusive start position in source file.
38
+ */
39
+ offset: number
40
+
31
41
  /**
32
42
  * Source code.
33
43
  */
@@ -131,6 +141,9 @@ declare class Input_ {
131
141
  */
132
142
  constructor(css: string, opts?: ProcessOptions)
133
143
 
144
+ /**
145
+ * Returns `CssSyntaxError` with information about the error and its position.
146
+ */
134
147
  error(
135
148
  message: string,
136
149
  start:
@@ -151,9 +164,6 @@ declare class Input_ {
151
164
  },
152
165
  opts?: { plugin?: CssSyntaxError['plugin'] }
153
166
  ): CssSyntaxError
154
- /**
155
- * Returns `CssSyntaxError` with information about the error and its position.
156
- */
157
167
  error(
158
168
  message: string,
159
169
  line: number,
@@ -165,12 +175,23 @@ declare class Input_ {
165
175
  offset: number,
166
176
  opts?: { plugin?: CssSyntaxError['plugin'] }
167
177
  ): CssSyntaxError
178
+
179
+ /**
180
+ * Converts source line and column to offset.
181
+ *
182
+ * @param line Source line.
183
+ * @param column Source column.
184
+ * @return Source offset.
185
+ */
186
+ fromLineAndColumn(line: number, column: number): number
187
+
168
188
  /**
169
189
  * Converts source offset to line and column.
170
190
  *
171
191
  * @param offset Source offset.
172
192
  */
173
193
  fromOffset(offset: number): { col: number; line: number } | null
194
+
174
195
  /**
175
196
  * Reads the input source map and returns a symbol position
176
197
  * in the input source (e.g., in a Sass file that was compiled
package/lib/input.js CHANGED
@@ -9,11 +9,26 @@ let CssSyntaxError = require('./css-syntax-error')
9
9
  let PreviousMap = require('./previous-map')
10
10
  let terminalHighlight = require('./terminal-highlight')
11
11
 
12
- let fromOffsetCache = Symbol('fromOffsetCache')
12
+ let lineToIndexCache = Symbol('lineToIndexCache')
13
13
 
14
14
  let sourceMapAvailable = Boolean(SourceMapConsumer && SourceMapGenerator)
15
15
  let pathAvailable = Boolean(resolve && isAbsolute)
16
16
 
17
+ function getLineToIndex(input) {
18
+ if (input[lineToIndexCache]) return input[lineToIndexCache]
19
+ let lines = input.css.split('\n')
20
+ let lineToIndex = new Array(lines.length)
21
+ let prevIndex = 0
22
+
23
+ for (let i = 0, l = lines.length; i < l; i++) {
24
+ lineToIndex[i] = prevIndex
25
+ prevIndex += lines[i].length + 1
26
+ }
27
+
28
+ input[lineToIndexCache] = lineToIndex
29
+ return lineToIndex
30
+ }
31
+
17
32
  class Input {
18
33
  get from() {
19
34
  return this.file || this.id
@@ -68,31 +83,38 @@ class Input {
68
83
  }
69
84
 
70
85
  error(message, line, column, opts = {}) {
71
- let endColumn, endLine, result
86
+ let endColumn, endLine, endOffset, offset, result
72
87
 
73
88
  if (line && typeof line === 'object') {
74
89
  let start = line
75
90
  let end = column
76
91
  if (typeof start.offset === 'number') {
77
- let pos = this.fromOffset(start.offset)
92
+ offset = start.offset
93
+ let pos = this.fromOffset(offset)
78
94
  line = pos.line
79
95
  column = pos.col
80
96
  } else {
81
97
  line = start.line
82
98
  column = start.column
99
+ offset = this.fromLineAndColumn(line, column)
83
100
  }
84
101
  if (typeof end.offset === 'number') {
85
- let pos = this.fromOffset(end.offset)
102
+ endOffset = end.offset
103
+ let pos = this.fromOffset(endOffset)
86
104
  endLine = pos.line
87
105
  endColumn = pos.col
88
106
  } else {
89
107
  endLine = end.line
90
108
  endColumn = end.column
109
+ endOffset = this.fromLineAndColumn(end.line, end.column)
91
110
  }
92
111
  } else if (!column) {
93
- let pos = this.fromOffset(line)
112
+ offset = line
113
+ let pos = this.fromOffset(offset)
94
114
  line = pos.line
95
115
  column = pos.col
116
+ } else {
117
+ offset = this.fromLineAndColumn(line, column)
96
118
  }
97
119
 
98
120
  let origin = this.origin(line, column, endLine, endColumn)
@@ -120,7 +142,7 @@ class Input {
120
142
  )
121
143
  }
122
144
 
123
- result.input = { column, endColumn, endLine, line, source: this.css }
145
+ result.input = { column, endColumn, endLine, endOffset, line, offset, source: this.css }
124
146
  if (this.file) {
125
147
  if (pathToFileURL) {
126
148
  result.input.url = pathToFileURL(this.file).toString()
@@ -131,23 +153,15 @@ class Input {
131
153
  return result
132
154
  }
133
155
 
134
- fromOffset(offset) {
135
- let lastLine, lineToIndex
136
- if (!this[fromOffsetCache]) {
137
- let lines = this.css.split('\n')
138
- lineToIndex = new Array(lines.length)
139
- let prevIndex = 0
140
-
141
- for (let i = 0, l = lines.length; i < l; i++) {
142
- lineToIndex[i] = prevIndex
143
- prevIndex += lines[i].length + 1
144
- }
156
+ fromLineAndColumn(line, column) {
157
+ let lineToIndex = getLineToIndex(this)
158
+ let index = lineToIndex[line - 1]
159
+ return index + column - 1
160
+ }
145
161
 
146
- this[fromOffsetCache] = lineToIndex
147
- } else {
148
- lineToIndex = this[fromOffsetCache]
149
- }
150
- lastLine = lineToIndex[lineToIndex.length - 1]
162
+ fromOffset(offset) {
163
+ let lineToIndex = getLineToIndex(this)
164
+ let lastLine = lineToIndex[lineToIndex.length - 1]
151
165
 
152
166
  let min = 0
153
167
  if (offset >= lastLine) {
package/lib/node.d.ts CHANGED
@@ -1,5 +1,4 @@
1
1
  import AtRule = require('./at-rule.js')
2
-
3
2
  import { AtRuleProps } from './at-rule.js'
4
3
  import Comment, { CommentProps } from './comment.js'
5
4
  import Container, { NewChild } from './container.js'
@@ -66,6 +65,22 @@ declare namespace Node {
66
65
  /**
67
66
  * The inclusive ending position for the source
68
67
  * code of a node.
68
+ *
69
+ * However, `end.offset` of a non `Root` node is the exclusive position.
70
+ * See https://github.com/postcss/postcss/pull/1879 for details.
71
+ *
72
+ * ```js
73
+ * const root = postcss.parse('a { color: black }')
74
+ * const a = root.first
75
+ * const color = a.first
76
+ *
77
+ * // The offset of `Root` node is the inclusive position
78
+ * css.source.end // { line: 1, column: 19, offset: 18 }
79
+ *
80
+ * // The offset of non `Root` node is the exclusive position
81
+ * a.source.end // { line: 1, column: 18, offset: 18 }
82
+ * color.source.end // { line: 1, column: 16, offset: 16 }
83
+ * ```
69
84
  */
70
85
  end?: Position
71
86
 
@@ -424,7 +439,7 @@ declare abstract class Node_ {
424
439
  * @return Range.
425
440
  */
426
441
  rangeBy(
427
- opts?: Pick<WarningOptions, 'endIndex' | 'index' | 'word'>
442
+ opts?: Pick<WarningOptions, 'end' | 'endIndex' | 'index' | 'start' | 'word'>
428
443
  ): Node.Range
429
444
 
430
445
  /**
package/lib/node.js CHANGED
@@ -34,11 +34,8 @@ function cloneNode(obj, parent) {
34
34
 
35
35
  function sourceOffset(inputCSS, position) {
36
36
  // Not all custom syntaxes support `offset` in `source.start` and `source.end`
37
- if (
38
- position &&
39
- typeof position.offset !== 'undefined'
40
- ) {
41
- return position.offset;
37
+ if (position && typeof position.offset !== 'undefined') {
38
+ return position.offset
42
39
  }
43
40
 
44
41
  let column = 1
@@ -208,14 +205,15 @@ class Node {
208
205
  return this.parent.nodes[index + 1]
209
206
  }
210
207
 
211
- positionBy(opts) {
208
+ positionBy(opts = {}) {
212
209
  let pos = this.source.start
213
210
  if (opts.index) {
214
211
  pos = this.positionInside(opts.index)
215
212
  } else if (opts.word) {
216
- let inputString = ('document' in this.source.input)
217
- ? this.source.input.document
218
- : this.source.input.css
213
+ let inputString =
214
+ 'document' in this.source.input
215
+ ? this.source.input.document
216
+ : this.source.input.css
219
217
  let stringRepresentation = inputString.slice(
220
218
  sourceOffset(inputString, this.source.start),
221
219
  sourceOffset(inputString, this.source.end)
@@ -229,9 +227,10 @@ class Node {
229
227
  positionInside(index) {
230
228
  let column = this.source.start.column
231
229
  let line = this.source.start.line
232
- let inputString = ('document' in this.source.input)
233
- ? this.source.input.document
234
- : this.source.input.css
230
+ let inputString =
231
+ 'document' in this.source.input
232
+ ? this.source.input.document
233
+ : this.source.input.css
235
234
  let offset = sourceOffset(inputString, this.source.start)
236
235
  let end = offset + index
237
236
 
@@ -244,7 +243,7 @@ class Node {
244
243
  }
245
244
  }
246
245
 
247
- return { column, line }
246
+ return { column, line, offset: end }
248
247
  }
249
248
 
250
249
  prev() {
@@ -253,25 +252,36 @@ class Node {
253
252
  return this.parent.nodes[index - 1]
254
253
  }
255
254
 
256
- rangeBy(opts) {
255
+ rangeBy(opts = {}) {
256
+ let inputString =
257
+ 'document' in this.source.input
258
+ ? this.source.input.document
259
+ : this.source.input.css
257
260
  let start = {
258
261
  column: this.source.start.column,
259
- line: this.source.start.line
262
+ line: this.source.start.line,
263
+ offset: sourceOffset(inputString, this.source.start)
260
264
  }
261
265
  let end = this.source.end
262
266
  ? {
263
267
  column: this.source.end.column + 1,
264
- line: this.source.end.line
268
+ line: this.source.end.line,
269
+ offset:
270
+ typeof this.source.end.offset === 'number'
271
+ ? // `source.end.offset` is exclusive, so we don't need to add 1
272
+ this.source.end.offset
273
+ : // Since line/column in this.source.end is inclusive,
274
+ // the `sourceOffset(... , this.source.end)` returns an inclusive offset.
275
+ // So, we add 1 to convert it to exclusive.
276
+ sourceOffset(inputString, this.source.end) + 1
265
277
  }
266
278
  : {
267
279
  column: start.column + 1,
268
- line: start.line
280
+ line: start.line,
281
+ offset: start.offset + 1
269
282
  }
270
283
 
271
284
  if (opts.word) {
272
- let inputString = ('document' in this.source.input)
273
- ? this.source.input.document
274
- : this.source.input.css
275
285
  let stringRepresentation = inputString.slice(
276
286
  sourceOffset(inputString, this.source.start),
277
287
  sourceOffset(inputString, this.source.end)
@@ -279,15 +289,14 @@ class Node {
279
289
  let index = stringRepresentation.indexOf(opts.word)
280
290
  if (index !== -1) {
281
291
  start = this.positionInside(index)
282
- end = this.positionInside(
283
- index + opts.word.length,
284
- )
292
+ end = this.positionInside(index + opts.word.length)
285
293
  }
286
294
  } else {
287
295
  if (opts.start) {
288
296
  start = {
289
297
  column: opts.start.column,
290
- line: opts.start.line
298
+ line: opts.start.line,
299
+ offset: sourceOffset(inputString, opts.start)
291
300
  }
292
301
  } else if (opts.index) {
293
302
  start = this.positionInside(opts.index)
@@ -296,7 +305,8 @@ class Node {
296
305
  if (opts.end) {
297
306
  end = {
298
307
  column: opts.end.column,
299
- line: opts.end.line
308
+ line: opts.end.line,
309
+ offset: sourceOffset(inputString, opts.end)
300
310
  }
301
311
  } else if (typeof opts.endIndex === 'number') {
302
312
  end = this.positionInside(opts.endIndex)
@@ -309,7 +319,11 @@ class Node {
309
319
  end.line < start.line ||
310
320
  (end.line === start.line && end.column <= start.column)
311
321
  ) {
312
- end = { column: start.column + 1, line: start.line }
322
+ end = {
323
+ column: start.column + 1,
324
+ line: start.line,
325
+ offset: start.offset + 1
326
+ }
313
327
  }
314
328
 
315
329
  return { end, start }
@@ -384,6 +398,7 @@ class Node {
384
398
  } else if (typeof value === 'object' && value.toJSON) {
385
399
  fixed[name] = value.toJSON(null, inputs)
386
400
  } else if (name === 'source') {
401
+ if (value == null) continue
387
402
  let inputId = inputs.get(value.input)
388
403
  if (inputId == null) {
389
404
  inputId = inputsNextIndex
@@ -423,7 +438,7 @@ class Node {
423
438
  return result
424
439
  }
425
440
 
426
- warn(result, text, opts) {
441
+ warn(result, text, opts = {}) {
427
442
  let data = { node: this }
428
443
  for (let i in opts) data[i] = opts[i]
429
444
  return result.warn(text, data)
package/lib/processor.js CHANGED
@@ -7,7 +7,7 @@ let Root = require('./root')
7
7
 
8
8
  class Processor {
9
9
  constructor(plugins = []) {
10
- this.version = '8.5.3'
10
+ this.version = '8.5.5'
11
11
  this.plugins = this.normalize(plugins)
12
12
  }
13
13
 
package/lib/result.js CHANGED
@@ -12,7 +12,7 @@ class Result {
12
12
  this.messages = []
13
13
  this.root = root
14
14
  this.opts = opts
15
- this.css = undefined
15
+ this.css = ''
16
16
  this.map = undefined
17
17
  }
18
18
 
@@ -25,7 +25,7 @@ declare class Stringifier_ {
25
25
  comment(node: Comment): void
26
26
  decl(node: Declaration, semicolon?: boolean): void
27
27
  document(node: Document): void
28
- raw(node: AnyNode, own: null | string, detect?: string): string
28
+ raw(node: AnyNode, own: null | string, detect?: string): boolean | string
29
29
  rawBeforeClose(root: Root): string | undefined
30
30
  rawBeforeComment(root: Root, node: Comment): string | undefined
31
31
  rawBeforeDecl(root: Root, node: Declaration): string | undefined
@@ -35,7 +35,7 @@ declare class Stringifier_ {
35
35
  rawEmptyBody(root: Root): string | undefined
36
36
  rawIndent(root: Root): string | undefined
37
37
  rawSemicolon(root: Root): boolean | undefined
38
- rawValue(node: AnyNode, prop: string): string
38
+ rawValue(node: AnyNode, prop: string): number | string
39
39
  root(node: Root): void
40
40
  rule(node: Rule): void
41
41
  stringify(node: AnyNode, semicolon?: boolean): void
package/package.json CHANGED
@@ -1,14 +1,14 @@
1
1
  {
2
2
  "name": "postcss",
3
- "version": "8.5.3",
3
+ "version": "8.5.5",
4
4
  "description": "Tool for transforming styles with JS plugins",
5
5
  "engines": {
6
6
  "node": "^10 || ^12 || >=14"
7
7
  },
8
8
  "exports": {
9
9
  ".": {
10
- "require": "./lib/postcss.js",
11
- "import": "./lib/postcss.mjs"
10
+ "import": "./lib/postcss.mjs",
11
+ "require": "./lib/postcss.js"
12
12
  },
13
13
  "./lib/at-rule": "./lib/at-rule.js",
14
14
  "./lib/comment": "./lib/comment.js",
@@ -74,7 +74,7 @@
74
74
  "url": "https://github.com/postcss/postcss/issues"
75
75
  },
76
76
  "dependencies": {
77
- "nanoid": "^3.3.8",
77
+ "nanoid": "^3.3.11",
78
78
  "picocolors": "^1.1.1",
79
79
  "source-map-js": "^1.2.1"
80
80
  },