Skip to content

Commit 12ceaef

Browse files
authored
work around const lints (flutter#14416)
* work around const lints * update nonconst doc
1 parent 0882359 commit 12ceaef

15 files changed

+63
-54
lines changed

dev/tools/lib/archive_publisher.dart

+1-1
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,7 @@ class ArchivePublisher {
143143
jsonData['releases'][revision] = metadata;
144144
final Directory localTempDir = tempDir ?? Directory.systemTemp.createTempSync('flutter_');
145145
final File tempFile = new File(path.join(localTempDir.absolute.path, 'releases.json'));
146-
final JsonEncoder encoder = const JsonEncoder.withIndent(' ');
146+
const JsonEncoder encoder = const JsonEncoder.withIndent(' ');
147147
tempFile.writeAsStringSync(encoder.convert(jsonData));
148148
_cloudCopy(tempFile.absolute.path, metadataGsPath);
149149
if (tempDir == null) {

packages/flutter/test/material/tabs_test.dart

+1-2
Original file line numberDiff line numberDiff line change
@@ -1503,8 +1503,7 @@ void main() {
15031503
});
15041504

15051505
test('illegal constructor combinations', () {
1506-
final Widget $null = null;
1507-
expect(() => new Tab(icon: $null), throwsAssertionError);
1506+
expect(() => new Tab(icon: nonconst(null)), throwsAssertionError);
15081507
expect(() => new Tab(icon: new Container(), text: 'foo', child: new Container()), throwsAssertionError);
15091508
expect(() => new Tab(text: 'foo', child: new Container()), throwsAssertionError);
15101509
});

packages/flutter/test/painting/alignment_test.dart

+1-2
Original file line numberDiff line numberDiff line change
@@ -92,8 +92,7 @@ void main() {
9292
expect(const AlignmentDirectional(0.0, 0.0).resolve(TextDirection.rtl), const Alignment(0.0, 0.0));
9393
expect(const AlignmentDirectional(1.0, 1.0).resolve(TextDirection.ltr), const Alignment(1.0, 1.0));
9494
expect(const AlignmentDirectional(1.0, 1.0).resolve(TextDirection.rtl), const Alignment(-1.0, 1.0));
95-
final double $1 = 1.0; // we want these instances to be separate instances so that we're not just checking with a single object
96-
expect(new AlignmentDirectional($1, 2.0), new AlignmentDirectional($1, 2.0));
95+
expect(new AlignmentDirectional(nonconst(1.0), 2.0), new AlignmentDirectional(nonconst(1.0), 2.0));
9796
expect(const AlignmentDirectional(1.0, 2.0), isNot(const AlignmentDirectional(2.0, 1.0)));
9897
expect(const AlignmentDirectional(-1.0, 0.0).resolve(TextDirection.ltr),
9998
const AlignmentDirectional(1.0, 0.0).resolve(TextDirection.rtl));

packages/flutter/test/painting/border_rtl_test.dart

+5-6
Original file line numberDiff line numberDiff line change
@@ -317,11 +317,10 @@ void main() {
317317
});
318318

319319
test('BorderDirectional constructor', () {
320-
final Null $null = null;
321-
expect(() => new BorderDirectional(top: $null), throwsAssertionError);
322-
expect(() => new BorderDirectional(start: $null), throwsAssertionError);
323-
expect(() => new BorderDirectional(end: $null), throwsAssertionError);
324-
expect(() => new BorderDirectional(bottom: $null), throwsAssertionError);
320+
expect(() => new BorderDirectional(top: nonconst(null)), throwsAssertionError);
321+
expect(() => new BorderDirectional(start: nonconst(null)), throwsAssertionError);
322+
expect(() => new BorderDirectional(end: nonconst(null)), throwsAssertionError);
323+
expect(() => new BorderDirectional(bottom: nonconst(null)), throwsAssertionError);
325324
});
326325

327326
test('BorderDirectional.merge', () {
@@ -622,7 +621,7 @@ void main() {
622621
});
623622

624623
test('BorderDirectional hashCode', () {
625-
final BorderSide side = const BorderSide(width: 2.0);
624+
final BorderSide side = new BorderSide(width: nonconst(2.0));
626625
expect(new BorderDirectional(top: side).hashCode, new BorderDirectional(top: side).hashCode);
627626
expect(new BorderDirectional(top: side).hashCode, isNot(new BorderDirectional(bottom: side).hashCode));
628627
});

packages/flutter/test/painting/border_side_test.dart

+4-7
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,10 @@ void main() {
1515
style: BorderStyle.solid,
1616
),
1717
);
18-
// so that we can use `new` below, we use these:
19-
final Null $null = null;
20-
final double $minus1 = -1.0;
21-
expect(() => new BorderSide(color: $null), throwsAssertionError);
22-
expect(() => new BorderSide(width: $null), throwsAssertionError);
23-
expect(() => new BorderSide(style: $null), throwsAssertionError);
24-
expect(() => new BorderSide(width: $minus1), throwsAssertionError);
18+
expect(() => new BorderSide(color: nonconst(null)), throwsAssertionError);
19+
expect(() => new BorderSide(width: nonconst(null)), throwsAssertionError);
20+
expect(() => new BorderSide(style: nonconst(null)), throwsAssertionError);
21+
expect(() => new BorderSide(width: nonconst(-1.0)), throwsAssertionError);
2522
expect(
2623
const BorderSide(width: -0.0),
2724
const BorderSide(

packages/flutter/test/painting/border_test.dart

+4-5
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,10 @@ import 'package:flutter_test/flutter_test.dart';
77

88
void main() {
99
test('Border constructor', () {
10-
final Null $null = null;
11-
expect(() => new Border(left: $null), throwsAssertionError);
12-
expect(() => new Border(top: $null), throwsAssertionError);
13-
expect(() => new Border(right: $null), throwsAssertionError);
14-
expect(() => new Border(bottom: $null), throwsAssertionError);
10+
expect(() => new Border(left: nonconst(null)), throwsAssertionError);
11+
expect(() => new Border(top: nonconst(null)), throwsAssertionError);
12+
expect(() => new Border(right: nonconst(null)), throwsAssertionError);
13+
expect(() => new Border(bottom: nonconst(null)), throwsAssertionError);
1514
});
1615

1716
test('Border.merge', () {

packages/flutter/test/painting/colors_test.dart

+1-1
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ void main() {
4040
});
4141

4242
test('ColorSwatch test', () {
43-
final int color = 0xFF027223;
43+
final int color = nonconst(0xFF027223);
4444
final ColorSwatch<String> greens1 = new ColorSwatch<String>(
4545
color, const <String, Color>{
4646
'2259 C': const Color(0xFF027223),

packages/flutter/test/painting/edge_insets_test.dart

+1-1
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ void main() {
7373
});
7474

7575
test('EdgeInsets equality', () {
76-
final double $5 = 5.0; // we want these instances to be separate instances so that we're not just checking with a single object
76+
final double $5 = nonconst(5.0);
7777
expect(new EdgeInsetsDirectional.only(top: $5, bottom: 7.0), new EdgeInsetsDirectional.only(top: $5, bottom: 7.0));
7878
expect(new EdgeInsets.only(top: $5, bottom: 7.0), new EdgeInsetsDirectional.only(top: $5, bottom: 7.0));
7979
expect(new EdgeInsetsDirectional.only(top: $5, bottom: 7.0), new EdgeInsets.only(top: $5, bottom: 7.0));

packages/flutter/test/painting/image_provider_test.dart

+1-2
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,7 @@ import 'package:flutter_test/flutter_test.dart';
88
void main() {
99
test('NetworkImage non-null url test', () {
1010
expect(() {
11-
final String url = null; // we don't want this instance to be const because otherwise it would throw at compile time.
12-
new NetworkImage(url);
11+
new NetworkImage(nonconst(null));
1312
}, throwsAssertionError);
1413
});
1514
}

packages/flutter/test/painting/text_span_test.dart

+5-7
Original file line numberDiff line numberDiff line change
@@ -3,19 +3,17 @@
33
// found in the LICENSE file.
44

55
import 'package:flutter/painting.dart';
6-
6+
import 'package:flutter_test/flutter_test.dart' show nonconst;
77
import 'package:test/test.dart';
88

99
void main() {
1010
test('TextSpan equals', () {
11-
final String text = 'a'; // we want these instances to be separate instances so that we're not just checking with a single object
12-
final TextSpan a1 = new TextSpan(text: text);
13-
final TextSpan a2 = new TextSpan(text: text);
11+
final TextSpan a1 = new TextSpan(text: nonconst('a'));
12+
final TextSpan a2 = new TextSpan(text: nonconst('a'));
1413
final TextSpan b1 = new TextSpan(children: <TextSpan>[ a1 ]);
1514
final TextSpan b2 = new TextSpan(children: <TextSpan>[ a2 ]);
16-
final String nullText = null; // we want these instances to be separate instances so that we're not just checking with a single object
17-
final TextSpan c1 = new TextSpan(text: nullText);
18-
final TextSpan c2 = new TextSpan(text: nullText);
15+
final TextSpan c1 = new TextSpan(text: nonconst(null));
16+
final TextSpan c2 = new TextSpan(text: nonconst(null));
1917

2018
expect(a1 == a2, isTrue);
2119
expect(b1 == b2, isTrue);

packages/flutter/test/widgets/directionality_test.dart

+1-2
Original file line numberDiff line numberDiff line change
@@ -65,8 +65,7 @@ void main() {
6565

6666
testWidgets('Directionality can\'t be null', (WidgetTester tester) async {
6767
expect(() {
68-
final TextDirection textDirection = null; // we don't want this instance to be const because otherwise it would throw at compile time.
69-
new Directionality(textDirection: textDirection, child: const Placeholder());
68+
new Directionality(textDirection: nonconst(null), child: const Placeholder());
7069
}, throwsAssertionError);
7170
});
7271
}

packages/flutter/test/widgets/key_test.dart

+11-14
Original file line numberDiff line numberDiff line change
@@ -19,21 +19,18 @@ class NotEquals {
1919

2020
void main() {
2121
testWidgets('Keys', (WidgetTester tester) async {
22-
final int int3 = 3; // we want these instances to be separate instances so that we're not just checking with a single object
23-
expect(new ValueKey<int>(int3) == new ValueKey<int>(int3), isTrue);
24-
expect(new ValueKey<num>(int3) == new ValueKey<int>(int3), isFalse);
25-
final int int2 = 2; // we want these instances to be separate instances so that we're not just checking with a single object
26-
expect(new ValueKey<int>(int3) == new ValueKey<int>(int2), isFalse);
22+
expect(new ValueKey<int>(nonconst(3)) == new ValueKey<int>(nonconst(3)), isTrue);
23+
expect(new ValueKey<num>(nonconst(3)) == new ValueKey<int>(nonconst(3)), isFalse);
24+
expect(new ValueKey<int>(nonconst(3)) == new ValueKey<int>(nonconst(2)), isFalse);
2725
expect(const ValueKey<double>(double.NAN) == const ValueKey<double>(double.NAN), isFalse);
2826

29-
final String empty = ''; // we want these instances to be separate instances so that we're not just checking with a single object
30-
expect(new Key(empty) == new ValueKey<String>(empty), isTrue);
31-
expect(new ValueKey<String>(empty) == new ValueKey<String>(empty), isTrue);
32-
expect(new TestValueKey<String>(empty) == new ValueKey<String>(empty), isFalse);
33-
expect(new TestValueKey<String>(empty) == new TestValueKey<String>(empty), isTrue);
27+
expect(new Key(nonconst('')) == new ValueKey<String>(nonconst('')), isTrue);
28+
expect(new ValueKey<String>(nonconst('')) == new ValueKey<String>(nonconst('')), isTrue);
29+
expect(new TestValueKey<String>(nonconst('')) == new ValueKey<String>(nonconst('')), isFalse);
30+
expect(new TestValueKey<String>(nonconst('')) == new TestValueKey<String>(nonconst('')), isTrue);
3431

35-
expect(new ValueKey<String>(empty) == new ValueKey<dynamic>(empty), isFalse);
36-
expect(new TestValueKey<String>(empty) == new TestValueKey<dynamic>(empty), isFalse);
32+
expect(new ValueKey<String>(nonconst('')) == new ValueKey<dynamic>(nonconst('')), isFalse);
33+
expect(new TestValueKey<String>(nonconst('')) == new TestValueKey<dynamic>(nonconst('')), isFalse);
3734

3835
expect(new UniqueKey() == new UniqueKey(), isFalse);
3936
final LocalKey k = new UniqueKey();
@@ -44,11 +41,11 @@ void main() {
4441
expect(new ValueKey<LocalKey>(k) == new ValueKey<UniqueKey>(k), isFalse);
4542
expect(new ObjectKey(k) == new ObjectKey(k), isTrue);
4643

47-
final NotEquals constNotEquals = const NotEquals(); // we want these instances to be separate instances so that we're not just checking with a single object
44+
final NotEquals constNotEquals = nonconst(const NotEquals());
4845
expect(new ValueKey<NotEquals>(constNotEquals) == new ValueKey<NotEquals>(constNotEquals), isFalse);
4946
expect(new ObjectKey(constNotEquals) == new ObjectKey(constNotEquals), isTrue);
5047

51-
final Object constObject = const Object(); // we want these instances to be separate instances so that we're not just checking with a single object
48+
final Object constObject = nonconst(const Object());
5249
expect(new ObjectKey(constObject) == new ObjectKey(constObject), isTrue);
5350
expect(new ObjectKey(new Object()) == new ObjectKey(new Object()), isFalse);
5451

packages/flutter/test/widgets/render_object_widget_test.dart

+3-4
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,9 @@ import 'package:flutter_test/flutter_test.dart';
66
import 'package:flutter/rendering.dart';
77
import 'package:flutter/widgets.dart';
88

9-
final Border nullBorder = null; // we want these instances to be separate instances so that we're not just checking with a single object
10-
final BoxDecoration kBoxDecorationA = new BoxDecoration(border: nullBorder);
11-
final BoxDecoration kBoxDecorationB = new BoxDecoration(border: nullBorder);
12-
final BoxDecoration kBoxDecorationC = new BoxDecoration(border: nullBorder);
9+
final BoxDecoration kBoxDecorationA = new BoxDecoration(border: nonconst(null));
10+
final BoxDecoration kBoxDecorationB = new BoxDecoration(border: nonconst(null));
11+
final BoxDecoration kBoxDecorationC = new BoxDecoration(border: nonconst(null));
1312

1413
class TestWidget extends StatelessWidget {
1514
const TestWidget({ this.child });

packages/flutter_test/lib/flutter_test.dart

+1
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ export 'src/binding.dart';
1212
export 'src/controller.dart';
1313
export 'src/finders.dart';
1414
export 'src/matchers.dart';
15+
export 'src/nonconst.dart';
1516
export 'src/stack_manipulation.dart';
1617
export 'src/test_async_utils.dart';
1718
export 'src/test_pointer.dart';
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
// Copyright 2018 The Chromium Authors. All rights reserved.
2+
// Use of this source code is governed by a BSD-style license that can be
3+
// found in the LICENSE file.
4+
5+
/// This function can be used to call a const constructor in such a way as to
6+
/// create a new instance rather than creating the common const instance.
7+
///
8+
/// ```dart
9+
/// class A {
10+
/// const A(this.i);
11+
/// int i;
12+
/// }
13+
///
14+
/// main () {
15+
/// // prevent prefer_const_constructors lint
16+
/// new A(nonconst(null));
17+
///
18+
/// // prevent prefer_const_declarations lint
19+
/// final int $null = nonconst(null);
20+
/// final A a = nonconst(const A(null));
21+
/// }
22+
/// ```
23+
T nonconst<T>(T t) => t;

0 commit comments

Comments
 (0)