Skip to content

fix(javascript): parse explicit CommonJS wrapper syntax - #2475

Merged
morluto merged 1 commit into
morluto:mainfrom
rudycelekli:fix/commonjs-source-parser-mode
Oct 13, 2026
Merged

morluto merged 1 commit into
morluto:mainfrom
rudycelekli:fix/commonjs-source-parser-mode

Conversation

@rudycelekli

@rudycelekli rudycelekli commented Oct 13, 2026 •

Copy link
Copy Markdown

Valid .cjs top-level return and new.target were reported as syntax failures in artifact analysis and reference imports. The shared parser now admits CommonJS wrapper syntax while preserving inert reconstruction of module declarations stored in .cjs assets. Reference imports retain extension-based runtime diagnostics; artifact parsing infers declaration syntax and uses CommonJS diagnostics for recovered scripts, so module-only strict errors do not contaminate valid sloppy CommonJS source.

The first published implementation forced artifact .cjs sources into CommonJS mode and caused two existing directory/ASAR reconstruction tests to become partial. This same-PR correction preserves the existing static-source contract at the producing parser; both in-process analysis and compiled analysis workers consume it without downstream patches.

Validation with pinned Node 24.18.0 and locked dependencies:

  • Existing reconstruction suite: 22 passed on frozen base; 2 failed/20 passed on the prior PR head; both formerly failing cases now pass.
  • Expanded native CommonJS wrapper/reference regressions: 5 failed/14 passed on frozen base. Node independently accepts the wrapper/sloppy examples as CommonJS and rejects them as modules without executing their source.
  • Focused reconstruction plus original CommonJS/TypeScript controls: 41 passed.
  • Full related parser/static/semantic/reconstruction and reference-source suites: 1664 passed across 86 files.
  • Required npm run check and npm run verify:test-discovery passed.

A first local repair draft exposed Babel's sticky module diagnostics on valid sloppy CommonJS scripts. Two new native controls caught that draft; the final parser obtains CommonJS diagnostics when syntax inference chose a recovered script. The failed draft and initial missing-artifact run are preserved separately. A check attempt blocked by a concurrent artifact writer was excluded; its raw log was overwritten by the successful sequential retry, and that limitation is recorded.

GitHub CI failed on the previous published revision. After correction, the GitHub CI run passed at commit 405f82c5c6e36d38906cd5b536bcf63b8c6dcd2c; the latest check rollup is successful, including the formerly failing test shard. Independent automated review also passed the same 41 consumer/wrapper/reference cases and required check. Maintainer acceptance is not established.

AI assistance: OpenAI Codex generated the implementation, tests, analysis and this description. Verification was automated; independent AI review is tracked separately. No human review or understanding is claimed.

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex usage limits have been reached for code reviews. Please check with the admins of this repo to increase the limits by adding credits.
Repo admins can enable using credits for code reviews in their settings.

@CassieFox23

Copy link
Copy Markdown
Contributor

TL;DR: Valid CommonJS wrapper code in a .cjs file now parses clean. That holds for both import paths and matches Node 22, 24 and 26. All but one control stayed the same. That one is under Edges.

I tested head 405f82c against base bb54082. I used 32 fixture folders and Node 22, 24 and 26.

Results

Check Node 22 Node 24 Node 26
PR tests on head (2 files, 19 tests) 19 pass 19 pass 19 pass
Same tests on base 5 fail, 14 pass 5 fail, 14 pass 5 fail, 14 pass
Node takes the five snippets as CommonJS, rejects them as a module yes yes yes

The five snippets in a .cjs, base then head. "Parse failure" means REA lists one for import-reference-source. "Partial" means analyze-javascript-application records a parser error.

Code Reference path Artifact path
return; clean, clean partial, clean
void new.target; parse failure, clean partial, clean
var await = 1; return; clean, clean partial, clean
sloppy with block clean, clean partial, clean
with block plus new.target parse failure, clean partial, clean

Relationships were found on every valid .cjs case, base and head. Output for the 15 .js, .mjs and .mts wrapper cases did not change at all. Control output was byte-identical, except the one case below.

Edges (not blockers)

  • A .cjs with import plus return: the artifact path flagged it on base. Head reports it clean. Node rejects it in both modes.
  • A plain .js with the same snippets still shows errors. Node accepts them as CommonJS. The PR does not claim to change .js.
  • Node rejects top-level await in .cjs. The artifact path accepts it on base and head alike.

