Skip to content

Commit 642a3d7

Browse files
alexander-akaitclaude
authored andcommitted
test: cover the finished-map path of the scopes encoder
The streaming writer reaches none of it, so a map handed in from elsewhere went untested: the early returns, a segment naming no source or one the map lacks, a source resumed after another, a name the map already carries, and a value too large for one base64 digit.
1 parent 30ebcaf commit 642a3d7

1 file changed

Lines changed: 90 additions & 1 deletion

File tree

‎test/scopes.js‎

Lines changed: 90 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,11 @@ const {
66
OriginalSource,
77
ReplaceSource,
88
} = require("../");
9-
const { collectSourceScopes, encodeScopes } = require("../lib/helpers/scopes");
9+
const {
10+
addScopesToSourceMap,
11+
collectSourceScopes,
12+
encodeScopes,
13+
} = require("../lib/helpers/scopes");
1014

1115
/** @typedef {import("../lib/Source").MapOptions} MapOptions */
1216
/** @typedef {import("../lib/Source").RawSourceMap} RawSourceMap */
@@ -157,4 +161,89 @@ describe("scopes", () => {
157161
);
158162
});
159163
});
164+
165+
describe("addScopesToSourceMap", () => {
166+
/**
167+
* @param {string} mappings mappings field
168+
* @param {string[]=} sources sources field
169+
* @returns {RawSourceMap} a map shaped like one another tool produced
170+
*/
171+
const mapOf_ = (mappings, sources = ["lib.js"]) => ({
172+
version: 3,
173+
file: "x",
174+
sources,
175+
names: [],
176+
mappings,
177+
});
178+
179+
it("does nothing when the map has no mappings", () => {
180+
const map = mapOf_("");
181+
addScopesToSourceMap(map, () => BINDINGS);
182+
expect(map.scopes).toBeUndefined();
183+
});
184+
185+
it("does nothing when no source is asked for bindings", () => {
186+
const map = mapOf_("AAAA");
187+
addScopesToSourceMap(map, () => undefined);
188+
expect(map.scopes).toBeUndefined();
189+
});
190+
191+
it("does nothing when a source reports an empty set", () => {
192+
const map = mapOf_("AAAA");
193+
addScopesToSourceMap(map, () => new Map());
194+
expect(map.scopes).toBeUndefined();
195+
});
196+
197+
it("names the bindings a finished map's source declares", () => {
198+
const map = mapOf_("AAAA,IAAI");
199+
addScopesToSourceMap(map, () => BINDINGS);
200+
expect(typeof map.scopes).toBe("string");
201+
expect(map.names).toContain("mutable");
202+
expect(map.names).toContain("ns.mutable");
203+
});
204+
205+
it("skips a segment that names no source", () => {
206+
// the second segment carries only a column delta, so it maps nowhere
207+
const map = mapOf_("AAAA,C");
208+
addScopesToSourceMap(map, () => BINDINGS);
209+
expect(typeof map.scopes).toBe("string");
210+
});
211+
212+
it("skips a segment whose source index the map does not have", () => {
213+
// the second segment steps sourceIndex to 1, past the single source
214+
const map = mapOf_("AAAA,ACAA");
215+
addScopesToSourceMap(map, () => BINDINGS);
216+
expect(typeof map.scopes).toBe("string");
217+
});
218+
219+
it("reaches the furthest original line a source explains", () => {
220+
// one source, interrupted and resumed, so its end has to be extended
221+
const map = mapOf_("AAAA;ACAA;ADEA", ["lib.js", "other.js"]);
222+
addScopesToSourceMap(map, (i) => (i === 0 ? BINDINGS : undefined));
223+
const scopes = collectSourceScopes(map.mappings, 2);
224+
expect(scopes[0].originalEnd.line).toBeGreaterThan(1);
225+
});
226+
227+
it("reuses a name the map already carries", () => {
228+
const map = mapOf_("AAAA");
229+
map.names = ["mutable", "mutable"];
230+
addScopesToSourceMap(map, () => new Map([["mutable", "ns.mutable"]]));
231+
expect(map.names.filter((n) => n === "mutable")).toHaveLength(2);
232+
expect(map.names).toContain("ns.mutable");
233+
});
234+
235+
it("encodes a value too large for one digit", () => {
236+
// forty lines puts the scope's end past what one base64 digit holds
237+
const map = mapOf_(Array.from({ length: 40 }, () => "AACA").join(";"));
238+
addScopesToSourceMap(map, () => BINDINGS);
239+
expect(typeof map.scopes).toBe("string");
240+
expect(/** @type {string} */ (map.scopes).length).toBeGreaterThan(10);
241+
});
242+
243+
it("orders the generated ranges it emits", () => {
244+
const map = mapOf_("AAAA,IAAI;AAAA,IAAI;AAAA,IAAI");
245+
addScopesToSourceMap(map, () => BINDINGS);
246+
expect(typeof map.scopes).toBe("string");
247+
});
248+
});
160249
});

0 commit comments

Comments
 (0)