Skip to content

Commit fef98fa

Browse files
edisigcidustinbyrne
authored andcommitted
fix: Import-attributes support
Note: should be bumped to upstream once davidbonnet/astring#713 is merged. - Upgrades Meriah to v5 to support import-attributes. - Replaces assert keword of old import-assertions with "with" keyword of import-attributes for json before parsing code with Meriah because Meriah does not support import-assertions. - Replaces Astring library with the forked version modifed to support import-attributes. - Adds a test which passes with the fix and fails with "Unexpected token: 'identifier'" error message which is encountered while running CyberChef project tests with appmap-node.
1 parent 4ddd343 commit fef98fa

12 files changed

+102
-11
lines changed
58.4 KB
Binary file not shown.
-57.1 KB
Binary file not shown.
-1.04 MB
Binary file not shown.
1.05 MB
Binary file not shown.

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -87,10 +87,10 @@
8787
"dependencies": {
8888
"@types/stack-utils": "^2.0.3",
8989
"acorn-walk": "^8.2.0",
90-
"astring": "^1.8.6",
90+
"astring": "https://github.com/getappmap/astring.git#v1.8.6-import-attributes",
9191
"chalk": "<5",
9292
"json5": "^2.2.3",
93-
"meriyah": "^4.3.7",
93+
"meriyah": "^5.0.0",
9494
"source-map-js": "^1.0.2",
9595
"stack-utils": "^2.0.6",
9696
"yaml": "^2.3.4"

src/config.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,9 @@ export class Config {
143143
};
144144
}
145145

146+
// For easily toggling features/fixes for testing.
147+
readonly fixJsonImportAssertions = true;
148+
146149
migrate() {
147150
if (!this.migrationPending) return;
148151

src/transform.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import * as mocha from "./hooks/mocha";
1414
import * as next from "./hooks/next";
1515
import * as vitest from "./hooks/vitest";
1616
import { warn } from "./message";
17+
import config from "./config";
1718

1819
const debug = debuglog("appmap");
1920
const treeDebug = debuglog("appmap-tree");
@@ -67,6 +68,14 @@ export default function transform(code: string, url: URL, hooks = defaultHooks):
6768

6869
if (hooks.some((h) => h.shouldIgnore?.(url))) return code;
6970

71+
if (config().fixJsonImportAssertions) {
72+
// Meriyah does not support old import-assertions.
73+
// Replace import-assertions with the import-attributes syntax
74+
// for json imports.
75+
const regex = /assert\s*\{/g;
76+
code = code.replace(regex, "with {");
77+
}
78+
7079
try {
7180
const comments: ESTree.Comment[] = [];
7281
const tree = parse(code, {

test/__snapshots__/simple.test.ts.snap

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -764,6 +764,70 @@ exports[`mapping a script using async tracking timeout 3000 1`] = `
764764
}
765765
`;
766766

767+
exports[`mapping a script with import attributes/assertions 1`] = `
768+
{
769+
"classMap": [
770+
{
771+
"children": [
772+
{
773+
"children": [
774+
{
775+
"location": "importAttributes.mjs:3",
776+
"name": "helloWorld",
777+
"static": true,
778+
"type": "function",
779+
},
780+
],
781+
"name": "importAttributes",
782+
"type": "class",
783+
},
784+
],
785+
"name": "simple",
786+
"type": "package",
787+
},
788+
],
789+
"events": [
790+
{
791+
"defined_class": "importAttributes",
792+
"event": "call",
793+
"id": 1,
794+
"lineno": 3,
795+
"method_id": "helloWorld",
796+
"parameters": [],
797+
"path": "importAttributes.mjs",
798+
"static": true,
799+
"thread_id": 0,
800+
},
801+
{
802+
"elapsed": 31.337,
803+
"event": "return",
804+
"id": 2,
805+
"parent_id": 1,
806+
"thread_id": 0,
807+
},
808+
],
809+
"metadata": {
810+
"app": "simple",
811+
"client": {
812+
"name": "appmap-node",
813+
"url": "https://github.com/getappmap/appmap-node",
814+
"version": "test node-appmap version",
815+
},
816+
"language": {
817+
"engine": "Node.js",
818+
"name": "javascript",
819+
"version": "test node version",
820+
},
821+
"name": "test process recording",
822+
"recorder": {
823+
"name": "process",
824+
"type": "process",
825+
},
826+
},
827+
"version": "1.12",
828+
}
829+
`;
830+
767831
exports[`mapping a script with tangled async functions 1`] = `
768832
{
769833
"classMap": [

test/simple.test.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,11 @@ integrationTest("mapping an mjs script", () => {
2828
expect(readAppmap()).toMatchSnapshot();
2929
});
3030

31+
integrationTest("mapping a script with import attributes/assertions", () => {
32+
expect(runAppmapNode("importAttributes.mjs").status).toBe(0);
33+
expect(readAppmap()).toMatchSnapshot();
34+
});
35+
3136
integrationTest("mapping js class methods and constructors containing super keyword", () => {
3237
expect(runAppmapNode("class.js").status).toBe(0);
3338
expect(readAppmap()).toMatchSnapshot();

test/simple/importAttributes.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"content": "This is for the import attributes test"
3+
}

0 commit comments

Comments
 (0)