Not run

  • The two directory and ASAR reconstruction tests the PR body mentions.
  • The full suite, npm run check and a merge with current main.
  • Any real ASAR file. All fixtures are plain folders.

Thanks, rudycelekli, for the fix and for the clear notes on the first revision.

Full case table (32 cases)

How to read it:

  • Node: ok is exit 0, ERR is exit 1. The mode is cjs for .cjs and .js, and esm for .mjs and .mts.
  • Node 22, 24 and 26 agreed on every row.
  • ref: import-reference-source. FAIL(n) is n parse failures listed. rel is relationships found.
  • art: analyze-javascript-application. The first word is the file's parse status.
  • err is the parser error count from a direct probe.
  • imp is import relationships found.
case file Node ref base ref head art base art head
w-return-cjs main.cjs ok (cjs) ok, rel 1 ok, rel 1 partial, err 1, imp 1 ok, err 0, imp 1
w-return-js main.js ok (cjs) ok, rel 1 ok, rel 1 partial, err 1, imp 1 partial, err 1, imp 1
w-return-mjs main.mjs ERR (esm) ok, rel 0 ok, rel 0 partial, err 1, imp 1 partial, err 1, imp 1
w-return-mts main.mts ERR (esm) ok, rel 0 ok, rel 0 partial, err 1, imp 1 partial, err 1, imp 1
w-newtarget-cjs main.cjs ok (cjs) FAIL(1), rel 1 ok, rel 1 partial, err 1, imp 1 ok, err 0, imp 1
w-newtarget-js main.js ok (cjs) FAIL(1), rel 1 FAIL(1), rel 1 partial, err 1, imp 1 partial, err 1, imp 1
w-newtarget-mjs main.mjs ERR (esm) FAIL(1), rel 0 FAIL(1), rel 0 partial, err 1, imp 1 partial, err 1, imp 1
w-newtarget-mts main.mts ERR (esm) FAIL(1), rel 0 FAIL(1), rel 0 partial, err 1, imp 1 partial, err 1, imp 1
w-awaitid-cjs main.cjs ok (cjs) ok, rel 1 ok, rel 1 partial, err 2, imp 1 ok, err 0, imp 1
w-awaitid-js main.js ok (cjs) ok, rel 1 ok, rel 1 partial, err 2, imp 1 partial, err 2, imp 1
w-awaitid-mjs main.mjs ERR (esm) FAIL(1), rel 0 FAIL(1), rel 0 partial, err 2, imp 1 partial, err 2, imp 1
w-awaitid-mts main.mts ERR (esm) FAIL(1), rel 0 FAIL(1), rel 0 partial, err 2, imp 1 partial, err 2, imp 1
w-with-cjs main.cjs ok (cjs) ok, rel 1 ok, rel 1 partial, err 1, imp 1 ok, err 0, imp 1
w-with-js main.js ok (cjs) ok, rel 1 ok, rel 1 partial, err 1, imp 1 partial, err 1, imp 1
w-with-mjs main.mjs ERR (esm) FAIL(1), rel 0 FAIL(1), rel 0 partial, err 1, imp 1 partial, err 1, imp 1
w-with-mts main.mts ERR (esm) FAIL(1), rel 0 FAIL(1), rel 0 partial, err 1, imp 1 partial, err 1, imp 1
w-withnt-cjs main.cjs ok (cjs) FAIL(1), rel 1 ok, rel 1 partial, err 2, imp 1 ok, err 0, imp 1
w-withnt-js main.js ok (cjs) FAIL(1), rel 1 FAIL(1), rel 1 partial, err 2, imp 1 partial, err 2, imp 1
w-withnt-mjs main.mjs ERR (esm) FAIL(2), rel 0 FAIL(2), rel 0 partial, err 2, imp 1 partial, err 2, imp 1
w-withnt-mts main.mts ERR (esm) FAIL(2), rel 0 FAIL(2), rel 0 partial, err 2, imp 1 partial, err 2, imp 1
c-cjs-esm main.cjs ERR (cjs) FAIL(2), rel 1 FAIL(2), rel 1 ok, err 0, imp 1 ok, err 0, imp 1
c-cjs-invalid main.cjs ERR (cjs) FAIL(1), rel 0 FAIL(1), rel 0 FAILED, err null, imp - FAILED, err null, imp -
c-js-esm main.js ok (esm) ok, rel 1 ok, rel 1 ok, err 0, imp 1 ok, err 0, imp 1
c-mjs-esm main.mjs ok (esm) ok, rel 1 ok, rel 1 ok, err 0, imp 1 ok, err 0, imp 1
c-mts-esm main.mts ok (esm) ok, rel 1 ok, rel 1 ok, err 0, imp 1 ok, err 0, imp 1
c-cjs-plain main.cjs ok (cjs) ok, rel 1 ok, rel 1 ok, err 0, imp 1 ok, err 0, imp 1
c-js-plain main.js ok (cjs) ok, rel 1 ok, rel 1 ok, err 0, imp 1 ok, err 0, imp 1
c-cjs-await main.cjs ERR (cjs) FAIL(1), rel 1 FAIL(1), rel 1 ok, err 0, imp 1 ok, err 0, imp 1
c-js-await main.js ERR (cjs) ok, rel 1 ok, rel 1 ok, err 0, imp 1 ok, err 0, imp 1
c-mjs-await main.mjs ok (esm) ok, rel 1 ok, rel 1 ok, err 0, imp 1 ok, err 0, imp 1
c-cjs-esm-return main.cjs ERR (cjs) FAIL(1), rel 1 FAIL(1), rel 1 partial, err 1, imp 1 ok, err 0, imp 1
c-js-invalid main.js ERR (cjs) FAIL(1), rel 0 FAIL(1), rel 0 FAILED, err null, imp - FAILED, err null, imp -
Commands
  • Each snippet went to node --check on stdin, so no fixture source ever ran.
  • npm ci, npm run build, npm run test:prepare on both trees (Node 24), exit 0.
  • rea import-reference-source <dir> --format json and rea analyze-javascript-application <dir> --format json, 32 folders, both trees, Node 22, 24 and 26. Exit 0, and the summaries match across Node versions.
  • node --check --input-type=commonjs and --input-type=module, source on stdin, 32 cases, Node 22, 24 and 26.
  • npm run test:focused -- tests/boundary/process/javascriptSourceModes.test.ts tests/boundary/filesystem/referenceSourceTypeScriptExtensions.test.ts on head: exit 0 on all three Nodes. On base with the PR's two test files copied in: exit 1 on all three.
  • I set no environment variables.
