Skip to content

add required when generating methods with nnbd named arguments. #3770

Closed

Description

@bsutton

If I use quickfix 'create method xxxx' and the method signature is:

final package = await DaoPackage().tryByMember(packageName: packageName);

The resulting generated signature is:

tryByMember({String packageName}) {}
`

The signature should be:
```dart
tryByMember({required String packageName}) {}

as nnbd named arguments require the 'required' keyword.

Activity

  1. added
    is enhancementAn enhancement or improvement that should be listed in release notes but is not a bug fix.
    on Jan 6, 2022
  2. DanTup commented on Jan 6, 2022

    @DanTup
    Member

    I think it would also be valid to have String? packageName (for example if packageName in the call site was nullable), in which case required would not be required.

    I thought this would be an easy fix, but it seems like in some situations the ? is not included so that should probably be fixed first (so we can conditionally add required). I've raised dart-lang/sdk#48086 about that and will look at this again after it's fixed.

  3. added this to the v3.34.0 milestone on Jan 6, 2022
  4. added
    in editorRelates to code editing or language features
    in lsp/analysis serverSomething to be fixed in the Dart analysis server
    on Jan 6, 2022
  5. bsutton commented on Jan 6, 2022

    @bsutton
    Author

    I'm only suggesting that we add the required for String not String?.

    We don't have enough information to know the intent of String? and I would think not requiring a nullable type would be the most common use case.

  6. DanTup commented on Jan 6, 2022

    @DanTup
    Member

    I'm only suggesting that we add the required for String not String?.

    Yep, I understand. But in the case linked above, I'm not certain if we're always getting the type correctly (it seems to be non-nullable in cases where I expected it to be nullable). However, I added another comment and think maybe it's working as intended an dit's because my example was a literal string and the type was narrowed to just String. I'll wait for confirmation on that though, but I now think it's probably a non-issue :-)

  7. DanTup commented on Jan 10, 2022

    @DanTup
    Member

    @bwilkerson interested in your opinion on this. Inserting required in the case above seems to make sense, because without it the code is invalid. However the code used to generate the parameters here is shared with some other fixes such as "add missing named parameter" and I think it's less clear what the behaviour should be there. For example, here's an existing test:

      Future<void> test_method_noParameters() async {
        await resolveTestCode('''
    class A {
      test() {}
    
      main() {
        test(named: 42);
      }
    }
    ''');
        await assertHasFix('''
    class A {
      test({int named}) {}
    
      main() {
        test(named: 42);
      }
    }
    ''');
      }

    The generated code is also invalid, but I'm not sure if it's clear that it's better to insert required int versus int?. I feel like both are going to be wrong at some point. Should we just pick one, or is it better to generate the code with a warning here and force the user to pick? (or, should there be two options in the fixes?)

    (Edit: After typing this, I'm actually wondering whether making the parameter nullable in the original case it also reasonable... just because you're supplying a non-null value here, doesn't necessarily mean that's how you want the new function to be?)

  8. bsutton commented on Jan 10, 2022

    @bsutton
    Author
  9. bwilkerson commented on Jan 13, 2022

    @bwilkerson

    Inserting required in the case above seems to make sense, because without it the code is invalid.

    I agree.

    After typing this, I'm actually wondering whether making the parameter nullable in the original case it also reasonable... just because you're supplying a non-null value here, doesn't necessarily mean that's how you want the new function to be?

    That's true, but I think in this case it's better to choose the most likely option (which hopefully for most cases will be to use non-nullable types) and let the user change the code manually if necessary. This case has the advantage that we don't have any other invocations of the method being added, which means choosing to make it required won't produce any follow-on errors.

    However the code used to generate the parameters here is shared with some other fixes such as "add missing named parameter" and I think it's less clear what the behaviour should be there.
    ...
    The generated code is also invalid, but I'm not sure if it's clear that it's better to insert required int versus int?. I feel like both are going to be wrong at some point.

    Yeah, that case is a bit more complicated. There might be other invocation sites, so there might not be a single change that won't cause follow-on errors.

    I'm not opposed to having two options for users, but I'm also not opposed to providing a single required int option now and seeing how often users want the other option before adding support for it.

  10. bsutton commented on Jan 14, 2022

    @bsutton
    Author

    I'm not opposed to having two options for users, but I'm also not opposed to providing a single required int option now and seeing how often users want the other option before adding support for it.

    agreed with single required int option for the moment.
    Less work and if no one complains it was never needed.

    We should be encouraging uses to use nnbd.
    99% of my args are nnbd and I would imagine other developers are heading in the same direction as it's just easier to work with.

  11. DanTup commented on Jan 20, 2022

    @DanTup
    Member

    Fixed by dart-lang/sdk@32b3e18. It's an SDK change so will show up in a future SDK release rather than a Dart-Code one.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    in editorRelates to code editing or language featuresin lsp/analysis serverSomething to be fixed in the Dart analysis serveris enhancementAn enhancement or improvement that should be listed in release notes but is not a bug fix.

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions