input.d.ts 5.1 KB

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