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.

206 lines
4.7 KiB

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