postcss 8.4.49 → 8.5.10

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/lib/input.js CHANGED
@@ -9,12 +9,31 @@ 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 {
33
+ get from() {
34
+ return this.file || this.id
35
+ }
36
+
18
37
  constructor(css, opts = {}) {
19
38
  if (
20
39
  css === null ||
@@ -33,6 +52,9 @@ class Input {
33
52
  this.hasBOM = false
34
53
  }
35
54
 
55
+ this.document = this.css
56
+ if (opts.document) this.document = opts.document.toString()
57
+
36
58
  if (opts.from) {
37
59
  if (
38
60
  !pathAvailable ||
@@ -61,31 +83,38 @@ class Input {
61
83
  }
62
84
 
63
85
  error(message, line, column, opts = {}) {
64
- let endColumn, endLine, result
86
+ let endColumn, endLine, endOffset, offset, result
65
87
 
66
88
  if (line && typeof line === 'object') {
67
89
  let start = line
68
90
  let end = column
69
91
  if (typeof start.offset === 'number') {
70
- let pos = this.fromOffset(start.offset)
92
+ offset = start.offset
93
+ let pos = this.fromOffset(offset)
71
94
  line = pos.line
72
95
  column = pos.col
73
96
  } else {
74
97
  line = start.line
75
98
  column = start.column
99
+ offset = this.fromLineAndColumn(line, column)
76
100
  }
77
101
  if (typeof end.offset === 'number') {
78
- let pos = this.fromOffset(end.offset)
102
+ endOffset = end.offset
103
+ let pos = this.fromOffset(endOffset)
79
104
  endLine = pos.line
80
105
  endColumn = pos.col
81
106
  } else {
82
107
  endLine = end.line
83
108
  endColumn = end.column
109
+ endOffset = this.fromLineAndColumn(end.line, end.column)
84
110
  }
85
111
  } else if (!column) {
86
- let pos = this.fromOffset(line)
112
+ offset = line
113
+ let pos = this.fromOffset(offset)
87
114
  line = pos.line
88
115
  column = pos.col
116
+ } else {
117
+ offset = this.fromLineAndColumn(line, column)
89
118
  }
90
119
 
91
120
  let origin = this.origin(line, column, endLine, endColumn)
@@ -113,7 +142,15 @@ class Input {
113
142
  )
114
143
  }
115
144
 
116
- result.input = { column, endColumn, endLine, line, source: this.css }
145
+ result.input = {
146
+ column,
147
+ endColumn,
148
+ endLine,
149
+ endOffset,
150
+ line,
151
+ offset,
152
+ source: this.css
153
+ }
117
154
  if (this.file) {
118
155
  if (pathToFileURL) {
119
156
  result.input.url = pathToFileURL(this.file).toString()
@@ -124,23 +161,15 @@ class Input {
124
161
  return result
125
162
  }
126
163
 
127
- fromOffset(offset) {
128
- let lastLine, lineToIndex
129
- if (!this[fromOffsetCache]) {
130
- let lines = this.css.split('\n')
131
- lineToIndex = new Array(lines.length)
132
- let prevIndex = 0
133
-
134
- for (let i = 0, l = lines.length; i < l; i++) {
135
- lineToIndex[i] = prevIndex
136
- prevIndex += lines[i].length + 1
137
- }
164
+ fromLineAndColumn(line, column) {
165
+ let lineToIndex = getLineToIndex(this)
166
+ let index = lineToIndex[line - 1]
167
+ return index + column - 1
168
+ }
138
169
 
139
- this[fromOffsetCache] = lineToIndex
140
- } else {
141
- lineToIndex = this[fromOffsetCache]
142
- }
143
- lastLine = lineToIndex[lineToIndex.length - 1]
170
+ fromOffset(offset) {
171
+ let lineToIndex = getLineToIndex(this)
172
+ let lastLine = lineToIndex[lineToIndex.length - 1]
144
173
 
145
174
  let min = 0
146
175
  if (offset >= lastLine) {
@@ -234,10 +263,6 @@ class Input {
234
263
  }
235
264
  return json
236
265
  }
237
-
238
- get from() {
239
- return this.file || this.id
240
- }
241
266
  }
242
267
 
243
268
  module.exports = Input
@@ -6,7 +6,6 @@ import Root from './root.js'
6
6
  import Warning from './warning.js'
7
7
 
8
8
  declare namespace LazyResult {
9
- // eslint-disable-next-line @typescript-eslint/no-use-before-define
10
9
  export { LazyResult_ as default }
11
10
  }
12
11
 
@@ -19,9 +18,9 @@ declare namespace LazyResult {
19
18
  * const lazy = postcss([autoprefixer]).process(css)
20
19
  * ```
21
20
  */
22
- declare class LazyResult_<RootNode = Document | Root>
23
- implements PromiseLike<Result<RootNode>>
24
- {
21
+ declare class LazyResult_<RootNode = Document | Root> implements PromiseLike<
22
+ Result<RootNode>
23
+ > {
25
24
  /**
26
25
  * Processes input CSS through synchronous and asynchronous plugins
27
26
  * and calls onRejected for each error thrown in any plugin.
@@ -67,46 +66,6 @@ declare class LazyResult_<RootNode = Document | Root>
67
66
  */
68
67
  then: Promise<Result<RootNode>>['then']
69
68
 
70
- /**
71
- * @param processor Processor used for this transformation.
72
- * @param css CSS to parse and transform.
73
- * @param opts Options from the `Processor#process` or `Root#toResult`.
74
- */
75
- constructor(processor: Processor, css: string, opts: ResultOptions)
76
-
77
- /**
78
- * Run plugin in async way and return `Result`.
79
- *
80
- * @return Result with output content.
81
- */
82
- async(): Promise<Result<RootNode>>
83
-
84
- /**
85
- * Run plugin in sync way and return `Result`.
86
- *
87
- * @return Result with output content.
88
- */
89
- sync(): Result<RootNode>
90
-
91
- /**
92
- * Alias for the `LazyResult#css` property.
93
- *
94
- * ```js
95
- * lazy + '' === lazy.css
96
- * ```
97
- *
98
- * @return Output CSS.
99
- */
100
- toString(): string
101
-
102
- /**
103
- * Processes input CSS through synchronous plugins
104
- * and calls `Result#warnings`.
105
- *
106
- * @return Warnings from plugins.
107
- */
108
- warnings(): Warning[]
109
-
110
69
  /**
111
70
  * An alias for the `css` property. Use it with syntaxes
112
71
  * that generate non-CSS output.
@@ -181,6 +140,46 @@ declare class LazyResult_<RootNode = Document | Root>
181
140
  * Required to implement the Promise interface.
182
141
  */
183
142
  get [Symbol.toStringTag](): string
143
+
144
+ /**
145
+ * @param processor Processor used for this transformation.
146
+ * @param css CSS to parse and transform.
147
+ * @param opts Options from the `Processor#process` or `Root#toResult`.
148
+ */
149
+ constructor(processor: Processor, css: string, opts: ResultOptions)
150
+
151
+ /**
152
+ * Run plugin in async way and return `Result`.
153
+ *
154
+ * @return Result with output content.
155
+ */
156
+ async(): Promise<Result<RootNode>>
157
+
158
+ /**
159
+ * Run plugin in sync way and return `Result`.
160
+ *
161
+ * @return Result with output content.
162
+ */
163
+ sync(): Result<RootNode>
164
+
165
+ /**
166
+ * Alias for the `LazyResult#css` property.
167
+ *
168
+ * ```js
169
+ * lazy + '' === lazy.css
170
+ * ```
171
+ *
172
+ * @return Output CSS.
173
+ */
174
+ toString(): string
175
+
176
+ /**
177
+ * Processes input CSS through synchronous plugins
178
+ * and calls `Result#warnings`.
179
+ *
180
+ * @return Warnings from plugins.
181
+ */
182
+ warnings(): Warning[]
184
183
  }
185
184
 
186
185
  declare class LazyResult<
@@ -105,6 +105,38 @@ function cleanMarks(node) {
105
105
  let postcss = {}
106
106
 
107
107
  class LazyResult {
108
+ get content() {
109
+ return this.stringify().content
110
+ }
111
+
112
+ get css() {
113
+ return this.stringify().css
114
+ }
115
+
116
+ get map() {
117
+ return this.stringify().map
118
+ }
119
+
120
+ get messages() {
121
+ return this.sync().messages
122
+ }
123
+
124
+ get opts() {
125
+ return this.result.opts
126
+ }
127
+
128
+ get processor() {
129
+ return this.result.processor
130
+ }
131
+
132
+ get root() {
133
+ return this.sync().root
134
+ }
135
+
136
+ get [Symbol.toStringTag]() {
137
+ return 'LazyResult'
138
+ }
139
+
108
140
  constructor(processor, css, opts) {
109
141
  this.stringified = false
110
142
  this.processed = false
@@ -505,38 +537,6 @@ class LazyResult {
505
537
  warnings() {
506
538
  return this.sync().warnings()
507
539
  }
508
-
509
- get content() {
510
- return this.stringify().content
511
- }
512
-
513
- get css() {
514
- return this.stringify().css
515
- }
516
-
517
- get map() {
518
- return this.stringify().map
519
- }
520
-
521
- get messages() {
522
- return this.sync().messages
523
- }
524
-
525
- get opts() {
526
- return this.result.opts
527
- }
528
-
529
- get processor() {
530
- return this.result.processor
531
- }
532
-
533
- get root() {
534
- return this.sync().root
535
- }
536
-
537
- get [Symbol.toStringTag]() {
538
- return 'LazyResult'
539
- }
540
540
  }
541
541
 
542
542
  LazyResult.registerPostcss = dependant => {
package/lib/list.d.ts CHANGED
@@ -55,6 +55,6 @@ declare namespace list {
55
55
  }
56
56
  }
57
57
 
58
- declare const list: list.List
58
+ declare let list: list.List
59
59
 
60
60
  export = list
@@ -75,7 +75,15 @@ class MapGenerator {
75
75
  }
76
76
  }
77
77
  } else if (this.css) {
78
- this.css = this.css.replace(/\n*\/\*#[\S\s]*?\*\/$/gm, '')
78
+ let startIndex
79
+ while ((startIndex = this.css.lastIndexOf('/*#')) !== -1) {
80
+ let endIndex = this.css.indexOf('*/', startIndex + 3)
81
+ if (endIndex === -1) break
82
+ while (startIndex > 0 && this.css[startIndex - 1] === '\n') {
83
+ startIndex--
84
+ }
85
+ this.css = this.css.slice(0, startIndex) + this.css.slice(endIndex + 2)
86
+ }
79
87
  }
80
88
  }
81
89
 
@@ -6,7 +6,6 @@ import Root from './root.js'
6
6
  import Warning from './warning.js'
7
7
 
8
8
  declare namespace NoWorkResult {
9
- // eslint-disable-next-line @typescript-eslint/no-use-before-define
10
9
  export { NoWorkResult_ as default }
11
10
  }
12
11
 
@@ -26,11 +25,6 @@ declare class NoWorkResult_ implements LazyResult<Root> {
26
25
  catch: Promise<Result<Root>>['catch']
27
26
  finally: Promise<Result<Root>>['finally']
28
27
  then: Promise<Result<Root>>['then']
29
- constructor(processor: Processor, css: string, opts: ResultOptions)
30
- async(): Promise<Result<Root>>
31
- sync(): Result<Root>
32
- toString(): string
33
- warnings(): Warning[]
34
28
  get content(): string
35
29
  get css(): string
36
30
  get map(): SourceMap
@@ -39,6 +33,11 @@ declare class NoWorkResult_ implements LazyResult<Root> {
39
33
  get processor(): Processor
40
34
  get root(): Root
41
35
  get [Symbol.toStringTag](): string
36
+ constructor(processor: Processor, css: string, opts: ResultOptions)
37
+ async(): Promise<Result<Root>>
38
+ sync(): Result<Root>
39
+ toString(): string
40
+ warnings(): Warning[]
42
41
  }
43
42
 
44
43
  declare class NoWorkResult extends NoWorkResult_ {}
@@ -2,11 +2,61 @@
2
2
 
3
3
  let MapGenerator = require('./map-generator')
4
4
  let parse = require('./parse')
5
- const Result = require('./result')
5
+ let Result = require('./result')
6
6
  let stringify = require('./stringify')
7
7
  let warnOnce = require('./warn-once')
8
8
 
9
9
  class NoWorkResult {
10
+ get content() {
11
+ return this.result.css
12
+ }
13
+
14
+ get css() {
15
+ return this.result.css
16
+ }
17
+
18
+ get map() {
19
+ return this.result.map
20
+ }
21
+
22
+ get messages() {
23
+ return []
24
+ }
25
+
26
+ get opts() {
27
+ return this.result.opts
28
+ }
29
+
30
+ get processor() {
31
+ return this.result.processor
32
+ }
33
+
34
+ get root() {
35
+ if (this._root) {
36
+ return this._root
37
+ }
38
+
39
+ let root
40
+ let parser = parse
41
+
42
+ try {
43
+ root = parser(this._css, this._opts)
44
+ } catch (error) {
45
+ this.error = error
46
+ }
47
+
48
+ if (this.error) {
49
+ throw this.error
50
+ } else {
51
+ this._root = root
52
+ return root
53
+ }
54
+ }
55
+
56
+ get [Symbol.toStringTag]() {
57
+ return 'NoWorkResult'
58
+ }
59
+
10
60
  constructor(processor, css, opts) {
11
61
  css = css.toString()
12
62
  this.stringified = false
@@ -15,10 +65,9 @@ class NoWorkResult {
15
65
  this._css = css
16
66
  this._opts = opts
17
67
  this._map = undefined
18
- let root
19
68
 
20
69
  let str = stringify
21
- this.result = new Result(this._processor, root, this._opts)
70
+ this.result = new Result(this._processor, undefined, this._opts)
22
71
  this.result.css = css
23
72
 
24
73
  let self = this
@@ -28,7 +77,7 @@ class NoWorkResult {
28
77
  }
29
78
  })
30
79
 
31
- let map = new MapGenerator(str, root, this._opts, css)
80
+ let map = new MapGenerator(str, undefined, this._opts, css)
32
81
  if (map.isMap()) {
33
82
  let [generatedCSS, generatedMap] = map.generate()
34
83
  if (generatedCSS) {
@@ -82,56 +131,6 @@ class NoWorkResult {
82
131
  warnings() {
83
132
  return []
84
133
  }
85
-
86
- get content() {
87
- return this.result.css
88
- }
89
-
90
- get css() {
91
- return this.result.css
92
- }
93
-
94
- get map() {
95
- return this.result.map
96
- }
97
-
98
- get messages() {
99
- return []
100
- }
101
-
102
- get opts() {
103
- return this.result.opts
104
- }
105
-
106
- get processor() {
107
- return this.result.processor
108
- }
109
-
110
- get root() {
111
- if (this._root) {
112
- return this._root
113
- }
114
-
115
- let root
116
- let parser = parse
117
-
118
- try {
119
- root = parser(this._css, this._opts)
120
- } catch (error) {
121
- this.error = error
122
- }
123
-
124
- if (this.error) {
125
- throw this.error
126
- } else {
127
- this._root = root
128
- return root
129
- }
130
- }
131
-
132
- get [Symbol.toStringTag]() {
133
- return 'NoWorkResult'
134
- }
135
134
  }
136
135
 
137
136
  module.exports = NoWorkResult
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
 
@@ -111,7 +126,6 @@ declare namespace Node {
111
126
  word?: string
112
127
  }
113
128
 
114
- // eslint-disable-next-line @typescript-eslint/no-shadow
115
129
  class Node extends Node_ {}
116
130
  export { Node as default }
117
131
  }
@@ -234,14 +248,6 @@ declare abstract class Node_ {
234
248
 
235
249
  constructor(defaults?: object)
236
250
 
237
- /**
238
- * If this node isn't already dirty, marks it and its ancestors as such. This
239
- * indicates to the LazyResult processor that the {@link Root} has been
240
- * modified by the current plugin and may need to be processed again by other
241
- * plugins.
242
- */
243
- protected markDirty(): void
244
-
245
251
  /**
246
252
  * Insert new node after current node to current node’s parent.
247
253
  *
@@ -432,7 +438,7 @@ declare abstract class Node_ {
432
438
  * @return Range.
433
439
  */
434
440
  rangeBy(
435
- opts?: Pick<WarningOptions, 'endIndex' | 'index' | 'word'>
441
+ opts?: Pick<WarningOptions, 'end' | 'endIndex' | 'index' | 'start' | 'word'>
436
442
  ): Node.Range
437
443
 
438
444
  /**
@@ -534,6 +540,14 @@ declare abstract class Node_ {
534
540
  * @return `Warning` instance is returned
535
541
  */
536
542
  warn(result: Result, message: string, options?: WarningOptions): Warning
543
+
544
+ /**
545
+ * If this node isn't already dirty, marks it and its ancestors as such. This
546
+ * indicates to the LazyResult processor that the {@link Root} has been
547
+ * modified by the current plugin and may need to be processed again by other
548
+ * plugins.
549
+ */
550
+ protected markDirty(): void
537
551
  }
538
552
 
539
553
  declare class Node extends Node_ {}