forked from postcss/postcss
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinput.es6
178 lines (158 loc) · 4.51 KB
/
input.es6
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
import path from 'path'
import CssSyntaxError from './css-syntax-error'
import PreviousMap from './previous-map'
let sequence = 0
/**
* Represents the source CSS.
*
* @example
* const root = postcss.parse(css, { from: file })
* const input = root.source.input
*/
class Input {
/**
* @param {string} css Input CSS source.
* @param {object} [opts] {@link Processor#process} options.
*/
constructor (css, opts = { }) {
if (css === null || (typeof css === 'object' && !css.toString)) {
throw new Error(`PostCSS received ${ css } instead of CSS string`)
}
/**
* Input CSS source
*
* @type {string}
*
* @example
* const input = postcss.parse('a{}', { from: file }).input
* input.css //=> "a{}"
*/
this.css = css.toString()
if (this.css[0] === '\uFEFF' || this.css[0] === '\uFFFE') {
this.hasBOM = true
this.css = this.css.slice(1)
} else {
this.hasBOM = false
}
if (opts.from) {
if (/^\w+:\/\//.test(opts.from)) {
/**
* The absolute path to the CSS source file defined
* with the `from` option.
*
* @type {string}
*
* @example
* const root = postcss.parse(css, { from: 'a.css' })
* root.source.input.file //=> '/home/ai/a.css'
*/
this.file = opts.from
} else {
this.file = path.resolve(opts.from)
}
}
let map = new PreviousMap(this.css, opts)
if (map.text) {
/**
* The input source map passed from a compilation step before PostCSS
* (for example, from Sass compiler).
*
* @type {PreviousMap}
*
* @example
* root.source.input.map.consumer().sources //=> ['a.sass']
*/
this.map = map
let file = map.consumer().file
if (!this.file && file) this.file = this.mapResolve(file)
}
if (!this.file) {
sequence += 1
/**
* The unique ID of the CSS source. It will be created if `from` option
* is not provided (because PostCSS does not know the file path).
*
* @type {string}
*
* @example
* const root = postcss.parse(css)
* root.source.input.file //=> undefined
* root.source.input.id //=> "<input css 1>"
*/
this.id = '<input css ' + sequence + '>'
}
if (this.map) this.map.file = this.from
}
error (message, line, column, opts = { }) {
let result
let origin = this.origin(line, column)
if (origin) {
result = new CssSyntaxError(
message, origin.line, origin.column,
origin.source, origin.file, opts.plugin
)
} else {
result = new CssSyntaxError(
message, line, column, this.css, this.file, opts.plugin)
}
result.input = { line, column, source: this.css }
if (this.file) result.input.file = this.file
return result
}
/**
* Reads the input source map and returns a symbol position
* in the input source (e.g., in a Sass file that was compiled
* to CSS before being passed to PostCSS).
*
* @param {number} line Line in input CSS.
* @param {number} column Column in input CSS.
*
* @return {filePosition} Position in input source.
*
* @example
* root.source.input.origin(1, 1) //=> { file: 'a.css', line: 3, column: 1 }
*/
origin (line, column) {
if (!this.map) return false
let consumer = this.map.consumer()
let from = consumer.originalPositionFor({ line, column })
if (!from.source) return false
let result = {
file: this.mapResolve(from.source),
line: from.line,
column: from.column
}
let source = consumer.sourceContentFor(from.source)
if (source) result.source = source
return result
}
mapResolve (file) {
if (/^\w+:\/\//.test(file)) {
return file
}
return path.resolve(this.map.consumer().sourceRoot || '.', file)
}
/**
* The CSS source identifier. Contains {@link Input#file} if the user
* set the `from` option, or {@link Input#id} if they did not.
*
* @type {string}
*
* @example
* const root = postcss.parse(css, { from: 'a.css' })
* root.source.input.from //=> "/home/ai/a.css"
*
* const root = postcss.parse(css)
* root.source.input.from //=> "<input css 1>"
*/
get from () {
return this.file || this.id
}
}
export default Input
/**
* @typedef {object} filePosition
* @property {string} file Path to file.
* @property {number} line Source line in file.
* @property {number} column Source column in file.
*/