Skip to content

Commit

Permalink
fix: Import-attributes support
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
zermelo-wisen committed Aug 8, 2024
1 parent 4ddd343 commit fef3988
Show file tree
Hide file tree
Showing 12 changed files with 102 additions and 11 deletions.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -87,10 +87,10 @@
"dependencies": {
"@types/stack-utils": "^2.0.3",
"acorn-walk": "^8.2.0",
"astring": "^1.8.6",
"astring": "https://github.com/getappmap/astring.git#v1.8.6-import-attributes",
"chalk": "<5",
"json5": "^2.2.3",
"meriyah": "^4.3.7",
"meriyah": "^5.0.0",
"source-map-js": "^1.0.2",
"stack-utils": "^2.0.6",
"yaml": "^2.3.4"
Expand Down
3 changes: 3 additions & 0 deletions src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,9 @@ export class Config {
};
}

// For easily toggling features/fixes for testing.
readonly fixJsonImportAssertions = true;

migrate() {
if (!this.migrationPending) return;

Expand Down
9 changes: 9 additions & 0 deletions src/transform.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import * as mocha from "./hooks/mocha";
import * as next from "./hooks/next";
import * as vitest from "./hooks/vitest";
import { warn } from "./message";
import config from "./config";

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

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

if (config().fixJsonImportAssertions) {
// Meriyah does not support old import-assertions.
// Replace import-assertions with the import-attributes syntax
// for json imports.
const regex = /assert\s*\{/g;
code = code.replace(regex, "with {");
}

try {
const comments: ESTree.Comment[] = [];
const tree = parse(code, {
Expand Down
64 changes: 64 additions & 0 deletions test/__snapshots__/simple.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -764,6 +764,70 @@ exports[`mapping a script using async tracking timeout 3000 1`] = `
}
`;

exports[`mapping a script with import attributes/assertions 1`] = `
{
"classMap": [
{
"children": [
{
"children": [
{
"location": "importAttributes.mjs:3",
"name": "helloWorld",
"static": true,
"type": "function",
},
],
"name": "importAttributes",
"type": "class",
},
],
"name": "simple",
"type": "package",
},
],
"events": [
{
"defined_class": "importAttributes",
"event": "call",
"id": 1,
"lineno": 3,
"method_id": "helloWorld",
"parameters": [],
"path": "importAttributes.mjs",
"static": true,
"thread_id": 0,
},
{
"elapsed": 31.337,
"event": "return",
"id": 2,
"parent_id": 1,
"thread_id": 0,
},
],
"metadata": {
"app": "simple",
"client": {
"name": "appmap-node",
"url": "https://github.com/getappmap/appmap-node",
"version": "test node-appmap version",
},
"language": {
"engine": "Node.js",
"name": "javascript",
"version": "test node version",
},
"name": "test process recording",
"recorder": {
"name": "process",
"type": "process",
},
},
"version": "1.12",
}
`;

exports[`mapping a script with tangled async functions 1`] = `
{
"classMap": [
Expand Down
5 changes: 5 additions & 0 deletions test/simple.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,11 @@ integrationTest("mapping an mjs script", () => {
expect(readAppmap()).toMatchSnapshot();
});

integrationTest("mapping a script with import attributes/assertions", () => {
expect(runAppmapNode("importAttributes.mjs").status).toBe(0);
expect(readAppmap()).toMatchSnapshot();
});

integrationTest("mapping js class methods and constructors containing super keyword", () => {
expect(runAppmapNode("class.js").status).toBe(0);
expect(readAppmap()).toMatchSnapshot();
Expand Down
3 changes: 3 additions & 0 deletions test/simple/importAttributes.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"content": "This is for the import attributes test"
}
7 changes: 7 additions & 0 deletions test/simple/importAttributes.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import importedJson from "./importAttributes.json" assert { type: "json" };

export function helloWorld() {
console.log(importedJson);
}

helloWorld();
18 changes: 9 additions & 9 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -2926,7 +2926,7 @@ __metadata:
"@typescript-eslint/parser": ^6.4.0
"@typescript-eslint/utils": ^6.4.0
acorn-walk: ^8.2.0
astring: ^1.8.6
astring: "https://github.com/getappmap/astring.git#v1.8.6-import-attributes"
caller: ^1.1.0
chalk: <5
eslint: ^8.47.0
Expand All @@ -2935,7 +2935,7 @@ __metadata:
fast-glob: ^3.3.1
jest: ^29.6.2
json5: ^2.2.3
meriyah: ^4.3.7
meriyah: ^5.0.0
mongodb: ^6.3.0
next: ^14.0.4
prettier: ^3.0.2
Expand Down Expand Up @@ -3056,12 +3056,12 @@ __metadata:
languageName: node
linkType: hard

"astring@npm:^1.8.6":
"astring@https://github.com/getappmap/astring.git#v1.8.6-import-attributes":
version: 1.8.6
resolution: "astring@npm:1.8.6"
resolution: "astring@https://github.com/getappmap/astring.git#commit=b5fc0ce6f2bb1cdb62e754270dae3f7d511ed791"
bin:
astring: bin/astring
checksum: 6f034d2acef1dac8bb231e7cc26c573d3c14e1975ea6e04f20312b43d4f462f963209bc64187d25d477a182dc3c33277959a0156ab7a3617aa79b1eac4d88e1f
checksum: c1c976da8a0fde41267c8af9ebaaf3ecaf2487341f6f1091811c473527762d7ef514be934f386a839036d2a10d42b885f24e152a8eeb70786e7807b91d73ec85
languageName: node
linkType: hard

Expand Down Expand Up @@ -7095,10 +7095,10 @@ __metadata:
languageName: node
linkType: hard

"meriyah@npm:^4.3.7":
version: 4.3.7
resolution: "meriyah@npm:4.3.7"
checksum: 537fed1981bb4c2dc5fffa0c2d159fc169bff252e7a31c8b15ce3ddb9e91d8b66cacf5c932d7fc31bd522dd70d9b007e19131ac40172cd3bd27feee79c447041
"meriyah@npm:^5.0.0":
version: 5.0.0
resolution: "meriyah@npm:5.0.0"
checksum: bdd628ab956fca90d44334cc4538cdcde73bff5cf64c32aee3ddce808fe77591fcd6e6c141ec0fc5a6451776f55f6600cdcc53643b106a9e09974f169c697961
languageName: node
linkType: hard

Expand Down

0 comments on commit fef3988

Please sign in to comment.