Skip to content

Customers would like a lint to prevent the "async" modifier w/ @mustBeSynchronous annotation #35024

@matanlurey

Description

@matanlurey

AngularDart and ACX have lifecycle events that are added via implements:

abstract class OnDestroy {
  /// Implement to receive a notification when the component is being removed.
  ///
  /// This is an ideal time to cleanup streams, cancel pending RPCs, clear caches, etc.
  void ngOnDestroy();
}

A frequently observed problem is users wanting to use await, so they add async:

class MyComp implements OnDestroy {
  StreamSubscription<void> _windowResizeSub;

  @override
  void ngOnDestroy() async {
    await doSomethingAsync();
    // The user might continue to receive window.onResize events after the component has been
    // effectively destroyed because they used "await" above instead of just "doSomethingAsync()".
    _windowResizeSub.cancel();
  }
}

... and this has been exasperated by allowing async on void methods unfortunately.

We would like a metadata annotation discouraging any use of async / await on certain methods:

import 'package:meta/meta.dart';

abstract class OnDestroy {
  /// Implement to receive a notification when the component is being removed.
  ///
  /// This is an ideal time to cleanup streams, cancel pending RPCs, clear caches, etc.
  @mustBeSynchronous
  void ngOnDestroy();
}

class MyComp implements OnDestroy {
  StreamSubscription<void> _windowResizeSub;

  @override
  // LINT: "OnDestroy.ngOnDestroy should not use async/await"
  void ngOnDestroy() async {
    await doSomethingAsync();
  }
}

/cc @srawlins

Metadata

Metadata

Assignees

Labels

P2A bug or feature request we're likely to work onarea-devexpFor issues related to the analysis server, IDE support, linter, `dart fix`, and diagnostic messages.customer-google3devexp-pkg-metaIssues related to package:metatype-enhancementA request for a change that isn't a bug

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions