Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make getters and setters actually look like their declarations #3858

Merged
merged 1 commit into from
Sep 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 16 additions & 30 deletions lib/src/generator/templates.aot_renderers_for_html.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4743,6 +4743,7 @@ String _deduplicated_lib_templates__accessor_getter_html(
buffer.write(''' <span class="returntype">''');
buffer.write(context1.modelType.returnType.linkedName);
buffer.write('''</span>
get
''');
buffer.write(
__deduplicated_lib_templates__accessor_getter_html_partial_name_summary_1(
Expand Down Expand Up @@ -4869,27 +4870,30 @@ String _deduplicated_lib_templates__accessor_setter_html(
__deduplicated_lib_templates__accessor_setter_html_partial_annotations_0(
context1));
buffer.writeln();
buffer.write(''' <span class="returntype">void</span>
''');
buffer.write(
__deduplicated_lib_templates__accessor_setter_html_partial_name_summary_1(
context1));
buffer.write('''<span class="signature">(<wbr>''');
buffer.write(''' set
<span class="name ''');
if (context1.isDeprecated) {
buffer.write('''deprecated''');
}
buffer.write('''">''');
buffer.writeEscaped(context1.definingCombo.name);
buffer.write('''</span>
<span class="signature">(<wbr>''');
buffer.write(context1.linkedParamsNoMetadata);
buffer.write(''')</span>
''');
buffer.write(
__deduplicated_lib_templates__accessor_setter_html_partial_attributes_2(
__deduplicated_lib_templates__accessor_setter_html_partial_attributes_1(
context1));
buffer.writeln();
buffer.write(''' </section>
''');
buffer.write(
__deduplicated_lib_templates__accessor_setter_html_partial_documentation_3(
__deduplicated_lib_templates__accessor_setter_html_partial_documentation_2(
context1));
buffer.write('\n ');
buffer.write(
__deduplicated_lib_templates__accessor_setter_html_partial_source_code_4(
__deduplicated_lib_templates__accessor_setter_html_partial_source_code_3(
context1));
buffer.writeln();
buffer.write(''' </section>''');
Expand Down Expand Up @@ -4921,25 +4925,7 @@ String __deduplicated_lib_templates__accessor_setter_html_partial_annotations_0(
return buffer.toString();
}

String
__deduplicated_lib_templates__accessor_setter_html_partial_name_summary_1(
Accessor context1) {
final buffer = StringBuffer();
if (context1.isConst) {
buffer.write('''const ''');
}
buffer.write('''<span class="name ''');
if (context1.isDeprecated) {
buffer.write('''deprecated''');
}
buffer.write('''">''');
buffer.writeEscaped(context1.name);
buffer.write('''</span>''');

return buffer.toString();
}

String __deduplicated_lib_templates__accessor_setter_html_partial_attributes_2(
String __deduplicated_lib_templates__accessor_setter_html_partial_attributes_1(
Accessor context1) {
final buffer = StringBuffer();
if (context1.hasAttributes) {
Expand All @@ -4953,7 +4939,7 @@ String __deduplicated_lib_templates__accessor_setter_html_partial_attributes_2(
}

String
__deduplicated_lib_templates__accessor_setter_html_partial_documentation_3(
__deduplicated_lib_templates__accessor_setter_html_partial_documentation_2(
Accessor context1) {
final buffer = StringBuffer();
if (context1.hasDocumentation) {
Expand All @@ -4969,7 +4955,7 @@ String
return buffer.toString();
}

String __deduplicated_lib_templates__accessor_setter_html_partial_source_code_4(
String __deduplicated_lib_templates__accessor_setter_html_partial_source_code_3(
Accessor context1) {
final buffer = StringBuffer();
if (context1.hasSourceCode) {
Expand Down
22 changes: 22 additions & 0 deletions lib/src/generator/templates.runtime_renderers.dart
Original file line number Diff line number Diff line change
Expand Up @@ -6585,6 +6585,27 @@ class _Renderer_GetterSetterCombo extends RendererBase<GetterSetterCombo> {
parent: r);
},
),
'name': Property(
getValue: (CT_ c) => c.name,
renderVariable:
(CT_ c, Property<CT_> self, List<String> remainingNames) {
if (remainingNames.isEmpty) {
return self.getValue(c).toString();
}
var name = remainingNames.first;
var nextProperty =
_Renderer_String.propertyMap().getValue(name);
return nextProperty.renderVariable(
self.getValue(c) as String,
nextProperty,
[...remainingNames.skip(1)]);
},
isNullValue: (CT_ c) => false,
renderValue: (CT_ c, RendererBase<CT_> r,
List<MustachioNode> ast, StringSink sink) {
_render_String(c.name, ast, r.template, sink, parent: r);
},
),
'oneLineDoc': Property(
getValue: (CT_ c) => c.oneLineDoc,
renderVariable:
Expand Down Expand Up @@ -16013,6 +16034,7 @@ const _invisibleGetters = {
'isPublic',
'linkedParamsNoMetadata',
'modelType',
'name',
'oneLineDoc',
'parameters',
'readOnly',
Expand Down
5 changes: 5 additions & 0 deletions lib/src/model/getter_setter_combo.dart
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,11 @@ mixin GetterSetterCombo on ModelElement {

bool get isInherited;

@override
// Food for mustachio; because this is a mixin, mustachio can't figure out
// that this implicitly has a `name` property.
String get name;

@override
String get fileName => isConst ? '$name-constant.html' : '$name.html';

Expand Down
2 changes: 1 addition & 1 deletion lib/templates/_accessor_getter.html
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
{{ #getter }}
<section id="getter">

<section class="multi-line-signature">
{{ >annotations }}
<span class="returntype">{{{ modelType.returnType.linkedName }}}</span>
get
{{ >name_summary }}
{{ >attributes }}
</section>
Expand Down
5 changes: 3 additions & 2 deletions lib/templates/_accessor_setter.html
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@

<section class="multi-line-signature">
{{ >annotations }}
<span class="returntype">void</span>
{{ >name_summary }}<span class="signature">(<wbr>{{{ linkedParamsNoMetadata }}})</span>
set
<span class="name {{#isDeprecated}}deprecated{{/isDeprecated}}">{{ definingCombo.name }}</span>
<span class="signature">(<wbr>{{{ linkedParamsNoMetadata }}})</span>
{{ >attributes }}
</section>

Expand Down
87 changes: 87 additions & 0 deletions test/templates/field_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,93 @@ extension type ET(
);
}

void test_getter_signature() async {
await createPackageWithLibrary('''
class C {
int get f1 => 1;
}
''');
var f1Lines = readLines(['lib', 'C', 'f1.html']);
f1Lines.expectMainContentContainsAllInOrder(
[
matches('<h1><span class="kind-property">f1</span> property'),
matches('<section class="multi-line-signature">'),
matches('<span class="returntype">int</span>'),
matches('get'),
matches('<span class="name ">f1</span>'),
],
);
}

void test_getter_overridingProperty_signature() async {
await createPackageWithLibrary('''
class C {
int f1 = 0;
}
class D extends C {
@override
int get f1 => 1;
}
''');
var f1Lines = readLines(['lib', 'D', 'f1.html']);
f1Lines.expectMainContentContainsAllInOrder(
[
matches('<h1><span class="kind-property">f1</span> property'),
matches('<section class="multi-line-signature">'),
matches('<span class="returntype">int</span>'),
matches('get'),
matches('<span class="name ">f1</span>'),
],
);
}

void test_setter_signature() async {
await createPackageWithLibrary('''
class C {
set f1(int value) {}
}
''');
var f1Lines = readLines(['lib', 'C', 'f1.html']);
f1Lines.expectMainContentContainsAllInOrder(
[
matches('<section class="multi-line-signature">'),
matches('set'),
matches('<span class="name ">f1</span>'),
matches(r'<span class="signature">\('
'<wbr><span class="parameter" id="f1=-param-value">'
'<span class="type-annotation">int</span> '
'<span class="parameter-name">value</span>'
r'</span>\)'
'</span>'),
],
);
}

void test_setter_overridingProperty_signature() async {
await createPackageWithLibrary('''
class C {
int f1 = 0;
}
class D extends C {
set f1(int value) {}
}
''');
var f1Lines = readLines(['lib', 'D', 'f1.html']);
f1Lines.expectMainContentContainsAllInOrder(
[
matches('<section class="multi-line-signature">'),
matches('set'),
matches('<span class="name ">f1</span>'),
matches(r'<span class="signature">\('
'<wbr><span class="parameter" id="f1=-param-value">'
'<span class="type-annotation">int</span> '
'<span class="parameter-name">value</span>'
r'</span>\)'
'</span>'),
],
);
}

// TODO(srawlins): Add rendering tests:
// * how inherited fields look on subclass page ('inherited' feature)
// * static fields
Expand Down