[ddc] Handle optional/defaulted parameters when created scoped parameter renames for sync* transform.

This was missed in the original implementation of the sync* transformer because prior to my recent change, ScopedIds couldn't end up within a DestructuredVariable.

Change-Id: I2733ce1e01edb50659634347204bac1769269615
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/401080
Reviewed-by: Nicholas Shahan <nshahan@google.com>
Commit-Queue: Nate Biggs <natebiggs@google.com>
This commit is contained in:
Nate Biggs
2024-12-16 10:01:23 -08:00
committed by Commit Queue
parent f6ecfba1ec
commit 46bd9f351a
2 changed files with 38 additions and 2 deletions
@@ -2197,8 +2197,13 @@ class SyncStarRewriter extends AsyncRewriterBase {
for (var parameter in parameters) {
final name = parameter.parameterName;
final renamedIdentifier = ScopedId(name);
final parameterRef =
parameter is ScopedId ? parameter : js_ast.Identifier(name);
final parameterRef = switch (parameter) {
ScopedId() => ScopedId.from(parameter),
js_ast.DestructuredVariable() when parameter.name is ScopedId =>
ScopedId.from(parameter.name as ScopedId),
_ => js_ast.Identifier(name)
};
innerDeclarationsList
.add(js_ast.VariableInitialization(parameterRef, renamedIdentifier));
outerDeclarationsList
@@ -0,0 +1,31 @@
// Copyright (c) 2024, the Dart project authors. Please see the AUTHORS file
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.
// Ensure optional parameters get renamed properly for sync* transformations.
Iterable<num> range(num startOrStop, [num? stop, num? step]) sync* {
final start = stop == null ? 0 : startOrStop;
stop ??= startOrStop;
step ??= 1;
if (step == 0) throw ArgumentError('step cannot be 0');
if (step > 0 && stop < start) {
throw ArgumentError('if step is positive, stop must be greater than start');
}
if (step < 0 && stop > start) {
throw ArgumentError('if step is negative, stop must be less than start');
}
for (
num value = start;
step < 0 ? value > stop : value < stop;
value += step
) {
yield value;
}
}
void main() {
print(range(10, 20, 2));
}