Skip to content

fix: recognize CommonJS require calls in JS/TS import graph - #720

Open
szupzj18 wants to merge 1 commit into
peteromallet:mainfrom
szupzj18:chris/fix/js-commonjs-require-import-edges
Open

fix: recognize CommonJS require calls in JS/TS import graph#720
szupzj18 wants to merge 1 commit into
peteromallet:mainfrom
szupzj18:chris/fix/js-commonjs-require-import-edges

Conversation

@szupzj18

Copy link
Copy Markdown

Problem

JS_SPEC.import_query (and TYPESCRIPT_SPEC) only matches ESM import_statement, so a CommonJS require('./module') call produces no import edge. Files imported only via require are reported with zero importers and show up as false orphaned findings. This is the second half of #715; #696 fixes the relative/absolute path-space mismatch in the same graph builder.

Fix

Add a second pattern to the JS/TS import query that matches require() calls, using an #eq? predicate so other calls with string arguments are not treated as imports:

(call_expression
    function: (identifier) @require_fn
    arguments: (arguments (string (string_fragment) @path))
    (#eq? @require_fn "require")) @import

resolve_js_import already skips bare package names, so require('lodash') does not create an edge.

Test

test_js_dep_graph_includes_commonjs_require builds a graph for a file with a relative require, a package require, and a non-require call, and asserts only the relative edge is kept. Fails without the query change, passes with it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant