matchVariable matches simple identifiers, select expressions, and presence test expressions which match the (potentially) qualified variable name provided as input. Note, this function does not support inlining against select expressions which includes optional field selection. This may be a future
(varName string)
| 240 | // Note, this function does not support inlining against select expressions which includes optional |
| 241 | // field selection. This may be a future refinement. |
| 242 | func (opt *inliningOptimizer) matchVariable(varName string) ast.ExprMatcher { |
| 243 | return func(e ast.NavigableExpr) bool { |
| 244 | name, found := maybeAsVariableName(e) |
| 245 | if !found || name != varName { |
| 246 | return false |
| 247 | } |
| 248 | |
| 249 | // Determine whether the variable being referenced has been shadowed by a comprehension |
| 250 | p, hasParent := e.Parent() |
| 251 | for hasParent { |
| 252 | if p.Kind() != ast.ComprehensionKind { |
| 253 | p, hasParent = p.Parent() |
| 254 | continue |
| 255 | } |
| 256 | // If the inline variable name matches any of the comprehension variables at any scope, |
| 257 | // return false as the variable has been shadowed. |
| 258 | compre := p.AsComprehension() |
| 259 | if varName == compre.AccuVar() || varName == compre.IterVar() || varName == compre.IterVar2() { |
| 260 | return false |
| 261 | } |
| 262 | p, hasParent = p.Parent() |
| 263 | } |
| 264 | |
| 265 | return true |
| 266 | } |
| 267 | } |
| 268 | |
| 269 | func maybeAsVariableName(e ast.NavigableExpr) (string, bool) { |
| 270 | if e.Kind() == ast.IdentKind { |
no test coverage detected