source-map.d.ts 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399
  1. // Type definitions for source-map 0.7
  2. // Project: https://github.com/mozilla/source-map
  3. // Definitions by: Morten Houston Ludvigsen <https://github.com/MortenHoustonLudvigsen>,
  4. // Ron Buckton <https://github.com/rbuckton>,
  5. // John Vilk <https://github.com/jvilk>
  6. // Definitions: https://github.com/mozilla/source-map
  7. export type SourceMapUrl = string
  8. export interface StartOfSourceMap {
  9. file?: string
  10. sourceRoot?: string
  11. skipValidation?: boolean
  12. }
  13. export interface RawSourceMap {
  14. version: number
  15. sources: string[]
  16. names: string[]
  17. sourceRoot?: string
  18. sourcesContent?: string[]
  19. mappings: string
  20. file: string
  21. }
  22. export interface RawIndexMap extends StartOfSourceMap {
  23. version: number
  24. sections: RawSection[]
  25. }
  26. export interface RawSection {
  27. offset: Position
  28. map: RawSourceMap
  29. }
  30. export interface Position {
  31. line: number
  32. column: number
  33. }
  34. export interface NullablePosition {
  35. line: number | null
  36. column: number | null
  37. lastColumn: number | null
  38. }
  39. export interface MappedPosition {
  40. source: string
  41. line: number
  42. column: number
  43. name?: string
  44. }
  45. export interface NullableMappedPosition {
  46. source: string | null
  47. line: number | null
  48. column: number | null
  49. name: string | null
  50. }
  51. export interface MappingItem {
  52. source: string
  53. generatedLine: number
  54. generatedColumn: number
  55. originalLine: number
  56. originalColumn: number
  57. name: string
  58. }
  59. export interface Mapping {
  60. generated: Position
  61. original: Position
  62. source: string
  63. name?: string
  64. }
  65. export interface CodeWithSourceMap {
  66. code: string
  67. map: SourceMapGenerator
  68. }
  69. export interface SourceMapConsumer {
  70. /**
  71. * Compute the last column for each generated mapping. The last column is
  72. * inclusive.
  73. */
  74. computeColumnSpans(): void
  75. /**
  76. * Returns the original source, line, and column information for the generated
  77. * source's line and column positions provided. The only argument is an object
  78. * with the following properties:
  79. *
  80. * - line: The line number in the generated source.
  81. * - column: The column number in the generated source.
  82. * - bias: Either 'SourceMapConsumer.GREATEST_LOWER_BOUND' or
  83. * 'SourceMapConsumer.LEAST_UPPER_BOUND'. Specifies whether to return the
  84. * closest element that is smaller than or greater than the one we are
  85. * searching for, respectively, if the exact element cannot be found.
  86. * Defaults to 'SourceMapConsumer.GREATEST_LOWER_BOUND'.
  87. *
  88. * and an object is returned with the following properties:
  89. *
  90. * - source: The original source file, or null.
  91. * - line: The line number in the original source, or null.
  92. * - column: The column number in the original source, or null.
  93. * - name: The original identifier, or null.
  94. */
  95. originalPositionFor(
  96. generatedPosition: Position & { bias?: number }
  97. ): NullableMappedPosition
  98. /**
  99. * Returns the generated line and column information for the original source,
  100. * line, and column positions provided. The only argument is an object with
  101. * the following properties:
  102. *
  103. * - source: The filename of the original source.
  104. * - line: The line number in the original source.
  105. * - column: The column number in the original source.
  106. * - bias: Either 'SourceMapConsumer.GREATEST_LOWER_BOUND' or
  107. * 'SourceMapConsumer.LEAST_UPPER_BOUND'. Specifies whether to return the
  108. * closest element that is smaller than or greater than the one we are
  109. * searching for, respectively, if the exact element cannot be found.
  110. * Defaults to 'SourceMapConsumer.GREATEST_LOWER_BOUND'.
  111. *
  112. * and an object is returned with the following properties:
  113. *
  114. * - line: The line number in the generated source, or null.
  115. * - column: The column number in the generated source, or null.
  116. */
  117. generatedPositionFor(
  118. originalPosition: MappedPosition & { bias?: number }
  119. ): NullablePosition
  120. /**
  121. * Returns all generated line and column information for the original source,
  122. * line, and column provided. If no column is provided, returns all mappings
  123. * corresponding to a either the line we are searching for or the next
  124. * closest line that has any mappings. Otherwise, returns all mappings
  125. * corresponding to the given line and either the column we are searching for
  126. * or the next closest column that has any offsets.
  127. *
  128. * The only argument is an object with the following properties:
  129. *
  130. * - source: The filename of the original source.
  131. * - line: The line number in the original source.
  132. * - column: Optional. the column number in the original source.
  133. *
  134. * and an array of objects is returned, each with the following properties:
  135. *
  136. * - line: The line number in the generated source, or null.
  137. * - column: The column number in the generated source, or null.
  138. */
  139. allGeneratedPositionsFor(originalPosition: MappedPosition): NullablePosition[]
  140. /**
  141. * Return true if we have the source content for every source in the source
  142. * map, false otherwise.
  143. */
  144. hasContentsOfAllSources(): boolean
  145. /**
  146. * Returns the original source content. The only argument is the url of the
  147. * original source file. Returns null if no original source content is
  148. * available.
  149. */
  150. sourceContentFor(source: string, returnNullOnMissing?: boolean): string | null
  151. /**
  152. * Iterate over each mapping between an original source/line/column and a
  153. * generated line/column in this source map.
  154. *
  155. * @param callback
  156. * The function that is called with each mapping.
  157. * @param context
  158. * Optional. If specified, this object will be the value of `this` every
  159. * time that `aCallback` is called.
  160. * @param order
  161. * Either `SourceMapConsumer.GENERATED_ORDER` or
  162. * `SourceMapConsumer.ORIGINAL_ORDER`. Specifies whether you want to
  163. * iterate over the mappings sorted by the generated file's line/column
  164. * order or the original's source/line/column order, respectively. Defaults to
  165. * `SourceMapConsumer.GENERATED_ORDER`.
  166. */
  167. eachMapping(
  168. callback: (mapping: MappingItem) => void,
  169. context?: any,
  170. order?: number
  171. ): void
  172. /**
  173. * Free this source map consumer's associated wasm data that is manually-managed.
  174. * Alternatively, you can use SourceMapConsumer.with to avoid needing to remember to call destroy.
  175. */
  176. destroy(): void
  177. }
  178. export interface SourceMapConsumerConstructor {
  179. prototype: SourceMapConsumer
  180. GENERATED_ORDER: number
  181. ORIGINAL_ORDER: number
  182. GREATEST_LOWER_BOUND: number
  183. LEAST_UPPER_BOUND: number
  184. new (
  185. rawSourceMap: RawSourceMap,
  186. sourceMapUrl?: SourceMapUrl
  187. ): Promise<BasicSourceMapConsumer>
  188. new (
  189. rawSourceMap: RawIndexMap,
  190. sourceMapUrl?: SourceMapUrl
  191. ): Promise<IndexedSourceMapConsumer>
  192. new (
  193. rawSourceMap: RawSourceMap | RawIndexMap | string,
  194. sourceMapUrl?: SourceMapUrl
  195. ): Promise<BasicSourceMapConsumer | IndexedSourceMapConsumer>
  196. /**
  197. * Create a BasicSourceMapConsumer from a SourceMapGenerator.
  198. *
  199. * @param sourceMap
  200. * The source map that will be consumed.
  201. */
  202. fromSourceMap(
  203. sourceMap: SourceMapGenerator,
  204. sourceMapUrl?: SourceMapUrl
  205. ): Promise<BasicSourceMapConsumer>
  206. /**
  207. * Construct a new `SourceMapConsumer` from `rawSourceMap` and `sourceMapUrl`
  208. * (see the `SourceMapConsumer` constructor for details. Then, invoke the `async
  209. * function f(SourceMapConsumer) -> T` with the newly constructed consumer, wait
  210. * for `f` to complete, call `destroy` on the consumer, and return `f`'s return
  211. * value.
  212. *
  213. * You must not use the consumer after `f` completes!
  214. *
  215. * By using `with`, you do not have to remember to manually call `destroy` on
  216. * the consumer, since it will be called automatically once `f` completes.
  217. *
  218. * ```js
  219. * const xSquared = await SourceMapConsumer.with(
  220. * myRawSourceMap,
  221. * null,
  222. * async function (consumer) {
  223. * // Use `consumer` inside here and don't worry about remembering
  224. * // to call `destroy`.
  225. *
  226. * const x = await whatever(consumer);
  227. * return x * x;
  228. * }
  229. * );
  230. *
  231. * // You may not use that `consumer` anymore out here; it has
  232. * // been destroyed. But you can use `xSquared`.
  233. * console.log(xSquared);
  234. * ```
  235. */
  236. with<T>(
  237. rawSourceMap: RawSourceMap | RawIndexMap | string,
  238. sourceMapUrl: SourceMapUrl | null | undefined,
  239. callback: (
  240. consumer: BasicSourceMapConsumer | IndexedSourceMapConsumer
  241. ) => Promise<T> | T
  242. ): Promise<T>
  243. }
  244. export const SourceMapConsumer: SourceMapConsumerConstructor
  245. export interface BasicSourceMapConsumer extends SourceMapConsumer {
  246. file: string
  247. sourceRoot: string
  248. sources: string[]
  249. sourcesContent: string[]
  250. }
  251. export interface BasicSourceMapConsumerConstructor {
  252. prototype: BasicSourceMapConsumer
  253. new (rawSourceMap: RawSourceMap | string): Promise<BasicSourceMapConsumer>
  254. /**
  255. * Create a BasicSourceMapConsumer from a SourceMapGenerator.
  256. *
  257. * @param sourceMap
  258. * The source map that will be consumed.
  259. */
  260. fromSourceMap(sourceMap: SourceMapGenerator): Promise<BasicSourceMapConsumer>
  261. }
  262. export const BasicSourceMapConsumer: BasicSourceMapConsumerConstructor
  263. export interface IndexedSourceMapConsumer extends SourceMapConsumer {
  264. sources: string[]
  265. }
  266. export interface IndexedSourceMapConsumerConstructor {
  267. prototype: IndexedSourceMapConsumer
  268. new (rawSourceMap: RawIndexMap | string): Promise<IndexedSourceMapConsumer>
  269. }
  270. export const IndexedSourceMapConsumer: IndexedSourceMapConsumerConstructor
  271. export class SourceMapGenerator {
  272. constructor(startOfSourceMap?: StartOfSourceMap)
  273. /**
  274. * Creates a new SourceMapGenerator based on a SourceMapConsumer
  275. *
  276. * @param sourceMapConsumer The SourceMap.
  277. */
  278. static fromSourceMap(sourceMapConsumer: SourceMapConsumer): SourceMapGenerator
  279. /**
  280. * Add a single mapping from original source line and column to the generated
  281. * source's line and column for this source map being created. The mapping
  282. * object should have the following properties:
  283. *
  284. * - generated: An object with the generated line and column positions.
  285. * - original: An object with the original line and column positions.
  286. * - source: The original source file (relative to the sourceRoot).
  287. * - name: An optional original token name for this mapping.
  288. */
  289. addMapping(mapping: Mapping): void
  290. /**
  291. * Set the source content for a source file.
  292. */
  293. setSourceContent(sourceFile: string, sourceContent: string): void
  294. /**
  295. * Applies the mappings of a sub-source-map for a specific source file to the
  296. * source map being generated. Each mapping to the supplied source file is
  297. * rewritten using the supplied source map. Note: The resolution for the
  298. * resulting mappings is the minimium of this map and the supplied map.
  299. *
  300. * @param sourceMapConsumer The source map to be applied.
  301. * @param sourceFile Optional. The filename of the source file.
  302. * If omitted, SourceMapConsumer's file property will be used.
  303. * @param sourceMapPath Optional. The dirname of the path to the source map
  304. * to be applied. If relative, it is relative to the SourceMapConsumer.
  305. * This parameter is needed when the two source maps aren't in the same
  306. * directory, and the source map to be applied contains relative source
  307. * paths. If so, those relative source paths need to be rewritten
  308. * relative to the SourceMapGenerator.
  309. */
  310. applySourceMap(
  311. sourceMapConsumer: SourceMapConsumer,
  312. sourceFile?: string,
  313. sourceMapPath?: string
  314. ): void
  315. toString(): string
  316. toJSON(): RawSourceMap
  317. }
  318. export class SourceNode {
  319. children: SourceNode[]
  320. sourceContents: any
  321. line: number
  322. column: number
  323. source: string
  324. name: string
  325. constructor()
  326. constructor(
  327. line: number | null,
  328. column: number | null,
  329. source: string | null,
  330. chunks?: Array<string | SourceNode> | SourceNode | string,
  331. name?: string
  332. )
  333. static fromStringWithSourceMap(
  334. code: string,
  335. sourceMapConsumer: SourceMapConsumer,
  336. relativePath?: string
  337. ): SourceNode
  338. add(chunk: Array<string | SourceNode> | SourceNode | string): SourceNode
  339. prepend(chunk: Array<string | SourceNode> | SourceNode | string): SourceNode
  340. setSourceContent(sourceFile: string, sourceContent: string): void
  341. walk(fn: (chunk: string, mapping: MappedPosition) => void): void
  342. walkSourceContents(fn: (file: string, content: string) => void): void
  343. join(sep: string): SourceNode
  344. replaceRight(pattern: string, replacement: string): SourceNode
  345. toString(): string
  346. toStringWithSourceMap(startOfSourceMap?: StartOfSourceMap): CodeWithSourceMap
  347. }