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