improved scope rules

This commit is contained in:
danhanson committed 2026-09-08 19:00:19 -05:00
1 parent ca01dd5f95
commit aceae012fa
7 files changed
+15412 -15051

No files matched your search

+5 -1
View File
@@ -222,7 +222,11 @@ captured through its field instead (`(filter name: (identifier))`, `(custom_tag
`queries/libraries.scm` is not a query editors run themselves; it is data for a linter, and every tag or
filter added from a library belongs in it. `queries/locals.scm` relies on the `variable:` field to tell a
binding from a reference — another reason to keep `asVariable` uniform.
binding from a reference — another reason to keep `asVariable` uniform. `{% blocktranslate %}`'s `asvar`
target carries an `asvar:` field instead, because it is written inside the tag but assigned outside it:
the scope on `blocktranslate_group` confines the `with` and `count` bindings, and would swallow `asvar`
too if it were spelled the same way. Defining it in the surrounding scope is left to the tool, since a
locals query cannot let a definition out of the scope that contains it.
## Testing
+4 -1
View File
@@ -542,7 +542,10 @@ const django = grammar({
seq(part("with"), $._tag_kwargs),
part("context", $.filtered_value),
part("trimmed"),
part("asvar", field("variable", $.identifier)),
// asvar names where the rendered text is stored, which is read
// after the block rather than inside it, so it is not a binding of
// the block the way "with" and "count" are
part("asvar", field("asvar", $.identifier)),
),
),
_translate_count: ($) =>
+12
View File
@@ -3,5 +3,17 @@
variable: (identifier) @local.definition
; The tags whose Node pushes a context, so that what they bind is not visible
; after them. Verified by rendering: "{% block b %}{% now "Y" as n %}{% endblock %}{{ n }}"
; is empty, while the same around an "{% if %}" is not, because if does not push.
(with_clause) @local.scope
(for_clause) @local.scope
(block_clause) @local.scope
(partialdef_clause) @local.scope
; blocktranslate pushes a context for its "with" and "count" bindings. Its
; asvar target is not one of them: it is written in the same tag but assigned
; outside it, and carries an "asvar" field rather than "variable" so that this
; scope does not swallow it. A tool that wants it must define it in the scope
; around the block, which a locals query has no way to say.
(blocktranslate_group) @local.scope
+1 -1
View File
@@ -2061,7 +2061,7 @@
},
{
"type": "FIELD",
"name": "variable",
"name": "asvar",
"content": {
"type": "SYMBOL",
"name": "identifier"
+10
View File
@@ -330,6 +330,16 @@
"type": "blocktranslate_group",
"named": true,
"fields": {
"asvar": {
"multiple": true,
"required": false,
"types": [
{
"type": "identifier",
"named": true
}
]
},
"tag": {
"multiple": true,
"required": true,
+15377 -15045
View File
File diff suppressed because it is too large. Load diff
+3 -3
View File
@@ -165,7 +165,7 @@ Blocktrans With Asvar
(template
(content)
(blocktranslate_group
variable: (identifier)
asvar: (identifier)
(content))
(content))
@@ -214,7 +214,7 @@ Blocktrans With Every Option
(value
(literal
(string))))
variable: (identifier)
asvar: (identifier)
(content)
(content))
(content))
@@ -230,7 +230,7 @@ Blocktrans With Its Options Reordered
(template
(content)
(blocktranslate_group
variable: (identifier)
asvar: (identifier)
(content))
(content))