From b112bb95cf230e7d69d51b0bdf4f18b0383778fd Mon Sep 17 00:00:00 2001 From: Moritz Date: Mon, 23 Jun 2025 09:53:03 -0700 Subject: [PATCH] [record_use] Fix signature parsing bug MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Another sign that we should switch to not having to use signature parsing anymore... Which needs Dart2Js to support named arguments. See also https://github.com/dart-lang/sdk/issues/60597 Change-Id: I34ca924be7f65021963c00f8eb9b9fda33164573 Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/436380 Reviewed-by: Daco Harkes Commit-Queue: Moritz Sümmermann --- pkg/record_use/CHANGELOG.md | 4 ++++ pkg/record_use/lib/src/signature.dart | 7 ++++++- pkg/record_use/pubspec.yaml | 2 +- pkg/record_use/test/signature_test.dart | 16 +++++++++++++++- 4 files changed, 26 insertions(+), 3 deletions(-) diff --git a/pkg/record_use/CHANGELOG.md b/pkg/record_use/CHANGELOG.md index 8d3da5f3f58..ca6b75d6f18 100644 --- a/pkg/record_use/CHANGELOG.md +++ b/pkg/record_use/CHANGELOG.md @@ -1,3 +1,7 @@ +## 0.4.1 + +- Fix bug in signature parsing. + ## 0.4.0 - Update SDK constraint to `^3.5.0`. diff --git a/pkg/record_use/lib/src/signature.dart b/pkg/record_use/lib/src/signature.dart index c89c740988d..d826ac96a9b 100644 --- a/pkg/record_use/lib/src/signature.dart +++ b/pkg/record_use/lib/src/signature.dart @@ -9,6 +9,10 @@ import 'constant.dart'; import 'helper.dart'; // Assuming helper.dart contains the deepEquals function /// Represents the signature of a Dart method, categorizing its parameters. +/// +/// This is a stop-gap due to https://github.com/dart-lang/sdk/issues/60597, +/// and this code should be removed once that bug is fixed. +// TODO(mosum): Delete this code class Signature { /// List of required positional parameter names. final List positionalParameters; @@ -41,7 +45,8 @@ class Signature { ...namedParameters, ...namedOptionalParameters, ]; - if (call.positionalArguments.length != names.length) { + if (call.positionalArguments.length + call.namedArguments.length != + names.length) { throw FormatException( ''' Invalid number of arguments - $names vs ${call.positionalArguments} and ${call.namedArguments}''', diff --git a/pkg/record_use/pubspec.yaml b/pkg/record_use/pubspec.yaml index 20fe1b91c3e..c42244111a3 100644 --- a/pkg/record_use/pubspec.yaml +++ b/pkg/record_use/pubspec.yaml @@ -1,7 +1,7 @@ name: record_use description: > The serialization logic and API for the usage recording SDK feature. -version: 0.4.0 +version: 0.4.1 repository: https://github.com/dart-lang/sdk/tree/main/pkg/record_use environment: diff --git a/pkg/record_use/test/signature_test.dart b/pkg/record_use/test/signature_test.dart index 563368d62ca..97c13e7b958 100644 --- a/pkg/record_use/test/signature_test.dart +++ b/pkg/record_use/test/signature_test.dart @@ -2,7 +2,7 @@ // 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. -import 'package:record_use/src/signature.dart'; +import 'package:record_use/record_use_internal.dart'; import 'package:test/test.dart'; void main() { @@ -92,6 +92,20 @@ Future fetchData(String url, int retries, [bool? cache, Duration? timeout] expect(Signature.parseMethodSignature(signature), expected); }); + test('mixed parameters', () { + final signature = 'void config(String apiKey, {required String apiUrl})'; + final parsed = Signature.parseMethodSignature(signature).parseArguments( + const CallWithArguments( + positionalArguments: [StringConstant('value')], + namedArguments: {'apiUrl': StringConstant('value2')}, + loadingUnit: null, + location: Location(uri: ''), + ), + ); + expect(parsed.named.entries.single.value?.toValue(), 'value2'); + expect(parsed.positional.single?.toValue(), 'value'); + }); + test('handles signatures with no parameters', () { final signature = 'void doSomething()'; final expected = const Signature(