1 | import { 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 | */
|
---|
32 | export 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 | stack: string
|
---|
51 |
|
---|
52 | /**
|
---|
53 | * Always equal to `'CssSyntaxError'`. You should always check error type
|
---|
54 | * by `error.name === 'CssSyntaxError'`
|
---|
55 | * instead of `error instanceof CssSyntaxError`,
|
---|
56 | * because npm could have several PostCSS versions.
|
---|
57 | *
|
---|
58 | * ```js
|
---|
59 | * if (error.name === 'CssSyntaxError') {
|
---|
60 | * error //=> CssSyntaxError
|
---|
61 | * }
|
---|
62 | * ```
|
---|
63 | */
|
---|
64 | name: 'CssSyntaxError'
|
---|
65 |
|
---|
66 | /**
|
---|
67 | * Error message.
|
---|
68 | *
|
---|
69 | * ```js
|
---|
70 | * error.message //=> 'Unclosed block'
|
---|
71 | * ```
|
---|
72 | */
|
---|
73 | reason: string
|
---|
74 |
|
---|
75 | /**
|
---|
76 | * Full error text in the GNU error format
|
---|
77 | * with plugin, file, line and column.
|
---|
78 | *
|
---|
79 | * ```js
|
---|
80 | * error.message //=> 'a.css:1:1: Unclosed block'
|
---|
81 | * ```
|
---|
82 | */
|
---|
83 | message: string
|
---|
84 |
|
---|
85 | /**
|
---|
86 | * Absolute path to the broken file.
|
---|
87 | *
|
---|
88 | * ```js
|
---|
89 | * error.file //=> 'a.sass'
|
---|
90 | * error.input.file //=> 'a.css'
|
---|
91 | * ```
|
---|
92 | *
|
---|
93 | * PostCSS will use the input source map to detect the original location.
|
---|
94 | * If you need the position in the PostCSS input, use `error.input.file`.
|
---|
95 | */
|
---|
96 | file?: string
|
---|
97 |
|
---|
98 | /**
|
---|
99 | * Source line of the error.
|
---|
100 | *
|
---|
101 | * ```js
|
---|
102 | * error.line //=> 2
|
---|
103 | * error.input.line //=> 4
|
---|
104 | * ```
|
---|
105 | *
|
---|
106 | * PostCSS will use the input source map to detect the original location.
|
---|
107 | * If you need the position in the PostCSS input, use `error.input.line`.
|
---|
108 | */
|
---|
109 | line?: number
|
---|
110 |
|
---|
111 | /**
|
---|
112 | * Source column of the error.
|
---|
113 | *
|
---|
114 | * ```js
|
---|
115 | * error.column //=> 1
|
---|
116 | * error.input.column //=> 4
|
---|
117 | * ```
|
---|
118 | *
|
---|
119 | * PostCSS will use the input source map to detect the original location.
|
---|
120 | * If you need the position in the PostCSS input, use `error.input.column`.
|
---|
121 | */
|
---|
122 | column?: number
|
---|
123 |
|
---|
124 | /**
|
---|
125 | * Source code of the broken file.
|
---|
126 | *
|
---|
127 | * ```js
|
---|
128 | * error.source //=> 'a { b {} }'
|
---|
129 | * error.input.source //=> 'a b { }'
|
---|
130 | * ```
|
---|
131 | */
|
---|
132 | source?: string
|
---|
133 |
|
---|
134 | /**
|
---|
135 | * Plugin name, if error came from plugin.
|
---|
136 | *
|
---|
137 | * ```js
|
---|
138 | * error.plugin //=> 'postcss-vars'
|
---|
139 | * ```
|
---|
140 | */
|
---|
141 | plugin?: string
|
---|
142 |
|
---|
143 | /**
|
---|
144 | * Input object with PostCSS internal information
|
---|
145 | * about input file. If input has source map
|
---|
146 | * from previous tool, PostCSS will use origin
|
---|
147 | * (for example, Sass) source. You can use this
|
---|
148 | * object to get PostCSS input source.
|
---|
149 | *
|
---|
150 | * ```js
|
---|
151 | * error.input.file //=> 'a.css'
|
---|
152 | * error.file //=> 'a.sass'
|
---|
153 | * ```
|
---|
154 | */
|
---|
155 | input?: FilePosition
|
---|
156 |
|
---|
157 | /**
|
---|
158 | * Returns error position, message and source code of the broken part.
|
---|
159 | *
|
---|
160 | * ```js
|
---|
161 | * error.toString() //=> "CssSyntaxError: app.css:1:1: Unclosed block
|
---|
162 | * // > 1 | a {
|
---|
163 | * // | ^"
|
---|
164 | * ```
|
---|
165 | *
|
---|
166 | * @return Error position, message and source code.
|
---|
167 | */
|
---|
168 | toString(): string
|
---|
169 |
|
---|
170 | /**
|
---|
171 | * Returns a few lines of CSS source that caused the error.
|
---|
172 | *
|
---|
173 | * If the CSS has an input source map without `sourceContent`,
|
---|
174 | * this method will return an empty string.
|
---|
175 | *
|
---|
176 | * ```js
|
---|
177 | * error.showSourceCode() //=> " 4 | }
|
---|
178 | * // 5 | a {
|
---|
179 | * // > 6 | bad
|
---|
180 | * // | ^
|
---|
181 | * // 7 | }
|
---|
182 | * // 8 | b {"
|
---|
183 | * ```
|
---|
184 | *
|
---|
185 | * @param color Whether arrow will be colored red by terminal
|
---|
186 | * color codes. By default, PostCSS will detect
|
---|
187 | * color support by `process.stdout.isTTY`
|
---|
188 | * and `process.env.NODE_DISABLE_COLORS`.
|
---|
189 | * @return Few lines of CSS source that caused the error.
|
---|
190 | */
|
---|
191 | showSourceCode(color?: boolean): string
|
---|
192 | }
|
---|