feat(extract): add a Twig template extractor#2001
Open
nitrobear wants to merge 1 commit into
Open
Conversation
Twig carries the entire presentation layer of Symfony, Drupal, Craft CMS and
Grav projects, and none of it reached the graph: `.twig` was absent from
CODE_EXTENSIONS, so the files were never walked in the first place. On a
mid-sized Symfony app that is ~143 templates — the whole admin UI — invisible
to `query`/`explain`/`path`.
The extractor follows the existing regex-based template extractors
(blade.py, razor.py) rather than adding a grammar:
- inheritance and composition: `{% extends %}`, `{% include %}`, `{% embed %}`,
`{% import %}`, `{% from %}`, `{% use %}`, plus the `{{ include(...) }}`
function form
- `{% block name %}` definitions, scoped per file so that two templates
defining `content` do not collapse onto one node
- `path()` / `url()` calls as `references_route` edges
That last relation is the one that pays: a route name emitted by a template
resolves to the controller declaring it, so the presentation layer stops being
a disconnected island in the graph.
Two details worth flagging for review:
- References resolve against the nearest ancestor directory named `templates`,
which is the loader root convention in all four frameworks. When the target
file exists, the node reuses that file's id, so `{% extends "base.html.twig" %}`
becomes a real template-to-template edge instead of a dangling label.
Namespaced (`@AcmeBundle/...`), out-of-tree and dynamic names stay as
standalone label nodes.
- `include()` / `path()` / `url()` are only scanned inside `{{ ... }}` and
`{% ... %}` regions. Templates carry a lot of inline <script>, and an
unscoped scan happily matched a JS function literally named `path(`.
Covered by tests/test_twig_extraction.py (15 tests): dispatch, extension
registration, each relation, whitespace-control markers (`{%-`), per-file block
scoping, line numbers, the inline-script false positive above, unresolvable
references, and unreadable files.
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.
Twig carries the entire presentation layer of Symfony, Drupal, Craft CMS and Grav projects, and none of it reached the graph:
.twigwas absent from CODE_EXTENSIONS, so the files were never walked in the first place. On a mid-sized Symfony app that is ~143 templates — the whole admin UI — invisible toquery/explain/path.The extractor follows the existing regex-based template extractors (blade.py, razor.py) rather than adding a grammar:
{% extends %},{% include %},{% embed %},{% import %},{% from %},{% use %}, plus the{{ include(...) }}function form{% block name %}definitions, scoped per file so that two templates definingcontentdo not collapse onto one nodepath()/url()calls asreferences_routeedgesThat last relation is the one that pays: a route name emitted by a template resolves to the controller declaring it, so the presentation layer stops being a disconnected island in the graph.
Two details worth flagging for review:
templates, which is the loader root convention in all four frameworks. When the target file exists, the node reuses that file's id, so{% extends "base.html.twig" %}becomes a real template-to-template edge instead of a dangling label. Namespaced (@AcmeBundle/...), out-of-tree and dynamic names stay as standalone label nodes.include()/path()/url()are only scanned inside{{ ... }}and{% ... %}regions. Templates carry a lot of inline <script>, and an unscoped scan happily matched a JS function literally namedpath(.Covered by tests/test_twig_extraction.py (15 tests): dispatch, extension registration, each relation, whitespace-control markers (
{%-), per-file block scoping, line numbers, the inline-script false positive above, unresolvable references, and unreadable files.