fix(javascript): count all source line terminators
This commit is contained in:
@@ -386,7 +386,8 @@ export const sourceRangesEqual = (
|
||||
|
||||
const pointForOffset = (source: string, offset: number) => {
|
||||
const before = source.slice(0, offset);
|
||||
const lines = before.split("\n");
|
||||
// Match JavaScript line terminators, treating CRLF as a single newline.
|
||||
const lines = before.split(/\r\n|[\n\r\u2028\u2029]/u);
|
||||
return { line: lines.length, column: lines.at(-1)?.length ?? 0 };
|
||||
};
|
||||
|
||||
|
||||
@@ -0,0 +1,65 @@
|
||||
import { parse } from "@babel/parser";
|
||||
import { describe, expect, it } from "vitest";
|
||||
|
||||
import { analyzeJavaScriptStaticSource } from "./javascriptStaticAnalysis.js";
|
||||
|
||||
const directive = "//# sourceMappingURL=app.js.map";
|
||||
|
||||
describe("source map directive coordinates", () => {
|
||||
it.each([
|
||||
["LF", "\n"],
|
||||
["CRLF", "\r\n"],
|
||||
["CR", "\r"],
|
||||
["line separator", "\u2028"],
|
||||
["paragraph separator", "\u2029"],
|
||||
["same-line UTF-16 columns", " "],
|
||||
["vertical tab whitespace", "\v"],
|
||||
["form feed whitespace", "\f"],
|
||||
["mixed terminators", "\r\n\r\u2028\n\u2029"],
|
||||
])("agrees with Babel after %s", (_name, separator) => {
|
||||
const source = `const marker = "😀";${separator} ${directive}`;
|
||||
const location = parse(source).comments?.[0]?.loc;
|
||||
if (location === undefined)
|
||||
throw new Error("Expected the directive comment location");
|
||||
const { start, end } = location;
|
||||
|
||||
const result = analyzeJavaScriptStaticSource(source);
|
||||
expect(result.parse_status).toBe("complete");
|
||||
expect(result.source_map_urls).toEqual([
|
||||
{
|
||||
declared_url: "app.js.map",
|
||||
location: {
|
||||
start: { line: start.line, column: start.column },
|
||||
end: { line: end.line, column: end.column },
|
||||
},
|
||||
},
|
||||
]);
|
||||
});
|
||||
|
||||
it("counts terminators inside a block directive without including its closer", () => {
|
||||
const source =
|
||||
"const value = 1;\r/*#\u2028 sourceMappingURL=\r\n app.js.map */";
|
||||
expect(analyzeJavaScriptStaticSource(source).source_map_urls).toEqual([
|
||||
{
|
||||
declared_url: "app.js.map",
|
||||
location: {
|
||||
start: { line: 2, column: 0 },
|
||||
end: { line: 4, column: " app.js.map".length },
|
||||
},
|
||||
},
|
||||
]);
|
||||
});
|
||||
|
||||
it("preserves the first location when a URL is repeated across terminators", () => {
|
||||
const source = `const marker = "😀";\r ${directive}\u2028${directive}`;
|
||||
expect(analyzeJavaScriptStaticSource(source).source_map_urls).toEqual([
|
||||
{
|
||||
declared_url: "app.js.map",
|
||||
location: {
|
||||
start: { line: 2, column: 2 },
|
||||
end: { line: 2, column: directive.length + 2 },
|
||||
},
|
||||
},
|
||||
]);
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,44 @@
|
||||
import { createHash } from "node:crypto";
|
||||
import { writeFile } from "node:fs/promises";
|
||||
import { join } from "node:path";
|
||||
|
||||
import { expect, it } from "vitest";
|
||||
|
||||
import { reconstructJavaScriptArtifact } from "../../../src/application/JavaScriptArtifactReconstruction.js";
|
||||
import { createTestTempDirectory } from "../../fixtures/temporaryDirectory.js";
|
||||
|
||||
it.each(["\n", "\r\n", "\r", "\u2028", "\u2029"])(
|
||||
"preserves source-map graph evidence coordinates after %j",
|
||||
async (separator) => {
|
||||
const root = await createTestTempDirectory("rea-source-map-locations-");
|
||||
const directive = "//# sourceMappingURL=app.js.map";
|
||||
const source = `const marker = "😀";${separator} ${directive}`;
|
||||
await writeFile(join(root, "app.js"), source);
|
||||
const result = await reconstructJavaScriptArtifact({ input_path: root });
|
||||
expect(result.statistics.parse_failures).toBe(0);
|
||||
const edges = result.graph.edges.filter(
|
||||
({ relation, properties }) =>
|
||||
relation === "maps_to" && properties.declared_url === "app.js.map",
|
||||
);
|
||||
expect(edges).toHaveLength(1);
|
||||
expect(edges[0]?.evidence).toMatchObject({
|
||||
state: "inferred",
|
||||
artifact: {
|
||||
sha256: createHash("sha256").update(source).digest("hex"),
|
||||
},
|
||||
location: {
|
||||
available: true,
|
||||
value: {
|
||||
kind: "source-range",
|
||||
source: "app.js",
|
||||
start: { line: 2, column: 2 },
|
||||
end: { line: 2, column: directive.length + 2 },
|
||||
},
|
||||
},
|
||||
});
|
||||
expect(edges[0]?.properties).toMatchObject({
|
||||
declared_url: "app.js.map",
|
||||
resolved_path: null,
|
||||
});
|
||||
},
|
||||
);
|
||||
Reference in New Issue
Block a user