scanOptTargets walks the AST once and reports whether the Optimize() decorator (needOpt) and the regex-constant compiler (needRegex) have any target node present. The conditions are a superset of what each decorator acts on, so a false negative — skipping a decorator that would have optimized someth
(root ast.Expr)
| 285 | // acts on, so a false negative — skipping a decorator that would have |
| 286 | // optimized something — is impossible. |
| 287 | func scanOptTargets(root ast.Expr) (needOpt, needRegex bool) { |
| 288 | ast.PostOrderVisit(root, ast.NewExprVisitor(func(e ast.Expr) { |
| 289 | switch e.Kind() { |
| 290 | case ast.ListKind, ast.MapKind: |
| 291 | needOpt = true // maybeBuildListLiteral / maybeBuildMapLiteral |
| 292 | case ast.CallKind: |
| 293 | switch fn := e.AsCall().FunctionName(); { |
| 294 | case fn == overloads.Matches: |
| 295 | needRegex = true |
| 296 | case fn == operators.In || fn == operators.OldIn: |
| 297 | needOpt = true // maybeOptimizeSetMembership |
| 298 | case overloads.IsTypeConversionFunction(fn): |
| 299 | needOpt = true // maybeOptimizeConstUnary |
| 300 | } |
| 301 | } |
| 302 | })) |
| 303 | return |
| 304 | } |
| 305 | |
| 306 | // newProgram creates a program instance with an environment, an ast, and an optional list of |
| 307 | // ProgramOption values. |
no test coverage detected