Skip to content

Commit

Permalink
Add ignoreList support
Browse files Browse the repository at this point in the history
  • Loading branch information
jridgewell committed Mar 1, 2024
1 parent d791f83 commit 72a4ee2
Show file tree
Hide file tree
Showing 10 changed files with 220 additions and 69 deletions.
91 changes: 44 additions & 47 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@
"typescript": "4.6.3"
},
"dependencies": {
"@jridgewell/gen-mapping": "^0.3.0",
"@jridgewell/trace-mapping": "^0.3.9"
"@jridgewell/gen-mapping": "^0.3.5",
"@jridgewell/trace-mapping": "^0.3.24"
}
}
10 changes: 6 additions & 4 deletions src/build-source-map-tree.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ function build(
importer: string,
importerDepth: number
): MapSourceType {
const { resolvedSources, sourcesContent } = map;
const { resolvedSources, sourcesContent, ignoreList } = map;

const depth = importerDepth + 1;
const children = resolvedSources.map((sourceFile: string | null, i: number): Sources => {
Expand All @@ -63,24 +63,26 @@ function build(
depth,
source: sourceFile || '',
content: undefined,
ignore: undefined,
};

// Use the provided loader callback to retrieve the file's sourcemap.
// TODO: We should eventually support async loading of sourcemap files.
const sourceMap = loader(ctx.source, ctx);

const { source, content } = ctx;
const { source, content, ignore } = ctx;

// If there is a sourcemap, then we need to recurse into it to load its source files.
if (sourceMap) return build(new TraceMap(sourceMap, source), loader, source, depth);

// Else, it's an an unmodified source file.
// Else, it's an unmodified source file.
// The contents of this unmodified source file can be overridden via the loader context,
// allowing it to be explicitly null or a string. If it remains undefined, we fall back to
// the importing sourcemap's `sourcesContent` field.
const sourceContent =
content !== undefined ? content : sourcesContent ? sourcesContent[i] : null;
return OriginalSource(source, sourceContent);
const ignored = ignore !== undefined ? ignore : ignoreList ? ignoreList.includes(i) : false;
return OriginalSource(source, sourceContent, ignored);
});

return MapSource(map, children);
Expand Down
42 changes: 30 additions & 12 deletions src/source-map-tree.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { GenMapping, maybeAddSegment, setSourceContent } from '@jridgewell/gen-mapping';
import { GenMapping, maybeAddSegment, setIgnore, setSourceContent } from '@jridgewell/gen-mapping';
import { traceSegment, decodedMappings } from '@jridgewell/trace-mapping';

import type { TraceMap } from '@jridgewell/trace-mapping';
Expand All @@ -9,55 +9,68 @@ export type SourceMapSegmentObject = {
name: string;
source: string;
content: string | null;
ignore: boolean;
};

export type OriginalSource = {
map: null;
sources: Sources[];
source: string;
content: string | null;
ignore: boolean;
};

export type MapSource = {
map: TraceMap;
sources: Sources[];
source: string;
content: null;
ignore: false;
};

export type Sources = OriginalSource | MapSource;

const SOURCELESS_MAPPING = /* #__PURE__ */ SegmentObject('', -1, -1, '', null);
const SOURCELESS_MAPPING = /* #__PURE__ */ SegmentObject('', -1, -1, '', null, false);
const EMPTY_SOURCES: Sources[] = [];

function SegmentObject(
source: string,
line: number,
column: number,
name: string,
content: string | null
content: string | null,
ignore: boolean
): SourceMapSegmentObject {
return { source, line, column, name, content };
return { source, line, column, name, content, ignore };
}

function Source(map: TraceMap, sources: Sources[], source: '', content: null): MapSource;
function Source(
map: TraceMap,
sources: Sources[],
source: '',
content: null,
ignore: false
): MapSource;
function Source(
map: null,
sources: Sources[],
source: string,
content: string | null
content: string | null,
ignore: boolean
): OriginalSource;
function Source(
map: TraceMap | null,
sources: Sources[],
source: string | '',
content: string | null
content: string | null,
ignore: boolean
): Sources {
return {
map,
sources,
source,
content,
ignore,
} as any;
}

Expand All @@ -66,15 +79,19 @@ function Source(
* (which may themselves be SourceMapTrees).
*/
export function MapSource(map: TraceMap, sources: Sources[]): MapSource {
return Source(map, sources, '', null);
return Source(map, sources, '', null, false);
}

/**
* A "leaf" node in the sourcemap tree, representing an original, unmodified source file. Recursive
* segment tracing ends at the `OriginalSource`.
*/
export function OriginalSource(source: string, content: string | null): OriginalSource {
return Source(null, EMPTY_SOURCES, source, content);
export function OriginalSource(
source: string,
content: string | null,
ignore: boolean
): OriginalSource {
return Source(null, EMPTY_SOURCES, source, content, ignore);
}

/**
Expand Down Expand Up @@ -113,10 +130,11 @@ export function traceMappings(tree: MapSource): GenMapping {
if (traced == null) continue;
}

const { column, line, name, content, source } = traced;
const { column, line, name, content, source, ignore } = traced;

maybeAddSegment(gen, i, genCol, source, line, column, name);
if (source && content != null) setSourceContent(gen, source, content);
if (ignore) setIgnore(gen, source, true);
}
}

Expand All @@ -134,7 +152,7 @@ export function originalPositionFor(
name: string
): SourceMapSegmentObject | null {
if (!source.map) {
return SegmentObject(source.source, line, column, name, source.content);
return SegmentObject(source.source, line, column, name, source.content, source.ignore);
}

const segment = traceSegment(source.map, line, column);
Expand Down
3 changes: 2 additions & 1 deletion src/source-map.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,15 @@ export default class SourceMap {
declare sources: (string | null)[];
declare sourcesContent?: (string | null)[];
declare version: 3;
declare ignoreList: number[] | undefined;

constructor(map: GenMapping, options: Options) {
const out = options.decodedMappings ? toDecodedMap(map) : toEncodedMap(map);
this.version = out.version; // SourceMap spec says this should be first.
this.file = out.file;
this.mappings = out.mappings as SourceMap['mappings'];
this.names = out.names as SourceMap['names'];

this.ignoreList = out.ignoreList as SourceMap['ignoreList'];
this.sourceRoot = out.sourceRoot;

this.sources = out.sources as SourceMap['sources'];
Expand Down
1 change: 1 addition & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ export type LoaderContext = {
readonly depth: number;
source: string;
content: string | null | undefined;
ignore: boolean | undefined;
};

export type SourceMapLoader = (
Expand Down
Loading

0 comments on commit 72a4ee2

Please sign in to comment.