|
| 1 | +// Copyright 2021 Google Inc. Use of this source code is governed by an |
| 2 | +// MIT-style license that can be found in the LICENSE file or at |
| 3 | +// https://opensource.org/licenses/MIT. |
| 4 | + |
| 5 | +import 'package:meta/meta.dart'; |
| 6 | + |
| 7 | +import '../ast/sass.dart'; |
| 8 | +import '../util/nullable.dart'; |
| 9 | +import 'interface/statement.dart'; |
| 10 | +import 'recursive_statement.dart'; |
| 11 | + |
| 12 | +/// A [StatementVisitor] whose `visit*` methods default to returning `null`, but |
| 13 | +/// which returns the first non-`null` value returned by any method. |
| 14 | +/// |
| 15 | +/// This can be extended to find the first instance of particular nodes in the |
| 16 | +/// AST. |
| 17 | +/// |
| 18 | +/// This supports the same additional methods as [RecursiveStatementVisitor]. |
| 19 | +abstract class StatementSearchVisitor<T> implements StatementVisitor<T?> { |
| 20 | + const StatementSearchVisitor(); |
| 21 | + |
| 22 | + T? visitAtRootRule(AtRootRule node) => |
| 23 | + node.query.andThen(visitInterpolation) ?? visitChildren(node.children); |
| 24 | + |
| 25 | + T? visitAtRule(AtRule node) => |
| 26 | + visitInterpolation(node.name) ?? |
| 27 | + node.value.andThen(visitInterpolation) ?? |
| 28 | + node.children.andThen(visitChildren); |
| 29 | + |
| 30 | + T? visitContentBlock(ContentBlock node) => visitCallableDeclaration(node); |
| 31 | + |
| 32 | + T? visitContentRule(ContentRule node) => |
| 33 | + visitArgumentInvocation(node.arguments); |
| 34 | + |
| 35 | + T? visitDebugRule(DebugRule node) => visitExpression(node.expression); |
| 36 | + |
| 37 | + T? visitDeclaration(Declaration node) => |
| 38 | + visitInterpolation(node.name) ?? |
| 39 | + node.value.andThen(visitExpression) ?? |
| 40 | + node.children.andThen(visitChildren); |
| 41 | + |
| 42 | + T? visitEachRule(EachRule node) => |
| 43 | + visitExpression(node.list) ?? visitChildren(node.children); |
| 44 | + |
| 45 | + T? visitErrorRule(ErrorRule node) => visitExpression(node.expression); |
| 46 | + |
| 47 | + T? visitExtendRule(ExtendRule node) => visitInterpolation(node.selector); |
| 48 | + |
| 49 | + T? visitForRule(ForRule node) => |
| 50 | + visitExpression(node.from) ?? |
| 51 | + visitExpression(node.to) ?? |
| 52 | + visitChildren(node.children); |
| 53 | + |
| 54 | + T? visitForwardRule(ForwardRule node) => null; |
| 55 | + |
| 56 | + T? visitFunctionRule(FunctionRule node) => visitCallableDeclaration(node); |
| 57 | + |
| 58 | + T? visitIfRule(IfRule node) => |
| 59 | + node.clauses._search((clause) => |
| 60 | + visitExpression(clause.expression) ?? |
| 61 | + clause.children._search((child) => child.accept(this))) ?? |
| 62 | + node.lastClause.andThen((lastClause) => |
| 63 | + lastClause.children._search((child) => child.accept(this))); |
| 64 | + |
| 65 | + T? visitImportRule(ImportRule node) => node.imports._search((import) { |
| 66 | + if (import is StaticImport) { |
| 67 | + return visitInterpolation(import.url) ?? |
| 68 | + import.supports.andThen(visitSupportsCondition) ?? |
| 69 | + import.media.andThen(visitInterpolation); |
| 70 | + } |
| 71 | + }); |
| 72 | + |
| 73 | + T? visitIncludeRule(IncludeRule node) => |
| 74 | + visitArgumentInvocation(node.arguments) ?? |
| 75 | + node.content.andThen(visitContentBlock); |
| 76 | + |
| 77 | + T? visitLoudComment(LoudComment node) => visitInterpolation(node.text); |
| 78 | + |
| 79 | + T? visitMediaRule(MediaRule node) => |
| 80 | + visitInterpolation(node.query) ?? visitChildren(node.children); |
| 81 | + |
| 82 | + T? visitMixinRule(MixinRule node) => visitCallableDeclaration(node); |
| 83 | + |
| 84 | + T? visitReturnRule(ReturnRule node) => visitExpression(node.expression); |
| 85 | + |
| 86 | + T? visitSilentComment(SilentComment node) => null; |
| 87 | + |
| 88 | + T? visitStyleRule(StyleRule node) => |
| 89 | + visitInterpolation(node.selector) ?? visitChildren(node.children); |
| 90 | + |
| 91 | + T? visitStylesheet(Stylesheet node) => visitChildren(node.children); |
| 92 | + |
| 93 | + T? visitSupportsRule(SupportsRule node) => |
| 94 | + visitSupportsCondition(node.condition) ?? visitChildren(node.children); |
| 95 | + |
| 96 | + T? visitUseRule(UseRule node) => null; |
| 97 | + |
| 98 | + T? visitVariableDeclaration(VariableDeclaration node) => |
| 99 | + visitExpression(node.expression); |
| 100 | + |
| 101 | + T? visitWarnRule(WarnRule node) => visitExpression(node.expression); |
| 102 | + |
| 103 | + T? visitWhileRule(WhileRule node) => |
| 104 | + visitExpression(node.condition) ?? visitChildren(node.children); |
| 105 | + |
| 106 | + /// Visits each of [node]'s expressions and children. |
| 107 | + /// |
| 108 | + /// The default implementations of [visitFunctionRule] and [visitMixinRule] |
| 109 | + /// call this. |
| 110 | + @protected |
| 111 | + T? visitCallableDeclaration(CallableDeclaration node) => |
| 112 | + node.arguments.arguments._search( |
| 113 | + (argument) => argument.defaultValue.andThen(visitExpression)) ?? |
| 114 | + visitChildren(node.children); |
| 115 | + |
| 116 | + /// Visits each expression in an [invocation]. |
| 117 | + /// |
| 118 | + /// The default implementation of the visit methods calls this to visit any |
| 119 | + /// argument invocation in a statement. |
| 120 | + @protected |
| 121 | + T? visitArgumentInvocation(ArgumentInvocation invocation) => |
| 122 | + invocation.positional |
| 123 | + ._search((expression) => visitExpression(expression)) ?? |
| 124 | + invocation.named.values |
| 125 | + ._search((expression) => visitExpression(expression)) ?? |
| 126 | + invocation.rest.andThen(visitExpression) ?? |
| 127 | + invocation.keywordRest.andThen(visitExpression); |
| 128 | + |
| 129 | + /// Visits each expression in [condition]. |
| 130 | + /// |
| 131 | + /// The default implementation of the visit methods call this to visit any |
| 132 | + /// [SupportsCondition] they encounter. |
| 133 | + @protected |
| 134 | + T? visitSupportsCondition(SupportsCondition condition) { |
| 135 | + if (condition is SupportsOperation) { |
| 136 | + return visitSupportsCondition(condition.left) ?? |
| 137 | + visitSupportsCondition(condition.right); |
| 138 | + } else if (condition is SupportsNegation) { |
| 139 | + return visitSupportsCondition(condition.condition); |
| 140 | + } else if (condition is SupportsInterpolation) { |
| 141 | + return visitExpression(condition.expression); |
| 142 | + } else if (condition is SupportsDeclaration) { |
| 143 | + return visitExpression(condition.name) ?? |
| 144 | + visitExpression(condition.value); |
| 145 | + } else { |
| 146 | + return null; |
| 147 | + } |
| 148 | + } |
| 149 | + |
| 150 | + /// Visits each child in [children]. |
| 151 | + /// |
| 152 | + /// The default implementation of the visit methods for all [ParentStatement]s |
| 153 | + /// call this. |
| 154 | + @protected |
| 155 | + T? visitChildren(List<Statement> children) => |
| 156 | + children._search((child) => child.accept(this)); |
| 157 | + |
| 158 | + /// Visits each expression in an [interpolation]. |
| 159 | + /// |
| 160 | + /// The default implementation of the visit methods call this to visit any |
| 161 | + /// interpolation in a statement. |
| 162 | + @protected |
| 163 | + T? visitInterpolation(Interpolation interpolation) => interpolation.contents |
| 164 | + ._search((node) => node is Expression ? visitExpression(node) : null); |
| 165 | + |
| 166 | + /// Visits [expression]. |
| 167 | + /// |
| 168 | + /// The default implementation of the visit methods call this to visit any |
| 169 | + /// expression in a statement. |
| 170 | + @protected |
| 171 | + T? visitExpression(Expression expression) => null; |
| 172 | +} |
| 173 | + |
| 174 | +extension _IterableExtension<E> on Iterable<E> { |
| 175 | + /// Returns the first `T` returned by [callback] for an element of [iterable], |
| 176 | + /// or `null` if it returns `null` for every element. |
| 177 | + T? _search<T>(T? Function(E element) callback) { |
| 178 | + for (var element in this) { |
| 179 | + var value = callback(element); |
| 180 | + if (value != null) return value; |
| 181 | + } |
| 182 | + } |
| 183 | +} |
0 commit comments