Skip to content

Commit

Permalink
Issue 48074. Display inherited default value for super-parameters.
Browse files Browse the repository at this point in the history
Bug: #48074
Change-Id: I964141c9b2a1ccf4a2d08d6bfff7675aa6df9d5a
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/229740
Reviewed-by: Brian Wilkerson <brianwilkerson@google.com>
Commit-Queue: Konstantin Shcheglov <scheglov@google.com>
  • Loading branch information
scheglov authored and Commit Bot committed Jan 25, 2022
1 parent 4c64181 commit 06e9046
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 13 deletions.
48 changes: 48 additions & 0 deletions pkg/analysis_server/test/analysis/get_hover_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -933,6 +933,54 @@ class A {
expect(hover.elementKind, 'parameter');
}

Future<void>
test_parameter_ofConstructor_optionalPositional_super_defaultValue_explicit() async {
addTestFile('''
class A {
A([int a = 1]);
}
class B extends A {
B([super.a = 2]);
}
''');
var hover = await prepareHover('a = 2]');
// element
expect(hover.elementDescription, '[int a = 2]');
expect(hover.elementKind, 'parameter');
}

Future<void>
test_parameter_ofConstructor_optionalPositional_super_defaultValue_inherited() async {
addTestFile('''
class A {
A([int a = 1]);
}
class B extends A {
B([super.a]);
}
''');
var hover = await prepareHover('a]');
// element
expect(hover.elementDescription, '[int a = 1]');
expect(hover.elementKind, 'parameter');
}

Future<void>
test_parameter_ofConstructor_optionalPositional_super_defaultValue_inherited2() async {
addTestFile('''
class A {
A([num a = 1.2]);
}
class B extends A{
B([int super.a]);
}
''');
var hover = await prepareHover('a]');
// element
expect(hover.elementDescription, '[int a]');
expect(hover.elementKind, 'parameter');
}

Future<void> test_parameter_reference_fieldFormal() async {
addTestFile('''
class A {
Expand Down
31 changes: 18 additions & 13 deletions pkg/analyzer/lib/src/dart/element/element.dart
Original file line number Diff line number Diff line change
Expand Up @@ -1584,23 +1584,19 @@ class DefaultSuperFormalParameterElementImpl

@override
String? get defaultValueCode {
return constantInitializer?.toSource();
}

@override
bool get hasDefaultValue {
if (super.hasDefaultValue) {
return true;
final constantInitializer = this.constantInitializer;
if (constantInitializer != null) {
return constantInitializer.toSource();
}
return computeConstantValue() != null;
}

@override
DartObject? computeConstantValue() {
if (constantInitializer != null) {
return super.computeConstantValue();
if (_superConstructorParameterDefaultValue != null) {
return superConstructorParameter?.defaultValueCode;
}

return null;
}

DartObject? get _superConstructorParameterDefaultValue {
var superDefault = superConstructorParameter?.computeConstantValue();
var superDefaultType = superDefault?.type;
var libraryElement = library;
Expand All @@ -1612,6 +1608,15 @@ class DefaultSuperFormalParameterElementImpl

return null;
}

@override
DartObject? computeConstantValue() {
if (constantInitializer != null) {
return super.computeConstantValue();
}

return _superConstructorParameterDefaultValue;
}
}

/// The synthetic element representing the declaration of the type `dynamic`.
Expand Down

0 comments on commit 06e9046

Please sign in to comment.