[dart-lang/sdk] bfe325: Update AddFieldFormalParameters to handle private ...

0 views
Skip to first unread message

Bob Nystrom

unread,
Jan 16, 2026, 3:11:38 PM (2 days ago) Jan 16
to com...@dartlang.org
Branch: refs/heads/main
Home: https://github.com/dart-lang/sdk
Commit: bfe325b834b3166c07f1e136b6fdeb226e120eae
https://github.com/dart-lang/sdk/commit/bfe325b834b3166c07f1e136b6fdeb226e120eae
Author: Robert Nystrom <rnys...@google.com>
Date: 2026-01-16 (Fri, 16 Jan 2026)

Changed paths:
M pkg/analysis_server/lib/src/services/correction/dart/add_field_formal_parameters.dart
M pkg/analysis_server/test/src/services/correction/fix/add_field_formal_parameters_test.dart

Log Message:
-----------
Update AddFieldFormalParameters to handle private named parameters.

With this CL, if you add required named parameters for private fields, it will use private named parameters:

```dart
// Before:
class C {
final int _foo;
}

// After:
class C {
final int _foo;
C({required this._foo});
}
```

Prior to this CL, it would have instead produced:

```dart
// After:
class C {
final int _foo;
C({required int foo}) : _foo = foo;
}
```

It will only do this if private named parameters are enabled and the field has a valid corresponding public name:

```dart
// Before:
class C {
final int _ok;
final int _123;
final int _for;
}

// After:
class C {
final int _ok;
final int _123;
final int _for;
C({required this.ok, required int p123, required int pfor})
: _123 = p123,
_for = pfor;
}
```

There is one other behavioral change in the CL. It will now also use initializing formals when adding positional parameters for private fields:

```dart
// Before:
class C {
final int _foo;
}

// After:
class C {
final int _foo;
C(this._foo);
}
```

Since private named parameters means that users will start seeing private initializing formals, it seemed pointless to me to avoid them for positional parameters. Also, doing this avoids a bug that the current implementation has. Before this CL, when adding positional parameters for private fields, it doesn't check that resulting parameter name isn't reserved:

```dart
// Before:
class C {
final int _class;
}

// After:
class C {
final int _class;
C(int class) : _class = class;
// ^^^^^ Oops!
}
```

With this CL, it will simply do:

```
// After:
class C {
final int _class;
C(this._class);
}
```

(It does correctly handle a private *named* parameter whose corresponding public name is a reserved word.)

BUG: https://github.com/dart-lang/sdk/issues/62430

Change-Id: I300066e7df0eb93b3674eac56bd48cdcdfa95758
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/473782
Auto-Submit: Bob Nystrom <rnys...@google.com>
Reviewed-by: Brian Wilkerson <brianwi...@google.com>
Commit-Queue: Bob Nystrom <rnys...@google.com>
Reviewed-by: Konstantin Shcheglov <sche...@google.com>



To unsubscribe from these emails, change your notification settings at https://github.com/dart-lang/sdk/settings/notifications

Bob Nystrom

unread,
Jan 16, 2026, 5:08:17 PM (2 days ago) Jan 16
to com...@dartlang.org
Branch: refs/heads/lkgr
Reply all
Reply to author
Forward
0 new messages