You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

239 lines
6.1 KiB

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