Skip to content

Commit 55301c0

Browse files
authored
Fix newly enforced package:pedantic lints (flutter#19)
- always_declare_return_types - annotate_overrides - prefer_conditional_assignment - prefer_if_null_operators - prefer_single_quotes - use_function_type_syntax_for_parameters Drop unused author field from pubspec. Simplify dependency on `string_scanner` since older versions aren't supported on the Dart 2 SDK and so wouldn't be picked up even with a wide constraint.
1 parent 85d0be2 commit 55301c0

22 files changed

+423
-346
lines changed

example/example.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import 'package:boolean_selector/boolean_selector.dart';
22

33
void main(List<String> args) {
4-
var selector = BooleanSelector.parse("(x && y) || z");
4+
var selector = BooleanSelector.parse('(x && y) || z');
55
print(selector.evaluate((variable) => args.contains(variable)));
66
}

lib/boolean_selector.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,5 +54,5 @@ abstract class BooleanSelector {
5454
///
5555
/// The [isDefined] function should return `true` for any variables that are
5656
/// considered valid, and `false` for any invalid or undefined variables.
57-
void validate(bool isDefined(String variable));
57+
void validate(bool Function(String variable) isDefined);
5858
}

lib/src/all.dart

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,17 +8,23 @@ import '../boolean_selector.dart';
88
class All implements BooleanSelector {
99
// TODO(nweiz): Stop explicitly providing a type argument when sdk#32412 is
1010
// fixed.
11+
@override
1112
final variables = const <String>[];
1213

1314
const All();
1415

16+
@override
1517
bool evaluate(semantics) => true;
1618

19+
@override
1720
BooleanSelector intersection(BooleanSelector other) => other;
1821

22+
@override
1923
BooleanSelector union(BooleanSelector other) => this;
2024

21-
void validate(bool isDefined(String variable)) {}
25+
@override
26+
void validate(bool Function(String variable) isDefined) {}
2227

23-
String toString() => "<all>";
28+
@override
29+
String toString() => '<all>';
2430
}

lib/src/ast.dart

Lines changed: 46 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -21,52 +21,65 @@ abstract class Node {
2121
Iterable<String> get variables;
2222

2323
/// Calls the appropriate [Visitor] method on [this] and returns the result.
24-
accept(Visitor visitor);
24+
dynamic accept(Visitor visitor);
2525
}
2626

2727
/// A single variable.
2828
class VariableNode implements Node {
29+
@override
2930
final FileSpan span;
3031

3132
/// The variable name.
3233
final String name;
3334

35+
@override
3436
Iterable<String> get variables => [name];
3537

3638
VariableNode(this.name, [this.span]);
3739

38-
accept(Visitor visitor) => visitor.visitVariable(this);
40+
@override
41+
dynamic accept(Visitor visitor) => visitor.visitVariable(this);
3942

43+
@override
4044
String toString() => name;
4145

46+
@override
4247
bool operator ==(other) => other is VariableNode && name == other.name;
4348

49+
@override
4450
int get hashCode => name.hashCode;
4551
}
4652

4753
/// A negation expression.
4854
class NotNode implements Node {
55+
@override
4956
final FileSpan span;
5057

5158
/// The expression being negated.
5259
final Node child;
5360

61+
@override
5462
Iterable<String> get variables => child.variables;
5563

5664
NotNode(this.child, [this.span]);
5765

58-
accept(Visitor visitor) => visitor.visitNot(this);
66+
@override
67+
dynamic accept(Visitor visitor) => visitor.visitNot(this);
5968

69+
@override
6070
String toString() =>
61-
child is VariableNode || child is NotNode ? "!$child" : "!($child)";
71+
child is VariableNode || child is NotNode ? '!$child' : '!($child)';
6272

73+
@override
6374
bool operator ==(other) => other is NotNode && child == other.child;
6475

76+
@override
6577
int get hashCode => ~child.hashCode;
6678
}
6779

6880
/// An or expression.
6981
class OrNode implements Node {
82+
@override
7083
FileSpan get span => _expandSafe(left.span, right.span);
7184

7285
/// The left-hand branch of the expression.
@@ -75,31 +88,37 @@ class OrNode implements Node {
7588
/// The right-hand branch of the expression.
7689
final Node right;
7790

91+
@override
7892
Iterable<String> get variables sync* {
7993
yield* left.variables;
8094
yield* right.variables;
8195
}
8296

8397
OrNode(this.left, this.right);
8498

85-
accept(Visitor visitor) => visitor.visitOr(this);
99+
@override
100+
dynamic accept(Visitor visitor) => visitor.visitOr(this);
86101

102+
@override
87103
String toString() {
88-
var string1 = left is AndNode || left is ConditionalNode ? "($left)" : left;
104+
var string1 = left is AndNode || left is ConditionalNode ? '($left)' : left;
89105
var string2 =
90-
right is AndNode || right is ConditionalNode ? "($right)" : right;
106+
right is AndNode || right is ConditionalNode ? '($right)' : right;
91107

92-
return "$string1 || $string2";
108+
return '$string1 || $string2';
93109
}
94110

111+
@override
95112
bool operator ==(other) =>
96113
other is OrNode && left == other.left && right == other.right;
97114

115+
@override
98116
int get hashCode => left.hashCode ^ right.hashCode;
99117
}
100118

101119
/// An and expression.
102120
class AndNode implements Node {
121+
@override
103122
FileSpan get span => _expandSafe(left.span, right.span);
104123

105124
/// The left-hand branch of the expression.
@@ -108,31 +127,37 @@ class AndNode implements Node {
108127
/// The right-hand branch of the expression.
109128
final Node right;
110129

130+
@override
111131
Iterable<String> get variables sync* {
112132
yield* left.variables;
113133
yield* right.variables;
114134
}
115135

116136
AndNode(this.left, this.right);
117137

118-
accept(Visitor visitor) => visitor.visitAnd(this);
138+
@override
139+
dynamic accept(Visitor visitor) => visitor.visitAnd(this);
119140

141+
@override
120142
String toString() {
121-
var string1 = left is OrNode || left is ConditionalNode ? "($left)" : left;
143+
var string1 = left is OrNode || left is ConditionalNode ? '($left)' : left;
122144
var string2 =
123-
right is OrNode || right is ConditionalNode ? "($right)" : right;
145+
right is OrNode || right is ConditionalNode ? '($right)' : right;
124146

125-
return "$string1 && $string2";
147+
return '$string1 && $string2';
126148
}
127149

150+
@override
128151
bool operator ==(other) =>
129152
other is AndNode && left == other.left && right == other.right;
130153

154+
@override
131155
int get hashCode => left.hashCode ^ right.hashCode;
132156
}
133157

134158
/// A ternary conditional expression.
135159
class ConditionalNode implements Node {
160+
@override
136161
FileSpan get span => _expandSafe(condition.span, whenFalse.span);
137162

138163
/// The condition expression to check.
@@ -144,6 +169,7 @@ class ConditionalNode implements Node {
144169
/// The branch to run if the condition is false.
145170
final Node whenFalse;
146171

172+
@override
147173
Iterable<String> get variables sync* {
148174
yield* condition.variables;
149175
yield* whenTrue.variables;
@@ -152,21 +178,25 @@ class ConditionalNode implements Node {
152178

153179
ConditionalNode(this.condition, this.whenTrue, this.whenFalse);
154180

155-
accept(Visitor visitor) => visitor.visitConditional(this);
181+
@override
182+
dynamic accept(Visitor visitor) => visitor.visitConditional(this);
156183

184+
@override
157185
String toString() {
158186
var conditionString =
159-
condition is ConditionalNode ? "($condition)" : condition;
160-
var trueString = whenTrue is ConditionalNode ? "($whenTrue)" : whenTrue;
161-
return "$conditionString ? $trueString : $whenFalse";
187+
condition is ConditionalNode ? '($condition)' : condition;
188+
var trueString = whenTrue is ConditionalNode ? '($whenTrue)' : whenTrue;
189+
return '$conditionString ? $trueString : $whenFalse';
162190
}
163191

192+
@override
164193
bool operator ==(other) =>
165194
other is ConditionalNode &&
166195
condition == other.condition &&
167196
whenTrue == other.whenTrue &&
168197
whenFalse == other.whenFalse;
169198

199+
@override
170200
int get hashCode =>
171201
condition.hashCode ^ whenTrue.hashCode ^ whenFalse.hashCode;
172202
}

lib/src/evaluator.dart

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,16 +18,21 @@ class Evaluator implements Visitor<bool> {
1818
? semantics.toSet().contains
1919
: semantics as _Semantics;
2020

21+
@override
2122
bool visitVariable(VariableNode node) => _semantics(node.name);
2223

24+
@override
2325
bool visitNot(NotNode node) => !node.child.accept(this);
2426

27+
@override
2528
bool visitOr(OrNode node) =>
2629
node.left.accept(this) || node.right.accept(this);
2730

31+
@override
2832
bool visitAnd(AndNode node) =>
2933
node.left.accept(this) && node.right.accept(this);
3034

35+
@override
3136
bool visitConditional(ConditionalNode node) => node.condition.accept(this)
3237
? node.whenTrue.accept(this)
3338
: node.whenFalse.accept(this);

lib/src/impl.dart

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,10 +28,13 @@ class BooleanSelectorImpl implements BooleanSelector {
2828

2929
BooleanSelectorImpl._(this._selector);
3030

31+
@override
3132
Iterable<String> get variables => _selector.variables;
3233

34+
@override
3335
bool evaluate(semantics) => _selector.accept(Evaluator(semantics));
3436

37+
@override
3538
BooleanSelector intersection(BooleanSelector other) {
3639
if (other == BooleanSelector.all) return this;
3740
if (other == BooleanSelector.none) return other;
@@ -40,6 +43,7 @@ class BooleanSelectorImpl implements BooleanSelector {
4043
: IntersectionSelector(this, other);
4144
}
4245

46+
@override
4347
BooleanSelector union(BooleanSelector other) {
4448
if (other == BooleanSelector.all) return other;
4549
if (other == BooleanSelector.none) return this;
@@ -48,14 +52,18 @@ class BooleanSelectorImpl implements BooleanSelector {
4852
: UnionSelector(this, other);
4953
}
5054

51-
void validate(bool isDefined(String variable)) {
55+
@override
56+
void validate(bool Function(String variable) isDefined) {
5257
_selector.accept(Validator(isDefined));
5358
}
5459

60+
@override
5561
String toString() => _selector.toString();
5662

63+
@override
5764
bool operator ==(other) =>
5865
other is BooleanSelectorImpl && _selector == other._selector;
5966

67+
@override
6068
int get hashCode => _selector.hashCode;
6169
}

lib/src/intersection_selector.dart

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,32 +10,40 @@ class IntersectionSelector implements BooleanSelector {
1010
final BooleanSelector _selector1;
1111
final BooleanSelector _selector2;
1212

13+
@override
1314
Iterable<String> get variables sync* {
1415
yield* _selector1.variables;
1516
yield* _selector2.variables;
1617
}
1718

1819
IntersectionSelector(this._selector1, this._selector2);
1920

21+
@override
2022
bool evaluate(semantics) =>
2123
_selector1.evaluate(semantics) && _selector2.evaluate(semantics);
2224

25+
@override
2326
BooleanSelector intersection(BooleanSelector other) =>
2427
IntersectionSelector(this, other);
2528

29+
@override
2630
BooleanSelector union(BooleanSelector other) => UnionSelector(this, other);
2731

28-
void validate(bool isDefined(String variable)) {
32+
@override
33+
void validate(bool Function(String variable) isDefined) {
2934
_selector1.validate(isDefined);
3035
_selector2.validate(isDefined);
3136
}
3237

33-
String toString() => "($_selector1) && ($_selector2)";
38+
@override
39+
String toString() => '($_selector1) && ($_selector2)';
3440

41+
@override
3542
bool operator ==(other) =>
3643
other is IntersectionSelector &&
3744
_selector1 == other._selector1 &&
3845
_selector2 == other._selector2;
3946

47+
@override
4048
int get hashCode => _selector1.hashCode ^ _selector2.hashCode;
4149
}

lib/src/none.dart

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,17 +8,23 @@ import '../boolean_selector.dart';
88
class None implements BooleanSelector {
99
// TODO(nweiz): Stop explicitly providing a type argument when sdk#32412 is
1010
// fixed.
11+
@override
1112
final variables = const <String>[];
1213

1314
const None();
1415

16+
@override
1517
bool evaluate(semantics) => false;
1618

19+
@override
1720
BooleanSelector intersection(BooleanSelector other) => this;
1821

22+
@override
1923
BooleanSelector union(BooleanSelector other) => other;
2024

21-
void validate(bool isDefined(String variable)) {}
25+
@override
26+
void validate(bool Function(String) isDefined) {}
2227

23-
String toString() => "<none>";
28+
@override
29+
String toString() => '<none>';
2430
}

lib/src/parser.dart

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ class Parser {
2828

2929
if (_scanner.peek().type != TokenType.endOfFile) {
3030
throw SourceSpanFormatException(
31-
"Expected end of input.", _scanner.peek().span);
31+
'Expected end of input.', _scanner.peek().span);
3232
}
3333

3434
return selector;
@@ -97,7 +97,7 @@ class Parser {
9797
return VariableNode((token as IdentifierToken).name, token.span);
9898

9999
default:
100-
throw SourceSpanFormatException("Expected expression.", token.span);
100+
throw SourceSpanFormatException('Expected expression.', token.span);
101101
}
102102
}
103103
}

0 commit comments

Comments
 (0)