input.d.ts 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. import { CssSyntaxError, ProcessOptions } from './postcss.js'
  2. import PreviousMap from './previous-map.js'
  3. declare namespace Input {
  4. export interface FilePosition {
  5. /**
  6. * URL for the source file.
  7. */
  8. url: string
  9. /**
  10. * Absolute path to the source file.
  11. */
  12. file?: string
  13. /**
  14. * Line of inclusive start position in source file.
  15. */
  16. line: number
  17. /**
  18. * Column of inclusive start position in source file.
  19. */
  20. column: number
  21. /**
  22. * Line of exclusive end position in source file.
  23. */
  24. endLine?: number
  25. /**
  26. * Column of exclusive end position in source file.
  27. */
  28. endColumn?: number
  29. /**
  30. * Source code.
  31. */
  32. source?: string
  33. }
  34. // eslint-disable-next-line @typescript-eslint/no-use-before-define
  35. export { Input_ as default }
  36. }
  37. /**
  38. * Represents the source CSS.
  39. *
  40. * ```js
  41. * const root = postcss.parse(css, { from: file })
  42. * const input = root.source.input
  43. * ```
  44. */
  45. declare class Input_ {
  46. /**
  47. * Input CSS source.
  48. *
  49. * ```js
  50. * const input = postcss.parse('a{}', { from: file }).input
  51. * input.css //=> "a{}"
  52. * ```
  53. */
  54. css: string
  55. /**
  56. * The input source map passed from a compilation step before PostCSS
  57. * (for example, from Sass compiler).
  58. *
  59. * ```js
  60. * root.source.input.map.consumer().sources //=> ['a.sass']
  61. * ```
  62. */
  63. map: PreviousMap
  64. /**
  65. * The absolute path to the CSS source file defined
  66. * with the `from` option.
  67. *
  68. * ```js
  69. * const root = postcss.parse(css, { from: 'a.css' })
  70. * root.source.input.file //=> '/home/ai/a.css'
  71. * ```
  72. */
  73. file?: string
  74. /**
  75. * The unique ID of the CSS source. It will be created if `from` option
  76. * is not provided (because PostCSS does not know the file path).
  77. *
  78. * ```js
  79. * const root = postcss.parse(css)
  80. * root.source.input.file //=> undefined
  81. * root.source.input.id //=> "<input css 8LZeVF>"
  82. * ```
  83. */
  84. id?: string
  85. /**
  86. * The flag to indicate whether or not the source code has Unicode BOM.
  87. */
  88. hasBOM: boolean
  89. /**
  90. * @param css Input CSS source.
  91. * @param opts Process options.
  92. */
  93. constructor(css: string, opts?: ProcessOptions)
  94. /**
  95. * The CSS source identifier. Contains `Input#file` if the user
  96. * set the `from` option, or `Input#id` if they did not.
  97. *
  98. * ```js
  99. * const root = postcss.parse(css, { from: 'a.css' })
  100. * root.source.input.from //=> "/home/ai/a.css"
  101. *
  102. * const root = postcss.parse(css)
  103. * root.source.input.from //=> "<input css 1>"
  104. * ```
  105. */
  106. get from(): string
  107. /**
  108. * Reads the input source map and returns a symbol position
  109. * in the input source (e.g., in a Sass file that was compiled
  110. * to CSS before being passed to PostCSS). Optionally takes an
  111. * end position, exclusive.
  112. *
  113. * ```js
  114. * root.source.input.origin(1, 1) //=> { file: 'a.css', line: 3, column: 1 }
  115. * root.source.input.origin(1, 1, 1, 4)
  116. * //=> { file: 'a.css', line: 3, column: 1, endLine: 3, endColumn: 4 }
  117. * ```
  118. *
  119. * @param line Line for inclusive start position in input CSS.
  120. * @param column Column for inclusive start position in input CSS.
  121. * @param endLine Line for exclusive end position in input CSS.
  122. * @param endColumn Column for exclusive end position in input CSS.
  123. *
  124. * @return Position in input source.
  125. */
  126. origin(
  127. line: number,
  128. column: number,
  129. endLine?: number,
  130. endColumn?: number
  131. ): Input.FilePosition | false
  132. /**
  133. * Converts source offset to line and column.
  134. *
  135. * @param offset Source offset.
  136. */
  137. fromOffset(offset: number): { line: number; col: number } | null
  138. /**
  139. * Returns `CssSyntaxError` with information about the error and its position.
  140. */
  141. error(
  142. message: string,
  143. line: number,
  144. column: number,
  145. opts?: { plugin?: CssSyntaxError['plugin'] }
  146. ): CssSyntaxError
  147. error(
  148. message: string,
  149. offset: number,
  150. opts?: { plugin?: CssSyntaxError['plugin'] }
  151. ): CssSyntaxError
  152. error(
  153. message: string,
  154. start:
  155. | {
  156. offset: number
  157. }
  158. | {
  159. line: number
  160. column: number
  161. },
  162. end:
  163. | {
  164. offset: number
  165. }
  166. | {
  167. line: number
  168. column: number
  169. },
  170. opts?: { plugin?: CssSyntaxError['plugin'] }
  171. ): CssSyntaxError
  172. }
  173. declare class Input extends Input_ {}
  174. export = Input