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