input.d.ts 5.0 KB

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