Skip to content

Commit 7c06ac8

Browse files
authored
This updates the typings to match Definitely Typed
The Definitely Typed typings are more accurate than this file. I've made a slight tweak to the currently published typings with this pull request: DefinitelyTyped/DefinitelyTyped#20573. After this is merged and upon the next version bump, it might be possible to remove the DefinitelyTyped typings.
1 parent ac518d2 commit 7c06ac8

File tree

1 file changed

+268
-37
lines changed

1 file changed

+268
-37
lines changed

source-map.d.ts

Lines changed: 268 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,39 +1,59 @@
1+
// Type definitions for source-map 0.5
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+
// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped
16
export interface StartOfSourceMap {
27
file?: string;
38
sourceRoot?: string;
9+
skipValidation?: boolean;
410
}
511

6-
export interface RawSourceMap extends StartOfSourceMap {
7-
version: string;
12+
export interface RawSourceMap {
13+
version: number;
814
sources: string[];
915
names: string[];
16+
sourceRoot?: string;
1017
sourcesContent?: string[];
1118
mappings: string;
19+
file: string;
1220
}
1321

14-
export interface Position {
15-
line: number;
16-
column: number;
22+
export interface RawIndexMap extends StartOfSourceMap {
23+
version: number;
24+
sections: RawSection[];
1725
}
1826

19-
export interface LineRange extends Position {
20-
lastColumn: number;
27+
export interface RawSection {
28+
offset: Position;
29+
map: RawSourceMap;
2130
}
2231

23-
export interface FindPosition extends Position {
24-
// SourceMapConsumer.GREATEST_LOWER_BOUND or SourceMapConsumer.LEAST_UPPER_BOUND
25-
bias?: number;
32+
export interface Position {
33+
line: number;
34+
column: number;
2635
}
2736

28-
export interface SourceFindPosition extends FindPosition {
29-
source: string;
37+
export interface NullablePosition {
38+
line: number | null;
39+
column: number | null;
40+
lastColumn: number | null;
3041
}
3142

32-
export interface MappedPosition extends Position {
43+
export interface MappedPosition {
3344
source: string;
45+
line: number;
46+
column: number;
3447
name?: string;
3548
}
3649

50+
export interface NullableMappedPosition {
51+
source: string | null;
52+
line: number | null;
53+
column: number | null;
54+
name: string | null;
55+
}
56+
3757
export interface MappingItem {
3858
source: string;
3959
generatedLine: number;
@@ -43,56 +63,267 @@ export interface MappingItem {
4363
name: string;
4464
}
4565

46-
export class SourceMapConsumer {
47-
static GENERATED_ORDER: number;
48-
static ORIGINAL_ORDER: number;
66+
export interface Mapping {
67+
generated: Position;
68+
original: Position;
69+
source: string;
70+
name?: string;
71+
}
4972

50-
static GREATEST_LOWER_BOUND: number;
51-
static LEAST_UPPER_BOUND: number;
73+
export interface CodeWithSourceMap {
74+
code: string;
75+
map: SourceMapGenerator;
76+
}
5277

53-
constructor(rawSourceMap: RawSourceMap);
78+
export interface SourceMapConsumer {
79+
/**
80+
* Compute the last column for each generated mapping. The last column is
81+
* inclusive.
82+
*/
5483
computeColumnSpans(): void;
55-
originalPositionFor(generatedPosition: FindPosition): MappedPosition;
56-
generatedPositionFor(originalPosition: SourceFindPosition): LineRange;
57-
allGeneratedPositionsFor(originalPosition: MappedPosition): Position[];
84+
85+
/**
86+
* Returns the original source, line, and column information for the generated
87+
* source's line and column positions provided. The only argument is an object
88+
* with the following properties:
89+
*
90+
* - line: The line number in the generated source.
91+
* - column: The column number in the generated source.
92+
* - bias: Either 'SourceMapConsumer.GREATEST_LOWER_BOUND' or
93+
* 'SourceMapConsumer.LEAST_UPPER_BOUND'. Specifies whether to return the
94+
* closest element that is smaller than or greater than the one we are
95+
* searching for, respectively, if the exact element cannot be found.
96+
* Defaults to 'SourceMapConsumer.GREATEST_LOWER_BOUND'.
97+
*
98+
* and an object is returned with the following properties:
99+
*
100+
* - source: The original source file, or null.
101+
* - line: The line number in the original source, or null.
102+
* - column: The column number in the original source, or null.
103+
* - name: The original identifier, or null.
104+
*/
105+
originalPositionFor(generatedPosition: Position & { bias?: number }): NullableMappedPosition;
106+
107+
/**
108+
* Returns the generated line and column information for the original source,
109+
* line, and column positions provided. The only argument is an object with
110+
* the following properties:
111+
*
112+
* - source: The filename of the original source.
113+
* - line: The line number in the original source.
114+
* - column: The column number in the original source.
115+
* - bias: Either 'SourceMapConsumer.GREATEST_LOWER_BOUND' or
116+
* 'SourceMapConsumer.LEAST_UPPER_BOUND'. Specifies whether to return the
117+
* closest element that is smaller than or greater than the one we are
118+
* searching for, respectively, if the exact element cannot be found.
119+
* Defaults to 'SourceMapConsumer.GREATEST_LOWER_BOUND'.
120+
*
121+
* and an object is returned with the following properties:
122+
*
123+
* - line: The line number in the generated source, or null.
124+
* - column: The column number in the generated source, or null.
125+
*/
126+
generatedPositionFor(originalPosition: MappedPosition & { bias?: number }): NullablePosition;
127+
128+
/**
129+
* Returns all generated line and column information for the original source,
130+
* line, and column provided. If no column is provided, returns all mappings
131+
* corresponding to a either the line we are searching for or the next
132+
* closest line that has any mappings. Otherwise, returns all mappings
133+
* corresponding to the given line and either the column we are searching for
134+
* or the next closest column that has any offsets.
135+
*
136+
* The only argument is an object with the following properties:
137+
*
138+
* - source: The filename of the original source.
139+
* - line: The line number in the original source.
140+
* - column: Optional. the column number in the original source.
141+
*
142+
* and an array of objects is returned, each with the following properties:
143+
*
144+
* - line: The line number in the generated source, or null.
145+
* - column: The column number in the generated source, or null.
146+
*/
147+
allGeneratedPositionsFor(originalPosition: MappedPosition): NullablePosition[];
148+
149+
/**
150+
* Return true if we have the source content for every source in the source
151+
* map, false otherwise.
152+
*/
58153
hasContentsOfAllSources(): boolean;
59-
sourceContentFor(source: string, returnNullOnMissing?: boolean): string;
154+
155+
/**
156+
* Returns the original source content. The only argument is the url of the
157+
* original source file. Returns null if no original source content is
158+
* available.
159+
*/
160+
sourceContentFor(source: string, returnNullOnMissing?: boolean): string | null;
161+
162+
/**
163+
* Iterate over each mapping between an original source/line/column and a
164+
* generated line/column in this source map.
165+
*
166+
* @param callback
167+
* The function that is called with each mapping.
168+
* @param context
169+
* Optional. If specified, this object will be the value of `this` every
170+
* time that `aCallback` is called.
171+
* @param order
172+
* Either `SourceMapConsumer.GENERATED_ORDER` or
173+
* `SourceMapConsumer.ORIGINAL_ORDER`. Specifies whether you want to
174+
* iterate over the mappings sorted by the generated file's line/column
175+
* order or the original's source/line/column order, respectively. Defaults to
176+
* `SourceMapConsumer.GENERATED_ORDER`.
177+
*/
60178
eachMapping(callback: (mapping: MappingItem) => void, context?: any, order?: number): void;
61179
}
62180

63-
export interface Mapping {
64-
generated: Position;
65-
original: Position;
66-
source: string;
67-
name?: string;
181+
export interface SourceMapConsumerConstructor {
182+
prototype: SourceMapConsumer;
183+
184+
GENERATED_ORDER: number;
185+
ORIGINAL_ORDER: number;
186+
GREATEST_LOWER_BOUND: number;
187+
LEAST_UPPER_BOUND: number;
188+
189+
new (rawSourceMap: RawSourceMap): BasicSourceMapConsumer;
190+
new (rawSourceMap: RawIndexMap): IndexedSourceMapConsumer;
191+
new (rawSourceMap: RawSourceMap | RawIndexMap | string): BasicSourceMapConsumer | IndexedSourceMapConsumer;
192+
193+
/**
194+
* Create a BasicSourceMapConsumer from a SourceMapGenerator.
195+
*
196+
* @param sourceMap
197+
* The source map that will be consumed.
198+
*/
199+
fromSourceMap(sourceMap: SourceMapGenerator): BasicSourceMapConsumer;
200+
}
201+
202+
export const SourceMapConsumer: SourceMapConsumerConstructor;
203+
204+
export interface BasicSourceMapConsumer extends SourceMapConsumer {
205+
file: string;
206+
sourceRoot: string;
207+
sources: string[];
208+
sourcesContent: string[];
209+
}
210+
211+
export interface BasicSourceMapConsumerConstructor {
212+
prototype: BasicSourceMapConsumer;
213+
214+
new (rawSourceMap: RawSourceMap | string): BasicSourceMapConsumer;
215+
216+
/**
217+
* Create a BasicSourceMapConsumer from a SourceMapGenerator.
218+
*
219+
* @param sourceMap
220+
* The source map that will be consumed.
221+
*/
222+
fromSourceMap(sourceMap: SourceMapGenerator): BasicSourceMapConsumer;
223+
}
224+
225+
export const BasicSourceMapConsumer: BasicSourceMapConsumerConstructor;
226+
227+
export interface IndexedSourceMapConsumer extends SourceMapConsumer {
228+
sources: string[];
68229
}
69230

231+
export interface IndexedSourceMapConsumerConstructor {
232+
prototype: IndexedSourceMapConsumer;
233+
234+
new (rawSourceMap: RawIndexMap | string): IndexedSourceMapConsumer;
235+
}
236+
237+
export const IndexedSourceMapConsumer: IndexedSourceMapConsumerConstructor;
238+
70239
export class SourceMapGenerator {
71240
constructor(startOfSourceMap?: StartOfSourceMap);
241+
242+
/**
243+
* Creates a new SourceMapGenerator based on a SourceMapConsumer
244+
*
245+
* @param sourceMapConsumer The SourceMap.
246+
*/
72247
static fromSourceMap(sourceMapConsumer: SourceMapConsumer): SourceMapGenerator;
248+
249+
/**
250+
* Add a single mapping from original source line and column to the generated
251+
* source's line and column for this source map being created. The mapping
252+
* object should have the following properties:
253+
*
254+
* - generated: An object with the generated line and column positions.
255+
* - original: An object with the original line and column positions.
256+
* - source: The original source file (relative to the sourceRoot).
257+
* - name: An optional original token name for this mapping.
258+
*/
73259
addMapping(mapping: Mapping): void;
260+
261+
/**
262+
* Set the source content for a source file.
263+
*/
74264
setSourceContent(sourceFile: string, sourceContent: string): void;
265+
266+
/**
267+
* Applies the mappings of a sub-source-map for a specific source file to the
268+
* source map being generated. Each mapping to the supplied source file is
269+
* rewritten using the supplied source map. Note: The resolution for the
270+
* resulting mappings is the minimium of this map and the supplied map.
271+
*
272+
* @param sourceMapConsumer The source map to be applied.
273+
* @param sourceFile Optional. The filename of the source file.
274+
* If omitted, SourceMapConsumer's file property will be used.
275+
* @param sourceMapPath Optional. The dirname of the path to the source map
276+
* to be applied. If relative, it is relative to the SourceMapConsumer.
277+
* This parameter is needed when the two source maps aren't in the same
278+
* directory, and the source map to be applied contains relative source
279+
* paths. If so, those relative source paths need to be rewritten
280+
* relative to the SourceMapGenerator.
281+
*/
75282
applySourceMap(sourceMapConsumer: SourceMapConsumer, sourceFile?: string, sourceMapPath?: string): void;
283+
76284
toString(): string;
77-
}
78285

79-
export interface CodeWithSourceMap {
80-
code: string;
81-
map: SourceMapGenerator;
286+
toJSON(): RawSourceMap;
82287
}
83288

84289
export class SourceNode {
290+
children: SourceNode[];
291+
sourceContents: any;
292+
line: number;
293+
column: number;
294+
source: string;
295+
name: string;
296+
85297
constructor();
86-
constructor(line: number, column: number, source: string);
87-
constructor(line: number, column: number, source: string, chunk?: string, name?: string);
88-
static fromStringWithSourceMap(code: string, sourceMapConsumer: SourceMapConsumer, relativePath?: string): SourceNode;
89-
add(chunk: string): void;
90-
prepend(chunk: string): void;
298+
constructor(
299+
line: number | null,
300+
column: number | null,
301+
source: string | null,
302+
chunks?: Array<(string | SourceNode)> | SourceNode | string,
303+
name?: string
304+
);
305+
306+
static fromStringWithSourceMap(
307+
code: string,
308+
sourceMapConsumer: SourceMapConsumer,
309+
relativePath?: string
310+
): SourceNode;
311+
312+
add(chunk: Array<(string | SourceNode)> | SourceNode | string): SourceNode;
313+
314+
prepend(chunk: Array<(string | SourceNode)> | SourceNode | string): SourceNode;
315+
91316
setSourceContent(sourceFile: string, sourceContent: string): void;
317+
92318
walk(fn: (chunk: string, mapping: MappedPosition) => void): void;
319+
93320
walkSourceContents(fn: (file: string, content: string) => void): void;
321+
94322
join(sep: string): SourceNode;
323+
95324
replaceRight(pattern: string, replacement: string): SourceNode;
325+
96326
toString(): string;
327+
97328
toStringWithSourceMap(startOfSourceMap?: StartOfSourceMap): CodeWithSourceMap;
98329
}

0 commit comments

Comments
 (0)