input.d.ts 2.7 KB

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