UNPKG

6.22 kBTypeScriptView Raw
1import { FilePosition } from './input.js'
2
3/**
4 * A position that is part of a range.
5 */
6export interface RangePosition {
7 /**
8 * The line number in the input.
9 */
10 line: number
11
12 /**
13 * The column number in the input.
14 */
15 column: number
16}
17
18/**
19 * The CSS parser throws this error for broken CSS.
20 *
21 * Custom parsers can throw this error for broken custom syntax using
22 * the `Node#error` method.
23 *
24 * PostCSS will use the input source map to detect the original error location.
25 * If you wrote a Sass file, compiled it to CSS and then parsed it with PostCSS,
26 * PostCSS will show the original position in the Sass file.
27 *
28 * If you need the position in the PostCSS input
29 * (e.g., to debug the previous compiler), use `error.input.file`.
30 *
31 * ```js
32 * // Raising error from plugin
33 * throw node.error('Unknown variable', { plugin: 'postcss-vars' })
34 * ```
35 *
36 * ```js
37 * // Catching and checking syntax error
38 * try {
39 * postcss.parse('a{')
40 * } catch (error) {
41 * if (error.name === 'CssSyntaxError') {
42 * error //=> CssSyntaxError
43 * }
44 * }
45 * ```
46 */
47export default class CssSyntaxError {
48 /**
49 * Instantiates a CSS syntax error. Can be instantiated for a single position
50 * or for a range.
51 * @param message Error message.
52 * @param lineOrStartPos If for a single position, the line number, or if for
53 * a range, the inclusive start position of the error.
54 * @param columnOrEndPos If for a single position, the column number, or if for
55 * a range, the exclusive end position of the error.
56 * @param source Source code of the broken file.
57 * @param file Absolute path to the broken file.
58 * @param plugin PostCSS plugin name, if error came from plugin.
59 */
60 constructor(
61 message: string,
62 lineOrStartPos?: number | RangePosition,
63 columnOrEndPos?: number | RangePosition,
64 source?: string,
65 file?: string,
66 plugin?: string
67 )
68
69 stack: string
70
71 /**
72 * Always equal to `'CssSyntaxError'`. You should always check error type
73 * by `error.name === 'CssSyntaxError'`
74 * instead of `error instanceof CssSyntaxError`,
75 * because npm could have several PostCSS versions.
76 *
77 * ```js
78 * if (error.name === 'CssSyntaxError') {
79 * error //=> CssSyntaxError
80 * }
81 * ```
82 */
83 name: 'CssSyntaxError'
84
85 /**
86 * Error message.
87 *
88 * ```js
89 * error.message //=> 'Unclosed block'
90 * ```
91 */
92 reason: string
93
94 /**
95 * Full error text in the GNU error format
96 * with plugin, file, line and column.
97 *
98 * ```js
99 * error.message //=> 'a.css:1:1: Unclosed block'
100 * ```
101 */
102 message: string
103
104 /**
105 * Absolute path to the broken file.
106 *
107 * ```js
108 * error.file //=> 'a.sass'
109 * error.input.file //=> 'a.css'
110 * ```
111 *
112 * PostCSS will use the input source map to detect the original location.
113 * If you need the position in the PostCSS input, use `error.input.file`.
114 */
115 file?: string
116
117 /**
118 * Source line of the error.
119 *
120 * ```js
121 * error.line //=> 2
122 * error.input.line //=> 4
123 * ```
124 *
125 * PostCSS will use the input source map to detect the original location.
126 * If you need the position in the PostCSS input, use `error.input.line`.
127 */
128 line?: number
129
130 /**
131 * Source column of the error.
132 *
133 * ```js
134 * error.column //=> 1
135 * error.input.column //=> 4
136 * ```
137 *
138 * PostCSS will use the input source map to detect the original location.
139 * If you need the position in the PostCSS input, use `error.input.column`.
140 */
141 column?: number
142
143 /**
144 * Source line of the error's end, exclusive. Provided if the error pertains
145 * to a range.
146 *
147 * ```js
148 * error.endLine //=> 3
149 * error.input.endLine //=> 4
150 * ```
151 *
152 * PostCSS will use the input source map to detect the original location.
153 * If you need the position in the PostCSS input, use `error.input.endLine`.
154 */
155 endLine?: number
156
157 /**
158 * Source column of the error's end, exclusive. Provided if the error pertains
159 * to a range.
160 *
161 * ```js
162 * error.endColumn //=> 1
163 * error.input.endColumn //=> 4
164 * ```
165 *
166 * PostCSS will use the input source map to detect the original location.
167 * If you need the position in the PostCSS input, use `error.input.endColumn`.
168 */
169 endColumn?: number
170
171 /**
172 * Source code of the broken file.
173 *
174 * ```js
175 * error.source //=> 'a { b {} }'
176 * error.input.source //=> 'a b { }'
177 * ```
178 */
179 source?: string
180
181 /**
182 * Plugin name, if error came from plugin.
183 *
184 * ```js
185 * error.plugin //=> 'postcss-vars'
186 * ```
187 */
188 plugin?: string
189
190 /**
191 * Input object with PostCSS internal information
192 * about input file. If input has source map
193 * from previous tool, PostCSS will use origin
194 * (for example, Sass) source. You can use this
195 * object to get PostCSS input source.
196 *
197 * ```js
198 * error.input.file //=> 'a.css'
199 * error.file //=> 'a.sass'
200 * ```
201 */
202 input?: FilePosition
203
204 /**
205 * Returns error position, message and source code of the broken part.
206 *
207 * ```js
208 * error.toString() //=> "CssSyntaxError: app.css:1:1: Unclosed block
209 * // > 1 | a {
210 * // | ^"
211 * ```
212 *
213 * @return Error position, message and source code.
214 */
215 toString(): string
216
217 /**
218 * Returns a few lines of CSS source that caused the error.
219 *
220 * If the CSS has an input source map without `sourceContent`,
221 * this method will return an empty string.
222 *
223 * ```js
224 * error.showSourceCode() //=> " 4 | }
225 * // 5 | a {
226 * // > 6 | bad
227 * // | ^
228 * // 7 | }
229 * // 8 | b {"
230 * ```
231 *
232 * @param color Whether arrow will be colored red by terminal
233 * color codes. By default, PostCSS will detect
234 * color support by `process.stdout.isTTY`
235 * and `process.env.NODE_DISABLE_COLORS`.
236 * @return Few lines of CSS source that caused the error.
237 */
238 showSourceCode(color?: boolean): string
239}
240
\No newline at end of file