diff --git a/src/__tests__/classMap.test.ts b/src/__tests__/classMap.test.ts index 42e0a5de..9ead322c 100644 --- a/src/__tests__/classMap.test.ts +++ b/src/__tests__/classMap.test.ts @@ -16,24 +16,11 @@ describe(makeClassMap, () => { expect(makeClassMap(functions)).toStrictEqual([ { type: "package", - name: "foo", - children: [ - { - type: "class", - name: "foo", - children: [ - { type: "function", name: "foo", static: true, location: "/test/app/src/foo.js:3" }, - ], - }, - ], - }, - { - type: "package", - name: "util", + name: "src", children: [ { type: "package", - name: "other", + name: "util", children: [ { type: "class", @@ -49,6 +36,13 @@ describe(makeClassMap, () => { }, ], }, + { + type: "class", + name: "foo", + children: [ + { type: "function", name: "foo", static: true, location: "/test/app/src/foo.js:3" }, + ], + }, { type: "class", name: "util", @@ -110,7 +104,7 @@ function f( id, params: [], static: static_, - klassOrPkg: klass, + klassOrFile: klass, location, }; } diff --git a/src/__tests__/registry.test.ts b/src/__tests__/registry.test.ts index 9cce8c28..ed02132e 100644 --- a/src/__tests__/registry.test.ts +++ b/src/__tests__/registry.test.ts @@ -21,7 +21,7 @@ describe(registry.createFunctionInfo, () => { params: [], location: { path: "test.js", lineno: 42 }, static: true, - klassOrPkg: "test", + klassOrFile: "test", }); }); }); @@ -56,7 +56,7 @@ describe(registry.createMethodInfo, () => { generator: false, params: [], id: "testMethod", - klassOrPkg: "TestClass", + klassOrFile: "TestClass", location: undefined, static: false, }); diff --git a/src/classMap.ts b/src/classMap.ts index 4dc49edf..ac0ecf3b 100644 --- a/src/classMap.ts +++ b/src/classMap.ts @@ -14,15 +14,14 @@ export function makeClassMap(funs: Iterable): AppMap.ClassMap { for (const fun of sortFunctions(funs)) { if (!fun.location) continue; const pkgs = fun.location.path.replace(/\..+$/, "").split(sep).reverse(); - assert(pkgs.length > 0); + if (pkgs.length > 1) pkgs.shift(); // remove the file name (e.g. "foo.js") let [tree, classes]: FNode = [root, {}]; - let pkg = ""; while (pkgs.length > 0) { - pkg = pkgs.pop()!; + const pkg = pkgs.pop()!; [tree, classes] = tree[pkg] ||= [{}, {}]; } - (classes[fun.klassOrPkg] ||= []).push(fun); + (classes[fun.klassOrFile] ||= []).push(fun); } let result = makeTree(root); diff --git a/src/event.ts b/src/event.ts index 713a8c1b..ab4bb22b 100644 --- a/src/event.ts +++ b/src/event.ts @@ -19,7 +19,7 @@ export function makeCallEvent( static: !thisArg, receiver: optParameter(thisArg), parameters: resolveParameters(args, fun), - defined_class: fun.klassOrPkg, + defined_class: fun.klassOrFile, ...fun.location, }); } diff --git a/src/hooks/__tests__/instrument.test.ts b/src/hooks/__tests__/instrument.test.ts index 5ed8d88e..23a894d2 100644 --- a/src/hooks/__tests__/instrument.test.ts +++ b/src/hooks/__tests__/instrument.test.ts @@ -46,7 +46,7 @@ describe(instrument.transform, () => { "path": "test.js", "lineno": 2 }, - "klassOrPkg": "test", + "klassOrFile": "test", "static": true }]; @@ -64,7 +64,7 @@ describe(instrument.transform, () => { generator: false, location: { path: "test.js", lineno: 2 }, static: true, - klassOrPkg: "test", + klassOrFile: "test", }); }); @@ -91,7 +91,7 @@ describe(instrument.transform, () => { "name": "value" }], "static": false, - "klassOrPkg": "TestClass", + "klassOrFile": "TestClass", "location": { "path": "test.js", "lineno": 3 @@ -110,7 +110,7 @@ describe(instrument.transform, () => { expect(instrument.transformedFunctionInfos[0]).toStrictEqual({ params: [{ type: "Identifier", name: "value" }], id: "foo", - klassOrPkg: "TestClass", + klassOrFile: "TestClass", static: false, async: false, generator: false, diff --git a/src/registry.ts b/src/registry.ts index 6f6635bc..778dee9a 100644 --- a/src/registry.ts +++ b/src/registry.ts @@ -14,7 +14,7 @@ export interface FunctionInfo { params: ESTree.Parameter[]; static: boolean; /** Class name, or the package name for a free function */ - klassOrPkg: string; + klassOrFile: string; location?: SourceLocation; } @@ -31,7 +31,7 @@ export function createFunctionInfo( id: id.name, params: params.map(stripLocation), location, - klassOrPkg: pkgOfPath(location.path), + klassOrFile: pkgOfPath(location.path), static: true, }; return info; @@ -52,7 +52,7 @@ export function createMethodInfo( id: key.name, params: params.map(stripLocation), static: method.static, - klassOrPkg: klass.id.name, + klassOrFile: klass.id.name, location, }; return info; diff --git a/test/__snapshots__/httpClient.test.ts.snap b/test/__snapshots__/httpClient.test.ts.snap index 33331a77..955284ed 100644 --- a/test/__snapshots__/httpClient.test.ts.snap +++ b/test/__snapshots__/httpClient.test.ts.snap @@ -83,7 +83,7 @@ exports[`mapping http client requests (ESM) 1`] = ` "type": "class", }, ], - "name": "esm", + "name": "httpClient", "type": "package", }, ], @@ -192,7 +192,7 @@ exports[`mapping http client requests 1`] = ` "type": "class", }, ], - "name": "index", + "name": "httpClient", "type": "package", }, ], @@ -360,7 +360,7 @@ exports[`mapping mocked http client requests 1`] = ` "type": "class", }, ], - "name": "index", + "name": "httpClient", "type": "package", }, ], diff --git a/test/__snapshots__/httpServer.test.ts.snap b/test/__snapshots__/httpServer.test.ts.snap index c48aa866..b7af8de9 100644 --- a/test/__snapshots__/httpServer.test.ts.snap +++ b/test/__snapshots__/httpServer.test.ts.snap @@ -19,7 +19,7 @@ exports[`mapping Express.js requests 1`] = ` "type": "class", }, ], - "name": "express", + "name": "httpServer", "type": "package", }, ], @@ -180,7 +180,7 @@ exports[`mapping Express.js requests 1`] = ` "type": "class", }, ], - "name": "express", + "name": "httpServer", "type": "package", }, ], @@ -351,7 +351,7 @@ exports[`mapping Express.js requests 1`] = ` "type": "class", }, ], - "name": "express", + "name": "httpServer", "type": "package", }, ], @@ -633,7 +633,7 @@ exports[`mapping Express.js requests with remote recording 1`] = ` "type": "class", }, ], - "name": "express", + "name": "httpServer", "type": "package", }, ], diff --git a/test/__snapshots__/mocha.test.ts.snap b/test/__snapshots__/mocha.test.ts.snap index 14c3a258..ab60ec18 100644 --- a/test/__snapshots__/mocha.test.ts.snap +++ b/test/__snapshots__/mocha.test.ts.snap @@ -19,7 +19,7 @@ exports[`mapping Mocha tests 1`] = ` "type": "class", }, ], - "name": "test", + "name": "mocha", "type": "package", }, ], @@ -92,7 +92,7 @@ exports[`mapping Mocha tests 1`] = ` "type": "class", }, ], - "name": "calc", + "name": "mocha", "type": "package", }, ], @@ -173,7 +173,7 @@ exports[`mapping Mocha tests 1`] = ` "type": "class", }, ], - "name": "calc", + "name": "mocha", "type": "package", }, ], diff --git a/test/__snapshots__/mysql.test.ts.snap b/test/__snapshots__/mysql.test.ts.snap index 6115a1b0..56a2961e 100644 --- a/test/__snapshots__/mysql.test.ts.snap +++ b/test/__snapshots__/mysql.test.ts.snap @@ -18,7 +18,7 @@ exports[`mapping MySQL tests 1`] = ` "type": "class", }, ], - "name": "index", + "name": "mysql", "type": "package", }, ], diff --git a/test/__snapshots__/next.test.ts.snap b/test/__snapshots__/next.test.ts.snap index 1be0d34d..c145a006 100644 --- a/test/__snapshots__/next.test.ts.snap +++ b/test/__snapshots__/next.test.ts.snap @@ -19,7 +19,7 @@ exports[`mapping a Next.js appmap 1`] = ` "type": "class", }, ], - "name": "route", + "name": "hello", "type": "package", }, ], diff --git a/test/__snapshots__/postgres.test.ts.snap b/test/__snapshots__/postgres.test.ts.snap index 33f18a8a..9dcf418f 100644 --- a/test/__snapshots__/postgres.test.ts.snap +++ b/test/__snapshots__/postgres.test.ts.snap @@ -18,7 +18,7 @@ exports[`mapping PostgreSQL tests 1`] = ` "type": "class", }, ], - "name": "index", + "name": "postgres", "type": "package", }, ], diff --git a/test/__snapshots__/simple.test.ts.snap b/test/__snapshots__/simple.test.ts.snap index fbf73052..53ea8e67 100644 --- a/test/__snapshots__/simple.test.ts.snap +++ b/test/__snapshots__/simple.test.ts.snap @@ -29,7 +29,7 @@ exports[`finish signal is handled 1`] = ` "type": "class", }, ], - "name": "server", + "name": "simple", "type": "package", }, ], @@ -93,7 +93,7 @@ exports[`forwarding signals to the child 1`] = ` "type": "class", }, ], - "name": "daemon", + "name": "simple", "type": "package", }, ], @@ -157,7 +157,7 @@ exports[`mapping a custom Error class with a message property 1`] = ` "type": "class", }, ], - "name": "inspect", + "name": "simple", "type": "package", }, ], @@ -244,7 +244,7 @@ exports[`mapping a simple script 1`] = ` "type": "class", }, ], - "name": "index", + "name": "simple", "type": "package", }, ], @@ -504,7 +504,7 @@ exports[`mapping an extensionless CommonJS file 1`] = ` "type": "class", }, ], - "name": "extensionless", + "name": "simple", "type": "package", }, ], @@ -558,7 +558,25 @@ exports[`mapping an mjs script 1`] = ` { "children": [ { - "location": "./index.mjs:1", + "children": [ + { + "location": "./subproject/util.mjs:1", + "name": "twice", + "static": true, + "type": "function", + }, + ], + "name": "util", + "type": "class", + }, + ], + "name": "subproject", + "type": "package", + }, + { + "children": [ + { + "location": "./index.mjs:3", "name": "helloWorld", "static": true, "type": "function", @@ -568,7 +586,7 @@ exports[`mapping an mjs script 1`] = ` "type": "class", }, ], - "name": "index", + "name": "simple", "type": "package", }, ], @@ -577,17 +595,45 @@ exports[`mapping an mjs script 1`] = ` "defined_class": "index", "event": "call", "id": 1, - "lineno": 1, + "lineno": 3, "method_id": "helloWorld", "parameters": [], "path": "./index.mjs", "static": true, "thread_id": 0, }, + { + "defined_class": "util", + "event": "call", + "id": 2, + "lineno": 1, + "method_id": "twice", + "parameters": [ + { + "class": "Number", + "name": "x", + "value": "21", + }, + ], + "path": "./subproject/util.mjs", + "static": true, + "thread_id": 0, + }, { "elapsed": 31.337, "event": "return", - "id": 2, + "id": 3, + "parent_id": 2, + "return_value": { + "class": "Number", + "value": "42", + }, + "thread_id": 0, + }, + { + "elapsed": 31.337, + "event": "return", + "id": 4, "parent_id": 1, "thread_id": 0, }, @@ -632,7 +678,7 @@ exports[`mapping generator functions 1`] = ` "type": "class", }, ], - "name": "generator", + "name": "simple", "type": "package", }, ], @@ -725,7 +771,7 @@ exports[`mapping js class methods and constructors containing super keyword 1`] "type": "class", }, ], - "name": "class", + "name": "simple", "type": "package", }, ], diff --git a/test/__snapshots__/sqlite.test.ts.snap b/test/__snapshots__/sqlite.test.ts.snap index 697e9785..3d9b2923 100644 --- a/test/__snapshots__/sqlite.test.ts.snap +++ b/test/__snapshots__/sqlite.test.ts.snap @@ -18,7 +18,7 @@ exports[`mapping Sqlite tests 1`] = ` "type": "class", }, ], - "name": "index", + "name": "sqlite", "type": "package", }, ], diff --git a/test/__snapshots__/typescript-esm.test.ts.snap b/test/__snapshots__/typescript-esm.test.ts.snap index 4eb0456c..b04354c1 100644 --- a/test/__snapshots__/typescript-esm.test.ts.snap +++ b/test/__snapshots__/typescript-esm.test.ts.snap @@ -18,7 +18,7 @@ exports[`esm-loader is loaded when required 1`] = ` "type": "class", }, ], - "name": "index", + "name": "typescript-esm", "type": "package", }, ], diff --git a/test/__snapshots__/typescript.test.ts.snap b/test/__snapshots__/typescript.test.ts.snap index 3a2ef62b..92e93376 100644 --- a/test/__snapshots__/typescript.test.ts.snap +++ b/test/__snapshots__/typescript.test.ts.snap @@ -18,7 +18,7 @@ exports[`mapping a simple script 1`] = ` "type": "class", }, ], - "name": "index", + "name": "typescript", "type": "package", }, ], @@ -92,7 +92,7 @@ exports[`mapping an mts script 1`] = ` "type": "class", }, ], - "name": "index", + "name": "typescript", "type": "package", }, ], diff --git a/test/simple/index.mjs b/test/simple/index.mjs index cde0e819..71ba2bda 100644 --- a/test/simple/index.mjs +++ b/test/simple/index.mjs @@ -1,5 +1,7 @@ +import { twice } from "./subproject/util.mjs"; + export function helloWorld() { - console.log("Hello World from mjs"); + console.log(`Hello World from mjs: ${twice(21)}`); } helloWorld(); diff --git a/test/simple/subproject/util.mjs b/test/simple/subproject/util.mjs new file mode 100644 index 00000000..f09ae343 --- /dev/null +++ b/test/simple/subproject/util.mjs @@ -0,0 +1,3 @@ +export function twice(x) { + return x * 2; +}