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.

208 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. * @param css Input CSS source.
  101. * @param opts Process options.
  102. */
  103. constructor(css: string, opts?: ProcessOptions)
  104. error(
  105. message: string,
  106. start:
  107. | {
  108. column: number
  109. line: number
  110. }
  111. | {
  112. offset: number
  113. },
  114. end:
  115. | {
  116. column: number
  117. line: number
  118. }
  119. | {
  120. offset: number
  121. },
  122. opts?: { plugin?: CssSyntaxError['plugin'] }
  123. ): CssSyntaxError
  124. /**
  125. * Returns `CssSyntaxError` with information about the error and its position.
  126. */
  127. error(
  128. message: string,
  129. line: number,
  130. column: number,
  131. opts?: { plugin?: CssSyntaxError['plugin'] }
  132. ): CssSyntaxError
  133. error(
  134. message: string,
  135. offset: number,
  136. opts?: { plugin?: CssSyntaxError['plugin'] }
  137. ): CssSyntaxError
  138. /**
  139. * Converts source offset to line and column.
  140. *
  141. * @param offset Source offset.
  142. */
  143. fromOffset(offset: number): { col: number; line: number } | null
  144. /**
  145. * Reads the input source map and returns a symbol position
  146. * in the input source (e.g., in a Sass file that was compiled
  147. * to CSS before being passed to PostCSS). Optionally takes an
  148. * end position, exclusive.
  149. *
  150. * ```js
  151. * root.source.input.origin(1, 1) //=> { file: 'a.css', line: 3, column: 1 }
  152. * root.source.input.origin(1, 1, 1, 4)
  153. * //=> { file: 'a.css', line: 3, column: 1, endLine: 3, endColumn: 4 }
  154. * ```
  155. *
  156. * @param line Line for inclusive start position in input CSS.
  157. * @param column Column for inclusive start position in input CSS.
  158. * @param endLine Line for exclusive end position in input CSS.
  159. * @param endColumn Column for exclusive end position in input CSS.
  160. *
  161. * @return Position in input source.
  162. */
  163. origin(
  164. line: number,
  165. column: number,
  166. endLine?: number,
  167. endColumn?: number
  168. ): false | Input.FilePosition
  169. /** Converts this to a JSON-friendly object representation. */
  170. toJSON(): object
  171. /**
  172. * The CSS source identifier. Contains `Input#file` if the user
  173. * set the `from` option, or `Input#id` if they did not.
  174. *
  175. * ```js
  176. * const root = postcss.parse(css, { from: 'a.css' })
  177. * root.source.input.from //=> "/home/ai/a.css"
  178. *
  179. * const root = postcss.parse(css)
  180. * root.source.input.from //=> "<input css 1>"
  181. * ```
  182. */
  183. get from(): string
  184. }
  185. declare class Input extends Input_ {}
  186. export = Input