Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Provide a built-in macro that copies the documentation of the superclass #3937

Open
mateusfccp opened this issue Dec 1, 2024 · 2 comments
Open
Labels
P3 A lower priority bug or feature request type-enhancement A request for a change that isn't a bug

Comments

@mateusfccp
Copy link

mateusfccp commented Dec 1, 2024

I looked for something like this in the issues but couldn't find anything.

Problem

Currently, if you have an interface A with a member a documented with doc(a), an interface B implementing A will inherit the documentation doc(a) for its a implementation unless it explicitly document the method.

For instance:

// foo.dart
class Foo {
  /// Bars the foo.
  void bar() {}

  /// Quxes the foo.
  void qux() {}
}
// baz.dart

import 'foo.dart';

class Baz extends Foo {
  @override
  void bar() {}

  /// Quxes the baz.
  @override
  void qux() {}
}

In this case, Baz.bar will have the same documentation as Foo.bar, but Baz.qux will have the documentation it specified instead of Foo.qux's one.

However, sometimes we want to add to the superclass documentation instead of overwriting it. If you own the library that defines the interface you are implementing, you can use a template:

// library_that_i_own.dart
class Foo {
  /// {@template foo.bar}
  /// Bars the foo.
  /// {@endtemplate}
  void bar() {}
}
// my_library.dart

import 'library_that_i_own.dart';

class Baz extends Foo {
  /// {@macro foo.bar}
  ///
  /// After barring, also bazes the Foo.
  @override
  void bar() {}
}

This is not possible, however, if you are implementing an interface that you don't own.

Suggestion

Implement a built-in macro that refers to the documentation of the supertype. For instance, {@macro super} or make it a different "command", like {@super} (so it's not breaking if someone ever defined a template called super).

The aforementioned would then be implemented like this:

// library_that_i_dont_own.dart
class Foo {
  /// Bars the foo.
  void bar() {}
}
// my_library.dart

import 'library_that_i_dont_own.dart';

class Baz extends Foo {
  /// {@super}
  ///
  /// After barring, also bazes the Foo.
  @override
  void bar() {}
}
@bwilkerson bwilkerson added type-enhancement A request for a change that isn't a bug P3 A lower priority bug or feature request labels Dec 16, 2024
@mateusfccp
Copy link
Author

If there's any interest in reviewing a PR for this, I may do it.

@srawlins
Copy link
Member

If we implement this feature (or accept a PR for it), it will take place in the analyzer, so that it is supported in IDEs as well.

Let me bring this up in an analyzer team meeting, and I'll post back here on whether we like it (personally I like it; I'm surprised it hasn't come up before, that I've seen).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
P3 A lower priority bug or feature request type-enhancement A request for a change that isn't a bug
Projects
None yet
Development

No branches or pull requests

3 participants