Skip to content

feat(extract): add EJS (.ejs) template extractor - #4338

Closed
nothariharan wants to merge 1 commit into
Graphify-Labs:v8from
nothariharan:fix/3811-ejs-extractor
Closed

nothariharan wants to merge 1 commit into
Graphify-Labs:v8from
nothariharan:fix/3811-ejs-extractor

Conversation

@nothariharan

Copy link
Copy Markdown
Contributor

What does this PR do?

Fixes #3811 (same gap as #2511).

.ejs is in CODE_EXTENSIONS but had no AST extractor, so an Express/Node app's
server-rendered views were counted as code and contributed nothing to the graph
(the #1689 "no AST extractor" warning). They are not dumb HTML: they carry
<% %> scriptlets (including require/import), inline <script> blocks, and
<%- include('partials/_page-header') %> composition.

Adds extract_ejs. A .ejs file fed whole to the JS grammar is one top-level
ERROR that loses every symbol (the .svelte lesson of #3928), so the extractor
masks the template down to the JavaScript it can actually read — the bodies of
code tags (<% %> / <%_ %>) and of JS <script> blocks — and parses them with
the TS grammar, the same strategy .svelte/.vue/.astro use. Output
expressions (<%= %>/<%- %>), comments (<%# %>) and markup are blanked while
preserving line numbers; an output tag inside a <script> body is replaced by a
length-preserving 0 so the surrounding statement still parses.

Two regex passes then add what no JS grammar can see: dynamic import('./X')
calls written in a scriptlet, and the EJS <%- include('partial') %> composition
edge an Express view graph is built on, resolved to the real partial .ejs file.

.ejs is wired into _DISPATCH, the jsts language family, the JS
resolve/cache sets, and the hook source extensions.

Type of change

  • Bug fix
  • New feature

Verification & Invariants

How was this tested?

uv run pytest tests/test_ejs_extractor.py \
  tests/test_extract.py tests/test_languages.py tests/test_detect.py \
  tests/test_import_extension_resolution.py tests/test_svelte_extraction.py \
  tests/test_vue_extraction.py tests/test_astro_extraction.py \
  tests/test_astro_import_ids.py tests/test_blade_extractor.py \
  tests/test_cjs_module_extension.py tests/test_typescript_module_extensions.py \
  tests/test_package_json_subpath_imports.py -q
# 1219 passed, 25 skipped

uv run ruff check <changed files>   # All checks passed
uv run python -m tools.skillgen --check   # check OK

The one failing test on this base (test_unmapped_at_alias_resolution.py::test_3357...)
also fails on upstream/v8 without this change (host VCS-root interference); it is
unrelated.

Graphify-specific checklist

  • I confirmed that AST/structural extraction remains deterministic (no
    ambient state dependencies like ENV variables).
  • I reviewed changes for security implications (no unsafe interpolation).
  • I confirmed no API keys or local-only graph data are included.
  • I disclosed AI authorship in the commit message.

Limitations

  • A tag whose closing %> sits inside a JS string literal
    (<% const s = "%>"; %>) is a known limitation of the tag scanner.
  • Custom EJS delimiters are not supported.

EJS views sat in CODE_EXTENSIONS with no extractor, so an Express/Node app's templates were counted as code and contributed nothing (the Graphify-Labs#1689 warning). A .ejs file is HTML with <% %> scriptlets and inline <script> blocks, so feeding the whole file to the JS grammar made it one top-level ERROR.

Add extract_ejs: mask the template down to its JS regions (code-tag and <script> bodies), parse them with the TS grammar, and add the two relationships no JS grammar can see - dynamic import('./X') and the EJS <%- include('partial') %> composition edge, resolved to the real partial file. Wire .ejs into the dispatch, the jsts language family, the JS resolve/cache sets, and the hook source extensions.
@github-actions

Copy link
Copy Markdown

Thanks for the pull request, @nothariharan. A maintainer will review it soon.

Want to talk it through while it is in review? Come join us on our Discord server. For longer-form discussion there is also GitHub Discussions.

A couple of things that speed up review: make sure the test suite passes on Python 3.10 and 3.13, and that the change keeps extraction deterministic.

@github-actions github-actions Bot added the contributor Pull request from a returning community contributor label Oct 11, 2026

@graphify-labs graphify-labs Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Graphify reviewed this change.

Worth a look — the grounded gate found no coupling regressions or blocking issues, but 3 advisory finding(s) below merit a look before merge.

Not checked: tests were not run; no formal proof of the changed code.


Graphify review — findings

Adds an extract_ejs extractor that masks templates down to their scriptlet and inline <script> JavaScript, keeping line numbers intact and substituting a placeholder for output tags inside scripts. The masked code goes through the TS grammar, so .ejs views yield real imports, functions and classes instead of one top-level parse error. Regex passes add dynamic import() edges and <%- include('partial') %> includes edges, which resolve through the JS resolver (now aware of .ejs/index.ejs) and fall back to a stub node when the partial can't be found, so the edge is never dropped.

Worth a look

  • Global JS resolution now prefers .ejs over .js/index.js — graphify/extractors/resolution.py:35 · Escalate · medium
    • agreed by 2 of 2 members but NOT verified (no proof, no reproducing execution) — consensus is not a verdict; needs human review
  • Trailing -%> / _%> close modifiers are kept as JS — graphify/extract.py:2783 · Escalate · medium
    • agreed by 2 of 2 members but NOT verified (no proof, no reproducing execution) — consensus is not a verdict; needs human review
  • Multiline EJS output tag in script becomes multiple placeholder expressions — graphify/extract.py · Escalate · medium
    • agreed by 2 of 2 members but NOT verified (no proof, no reproducing execution) — consensus is not a verdict; needs human review
Analysis details — impact, health, verification

Impact & health

Graphify review

Impact — 3082 functions depend on the 548 functions this change touches.

Health — this change adds coupling hotspots:

  • worse: extract() — 854 callers, 51 callees
  • worse: _extract_generic() — 20 callers, 35 callees
  • new: extract_ejs() — 10 callers, 9 callees
  • worse: _normalize_ts_import_types() — 10 callers, 6 callees
  • worse: _load_tsconfig_base_url() — 7 callers, 3 callees

Verification — 3082 functions in the blast radius were not formally verified this run (proofs are advisory here).

Gate & verification

graphify gate

PASS — objectively clean (no health regressions, tests not run — proofs not run this pass (advisory)). Grounded, not self-assessed.

Advisory (not blocking):

  • verification_scope: 3005 function(s) in the blast radius were not formally verified this run

Test selection

Test selection

199 of 368 test file(s) selected (54%) via static blast radius.

  • tests/test_affected_cli.py — impact
  • tests/test_agents_platform.py — impact
  • tests/test_astro_extraction.py — impact
  • tests/test_astro_import_ids.py — impact
  • tests/test_blade_extractor.py — impact
  • tests/test_build.py — impact
  • tests/test_builtin_global_type_refs.py — impact
  • tests/test_cache.py — impact
  • tests/test_case_sensitive_resolution.py — impact
  • tests/test_cjs_module_extension.py — impact
  • tests/test_cloud_cta.py — impact
  • tests/test_cobol_extractor.py — impact
  • tests/test_codebuddy.py — impact
  • tests/test_cpp_method_declarations.py — impact
  • tests/test_cpp_nested_and_cli.py — impact
  • tests/test_cpp_objc_cross_file_calls.py — impact
  • tests/test_cross_extension_reexport_self_cycle.py — impact
  • tests/test_cross_language_call_resolution.py — impact
  • tests/test_cross_repo_external_call_guards.py — impact
  • tests/test_cross_repo_member_calls.py — impact
  • tests/test_csharp_call_site_generic_args.py — impact
  • tests/test_csharp_enum_members.py — impact
  • tests/test_csharp_field_generic_args.py — impact
  • tests/test_csharp_generic_callsites.py — impact
  • tests/test_csharp_interface_dispatch.py — impact
  • tests/test_csharp_member_calls.py — impact
  • tests/test_csharp_member_chain_calls.py — impact
  • tests/test_csharp_member_nodes.py — impact
  • tests/test_csharp_object_creation.py — impact
  • tests/test_csharp_partial_classes.py — impact
  • tests/test_csharp_tuple_type_refs.py — impact
  • tests/test_csharp_type_resolution.py — impact
  • tests/test_dedup_shrink_refuses_force_write.py — impact
  • tests/test_definition_file_portability.py — impact
  • tests/test_detect.py — impact
  • tests/test_devin.py — impact
  • tests/test_dotnet.py — impact
  • tests/test_duplicate_annotation_edges.py — impact
  • tests/test_ejs_extractor.py — impact, changed-test
  • tests/test_elixir_defdelegate.py — impact
  • tests/test_elixir_import_resolution.py — impact
  • tests/test_elixir_keyword_def_calls.py — impact
  • tests/test_elixir_qualified_calls.py — impact
  • tests/test_elixir_unqualified_call_scope.py — impact
  • tests/test_erlang_extractor.py — impact
  • tests/test_explain_cli.py — impact
  • tests/test_extract.py — impact
  • tests/test_extract_cache_location.py — impact
  • tests/test_extract_cli.py — impact
  • tests/test_extract_path_memo.py — impact
  • … and 149 more

Selection is safe under the controlled-regression assumption; always-run tests + a periodic full run are the backstops. Advisory — it never changes the check verdict.

· 1 grounded finding(s) anchored inline below; 4 more finding(s) on lines outside this diff (see the check run).

Comment thread graphify/extract.py
result.setdefault("edges", []).append(edge)


def extract_ejs(path: Path) -> dict:

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Health regression — extract_ejs()

fans out to 9 callees (efferent coupling); 10 callers depend on it (afferent coupling).

Grounded coupling-delta finding (deterministic), not an LLM guess.

@safishamsi

Copy link
Copy Markdown
Member

Shipped in v0.9.88, just released to PyPI as graphifyy 0.9.88. Landed via an authorship-preserving cherry-pick (your commit keeps your name and counts on your contributor graph), which is why GitHub marks this closed rather than merged. Thanks @nothariharan!

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

Labels

contributor Pull request from a returning community contributor

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug]: AST Extractor for EJS code files.

2 participants