Skip to content

Commit c9cb43c

Browse files
authored
Migrate the first batch of API samples to null safety (flutter#72169)
1 parent fb3dba7 commit c9cb43c

File tree

13 files changed

+62
-79
lines changed

13 files changed

+62
-79
lines changed

dev/bots/analyze-sample-code.dart

Lines changed: 32 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
// bin/cache/dart-sdk/bin/dart dev/bots/analyze-sample-code.dart
99

1010
import 'dart:io';
11-
import 'dart:isolate';
1211

1312
import 'package:args/args.dart';
1413
import 'package:path/path.dart' as path;
@@ -203,6 +202,8 @@ class SampleChecker {
203202
return _headers ??= <String>[
204203
'// generated code',
205204
'// ignore_for_file: unused_import',
205+
'// ignore_for_file: unused_element',
206+
'// ignore_for_file: unused_local_variable',
206207
"import 'dart:async';",
207208
"import 'dart:convert';",
208209
"import 'dart:math' as math;",
@@ -547,8 +548,9 @@ linter:
547548
}
548549

549550
/// Invokes the analyzer on the given [directory] and returns the stdout.
550-
List<String> _runAnalyzer(Directory directory) {
551-
print('Starting analysis of code samples.');
551+
List<String> _runAnalyzer(Directory directory, {bool silent}) {
552+
if (!silent)
553+
print('Starting analysis of code samples.');
552554
_createConfigurationFiles(directory);
553555
final ProcessResult result = Process.runSync(
554556
_flutter,
@@ -570,7 +572,7 @@ linter:
570572
stderr.removeLast();
571573
}
572574
}
573-
if (stderr.isNotEmpty) {
575+
if (stderr.isNotEmpty && stderr.any((String line) => line.isNotEmpty)) {
574576
throw 'Cannot analyze dartdocs; unexpected error output:\n$stderr';
575577
}
576578
if (stdout.isNotEmpty && stdout.first == 'Building flutter tool...') {
@@ -588,9 +590,10 @@ linter:
588590
Map<String, List<AnalysisError>> _analyze(
589591
Directory directory,
590592
Map<String, Section> sections,
591-
Map<String, Sample> samples,
592-
) {
593-
final List<String> errors = _runAnalyzer(directory);
593+
Map<String, Sample> samples, {
594+
bool silent = false,
595+
}) {
596+
final List<String> errors = _runAnalyzer(directory, silent: silent);
594597
final Map<String, List<AnalysisError>> analysisErrors = <String, List<AnalysisError>>{};
595598
void addAnalysisError(File file, AnalysisError error) {
596599
if (analysisErrors.containsKey(file.path)) {
@@ -621,10 +624,6 @@ linter:
621624
final String errorCode = parts[6];
622625
final int lineNumber = int.parse(line, radix: 10) - (isSnippet ? headerLength : 0);
623626
final int columnNumber = int.parse(column, radix: 10);
624-
if (lineNumber < 0 && errorCode == 'unused_import') {
625-
// We don't care about unused imports.
626-
continue;
627-
}
628627

629628
// For when errors occur outside of the things we're trying to analyze.
630629
if (!isSnippet && !isSample) {
@@ -649,10 +648,6 @@ linter:
649648
);
650649
}
651650

652-
if (errorCode == 'unused_element' || errorCode == 'unused_local_variable') {
653-
// We don't really care if sample code isn't used!
654-
continue;
655-
}
656651
if (isSample) {
657652
addAnalysisError(
658653
file,
@@ -739,7 +734,8 @@ linter:
739734
_exitCode = 0;
740735
}
741736
if (_exitCode == 0) {
742-
print('No analysis errors in samples!');
737+
if (!silent)
738+
print('No analysis errors in samples!');
743739
assert(analysisErrors.isEmpty);
744740
}
745741
return analysisErrors;
@@ -960,24 +956,28 @@ Future<void> _runInteractive(Directory tempDir, Directory flutterPackage, String
960956
print('Using temp dir ${tempDir.path}');
961957
}
962958

963-
final SampleChecker checker = SampleChecker(flutterPackage, tempDirectory: tempDir)
964-
.._createConfigurationFiles(tempDir)
965-
.._extractSamples(<File>[file], silent: true);
966-
967-
await Isolate.spawn(_watcher, <dynamic>[checker, file]);
959+
void analyze(SampleChecker checker, File file) {
960+
final Map<String, Section> sections = <String, Section>{};
961+
final Map<String, Sample> snippets = <String, Sample>{};
962+
checker._extractSamples(<File>[file], silent: true, sectionMap: sections, sampleMap: snippets);
963+
final Map<String, List<AnalysisError>> errors = checker._analyze(checker._tempDirectory, sections, snippets, silent: true);
964+
stderr.writeln('\u001B[2J\u001B[H'); // Clears the old results from the terminal.
965+
if (errors.isNotEmpty) {
966+
for (final String filePath in errors.keys) {
967+
errors[filePath].forEach(stderr.writeln);
968+
}
969+
stderr.writeln('\nFound ${errors.length} errors.');
970+
} else {
971+
stderr.writeln('\nNo issues found.');
972+
}
973+
}
968974

969-
await Process.start(
970-
_flutter,
971-
<String>['--no-wrap', 'analyze', '--no-preamble', '--no-congratulate', '--watch', '.'],
972-
workingDirectory: tempDir.absolute.path,
973-
mode: ProcessStartMode.inheritStdio
974-
);
975-
}
975+
final SampleChecker checker = SampleChecker(flutterPackage, tempDirectory: tempDir)
976+
.._createConfigurationFiles(tempDir);
977+
analyze(checker, file);
976978

977-
void _watcher(List<dynamic> args) {
978-
final File file = args.last as File;
979-
final SampleChecker checker = args.first as SampleChecker;
980979
Watcher(file.absolute.path).events.listen((_) {
981-
checker._extractSamples(<File>[file], silent: true);
980+
print('\n\nRerunning...');
981+
analyze(checker, file);
982982
});
983983
}

packages/flutter/lib/src/animation/animation.dart

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,7 @@ import 'package:flutter/foundation.dart';
88
import 'tween.dart';
99

1010
// Examples can assume:
11-
// // @dart = 2.9
12-
// AnimationController _controller;
11+
// late AnimationController _controller;
1312

1413
/// The status of an animation.
1514
enum AnimationStatus {

packages/flutter/lib/src/animation/animation_controller.dart

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,8 @@ import 'listener_helpers.dart';
1717
export 'package:flutter/scheduler.dart' show TickerFuture, TickerCanceled;
1818

1919
// Examples can assume:
20-
// // @dart = 2.9
21-
// AnimationController _controller, fadeAnimationController, sizeAnimationController;
22-
// bool dismissed;
20+
// late AnimationController _controller, fadeAnimationController, sizeAnimationController;
21+
// late bool dismissed;
2322
// void setState(VoidCallback fn) { }
2423

2524
/// The direction in which an animation is running.
@@ -137,7 +136,7 @@ enum AnimationBehavior {
137136
///
138137
/// ```dart
139138
/// class Foo extends StatefulWidget {
140-
/// Foo({ Key key, this.duration }) : super(key: key);
139+
/// Foo({ Key? key, required this.duration }) : super(key: key);
141140
///
142141
/// final Duration duration;
143142
///
@@ -146,7 +145,7 @@ enum AnimationBehavior {
146145
/// }
147146
///
148147
/// class _FooState extends State<Foo> with SingleTickerProviderStateMixin {
149-
/// AnimationController _controller;
148+
/// late AnimationController _controller;
150149
///
151150
/// @override
152151
/// void initState() {

packages/flutter/lib/src/animation/animations.dart

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,7 @@ import 'curves.dart';
1212
import 'listener_helpers.dart';
1313

1414
// Examples can assume:
15-
// // @dart = 2.9
16-
// AnimationController controller;
15+
// late AnimationController controller;
1716

1817
class _AlwaysCompleteAnimation extends Animation<double> {
1918
const _AlwaysCompleteAnimation();

packages/flutter/lib/src/animation/tween.dart

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
// Use of this source code is governed by a BSD-style license that can be
33
// found in the LICENSE file.
44

5-
65
import 'dart:ui' show Color, Size, Rect;
76

87
import 'package:flutter/foundation.dart';
@@ -12,9 +11,8 @@ import 'animations.dart';
1211
import 'curves.dart';
1312

1413
// Examples can assume:
15-
// // @dart = 2.9
16-
// Animation<Offset> _animation;
17-
// AnimationController _controller;
14+
// late Animation<Offset> _animation;
15+
// late AnimationController _controller;
1816

1917
/// An object that can produce a value of type `T` given an [Animation<double>]
2018
/// as input.

packages/flutter/lib/src/animation/tween_sequence.dart

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,7 @@ import 'animation.dart';
77
import 'tween.dart';
88

99
// Examples can assume:
10-
// // @dart = 2.9
11-
// AnimationController myAnimationController;
10+
// late AnimationController myAnimationController;
1211

1312
/// Enables creating an [Animation] whose value is defined by a sequence of
1413
/// [Tween]s.

packages/flutter/lib/src/foundation/annotations.dart

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
// found in the LICENSE file.
44

55
// Examples can assume:
6-
// // @dart = 2.9
76
// class Cat { }
87

98
/// A category with which to annotate a class, for documentation

packages/flutter/lib/src/foundation/assertions.dart

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,10 @@ import 'print.dart';
1111
import 'stack_frame.dart';
1212

1313
// Examples can assume:
14-
// // @dart = 2.9
15-
// String runtimeType;
16-
// bool draconisAlive;
17-
// bool draconisAmulet;
18-
// Diagnosticable draconis;
14+
// late String runtimeType;
15+
// late bool draconisAlive;
16+
// late bool draconisAmulet;
17+
// late Diagnosticable draconis;
1918

2019
/// Signature for [FlutterError.onError] handler.
2120
typedef FlutterExceptionHandler = void Function(FlutterErrorDetails details);

packages/flutter/lib/src/foundation/diagnostics.dart

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,9 @@ import 'debug.dart';
1212
import 'object.dart';
1313

1414
// Examples can assume:
15-
// // @dart = 2.9
16-
// int rows, columns;
17-
// String _name;
18-
// bool inherit;
15+
// late int rows, columns;
16+
// late String _name;
17+
// late bool inherit;
1918

2019
/// The various priority levels used to filter which diagnostics are shown and
2120
/// omitted.
@@ -3069,7 +3068,7 @@ class DiagnosticPropertiesBuilder {
30693068
}
30703069

30713070
// Examples can assume:
3072-
// class ExampleSuperclass with Diagnosticable { String message; double stepWidth; double scale; double paintExtent; double hitTestExtent; double paintExtend; double maxWidth; bool primary; double progress; int maxLines; Duration duration; int depth; dynamic boxShadow; dynamic style; bool hasSize; Matrix4 transform; Map<Listenable, VoidCallback> handles; Color color; bool obscureText; ImageRepeat repeat; Size size; Widget widget; bool isCurrent; bool keepAlive; TextAlign textAlign; }
3071+
// class ExampleSuperclass with Diagnosticable { late String message; late double stepWidth; late double scale; late double paintExtent; late double hitTestExtent; late double paintExtend; late double maxWidth; late bool primary; late double progress; late int maxLines; late Duration duration; late int depth; late dynamic boxShadow; late dynamic style; late bool hasSize; late Matrix4 transform; Map<Listenable, VoidCallback>? handles; late Color color; late bool obscureText; late ImageRepeat repeat; late Size size; late Widget widget; late bool isCurrent; late bool keepAlive; late TextAlign textAlign; }
30733072

30743073
/// A mixin class for providing string and [DiagnosticsNode] debug
30753074
/// representations describing the properties of an object.
@@ -3326,9 +3325,9 @@ mixin Diagnosticable {
33263325
/// properties.add(DiagnosticsProperty<Map<Listenable, VoidCallback>>(
33273326
/// 'handles',
33283327
/// handles,
3329-
/// description: handles != null ?
3330-
/// '${handles.length} active client${ handles.length == 1 ? "" : "s" }' :
3331-
/// null,
3328+
/// description: handles != null
3329+
/// ? '${handles!.length} active client${ handles!.length == 1 ? "" : "s" }'
3330+
/// : null,
33323331
/// ifNull: 'no notifications ever received',
33333332
/// showName: false,
33343333
/// ));

packages/flutter/lib/src/painting/borders.dart

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
// Use of this source code is governed by a BSD-style license that can be
33
// found in the LICENSE file.
44

5-
65
import 'dart:math' as math;
76
import 'dart:ui' as ui show lerpDouble;
87

0 commit comments

Comments
 (0)