css-syntax-error.d.ts 4.7 KB

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