From bf602914937f049c83b38cfd030f6d6d7fd5ca74 Mon Sep 17 00:00:00 2001 From: Dan Field Date: Tue, 14 Dec 2021 19:00:52 -0800 Subject: [PATCH] Improve sync*/async* opt outs (#95286) --- dev/bots/analyze.dart | 19 +++++++++++++++---- .../lib/widgets/async/stream_builder.0.dart | 18 +++++++++++++----- packages/flutter/lib/src/rendering/table.dart | 6 ++++-- 3 files changed, 32 insertions(+), 11 deletions(-) diff --git a/dev/bots/analyze.dart b/dev/bots/analyze.dart index 68dae5442e..4ddf900bc1 100644 --- a/dev/bots/analyze.dart +++ b/dev/bots/analyze.dart @@ -163,8 +163,8 @@ Future run(List arguments) async { Future verifyNoSyncAsyncStar(String workingDirectory, {int minimumMatches = 2000 }) async { final RegExp syncPattern = RegExp(r'\s*?a?sync\*\s*?{'); - const String ignorePattern = 'no_sync_async_star'; - final RegExp commentPattern = RegExp(r'^\s*?///?'); + final RegExp ignorePattern = RegExp(r'^\s*?// The following uses a?sync\* because:? '); + final RegExp commentPattern = RegExp(r'^\s*?//'); final List errors = []; await for (final File file in _allFiles(workingDirectory, 'dart', minimumMatches: minimumMatches)) { if (file.path.contains('test')) { @@ -176,8 +176,19 @@ Future verifyNoSyncAsyncStar(String workingDirectory, {int minimumMatches if (line.startsWith(commentPattern)) { continue; } - if (line.contains(syncPattern) && !line.contains(ignorePattern) && (index == 0 || !lines[index - 1].contains(ignorePattern))) { - errors.add('${file.path}:$index: sync*/async* without an ignore (no_sync_async_star).'); + if (line.contains(syncPattern)) { + int lookBehindIndex = index - 1; + bool hasExplanation = false; + while (lookBehindIndex >= 0 && lines[lookBehindIndex].startsWith(commentPattern)) { + if (lines[lookBehindIndex].startsWith(ignorePattern)) { + hasExplanation = true; + break; + } + lookBehindIndex -= 1; + } + if (!hasExplanation) { + errors.add('${file.path}:$index: sync*/async* without an explanation.'); + } } } } diff --git a/examples/api/lib/widgets/async/stream_builder.0.dart b/examples/api/lib/widgets/async/stream_builder.0.dart index 3d4e087aef..c7e313f0b8 100644 --- a/examples/api/lib/widgets/async/stream_builder.0.dart +++ b/examples/api/lib/widgets/async/stream_builder.0.dart @@ -4,6 +4,8 @@ // Flutter code sample for StreamBuilder +import 'dart:async'; + import 'package:flutter/material.dart'; void main() => runApp(const MyApp()); @@ -30,11 +32,17 @@ class MyStatefulWidget extends StatefulWidget { } class _MyStatefulWidgetState extends State { - // ignore: no_sync_async_star - final Stream _bids = (() async* { - await Future.delayed(const Duration(seconds: 1)); - yield 1; - await Future.delayed(const Duration(seconds: 1)); + final Stream _bids = (() { + late final StreamController controller; + controller = StreamController( + onListen: () async { + await Future.delayed(const Duration(seconds: 1)); + controller.add(1); + await Future.delayed(const Duration(seconds: 1)); + await controller.close(); + }, + ); + return controller.stream; })(); @override diff --git a/packages/flutter/lib/src/rendering/table.dart b/packages/flutter/lib/src/rendering/table.dart index e2db5d0583..c0fdc35fca 100644 --- a/packages/flutter/lib/src/rendering/table.dart +++ b/packages/flutter/lib/src/rendering/table.dart @@ -795,7 +795,8 @@ class RenderTable extends RenderBox { /// column, in row order, starting from the first row. /// /// This is a lazily-evaluated iterable. - // flutter_ignore: no_sync_async_star + // The following uses sync* because it is public API documented to return a + // lazy iterable. Iterable column(int x) sync* { for (int y = 0; y < rows; y += 1) { final int xy = x + y * columns; @@ -809,7 +810,8 @@ class RenderTable extends RenderBox { /// row, in column order, starting with the first column. /// /// This is a lazily-evaluated iterable. - // flutter_ignore: no_sync_async_star + // The following uses sync* because it is public API documented to return a + // lazy iterable. Iterable row(int y) sync* { final int start = y * columns; final int end = (y + 1) * columns;