UNPKG

4.18 kBTypeScriptView Raw
1import { CssSyntaxError, ProcessOptions } from './postcss.js'
2import PreviousMap from './previous-map.js'
3
4export interface FilePosition {
5 /**
6 * URL for the source file.
7 */
8 url: string
9
10 /**
11 * Absolute path to the source file.
12 */
13 file?: string
14
15 /**
16 * Line of inclusive start position in source file.
17 */
18 line: number
19
20 /**
21 * Column of inclusive start position in source file.
22 */
23 column: number
24
25 /**
26 * Line of exclusive end position in source file.
27 */
28 endLine?: number
29
30 /**
31 * Column of exclusive end position in source file.
32 */
33 endColumn?: number
34
35 /**
36 * Source code.
37 */
38 source?: string
39}
40
41/**
42 * Represents the source CSS.
43 *
44 * ```js
45 * const root = postcss.parse(css, { from: file })
46 * const input = root.source.input
47 * ```
48 */
49export default class Input {
50 /**
51 * Input CSS source.
52 *
53 * ```js
54 * const input = postcss.parse('a{}', { from: file }).input
55 * input.css //=> "a{}"
56 * ```
57 */
58 css: string
59
60 /**
61 * The input source map passed from a compilation step before PostCSS
62 * (for example, from Sass compiler).
63 *
64 * ```js
65 * root.source.input.map.consumer().sources //=> ['a.sass']
66 * ```
67 */
68 map: PreviousMap
69
70 /**
71 * The absolute path to the CSS source file defined
72 * with the `from` option.
73 *
74 * ```js
75 * const root = postcss.parse(css, { from: 'a.css' })
76 * root.source.input.file //=> '/home/ai/a.css'
77 * ```
78 */
79 file?: string
80
81 /**
82 * The unique ID of the CSS source. It will be created if `from` option
83 * is not provided (because PostCSS does not know the file path).
84 *
85 * ```js
86 * const root = postcss.parse(css)
87 * root.source.input.file //=> undefined
88 * root.source.input.id //=> "<input css 8LZeVF>"
89 * ```
90 */
91 id?: string
92
93 /**
94 * The flag to indicate whether or not the source code has Unicode BOM.
95 */
96 hasBOM: boolean
97
98 /**
99 * @param css Input CSS source.
100 * @param opts Process options.
101 */
102 constructor(css: string, opts?: ProcessOptions)
103
104 /**
105 * The CSS source identifier. Contains `Input#file` if the user
106 * set the `from` option, or `Input#id` if they did not.
107 *
108 * ```js
109 * const root = postcss.parse(css, { from: 'a.css' })
110 * root.source.input.from //=> "/home/ai/a.css"
111 *
112 * const root = postcss.parse(css)
113 * root.source.input.from //=> "<input css 1>"
114 * ```
115 */
116 get from(): string
117
118 /**
119 * Reads the input source map and returns a symbol position
120 * in the input source (e.g., in a Sass file that was compiled
121 * to CSS before being passed to PostCSS). Optionally takes an
122 * end position, exclusive.
123 *
124 * ```js
125 * root.source.input.origin(1, 1) //=> { file: 'a.css', line: 3, column: 1 }
126 * root.source.input.origin(1, 1, 1, 4)
127 * //=> { file: 'a.css', line: 3, column: 1, endLine: 3, endColumn: 4 }
128 * ```
129 *
130 * @param line Line for inclusive start position in input CSS.
131 * @param column Column for inclusive start position in input CSS.
132 * @param endLine Line for exclusive end position in input CSS.
133 * @param endColumn Column for exclusive end position in input CSS.
134 *
135 * @return Position in input source.
136 */
137 origin(
138 line: number,
139 column: number,
140 endLine?: number,
141 endColumn?: number
142 ): FilePosition | false
143
144 /**
145 * Converts source offset to line and column.
146 *
147 * @param offset Source offset.
148 */
149 fromOffset(offset: number): { line: number; col: number } | null
150
151 /**
152 * Returns `CssSyntaxError` with information about the error and its position.
153 */
154 error(
155 message: string,
156 line: number,
157 column: number,
158 opts?: { plugin?: CssSyntaxError['plugin'] }
159 ): CssSyntaxError
160 error(
161 message: string,
162 offset: number,
163 opts?: { plugin?: CssSyntaxError['plugin'] }
164 ): CssSyntaxError
165 error(
166 message: string,
167 start:
168 | {
169 offset: number
170 }
171 | {
172 line: number
173 column: number
174 },
175 end:
176 | {
177 offset: number
178 }
179 | {
180 line: number
181 column: number
182 },
183 opts?: { plugin?: CssSyntaxError['plugin'] }
184 ): CssSyntaxError
185}
186
\No newline at end of file