Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixes for dots in paths and relative paths #108

Merged
merged 3 commits into from
Feb 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,6 @@
"acorn-walk": "^8.2.0",
"astring": "^1.8.6",
"chalk": "<5",
"convert-source-map": "^2.0.0",
"meriyah": "^4.3.7",
"source-map-js": "^1.0.2",
"strip-json-comments": "^3",
Expand Down
128 changes: 61 additions & 67 deletions src/__tests__/classMap.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import assert from "node:assert";
import AppMap from "../AppMap";
import { makeClassMap } from "../classMap";
import config from "../config";
import { FunctionInfo, SourceLocation } from "../registry";

describe(makeClassMap, () => {
Expand All @@ -13,78 +15,70 @@ describe(makeClassMap, () => {
f("statik", "/test/app/src/util.js:14", "Util"),
];

expect(makeClassMap(functions)).toStrictEqual<AppMap.ClassMap>([
check(functions, {
src: {
util: {
c_other: ["otherFunction@/test/app/src/util/other.js:1"],
},
c_Util: ["m:method@/test/app/src/util.js:17", "statik@/test/app/src/util.js:14"],
c_util: ["freeFunction@/test/app/src/util.js:1", "otherFunction@/test/app/src/util.js:3"],
c_foo: ["foo@/test/app/src/foo.js:3"],
},
});
});

it("handles dots in paths correctly", () => {
check(
[
f("fun", "/test/app.map/src/util.js:42", "util"),
f("fun", "/test/app.map/src/other.js:42", "other"),
],
{
type: "package",
name: "src",
children: [
{
type: "package",
name: "util",
children: [
{
type: "class",
name: "other",
children: [
{
type: "function",
name: "otherFunction",
static: true,
location: "/test/app/src/util/other.js:1",
},
],
},
],
},
{
type: "class",
name: "foo",
children: [
{ type: "function", name: "foo", static: true, location: "/test/app/src/foo.js:3" },
],
},
{
type: "class",
name: "util",
children: [
{
type: "function",
name: "freeFunction",
static: true,
location: "/test/app/src/util.js:1",
},
{
type: "function",
name: "otherFunction",
static: true,
location: "/test/app/src/util.js:3",
},
],
},
{
type: "class",
name: "Util",
children: [
{
type: "function",
name: "statik",
static: true,
location: "/test/app/src/util.js:14",
},
{
type: "function",
name: "method",
static: false,
location: "/test/app/src/util.js:17",
},
],
},
],
src: {
c_util: ["fun@/test/app.map/src/util.js:42"],
c_other: ["fun@/test/app.map/src/other.js:42"],
},
},
);
});

it("uses the app name as the top level package if required", () => {
jest.replaceProperty(config, "appName", "testApp");
// if a path is just a file, the class would have been
// toplevel which is prohibited by the spec
check([f("fun", "util.js:42", "util"), f("other", "src/other.js:42", "other")], {
testApp: {
c_util: ["fun@util.js:42"],
src: {
c_other: ["other@src/other.js:42"],
},
},
]);
});
});
});

function check(funs: FunctionInfo[], expected: ClassMapTemplate) {
expect(concise(makeClassMap(funs))).toStrictEqual(expected);
}

type ClassMapTemplate = PkgTemplate | string[];
// eslint-disable-next-line @typescript-eslint/no-empty-interface
interface PkgTemplate extends Record<string, ClassMapTemplate> {}

function concise(map: (AppMap.Package | AppMap.Class | AppMap.FunctionInfo)[]): PkgTemplate {
// eslint-disable-next-line @typescript-eslint/no-explicit-any, @typescript-eslint/no-unsafe-member-access
const fname = (entry: any) => (entry.static ? "" : "m:") + entry.name + "@" + entry.location;

return Object.fromEntries(
map.map((entry) => {
assert(entry.type !== "function");
if (entry.type === "class")
return ["c_" + entry.name, expect.arrayContaining(entry.children!.map(fname))];
else return [entry.name, concise(entry.children!)];
}),
) as PkgTemplate;
}

