css-syntax-error.d.ts 6.4 KB

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