css-syntax-error.d.ts 6.3 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. export { CssSyntaxError_ as default }
  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. */
  47. declare class CssSyntaxError_ extends Error {
  48. /**
  49. * Source column of the error.
  50. *
  51. * ```js
  52. * error.column //=> 1
  53. * error.input.column //=> 4
  54. * ```
  55. *
  56. * PostCSS will use the input source map to detect the original location.
  57. * If you need the position in the PostCSS input, use `error.input.column`.
  58. */
  59. column?: number
  60. /**
  61. * Source column of the error's end, exclusive. Provided if the error pertains
  62. * to a range.
  63. *
  64. * ```js
  65. * error.endColumn //=> 1
  66. * error.input.endColumn //=> 4
  67. * ```
  68. *
  69. * PostCSS will use the input source map to detect the original location.
  70. * If you need the position in the PostCSS input, use `error.input.endColumn`.
  71. */
  72. endColumn?: number
  73. /**
  74. * Source line of the error's end, exclusive. Provided if the error pertains
  75. * to a range.
  76. *
  77. * ```js
  78. * error.endLine //=> 3
  79. * error.input.endLine //=> 4
  80. * ```
  81. *
  82. * PostCSS will use the input source map to detect the original location.
  83. * If you need the position in the PostCSS input, use `error.input.endLine`.
  84. */
  85. endLine?: number
  86. /**
  87. * Absolute path to the broken file.
  88. *
  89. * ```js
  90. * error.file //=> 'a.sass'
  91. * error.input.file //=> 'a.css'
  92. * ```
  93. *
  94. * PostCSS will use the input source map to detect the original location.
  95. * If you need the position in the PostCSS input, use `error.input.file`.
  96. */
  97. file?: string
  98. /**
  99. * Input object with PostCSS internal information
  100. * about input file. If input has source map
  101. * from previous tool, PostCSS will use origin
  102. * (for example, Sass) source. You can use this
  103. * object to get PostCSS input source.
  104. *
  105. * ```js
  106. * error.input.file //=> 'a.css'
  107. * error.file //=> 'a.sass'
  108. * ```
  109. */
  110. input?: FilePosition
  111. /**
  112. * Source line of the error.
  113. *
  114. * ```js
  115. * error.line //=> 2
  116. * error.input.line //=> 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.line`.
  121. */
  122. line?: number
  123. /**
  124. * Full error text in the GNU error format
  125. * with plugin, file, line and column.
  126. *
  127. * ```js
  128. * error.message //=> 'a.css:1:1: Unclosed block'
  129. * ```
  130. */
  131. message: string
  132. /**
  133. * Always equal to `'CssSyntaxError'`. You should always check error type
  134. * by `error.name === 'CssSyntaxError'`
  135. * instead of `error instanceof CssSyntaxError`,
  136. * because npm could have several PostCSS versions.
  137. *
  138. * ```js
  139. * if (error.name === 'CssSyntaxError') {
  140. * error //=> CssSyntaxError
  141. * }
  142. * ```
  143. */
  144. name: 'CssSyntaxError'
  145. /**
  146. * Plugin name, if error came from plugin.
  147. *
  148. * ```js
  149. * error.plugin //=> 'postcss-vars'
  150. * ```
  151. */
  152. plugin?: string
  153. /**
  154. * Error message.
  155. *
  156. * ```js
  157. * error.message //=> 'Unclosed block'
  158. * ```
  159. */
  160. reason: string
  161. /**
  162. * Source code of the broken file.
  163. *
  164. * ```js
  165. * error.source //=> 'a { b {} }'
  166. * error.input.source //=> 'a b { }'
  167. * ```
  168. */
  169. source?: string
  170. stack: string
  171. /**
  172. * Instantiates a CSS syntax error. Can be instantiated for a single position
  173. * or for a range.
  174. * @param message Error message.
  175. * @param lineOrStartPos If for a single position, the line number, or if for
  176. * a range, the inclusive start position of the error.
  177. * @param columnOrEndPos If for a single position, the column number, or if for
  178. * a range, the exclusive end position of the error.
  179. * @param source Source code of the broken file.
  180. * @param file Absolute path to the broken file.
  181. * @param plugin PostCSS plugin name, if error came from plugin.
  182. */
  183. constructor(
  184. message: string,
  185. lineOrStartPos?: CssSyntaxError.RangePosition | number,
  186. columnOrEndPos?: CssSyntaxError.RangePosition | number,
  187. source?: string,
  188. file?: string,
  189. plugin?: string
  190. )
  191. /**
  192. * Returns a few lines of CSS source that caused the error.
  193. *
  194. * If the CSS has an input source map without `sourceContent`,
  195. * this method will return an empty string.
  196. *
  197. * ```js
  198. * error.showSourceCode() //=> " 4 | }
  199. * // 5 | a {
  200. * // > 6 | bad
  201. * // | ^
  202. * // 7 | }
  203. * // 8 | b {"
  204. * ```
  205. *
  206. * @param color Whether arrow will be colored red by terminal
  207. * color codes. By default, PostCSS will detect
  208. * color support by `process.stdout.isTTY`
  209. * and `process.env.NODE_DISABLE_COLORS`.
  210. * @return Few lines of CSS source that caused the error.
  211. */
  212. showSourceCode(color?: boolean): string
  213. /**
  214. * Returns error position, message and source code of the broken part.
  215. *
  216. * ```js
  217. * error.toString() //=> "CssSyntaxError: app.css:1:1: Unclosed block
  218. * // > 1 | a {
  219. * // | ^"
  220. * ```
  221. *
  222. * @return Error position, message and source code.
  223. */
  224. toString(): string
  225. }
  226. declare class CssSyntaxError extends CssSyntaxError_ {}
  227. export = CssSyntaxError