function f(
id: string,
loc: string,
Expand Down
6 changes: 4 additions & 2 deletions src/classMap.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import assert from "node:assert";

import type AppMap from "./AppMap";
import config from "./config";
import type { FunctionInfo, SourceLocation } from "./registry";

type FNode = [FTree, Record<string, FunctionInfo[]>];
Expand All @@ -13,8 +14,9 @@ export function makeClassMap(funs: Iterable<FunctionInfo>): AppMap.ClassMap {
for (const fun of sortFunctions(funs)) {
if (!fun.location) continue;
// fun.location can contain "/" as separator even in Windows
const pkgs = fun.location.path.replace(/\..+$/, "").split(/[/\\]/).reverse();
if (pkgs.length > 1) pkgs.shift(); // remove the file name (e.g. "foo.js")
const pkgs = fun.location.path.split(/[/\\]/).reverse().slice(1);
// add app name as fallback top level package
pkgs.push(config.appName);

let [tree, classes]: FNode = [root, {}];
while (pkgs.length > 0) {
Expand Down
4 changes: 2 additions & 2 deletions src/hooks/__tests__/instrument.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ describe(instrument.transform, () => {
return arg + 1;
}
`,
{ loc: true, source: "test.js" },
{ loc: true, source: fixAbsPath("/test/test.js") },
);
expect(stripLocations(instrument.transform(program))).toStrictEqual(
parse(`
Expand Down Expand Up @@ -79,7 +79,7 @@ describe(instrument.transform, () => {
}
}
`,
{ loc: true, source: "test.js" },
{ loc: true, source: fixAbsPath("/test/test.js") },
);

expect(stripLocations(instrument.transform(program))).toStrictEqual(
Expand Down
17 changes: 14 additions & 3 deletions src/registry.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import { basename } from "node:path";
import { basename, relative } from "node:path";

import type { ESTree } from "meriyah";

import config from "./config";

export interface SourceLocation {
path: string;
lineno: number;
Expand Down Expand Up @@ -30,7 +32,7 @@ export function createFunctionInfo(
generator,
id: id.name,
params: params.map(stripLocation),
location,
location: relativeLocation(location),
klassOrFile: pkgOfPath(location.path),
static: true,
};
Expand All @@ -53,7 +55,7 @@ export function createMethodInfo(
params: params.map(stripLocation),
static: method.static,
klassOrFile: klass.id.name,
location,
location: relativeLocation(location),
};
return info;
}
Expand All @@ -69,3 +71,12 @@ function pkgOfPath(path: string): string {
if (base.includes(".")) return base.split(".").slice(0, -1).join(".");
else return base;
}

// return location relative to config.root
function relativeLocation(location: SourceLocation | undefined): SourceLocation | undefined {
if (!location) return undefined;
return {
path: relative(config.root, location.path),
lineno: location.lineno,
};
}
49 changes: 23 additions & 26 deletions src/transform.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import { debuglog } from "node:util";
import { isNativeError } from "node:util/types";

import { generate } from "astring";
import * as SourceMapConverter from "convert-source-map";
import { parse, type ESTree } from "meriyah";
import { RawSourceMap, SourceMapConsumer } from "source-map-js";

Expand Down Expand Up @@ -64,34 +63,32 @@ function dumpTree(xformed: ESTree.Program, url: URL) {
treeDebug("wrote transformed tree to %s", path);
}

function getSourceMap(url: URL, code: string): SourceMapConsumer | undefined {
if (
process.versions.node.split(".").map(Number) < [18, 19] &&
[".ts", ".mts", ".tsx"].some((e) => url.pathname.toLowerCase().endsWith(e))
) {
// HACK: In node 18.18, when using --loader node-ts/esm
// sourcemap gets inserted twice to the typescript file. We remove the second one
// which reflects transpiled files map, instead of the original typescript file map.
code = SourceMapConverter.removeComments(code);
if (code.indexOf("//# sourceMappingURL") > -1)
// Correct one remains, it needs to start in new line.
code.replace("//# sourceMappingURL", "\n//# sourceMappingURL");
function getSourceMap(fileUrl: URL, code: string): SourceMapConsumer | undefined {
const sourceMappingUrl = code.match(/\/\/# sourceMappingURL=(.*)/)?.[1];
if (!sourceMappingUrl) return;

const sourceMapUrl = new URL(sourceMappingUrl, fileUrl);

let sourceMap: RawSourceMap;

switch (sourceMapUrl.protocol) {
case "data:":
sourceMap = JSON.parse(parseDataUrl(sourceMapUrl)) as RawSourceMap;
break;
case "file:":
fileUrl = sourceMapUrl;
sourceMap = JSON.parse(readFileSync(fileURLToPath(sourceMapUrl), "utf8")) as RawSourceMap;
break;
default:
throw new Error(`Unsupported source map protocol: ${sourceMapUrl.protocol}`);
}

const readFile = (filename: string) => {
const fileUrl = new URL(filename, url);
switch (fileUrl.protocol) {
case "file:":
return readFileSync(fileUrl, "utf8");
case "data:":
return parseDataUrl(fileUrl);
default:
throw new Error(`unhandled protocol reading source map: ${fileUrl.protocol}`);
}
};
sourceMap.sources = sourceMap.sources.map((source) => {
const url = new URL((sourceMap.sourceRoot ?? "") + source, fileUrl);
return url.protocol === "file:" ? fileURLToPath(url) : url.toString();
});

const map = SourceMapConverter.fromMapFileSource(code, readFile);
if (map) return new SourceMapConsumer(map.sourcemap as RawSourceMap);
return new SourceMapConsumer(sourceMap);
}

function parseDataUrl(fileUrl: URL) {
Expand Down
Loading
Loading