Module: Liquid2::StaticAnalysis
- Defined in:
- lib/liquid2/static_analysis.rb
Overview
Template static analysis.
Defined Under Namespace
Classes: Result, Span, StaticScope, Variable, VariableMap
Class Method Summary collapse
- .analyze(template, include_partials:) ⇒ Object
- .analyze_variables(expression, template_name, scope, globals, variables) ⇒ Object
- .extract_filters(expression, template_name) ⇒ Object
- .segments(path, template_name) ⇒ Object
Class Method Details
.analyze(template, include_partials:) ⇒ Object
153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 |
# File 'lib/liquid2/static_analysis.rb', line 153 def self.analyze(template, include_partials:) variables = VariableMap.new globals = VariableMap.new locals = VariableMap.new # @type var filters: Hash[String, Array[Span]] filters = Hash.new { |hash, key| hash[key] = [] } # @type var tags: Hash[String, Array[Span]] = Hash.new { |hash, key| hash[key] = [] } # @type var template_scope: Set[String] template_scope = Set[] root_scope = StaticScope.new(template_scope) static_context = Liquid2::RenderContext.new(template) # Names of partial templates that have already been analyzed. # @type var seen: Set[String] seen = Set[] # @type var visit: ^(Node, String, StaticScope) -> void visit = lambda do |node, template_name, scope| seen.add(template_name) unless template_name.empty? # Update tags [node.name] << Span.new(template_name, node.token.last) if node.is_a?(Liquid2::Tag) # Update variables from node.expressions node.expressions.each do |expr| if expr.is_a?(Liquid2::Expression) analyze_variables(expr, template_name, scope, globals, variables) end # Update filters from expr extract_filters(expr, template_name).each do |name, span| filters[name] << span end end # Update template scope from node.template_scope node.template_scope.each do |ident| scope.add(ident.name) locals.add(Variable.new([ident.name], Span.new(template_name, ident.token.last))) end if (partial = node.partial_scope) partial_name = static_context.evaluate(partial.name).to_s unless seen.include?(partial_name) partial_scope = if partial.scope == :isolated StaticScope.new(Set.new(partial.in_scope.map(&:name))) else root_scope.push(Set.new(partial.in_scope.map(&:name))) end node.children(static_context, include_partials: include_partials).each do |child| seen.add(partial_name) visit.call(child, partial_name, partial_scope) if child.is_a?(Liquid2::Node) end partial_scope.pop end else scope.push(Set.new(node.block_scope.map(&:name))) node.children(static_context, include_partials: include_partials).each do |child| visit.call(child, template_name, scope) if child.is_a?(Liquid2::Node) end scope.pop end end template.ast.each do |node| visit.call(node, template.name, root_scope) if node.is_a?(Liquid2::Node) end Result.new(variables.data, globals.data, locals.data, filters, ) end |
.analyze_variables(expression, template_name, scope, globals, variables) ⇒ Object
257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 |
# File 'lib/liquid2/static_analysis.rb', line 257 def self.analyze_variables(expression, template_name, scope, globals, variables) if expression.is_a?(Path) var = Variable.new(segments(expression, template_name), Span.new(template_name, expression.token.last)) variables.add(var) root = var.segments.first.to_s globals.add(var) unless scope.include?(root) end if (child_scope = expression.scope) scope.push(Set.new(child_scope.map(&:name))) expression.children.each do |expr| if expr.is_a?(Expression) analyze_variables(expr, template_name, scope, globals, variables) end end scope.pop else expression.children.each do |expr| if expr.is_a?(Expression) analyze_variables(expr, template_name, scope, globals, variables) end end end end |
.extract_filters(expression, template_name) ⇒ Object
231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 |
# File 'lib/liquid2/static_analysis.rb', line 231 def self.extract_filters(expression, template_name) filters = [] # : Array[[String, Span]] if expression.is_a?(Liquid2::FilteredExpression) && !expression.filters.nil? expression.filters.each do |filter| filters << [filter.name, Span.new(template_name, filter.token.last)] end elsif expression.is_a?(Liquid2::TernaryExpression) expression.filters.each do |filter| filters << [filter.name, Span.new(template_name, filter.token.last)] end expression.tail_filters.each do |filter| filters << [filter.name, Span.new(template_name, filter.token.last)] end end if expression.is_a?(Liquid2::Expression) expression.children.each do |expr| filters.concat(extract_filters(expr, template_name)) end end filters end |
.segments(path, template_name) ⇒ Object
286 287 288 289 290 291 292 293 294 295 296 297 298 299 |
# File 'lib/liquid2/static_analysis.rb', line 286 def self.segments(path, template_name) # @type var segments_: Array[untyped] segments_ = [path.head.is_a?(Path) ? segments(path.head, template_name) : path.head] path.segments.each do |segment| segments_ << if segment.is_a?(Path) segments(segment, template_name) else segment end end segments_ end |