Test setup
  • Box: Nobara 44 (Fedora 44 base), Linux 7.2.9
  • CPU: AMD Ryzen 9 5900X, 12 cores / 24 threads
  • RAM: 32 GB DDR4-3600
  • Node: 22.23.1, 24.18.0, 26.11.1
  • Head: 405f82c
  • Base: bb54082
  • Other: the box was shared with a second test agent. The head's own parent is f254f5a, nine commits behind that base. The PR's three files are the same in both.

🤖 Generated with Claude Code (Sonnet 5.5, verified by Opus 5.5) 🦊

@morluto

morluto commented Oct 13, 2026

Copy link
Copy Markdown
Owner

@greptileai review

@greptile-apps

greptile-apps Bot commented Oct 13, 2026 •

Copy link
Copy Markdown
Contributor

RetriggerConfidence Score: 5/5 Tier: plus

[Medium impact] The PR appears safe to merge; no actionable defect or rule violation was found.

Summary

The shared parser now accepts CommonJS wrapper syntax in .cjs files.

  • Reference imports read .cjs files as CommonJS.
  • Artifact parsing accepts wrapper syntax while still inferring module declarations.

No actionable issues were found. Tests were inspected but not run during this review.

Diagram

%%{init: {'theme': 'neutral'}}%%
flowchart TD
  A[Source and filename] --> B{Parser entry point}
  B -->|Reference import| C{Filename mode}
  C -->|.cjs| D[CommonJS parse]
  C -->|.mjs or .mts| E[Module parse]
  C -->|Other| F[Infer script or module]
  B -->|Artifact analysis: .cjs| G[Infer syntax with wrapper syntax allowed]
  G --> H{Script with errors?}
  H -->|Yes| D
  H -->|No| I[Keep inferred parse and declarations]
  B -->|Artifact analysis: other files| C
Loading

Reviews (1) · Last reviewed commit: "fix(javascript): parse explicit CommonJS..." · Reviewed by Greptile

@morluto
morluto merged commit 6a04f92 into morluto:main Oct 13, 2026
35 checks passed
@morluto morluto mentioned this pull request Oct 13, 2026
3 tasks done
@morluto morluto added bug Something isn't working area: native Native analysis and decompilation, including Ghidra, IDA, and Hopper analysis behavior labels Oct 14, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area: native Native analysis and decompilation, including Ghidra, IDA, and Hopper analysis behavior bug Something isn't working

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants