exclude globals from find_undeclared_variables

This commit is contained in:
Brendan 2019-05-07 12:00:06 -04:00 committed by David Lord
parent c5f1fd328b
commit f7b110cbd0
No known key found for this signature in database
GPG Key ID: 7A1C87E3F5BC42A8
3 changed files with 7 additions and 1 deletions

View File

@ -19,6 +19,8 @@ unreleased
- Allow ``{%+`` syntax (with NOP behavior) when
``lstrip_blocks == False`` (`#748`_)
- Added a ``default`` parameter for the ``map`` filter. (`#557`_)
- Exclude environment globals from
:func:`meta.find_undeclared_variables`. #931
.. _#557: https://github.com/pallets/jinja/issues/557
.. _#765: https://github.com/pallets/jinja/issues/765

View File

@ -29,7 +29,7 @@ class TrackingCodeGenerator(CodeGenerator):
"""Remember all undeclared identifiers."""
CodeGenerator.enter_frame(self, frame)
for _, (action, param) in iteritems(frame.symbols.loads):
if action == 'resolve':
if action == 'resolve' and param not in self.environment.globals:
self.undeclared_identifiers.add(param)

View File

@ -121,6 +121,10 @@ class TestMeta(object):
x = meta.find_undeclared_variables(ast)
assert x == set(['bar', 'seq', 'muh'])
ast = env.parse('{% for x in range(5) %}{{ x }}{% endfor %}{{ foo }}')
x = meta.find_undeclared_variables(ast)
assert x == set(['foo'])
def test_find_refererenced_templates(self, env):
ast = env.parse('{% extends "layout.html" %}{% include helper %}')
i = meta.find_referenced_templates(ast)