UNPKG

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