Repository navigation
fix: Unicode escapes and dynamic import attributes in instrumented code - #266
Open
dividedmind wants to merge 2 commits into
Open
dividedmind wants to merge 2 commits into
dividedmind wants to merge 2 commits into
Conversation
String literals using code point escapes above U+FFFF, such as
"\u{1F600}", came out of instrumentation as different characters
("}Ȁ"), silently changing program behaviour. meriyah 5.0.0 computes the
wrong value for such escapes, and since we regenerate code from the
AST with astring, the wrong value replaced the original literal. This
broke four of CyberChef's operation tests when run under appmap-node.
Upgrade meriyah to 7, which parses these escapes correctly. Its
stricter ESTree types now require optional on call and member
expressions and generator on arrow functions, narrow assignment
targets and operators, and make Literal a union which a plain
{ type, value } node only satisfies for strings, numbers and booleans,
so the AST builders are adjusted accordingly. No callers construct null,
bigint or regex literals.
meriyah 7 declares Node.js >= 20 in its engines field. It still parses
and round-trips fine on Node 18, which we continue to support.
Assisted-by: Claude:claude-opus-5-5
astring, which we use to regenerate instrumented code, ignores the
options argument of ImportExpression, so
import("./data.json", { with: { type: "json" } })
became a bare import("./data.json"). Node then rejects the JSON import
for missing its type attribute (ERR_IMPORT_ATTRIBUTE_MISSING), so code
which worked without appmap-node failed under it. Static imports were
not affected.
Override astring's ImportExpression handler in our extended generator
to emit the options, and also the source/defer phase
(import.source(...)), which astring drops the same way.
Assisted-by: Claude:claude-opus-5-5
There was a problem hiding this comment.
🟢 Approval recommended
No active review issues are reported for the code-regeneration fixes.
No open issues 🎉
What changed in this PR
Fixes code regeneration errors discovered while investigating CyberChef failures under appmap-node. This addresses incorrect output, not the slowdown tracked in #162.
Changes:
- Upgrades meriyah to preserve astral Unicode escapes.
- Preserves dynamic import options and phases.
- Updates AST construction for stricter types and adds regression tests.
| File | Description |
|---|---|
yarn.lock |
Locks meriyah to 7.4.4. |
src/jsxGenerate.ts |
Preserves dynamic import options and phases. |
src/hooks/next.ts |
Adds the required arrow-function generator flag. |
src/hooks/instrument.ts |
Updates arrow nodes and assignment-target typing. |
src/generate.ts |
Aligns AST helpers with stricter parser types. |
src/__tests__/transform.test.ts |
Tests Unicode escapes and dynamic imports. |
package.json |
Upgrades the meriyah dependency. |
Review effort: Balanced
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
While looking into #162 (CyberChef tests hanging under appmap-node), I found that four of CyberChef's operation tests also failed under appmap-node, independently of the hang. The cause was in how we regenerate instrumented code: string literals came out with the wrong characters. Fixing that turned up a second code generation bug, which drops the import attributes from dynamic imports. Each fix is in its own commit.
Astral Unicode escapes in string literals
String literals with code point escapes above U+FFFF were changed by instrumentation, e.g.
"\u{1F600}"became"}Ȁ". meriyah 5.0.0 parses these escapes to the wrong value, and since we generate the output from the AST, the wrong value replaced the original literal. Template literals were not affected.This upgrades meriyah to 7.4.4, which parses them correctly (6.x would also work). meriyah 7's ESTree types are stricter, so the AST nodes we build by hand needed some small changes:
optionalon calls and member expressions,generatoron arrow functions, narrower assignment target and operator types, andliteral()now only accepts the value types a plain{ type, value }node can represent.meriyah 7 declares
engines: node >=20, while we still claim Node 18. I checked that parsing and code generation work on Node 18.20.5, so Node 18 support continues on a best-effort basis rather than breaking outright.Import attributes on dynamic imports
astring ignores the options argument of
ImportExpression, soimport("./data.json", { with: { type: "json" } })was regenerated asimport("./data.json"). Node rejects a JSON import without its type attribute, so code that worked on its own crashed under appmap-node. Static imports were fine. This overrides astring'sImportExpressionhandler in our extended generator to write out the options, and also theimport.source()/import.defer()phase, which astring drops the same way.Testing
src/__tests__/transform.test.tsfor both bugs; each fails without its fix."\u{1F600}", and a dynamic JSON import with attributes on Node 22, both behave the same with and without appmap-node.This doesn't address the slowdown in #162 itself; that is a separate problem and is still open.
Fixes #172
Assisted-by: Claude:claude-opus-5-5