index.d.ts 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388
  1. export class VFile {
  2. /**
  3. * Create a new virtual file.
  4. *
  5. * `options` is treated as:
  6. *
  7. * * `string` or `Uint8Array` — `{value: options}`
  8. * * `URL` — `{path: options}`
  9. * * `VFile` — shallow copies its data over to the new file
  10. * * `object` — all fields are shallow copied over to the new file
  11. *
  12. * Path related fields are set in the following order (least specific to
  13. * most specific): `history`, `path`, `basename`, `stem`, `extname`,
  14. * `dirname`.
  15. *
  16. * You cannot set `dirname` or `extname` without setting either `history`,
  17. * `path`, `basename`, or `stem` too.
  18. *
  19. * @param {Compatible | null | undefined} [value]
  20. * File value.
  21. * @returns
  22. * New instance.
  23. */
  24. constructor(value?: Compatible | null | undefined)
  25. /**
  26. * Base of `path` (default: `process.cwd()` or `'/'` in browsers).
  27. *
  28. * @type {string}
  29. */
  30. cwd: string
  31. /**
  32. * Place to store custom info (default: `{}`).
  33. *
  34. * It’s OK to store custom data directly on the file but moving it to
  35. * `data` is recommended.
  36. *
  37. * @type {Data}
  38. */
  39. data: Data
  40. /**
  41. * List of file paths the file moved between.
  42. *
  43. * The first is the original path and the last is the current path.
  44. *
  45. * @type {Array<string>}
  46. */
  47. history: Array<string>
  48. /**
  49. * List of messages associated with the file.
  50. *
  51. * @type {Array<VFileMessage>}
  52. */
  53. messages: Array<VFileMessage>
  54. /**
  55. * Raw value.
  56. *
  57. * @type {Value}
  58. */
  59. value: Value
  60. /**
  61. * Source map.
  62. *
  63. * This type is equivalent to the `RawSourceMap` type from the `source-map`
  64. * module.
  65. *
  66. * @type {Map | null | undefined}
  67. */
  68. map: Map | null | undefined
  69. /**
  70. * Custom, non-string, compiled, representation.
  71. *
  72. * This is used by unified to store non-string results.
  73. * One example is when turning markdown into React nodes.
  74. *
  75. * @type {unknown}
  76. */
  77. result: unknown
  78. /**
  79. * Whether a file was saved to disk.
  80. *
  81. * This is used by vfile reporters.
  82. *
  83. * @type {boolean}
  84. */
  85. stored: boolean
  86. /**
  87. * Set basename (including extname) (`'index.min.js'`).
  88. *
  89. * Cannot contain path separators (`'/'` on unix, macOS, and browsers, `'\'`
  90. * on windows).
  91. * Cannot be nullified (use `file.path = file.dirname` instead).
  92. *
  93. * @param {string} basename
  94. * Basename.
  95. * @returns {undefined}
  96. * Nothing.
  97. */
  98. set basename(arg: string | undefined)
  99. /**
  100. * Get the basename (including extname) (example: `'index.min.js'`).
  101. *
  102. * @returns {string | undefined}
  103. * Basename.
  104. */
  105. get basename(): string | undefined
  106. /**
  107. * Set the full path (example: `'~/index.min.js'`).
  108. *
  109. * Cannot be nullified.
  110. * You can set a file URL (a `URL` object with a `file:` protocol) which will
  111. * be turned into a path with `url.fileURLToPath`.
  112. *
  113. * @param {URL | string} path
  114. * Path.
  115. * @returns {undefined}
  116. * Nothing.
  117. */
  118. set path(arg: string)
  119. /**
  120. * Get the full path (example: `'~/index.min.js'`).
  121. *
  122. * @returns {string}
  123. * Path.
  124. */
  125. get path(): string
  126. /**
  127. * Set the parent path (example: `'~'`).
  128. *
  129. * Cannot be set if there’s no `path` yet.
  130. *
  131. * @param {string | undefined} dirname
  132. * Dirname.
  133. * @returns {undefined}
  134. * Nothing.
  135. */
  136. set dirname(arg: string | undefined)
  137. /**
  138. * Get the parent path (example: `'~'`).
  139. *
  140. * @returns {string | undefined}
  141. * Dirname.
  142. */
  143. get dirname(): string | undefined
  144. /**
  145. * Set the extname (including dot) (example: `'.js'`).
  146. *
  147. * Cannot contain path separators (`'/'` on unix, macOS, and browsers, `'\'`
  148. * on windows).
  149. * Cannot be set if there’s no `path` yet.
  150. *
  151. * @param {string | undefined} extname
  152. * Extname.
  153. * @returns {undefined}
  154. * Nothing.
  155. */
  156. set extname(arg: string | undefined)
  157. /**
  158. * Get the extname (including dot) (example: `'.js'`).
  159. *
  160. * @returns {string | undefined}
  161. * Extname.
  162. */
  163. get extname(): string | undefined
  164. /**
  165. * Set the stem (basename w/o extname) (example: `'index.min'`).
  166. *
  167. * Cannot contain path separators (`'/'` on unix, macOS, and browsers, `'\'`
  168. * on windows).
  169. * Cannot be nullified (use `file.path = file.dirname` instead).
  170. *
  171. * @param {string} stem
  172. * Stem.
  173. * @returns {undefined}
  174. * Nothing.
  175. */
  176. set stem(arg: string | undefined)
  177. /**
  178. * Get the stem (basename w/o extname) (example: `'index.min'`).
  179. *
  180. * @returns {string | undefined}
  181. * Stem.
  182. */
  183. get stem(): string | undefined
  184. fail(reason: string, options?: MessageOptions | null | undefined): never
  185. fail(
  186. reason: string,
  187. parent: Node | NodeLike | null | undefined,
  188. origin?: string | null | undefined
  189. ): never
  190. fail(
  191. reason: string,
  192. place: Point | Position | null | undefined,
  193. origin?: string | null | undefined
  194. ): never
  195. fail(reason: string, origin?: string | null | undefined): never
  196. fail(
  197. cause: Error | VFileMessage,
  198. parent: Node | NodeLike | null | undefined,
  199. origin?: string | null | undefined
  200. ): never
  201. fail(
  202. cause: Error | VFileMessage,
  203. place: Point | Position | null | undefined,
  204. origin?: string | null | undefined
  205. ): never
  206. fail(cause: Error | VFileMessage, origin?: string | null | undefined): never
  207. info(
  208. reason: string,
  209. options?: MessageOptions | null | undefined
  210. ): VFileMessage
  211. info(
  212. reason: string,
  213. parent: Node | NodeLike | null | undefined,
  214. origin?: string | null | undefined
  215. ): VFileMessage
  216. info(
  217. reason: string,
  218. place: Point | Position | null | undefined,
  219. origin?: string | null | undefined
  220. ): VFileMessage
  221. info(reason: string, origin?: string | null | undefined): VFileMessage
  222. info(
  223. cause: Error | VFileMessage,
  224. parent: Node | NodeLike | null | undefined,
  225. origin?: string | null | undefined
  226. ): VFileMessage
  227. info(
  228. cause: Error | VFileMessage,
  229. place: Point | Position | null | undefined,
  230. origin?: string | null | undefined
  231. ): VFileMessage
  232. info(
  233. cause: Error | VFileMessage,
  234. origin?: string | null | undefined
  235. ): VFileMessage
  236. message(
  237. reason: string,
  238. options?: MessageOptions | null | undefined
  239. ): VFileMessage
  240. message(
  241. reason: string,
  242. parent: Node | NodeLike | null | undefined,
  243. origin?: string | null | undefined
  244. ): VFileMessage
  245. message(
  246. reason: string,
  247. place: Point | Position | null | undefined,
  248. origin?: string | null | undefined
  249. ): VFileMessage
  250. message(reason: string, origin?: string | null | undefined): VFileMessage
  251. message(
  252. cause: Error | VFileMessage,
  253. parent: Node | NodeLike | null | undefined,
  254. origin?: string | null | undefined
  255. ): VFileMessage
  256. message(
  257. cause: Error | VFileMessage,
  258. place: Point | Position | null | undefined,
  259. origin?: string | null | undefined
  260. ): VFileMessage
  261. message(
  262. cause: Error | VFileMessage,
  263. origin?: string | null | undefined
  264. ): VFileMessage
  265. /**
  266. * Serialize the file.
  267. *
  268. * > **Note**: which encodings are supported depends on the engine.
  269. * > For info on Node.js, see:
  270. * > <https://nodejs.org/api/util.html#whatwg-supported-encodings>.
  271. *
  272. * @param {string | null | undefined} [encoding='utf8']
  273. * Character encoding to understand `value` as when it’s a `Uint8Array`
  274. * (default: `'utf-8'`).
  275. * @returns {string}
  276. * Serialized file.
  277. */
  278. toString(encoding?: string | null | undefined): string
  279. }
  280. export type Node = import('unist').Node
  281. export type Point = import('unist').Point
  282. export type Position = import('unist').Position
  283. export type MessageOptions = import('vfile-message').Options
  284. export type Data = import('../index.js').Data
  285. export type Value = import('../index.js').Value
  286. export type NodeLike = object & {
  287. type: string
  288. position?: Position | undefined
  289. }
  290. /**
  291. * Things that can be passed to the constructor.
  292. */
  293. export type Compatible = Options | URL | VFile | Value
  294. /**
  295. * Set multiple values.
  296. */
  297. export type VFileCoreOptions = {
  298. /**
  299. * Set `basename` (name).
  300. */
  301. basename?: string | null | undefined
  302. /**
  303. * Set `cwd` (working directory).
  304. */
  305. cwd?: string | null | undefined
  306. /**
  307. * Set `data` (associated info).
  308. */
  309. data?: Data | null | undefined
  310. /**
  311. * Set `dirname` (path w/o basename).
  312. */
  313. dirname?: string | null | undefined
  314. /**
  315. * Set `extname` (extension with dot).
  316. */
  317. extname?: string | null | undefined
  318. /**
  319. * Set `history` (paths the file moved between).
  320. */
  321. history?: Array<string> | null | undefined
  322. /**
  323. * Set `path` (current path).
  324. */
  325. path?: URL | string | null | undefined
  326. /**
  327. * Set `stem` (name without extension).
  328. */
  329. stem?: string | null | undefined
  330. /**
  331. * Set `value` (the contents of the file).
  332. */
  333. value?: Value | null | undefined
  334. }
  335. /**
  336. * Raw source map.
  337. *
  338. * See:
  339. * <https://github.com/mozilla/source-map/blob/60adcb0/source-map.d.ts#L15-L23>.
  340. */
  341. export type Map = {
  342. /**
  343. * Which version of the source map spec this map is following.
  344. */
  345. version: number
  346. /**
  347. * An array of URLs to the original source files.
  348. */
  349. sources: Array<string>
  350. /**
  351. * An array of identifiers which can be referenced by individual mappings.
  352. */
  353. names: Array<string>
  354. /**
  355. * The URL root from which all sources are relative.
  356. */
  357. sourceRoot?: string | undefined
  358. /**
  359. * An array of contents of the original source files.
  360. */
  361. sourcesContent?: Array<string> | undefined
  362. /**
  363. * A string of base64 VLQs which contain the actual mappings.
  364. */
  365. mappings: string
  366. /**
  367. * The generated file this source map is associated with.
  368. */
  369. file: string
  370. }
  371. /**
  372. * Configuration.
  373. *
  374. * A bunch of keys that will be shallow copied over to the new file.
  375. */
  376. export type Options = Record<string, unknown> & VFileCoreOptions
  377. /**
  378. * Configuration for reporters.
  379. */
  380. export type ReporterSettings = Record<string, unknown>
  381. /**
  382. * Type for a reporter.
  383. */
  384. export type Reporter<Settings = ReporterSettings> = (
  385. files: Array<VFile>,
  386. options: Settings
  387. ) => string
  388. import {VFileMessage} from 'vfile-message'