Skip to content

Commit 8c732cd

Browse files
authored
Merge pull request #293 from AaronFriel/patch-1
This updates the typings to match Definitely Typed
2 parents d0d00a6 + 555291a commit 8c732cd

File tree

1 file changed

+270
-37
lines changed

1 file changed

+270
-37
lines changed

source-map.d.ts

Lines changed: 270 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,39 +1,61 @@
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
6+
export type SourceMapUrl = string;
7+
18
export interface StartOfSourceMap {
29
file?: string;
310
sourceRoot?: string;
11+
skipValidation?: boolean;
412
}
513

6-
export interface RawSourceMap extends StartOfSourceMap {
7-
version: string;
14+
export interface RawSourceMap {
15+
version: number;
816
sources: string[];
917
names: string[];
18+
sourceRoot?: string;
1019
sourcesContent?: string[];
1120
mappings: string;
21+
file: string;
1222
}
1323

14-
export interface Position {
15-
line: number;
16-
column: number;
24+
export interface RawIndexMap extends StartOfSourceMap {
25+
version: number;
26+
sections: RawSection[];
1727
}
1828

19-
export interface LineRange extends Position {
20-
lastColumn: number;
29+
export interface RawSection {
30+
offset: Position;
31+
map: RawSourceMap;
2132
}
2233

23-
export interface FindPosition extends Position {
24-
// SourceMapConsumer.GREATEST_LOWER_BOUND or SourceMapConsumer.LEAST_UPPER_BOUND
25-
bias?: number;
34+
export interface Position {
35+
line: number;
36+
column: number;
2637
}
2738

28-
export interface SourceFindPosition extends FindPosition {
29-
source: string;
39+
export interface NullablePosition {
40+
line: number | null;
41+
column: number | null;
42+
lastColumn: number | null;
3043
}
3144

32-
export interface MappedPosition extends Position {
45+
export interface MappedPosition {
3346
source: string;
47+
line: number;
48+
column: number;
3449
name?: string;
3550
}
3651

52+
export interface NullableMappedPosition {
53+
source: string | null;
54+
line: number | null;
55+
column: number | null;
56+
name: string | null;
57+
}
58+
3759
export interface MappingItem {
3860
source: string;
3961
generatedLine: number;
@@ -43,56 +65,267 @@ export interface MappingItem {
4365
name: string;
4466
}
4567

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

50-
static GREATEST_LOWER_BOUND: number;
51-
static LEAST_UPPER_BOUND: number;
75+
export interface CodeWithSourceMap {
76+
code: string;
77+
map: SourceMapGenerator;
78+
}
5279

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

63-
export interface Mapping {
64-
generated: Position;
65-
original: Position;
66-
source: string;
67-
name?: string;
183+
export interface SourceMapConsumerConstructor {
184+
prototype: SourceMapConsumer;
185+
186+
GENERATED_ORDER: number;
187+
ORIGINAL_ORDER: number;
188+
GREATEST_LOWER_BOUND: number;
189+
LEAST_UPPER_BOUND: number;
190+
191+
new (rawSourceMap: RawSourceMap, sourceMapUrl?: SourceMapUrl): BasicSourceMapConsumer;
192+
new (rawSourceMap: RawIndexMap, sourceMapUrl?: SourceMapUrl): IndexedSourceMapConsumer;
193+
new (rawSourceMap: RawSourceMap | RawIndexMap | string, sourceMapUrl?: SourceMapUrl): BasicSourceMapConsumer | IndexedSourceMapConsumer;
194+
195+
/**
196+
* Create a BasicSourceMapConsumer from a SourceMapGenerator.
197+
*
198+
* @param sourceMap
199+
* The source map that will be consumed.
200+
*/
201+
fromSourceMap(sourceMap: SourceMapGenerator, sourceMapUrl?: SourceMapUrl): BasicSourceMapConsumer;
68202
}
69203

204+
export const SourceMapConsumer: SourceMapConsumerConstructor;
205+
206+
export interface BasicSourceMapConsumer extends SourceMapConsumer {
207+
file: string;
208+
sourceRoot: string;
209+
sources: string[];
210+
sourcesContent: string[];
211+
}
212+
213+
export interface BasicSourceMapConsumerConstructor {
214+
prototype: BasicSourceMapConsumer;
215+
216+
new (rawSourceMap: RawSourceMap | string): BasicSourceMapConsumer;
217+
218+
/**
219+
* Create a BasicSourceMapConsumer from a SourceMapGenerator.
220+
*
221+
* @param sourceMap
222+
* The source map that will be consumed.
223+
*/
224+
fromSourceMap(sourceMap: SourceMapGenerator): BasicSourceMapConsumer;
225+
}
226+
227+
export const BasicSourceMapConsumer: BasicSourceMapConsumerConstructor;
228+
229+
export interface IndexedSourceMapConsumer extends SourceMapConsumer {
230+
sources: string[];
231+
}
232+
233+
export interface IndexedSourceMapConsumerConstructor {
234+
prototype: IndexedSourceMapConsumer;
235+
236+
new (rawSourceMap: RawIndexMap | string): IndexedSourceMapConsumer;
237+
}
238+
239+
export const IndexedSourceMapConsumer: IndexedSourceMapConsumerConstructor;
240+
70241
export class SourceMapGenerator {
71242
constructor(startOfSourceMap?: StartOfSourceMap);
243+
244+
/**
245+
* Creates a new SourceMapGenerator based on a SourceMapConsumer
246+
*
247+
* @param sourceMapConsumer The SourceMap.
248+
*/
72249
static fromSourceMap(sourceMapConsumer: SourceMapConsumer): SourceMapGenerator;
250+
251+
/**
252+
* Add a single mapping from original source line and column to the generated
253+
* source's line and column for this source map being created. The mapping
254+
* object should have the following properties:
255+
*
256+
* - generated: An object with the generated line and column positions.
257+
* - original: An object with the original line and column positions.
258+
* - source: The original source file (relative to the sourceRoot).
259+
* - name: An optional original token name for this mapping.
260+
*/
73261
addMapping(mapping: Mapping): void;
262+
263+
/**
264+
* Set the source content for a source file.
265+
*/
74266
setSourceContent(sourceFile: string, sourceContent: string): void;
267+
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+
*/
75284
applySourceMap(sourceMapConsumer: SourceMapConsumer, sourceFile?: string, sourceMapPath?: string): void;
285+
76286
toString(): string;
77-
}
78287

79-
export interface CodeWithSourceMap {
80-
code: string;
81-
map: SourceMapGenerator;
288+
toJSON(): RawSourceMap;
82289
}
83290

84291
export class SourceNode {
292+
children: SourceNode[];
293+
sourceContents: any;
294+
line: number;
295+
column: number;
296+
source: string;
297+
name: string;
298+
85299
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;
300+
constructor(
301+
line: number | null,
302+
column: number | null,
303+
source: string | null,
304+
chunks?: Array<(string | SourceNode)> | SourceNode | string,
305+
name?: string
306+
);
307+
308+
static fromStringWithSourceMap(
309+
code: string,
310+
sourceMapConsumer: SourceMapConsumer,
311+
relativePath?: string
312+
): SourceNode;
313+
314+
add(chunk: Array<(string | SourceNode)> | SourceNode | string): SourceNode;
315+
316+
prepend(chunk: Array<(string | SourceNode)> | SourceNode | string): SourceNode;
317+
91318
setSourceContent(sourceFile: string, sourceContent: string): void;
319+
92320
walk(fn: (chunk: string, mapping: MappedPosition) => void): void;
321+
93322
walkSourceContents(fn: (file: string, content: string) => void): void;
323+
94324
join(sep: string): SourceNode;
325+
95326
replaceRight(pattern: string, replacement: string): SourceNode;
327+
96328
toString(): string;
329+
97330
toStringWithSourceMap(startOfSourceMap?: StartOfSourceMap): CodeWithSourceMap;
98331
}

0 commit comments

Comments
 (0)