Skip to content

Commit

Permalink
Use our own SourceMapSegment tuple type
Browse files Browse the repository at this point in the history
  • Loading branch information
jridgewell committed Jul 6, 2019
1 parent 597ad00 commit 37ac3e9
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 14 deletions.
7 changes: 6 additions & 1 deletion src/rollup/types.d.ts
Expand Up @@ -38,9 +38,14 @@ export interface RollupLogProps {
url?: string;
}

export type SourceMapSegment =
| [number]
| [number, number, number, number]
| [number, number, number, number, number];

export interface ExistingDecodedSourceMap {
file?: string;
mappings: number[][][];
mappings: SourceMapSegment[][];
names: string[];
sourceRoot?: string;
sources: string[];
Expand Down
26 changes: 17 additions & 9 deletions src/utils/collapseSourcemaps.ts
@@ -1,8 +1,11 @@
import { DecodedSourceMap, SourceMap } from 'magic-string';
import { SourceMapLine, SourceMapMappings, SourceMapSegment } from 'sourcemap-codec';
import Chunk from '../Chunk';
import Module from '../Module';
import { DecodedSourceMapOrMissing } from '../rollup/types';
import {
DecodedSourceMapOrMissing,
ExistingDecodedSourceMap,
SourceMapSegment
} from '../rollup/types';
import { error } from './error';
import { basename, dirname, relative, resolve } from './path';

Expand Down Expand Up @@ -30,14 +33,17 @@ interface SourceMapSegmentObject {
}

class Link {
mappings: SourceMapMappings;
mappings: SourceMapSegment[][];
names: string[];
sources: (Source | Link)[];

constructor(map: { mappings: number[][][]; names: string[] }, sources: (Source | Link)[]) {
constructor(
map: { mappings: SourceMapSegment[][]; names: string[] },
sources: (Source | Link)[]
) {
this.sources = sources;
this.names = map.names;
this.mappings = map.mappings as SourceMapMappings;
this.mappings = map.mappings;
}

traceMappings() {
Expand All @@ -48,7 +54,7 @@ class Link {
const mappings = [];

for (const line of this.mappings) {
const tracedLine: SourceMapLine = [];
const tracedLine: SourceMapSegment[] = [];

for (const segment of line) {
if (segment.length == 1) continue;
Expand All @@ -58,7 +64,7 @@ class Link {
const traced = source.traceSegment(
segment[2],
segment[3],
this.names[segment[4] as number]
segment.length === 5 ? this.names[segment[4]] : ''
);

if (traced) {
Expand Down Expand Up @@ -125,7 +131,7 @@ class Link {
return source.traceSegment(
segment[2],
segment[3],
this.names[segment[4] as number] || name
segment.length === 5 ? this.names[segment[4]] : name
);
}
if (segment[0] > column) {
Expand Down Expand Up @@ -197,7 +203,9 @@ export default function collapseSourcemaps(
return source;
});

let source = new Link(map, moduleSources);
// DecodedSourceMap (from magic-string) uses a number[] instead of the more
// correct SourceMapSegment tuples. Cast it here to gain type safety.
let source = new Link(map as ExistingDecodedSourceMap, moduleSources);

source = bundleSourcemapChain.reduce(linkMap, source);

Expand Down
2 changes: 1 addition & 1 deletion src/utils/decodedSourcemap.ts
Expand Up @@ -18,7 +18,7 @@ export function decodedSourcemap(map: Input): ExistingDecodedSourceMap | null {
};
}

let mappings: number[][][];
let mappings;
if (typeof map.mappings === 'string') {
mappings = decode(map.mappings);
} else {
Expand Down
6 changes: 3 additions & 3 deletions src/utils/getOriginalLocation.ts
Expand Up @@ -11,17 +11,17 @@ export function getOriginalLocation(

while (filteredSourcemapChain.length > 0) {
const sourcemap = filteredSourcemapChain.pop()!;
const line: any = sourcemap.mappings[location.line - 1];
const line = sourcemap.mappings[location.line - 1];
let locationFound = false;

if (line !== undefined) {
for (const segment of line) {
if (segment[0] >= location.column) {
if (segment.length < 4) break;
if (segment.length === 1) break;
location = {
column: segment[3],
line: segment[2] + 1,
name: sourcemap.names[segment[4]],
name: segment.length === 5 ? sourcemap.names[segment[4]] : undefined,
source: sourcemap.sources[segment[1]]
};
locationFound = true;
Expand Down

0 comments on commit 37ac3e9

Please sign in to comment.