code_builder icon indicating copy to clipboard operation
code_builder copied to clipboard

Named Required Parameters are expected to be passed as optionalParameters

Open eseidel opened this issue 3 years ago • 1 comments

Named Required Parameters are expected to be passed as optionalParameters

I had initially written my code to pass required named parameters into the requiredParameters array, that appears to be wrong. It generates this:

// generates "fib(required int i)" // which is invalid dart.
      Method(
        (b) => b
          ..name = 'fib'
          ..requiredParameters.add(
            Parameter(
              (b) => b
                ..name = 'i'
                ..named = true
                ..required = true
                ..type = refer('int').type,
            ),
          ),
      );

There is even a test for this. 🤣 https://github.com/dart-lang/code_builder/blob/master/test/specs/method_test.dart#L496

The meta issue is that the split between "requiredParameters" and "optionalParameters" doesn't make sense (and isn't documented): https://pub.dev/documentation/code_builder/latest/code_builder/MethodBuilder-class.html (There is no mention of what type of Parameters should end up in one vs the other.)

eseidel avatar Dec 08 '22 20:12 eseidel