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.

150 lines
3.4 KiB

2 years ago
  1. import { ProcessOptions } from './postcss.js'
  2. import PreviousMap from './previous-map.js'
  3. export interface FilePosition {
  4. /**
  5. * URL for the source file.
  6. */
  7. url: string
  8. /**
  9. * Absolute path to the source file.
  10. */
  11. file?: string
  12. /**
  13. * Line of inclusive start position in source file.
  14. */
  15. line: number
  16. /**
  17. * Column of inclusive start position in source file.
  18. */
  19. column: number
  20. /**
  21. * Line of exclusive end position in source file.
  22. */
  23. endLine?: number
  24. /**
  25. * Column of exclusive end position in source file.
  26. */
  27. endColumn?: number
  28. /**
  29. * Source code.
  30. */
  31. source?: string
  32. }
  33. /**
  34. * Represents the source CSS.
  35. *
  36. * ```js
  37. * const root = postcss.parse(css, { from: file })
  38. * const input = root.source.input
  39. * ```
  40. */
  41. export default class Input {
  42. /**
  43. * Input CSS source.
  44. *
  45. * ```js
  46. * const input = postcss.parse('a{}', { from: file }).input
  47. * input.css //=> "a{}"
  48. * ```
  49. */
  50. css: string
  51. /**
  52. * The input source map passed from a compilation step before PostCSS
  53. * (for example, from Sass compiler).
  54. *
  55. * ```js
  56. * root.source.input.map.consumer().sources //=> ['a.sass']
  57. * ```
  58. */
  59. map: PreviousMap
  60. /**
  61. * The absolute path to the CSS source file defined
  62. * with the `from` option.
  63. *
  64. * ```js
  65. * const root = postcss.parse(css, { from: 'a.css' })
  66. * root.source.input.file //=> '/home/ai/a.css'
  67. * ```
  68. */
  69. file?: string
  70. /**
  71. * The unique ID of the CSS source. It will be created if `from` option
  72. * is not provided (because PostCSS does not know the file path).
  73. *
  74. * ```js
  75. * const root = postcss.parse(css)
  76. * root.source.input.file //=> undefined
  77. * root.source.input.id //=> "<input css 8LZeVF>"
  78. * ```
  79. */
  80. id?: string
  81. /**
  82. * The flag to indicate whether or not the source code has Unicode BOM.
  83. */
  84. hasBOM: boolean
  85. /**
  86. * @param css Input CSS source.
  87. * @param opts Process options.
  88. */
  89. constructor(css: string, opts?: ProcessOptions)
  90. /**
  91. * The CSS source identifier. Contains `Input#file` if the user
  92. * set the `from` option, or `Input#id` if they did not.
  93. *
  94. * ```js
  95. * const root = postcss.parse(css, { from: 'a.css' })
  96. * root.source.input.from //=> "/home/ai/a.css"
  97. *
  98. * const root = postcss.parse(css)
  99. * root.source.input.from //=> "<input css 1>"
  100. * ```
  101. */
  102. get from(): string
  103. /**
  104. * Reads the input source map and returns a symbol position
  105. * in the input source (e.g., in a Sass file that was compiled
  106. * to CSS before being passed to PostCSS). Optionally takes an
  107. * end position, exclusive.
  108. *
  109. * ```js
  110. * root.source.input.origin(1, 1) //=> { file: 'a.css', line: 3, column: 1 }
  111. * root.source.input.origin(1, 1, 1, 4)
  112. * //=> { file: 'a.css', line: 3, column: 1, endLine: 3, endColumn: 4 }
  113. * ```
  114. *
  115. * @param line Line for inclusive start position in input CSS.
  116. * @param column Column for inclusive start position in input CSS.
  117. * @param endLine Line for exclusive end position in input CSS.
  118. * @param endColumn Column for exclusive end position in input CSS.
  119. *
  120. * @return Position in input source.
  121. */
  122. origin(
  123. line: number,
  124. column: number,
  125. endLine?: number,
  126. endColumn?: number
  127. ): FilePosition | false
  128. /**
  129. * Converts source offset to line and column.
  130. *
  131. * @param offset Source offset.
  132. */
  133. fromOffset(offset: number): { line: number; col: number } | null
  134. }