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.

369 lines
13 KiB

4 years ago
  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(generatedPosition: Position & { bias?: number }): NullableMappedPosition;
  96. /**
  97. * Returns the generated line and column information for the original source,
  98. * line, and column positions provided. The only argument is an object with
  99. * the following properties:
  100. *
  101. * - source: The filename of the original source.
  102. * - line: The line number in the original source.
  103. * - column: The column number in the original source.
  104. * - bias: Either 'SourceMapConsumer.GREATEST_LOWER_BOUND' or
  105. * 'SourceMapConsumer.LEAST_UPPER_BOUND'. Specifies whether to return the
  106. * closest element that is smaller than or greater than the one we are
  107. * searching for, respectively, if the exact element cannot be found.
  108. * Defaults to 'SourceMapConsumer.GREATEST_LOWER_BOUND'.
  109. *
  110. * and an object is returned with the following properties:
  111. *
  112. * - line: The line number in the generated source, or null.
  113. * - column: The column number in the generated source, or null.
  114. */
  115. generatedPositionFor(originalPosition: MappedPosition & { bias?: number }): NullablePosition;
  116. /**
  117. * Returns all generated line and column information for the original source,
  118. * line, and column provided. If no column is provided, returns all mappings
  119. * corresponding to a either the line we are searching for or the next
  120. * closest line that has any mappings. Otherwise, returns all mappings
  121. * corresponding to the given line and either the column we are searching for
  122. * or the next closest column that has any offsets.
  123. *
  124. * The only argument is an object with the following properties:
  125. *
  126. * - source: The filename of the original source.
  127. * - line: The line number in the original source.
  128. * - column: Optional. the column number in the original source.
  129. *
  130. * and an array of objects is returned, each with the following properties:
  131. *
  132. * - line: The line number in the generated source, or null.
  133. * - column: The column number in the generated source, or null.
  134. */
  135. allGeneratedPositionsFor(originalPosition: MappedPosition): NullablePosition[];
  136. /**
  137. * Return true if we have the source content for every source in the source
  138. * map, false otherwise.
  139. */
  140. hasContentsOfAllSources(): boolean;
  141. /**
  142. * Returns the original source content. The only argument is the url of the
  143. * original source file. Returns null if no original source content is
  144. * available.
  145. */
  146. sourceContentFor(source: string, returnNullOnMissing?: boolean): string | null;
  147. /**
  148. * Iterate over each mapping between an original source/line/column and a
  149. * generated line/column in this source map.
  150. *
  151. * @param callback
  152. * The function that is called with each mapping.
  153. * @param context
  154. * Optional. If specified, this object will be the value of `this` every
  155. * time that `aCallback` is called.
  156. * @param order
  157. * Either `SourceMapConsumer.GENERATED_ORDER` or
  158. * `SourceMapConsumer.ORIGINAL_ORDER`. Specifies whether you want to
  159. * iterate over the mappings sorted by the generated file's line/column
  160. * order or the original's source/line/column order, respectively. Defaults to
  161. * `SourceMapConsumer.GENERATED_ORDER`.
  162. */
  163. eachMapping(callback: (mapping: MappingItem) => void, context?: any, order?: number): void;
  164. /**
  165. * Free this source map consumer's associated wasm data that is manually-managed.
  166. * Alternatively, you can use SourceMapConsumer.with to avoid needing to remember to call destroy.
  167. */
  168. destroy(): void;
  169. }
  170. export interface SourceMapConsumerConstructor {
  171. prototype: SourceMapConsumer;
  172. GENERATED_ORDER: number;
  173. ORIGINAL_ORDER: number;
  174. GREATEST_LOWER_BOUND: number;
  175. LEAST_UPPER_BOUND: number;
  176. new (rawSourceMap: RawSourceMap, sourceMapUrl?: SourceMapUrl): Promise<BasicSourceMapConsumer>;
  177. new (rawSourceMap: RawIndexMap, sourceMapUrl?: SourceMapUrl): Promise<IndexedSourceMapConsumer>;
  178. new (rawSourceMap: RawSourceMap | RawIndexMap | string, sourceMapUrl?: SourceMapUrl): Promise<BasicSourceMapConsumer | IndexedSourceMapConsumer>;
  179. /**
  180. * Create a BasicSourceMapConsumer from a SourceMapGenerator.
  181. *
  182. * @param sourceMap
  183. * The source map that will be consumed.
  184. */
  185. fromSourceMap(sourceMap: SourceMapGenerator, sourceMapUrl?: SourceMapUrl): Promise<BasicSourceMapConsumer>;
  186. /**
  187. * Construct a new `SourceMapConsumer` from `rawSourceMap` and `sourceMapUrl`
  188. * (see the `SourceMapConsumer` constructor for details. Then, invoke the `async
  189. * function f(SourceMapConsumer) -> T` with the newly constructed consumer, wait
  190. * for `f` to complete, call `destroy` on the consumer, and return `f`'s return
  191. * value.
  192. *
  193. * You must not use the consumer after `f` completes!
  194. *
  195. * By using `with`, you do not have to remember to manually call `destroy` on
  196. * the consumer, since it will be called automatically once `f` completes.
  197. *
  198. * ```js
  199. * const xSquared = await SourceMapConsumer.with(
  200. * myRawSourceMap,
  201. * null,
  202. * async function (consumer) {
  203. * // Use `consumer` inside here and don't worry about remembering
  204. * // to call `destroy`.
  205. *
  206. * const x = await whatever(consumer);
  207. * return x * x;
  208. * }
  209. * );
  210. *
  211. * // You may not use that `consumer` anymore out here; it has
  212. * // been destroyed. But you can use `xSquared`.
  213. * console.log(xSquared);
  214. * ```
  215. */
  216. with<T>(rawSourceMap: RawSourceMap | RawIndexMap | string, sourceMapUrl: SourceMapUrl | null | undefined, callback: (consumer: BasicSourceMapConsumer | IndexedSourceMapConsumer) => Promise<T> | T): Promise<T>;
  217. }
  218. export const SourceMapConsumer: SourceMapConsumerConstructor;
  219. export interface BasicSourceMapConsumer extends SourceMapConsumer {
  220. file: string;
  221. sourceRoot: string;
  222. sources: string[];
  223. sourcesContent: string[];
  224. }
  225. export interface BasicSourceMapConsumerConstructor {
  226. prototype: BasicSourceMapConsumer;
  227. new (rawSourceMap: RawSourceMap | string): Promise<BasicSourceMapConsumer>;
  228. /**
  229. * Create a BasicSourceMapConsumer from a SourceMapGenerator.
  230. *
  231. * @param sourceMap
  232. * The source map that will be consumed.
  233. */
  234. fromSourceMap(sourceMap: SourceMapGenerator): Promise<BasicSourceMapConsumer>;
  235. }
  236. export const BasicSourceMapConsumer: BasicSourceMapConsumerConstructor;
  237. export interface IndexedSourceMapConsumer extends SourceMapConsumer {
  238. sources: string[];
  239. }
  240. export interface IndexedSourceMapConsumerConstructor {
  241. prototype: IndexedSourceMapConsumer;
  242. new (rawSourceMap: RawIndexMap | string): Promise<IndexedSourceMapConsumer>;
  243. }
  244. export const IndexedSourceMapConsumer: IndexedSourceMapConsumerConstructor;
  245. export class SourceMapGenerator {
  246. constructor(startOfSourceMap?: StartOfSourceMap);
  247. /**
  248. * Creates a new SourceMapGenerator based on a SourceMapConsumer
  249. *
  250. * @param sourceMapConsumer The SourceMap.
  251. */
  252. static fromSourceMap(sourceMapConsumer: SourceMapConsumer): SourceMapGenerator;
  253. /**
  254. * Add a single mapping from original source line and column to the generated
  255. * source's line and column for this source map being created. The mapping
  256. * object should have the following properties:
  257. *
  258. * - generated: An object with the generated line and column positions.
  259. * - original: An object with the original line and column positions.
  260. * - source: The original source file (relative to the sourceRoot).
  261. * - name: An optional original token name for this mapping.
  262. */
  263. addMapping(mapping: Mapping): void;
  264. /**
  265. * Set the source content for a source file.
  266. */
  267. setSourceContent(sourceFile: string, sourceContent: string): void;
  268. /**
  269. * Applies the mappings of a sub-source-map for a specific source file to the
  270. * source map being generated. Each mapping to the supplied source file is
  271. * rewritten using the supplied source map. Note: The resolution for the
  272. * resulting mappings is the minimium of this map and the supplied map.
  273. *
  274. * @param sourceMapConsumer The source map to be applied.
  275. * @param sourceFile Optional. The filename of the source file.
  276. * If omitted, SourceMapConsumer's file property will be used.
  277. * @param sourceMapPath Optional. The dirname of the path to the source map
  278. * to be applied. If relative, it is relative to the SourceMapConsumer.
  279. * This parameter is needed when the two source maps aren't in the same
  280. * directory, and the source map to be applied contains relative source
  281. * paths. If so, those relative source paths need to be rewritten
  282. * relative to the SourceMapGenerator.
  283. */
  284. applySourceMap(sourceMapConsumer: SourceMapConsumer, sourceFile?: string, sourceMapPath?: string): void;
  285. toString(): string;
  286. toJSON(): RawSourceMap;
  287. }
  288. export class SourceNode {
  289. children: SourceNode[];
  290. sourceContents: any;
  291. line: number;
  292. column: number;
  293. source: string;
  294. name: string;
  295. constructor();
  296. constructor(
  297. line: number | null,
  298. column: number | null,
  299. source: string | null,
  300. chunks?: Array<(string | SourceNode)> | SourceNode | string,
  301. name?: string
  302. );
  303. static fromStringWithSourceMap(
  304. code: string,
  305. sourceMapConsumer: SourceMapConsumer,
  306. relativePath?: string
  307. ): SourceNode;
  308. add(chunk: Array<(string | SourceNode)> | SourceNode | string): SourceNode;
  309. prepend(chunk: Array<(string | SourceNode)> | SourceNode | string): SourceNode;
  310. setSourceContent(sourceFile: string, sourceContent: string): void;
  311. walk(fn: (chunk: string, mapping: MappedPosition) => void): void;
  312. walkSourceContents(fn: (file: string, content: string) => void): void;
  313. join(sep: string): SourceNode;
  314. replaceRight(pattern: string, replacement: string): SourceNode;
  315. toString(): string;
  316. toStringWithSourceMap(startOfSourceMap?: StartOfSourceMap): CodeWithSourceMap;
  317. }