sdk icon indicating copy to clipboard operation
sdk copied to clipboard

Encapsulate field assist miss-types the constructor field if the type is from an aliased import

Open FMorschel opened this issue 1 year ago • 1 comments
trafficstars

Repro:

type.dart

class MyType {}

main.dart

import 'type.dart' as type;

class C {
  C({required this.t});

  type.MyType t;
}

Now use the "encapsulate field" quick-fix on t. It will generate a getter and setter of the correct type (with alias) but not the constructor.

Result:

import 'type.dart' as type;

class C {
  C({required MyType t}) : _t = t;   //  <-- "MyType" here throws `undefined_class`

  type.MyType _t;

  type.MyType get t => _t;

  set t(type.MyType value) {
    _t = value;
  }
}

As stated in the above comment, this sample shows an error for undefined_class, but this could be a problem with classes with the same name (aliases usually are added for that reason) that are related in some way so this would not show any errors now only when attributing the values later.

FMorschel avatar Aug 23 '24 02:08 FMorschel

Summary: The "encapsulate field" quick-fix incorrectly uses the unqualified type name in the constructor parameter when the field type is from an aliased import. This leads to a compile-time error if the unqualified type name is not defined in the current scope.

dart-github-bot avatar Aug 23 '24 02:08 dart-github-bot

@fshcheglov This is related to https://github.com/dart-lang/sdk/issues/56559, you should be able to solve it.

scheglov avatar Sep 11 '24 23:09 scheglov

https://dart-review.googlesource.com/c/sdk/+/385322

fshcheglov avatar Sep 13 '24 21:09 fshcheglov