-
-
Notifications
You must be signed in to change notification settings - Fork 237
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: static variable analysis #770
Conversation
Pull Request Test Coverage Report for Build 12525259260Details
💛 - Coveralls |
With the latest commit, I've changed several of the built-in tags to fix their row and column numbers reported from I guess this is a good point to decide if having correct row and column is important enough to warrant these changes. Notice that when parsing |
I've provisionally implemented some convenience analysis methods on the Other ideas that I've yet to implement:
|
@harttle 👋 , I've not forgotten about this or your previous comments about keeping track of aliases. I'll get back to it soon. |
Can you think of better names for At the moment:
|
your current naming is OK for me. Sorry some comments are not submitted yesterday, causing confusion. |
I can help update the docs afterwards. Are we good to merge this PR now? |
There's still a couple of corner cases to be addressed when tracking aliased variables in deeply nested |
Merged and ddded an experimental notice in documents so we people know we're improving it. |
🎉 This PR is included in version 10.20.0 🎉 The release is available on: Your semantic-release bot 📦🚀 |
Statically analyze templates and report variable usage.
Usage
Retrieve the names of variables used in a template with
Liquid.variables(template)
. It returns an array of strings, one string for each distinct variable, without its properties.Output
Alternatively, use
Liquid.fullVariables(template)
to get a list of variables including their properties. Notice that variables from tag and filter arguments are included too.Output
Or use
Liquid.variableSegments(template)
to get an array of strings and numbers that make up each variable's path.Output
Global Variables
Notice, in the examples above, that
title
andemail
are included in the results. Often you'll want to exclude names that are in scope from{% assign %}
tags, and temporary variables like those introduced by a{% for %}
tag.To get names that are expected to be global, that is, provided by application developers rather than template authors, use the
globalVariables
,globalFullVariables
orglobalVariableSegments
methods (or their synchronous equivalents) of aLiquid
class instance.Output
Partial Templates
By default, LiquidJS will try to load and analyze any included and rendered templates too.
Output
You can disable analysis of partial templates by setting the
partials
options tofalse
.Output
If an
{% include %}
tag uses a dynamic template name (one that can't be determined without rendering the template) it will be ignored, even ifpartials
is set totrue
.