Skip to content

Commit

Permalink
fix: Import-attributes support
Browse files Browse the repository at this point in the history
- 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.
- After generating code from the AST using Astring, adds
  necessary import-attributes syntax becasue Astring does
  not generate it yet.
- 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 Jul 31, 2024
1 parent 6f9be83 commit 63d024a
Show file tree
Hide file tree
Showing 11 changed files with 109 additions and 7 deletions.
Binary file not shown.
Binary file not shown.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@
"astring": "^1.8.6",
"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
4 changes: 4 additions & 0 deletions src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,10 @@ export class Config {
};
}

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

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

Expand Down
12 changes: 11 additions & 1 deletion src/loader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,17 @@ export const load: NodeLoaderHooksAPI2["load"] = async function load(url, contex
const original = await defaultLoad(url, context, defaultLoad);

if (original.source) {
const xformed = transform(original.source.toString(), new URL(url));
let xformed = transform(original.source.toString(), new URL(url));

if (config.addJsonImportAttributes) {
// Add import attributes to json imports if they don't have it.
// Import-attributes is lost after generating the instrumented
// code from the AST with the Astring library.
const regex = /(import\s+\w+\s+from\s+['"][^'"]+\.json['"])\s*;/g;
xformed = xformed.replace(regex, (match, importPart) => {
return `${importPart} with {type: "json"};`;
});
}

return {
shortCircuit: false,
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 @@ -41,6 +42,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*\{\s*type\s*:\s*"json"\s*\}/g;
code = code.replace(regex, 'with {type: "json"}');
}

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 @@ -277,6 +277,70 @@ exports[`mapping a custom Error class with a message property 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 simple script 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 @@ -27,6 +27,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();
10 changes: 5 additions & 5 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -2774,7 +2774,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 @@ -6854,10 +6854,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 63d024a

Please sign in to comment.