Skip to content

Commit 5e2a950

Browse files
committed
Support accessing the documentation comment of an ExtensionElement
Change-Id: If433a7e795369a195a917653f303c71df84a4a0c Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/110000 Reviewed-by: Konstantin Shcheglov <[email protected]> Reviewed-by: Phil Quitslund <[email protected]>
1 parent 9c14862 commit 5e2a950

File tree

5 files changed

+77
-15
lines changed

5 files changed

+77
-15
lines changed

pkg/analyzer/lib/src/dart/element/element.dart

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5019,6 +5019,19 @@ class ExtensionElementImpl extends ElementImpl
50195019
@override
50205020
String get displayName => name;
50215021

5022+
@override
5023+
String get documentationComment {
5024+
if (linkedNode != null) {
5025+
var context = enclosingUnit.linkedContext;
5026+
var comment = context.getDocumentationComment(linkedNode);
5027+
return getCommentNodeRawText(comment);
5028+
}
5029+
if (_unlinkedExtension != null) {
5030+
return _unlinkedExtension.documentationComment?.text;
5031+
}
5032+
return super.documentationComment;
5033+
}
5034+
50225035
@override
50235036
TypeParameterizedElementMixin get enclosingTypeParameterContext => null;
50245037

pkg/analyzer/lib/src/summary2/linked_unit_context.dart

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -256,6 +256,9 @@ class LinkedUnitContext {
256256
} else if (node is EnumDeclaration) {
257257
LazyEnumDeclaration.readDocumentationComment(this, node);
258258
return node.documentationComment;
259+
} else if (node is ExtensionDeclaration) {
260+
LazyExtensionDeclaration.readDocumentationComment(this, node);
261+
return node.documentationComment;
259262
} else if (node is FunctionDeclaration) {
260263
LazyFunctionDeclaration.readDocumentationComment(this, node);
261264
return node.documentationComment;

pkg/analyzer/test/src/summary/element_text.dart

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -331,6 +331,26 @@ class _ElementWriter {
331331
}
332332
}
333333

334+
void writeExtensionElement(ExtensionElement e) {
335+
writeDocumentation(e);
336+
writeMetadata(e, '', '\n');
337+
338+
buffer.write('extension ');
339+
writeName(e);
340+
writeCodeRange(e);
341+
writeTypeParameterElements(e.typeParameters);
342+
if (e.extendedType != null) {
343+
buffer.write(' on ');
344+
writeType(e.extendedType);
345+
}
346+
347+
buffer.writeln(' {');
348+
e.fields.forEach(writePropertyInducingElement);
349+
e.accessors.forEach(writePropertyAccessorElement);
350+
e.methods.forEach(writeMethodElement);
351+
buffer.writeln('}');
352+
}
353+
334354
void writeFunctionElement(FunctionElement e) {
335355
writeDocumentation(e);
336356
writeMetadata(e, '', '\n');
@@ -1016,6 +1036,7 @@ class _ElementWriter {
10161036
e.enums.forEach(writeClassElement);
10171037
e.types.forEach(writeClassElement);
10181038
e.mixins.forEach(writeClassElement);
1039+
e.extensions.forEach(writeExtensionElement);
10191040
e.topLevelVariables.forEach(writePropertyInducingElement);
10201041
e.accessors.forEach(writePropertyAccessorElement);
10211042
e.functions.forEach(writeFunctionElement);

pkg/analyzer/test/src/summary/resynthesize_ast2_test.dart

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,12 @@ class ResynthesizeAst2Test extends ResynthesizeTestStrategyTwoPhase
136136
return elementFactory.libraryOfUri('${source.uri}');
137137
}
138138

139+
@failingTest
140+
@override
141+
test_extension_documented_tripleSlash() async {
142+
await super.test_extension_documented_tripleSlash();
143+
}
144+
139145
void _addLibraryUnits(
140146
Source definingSource,
141147
CompilationUnit definingUnit,

pkg/analyzer/test/src/summary/resynthesize_common.dart

Lines changed: 34 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -208,6 +208,9 @@ mixin GetElementTestCases implements ResynthesizeTestHelpers {
208208
mixin ResynthesizeTestCases implements ResynthesizeTestHelpers {
209209
FeatureSet get disableNnbd => FeatureSet.forTesting(sdkVersion: '2.2.2');
210210

211+
FeatureSet get enableExtensionMethods =>
212+
FeatureSet.forTesting(additionalFeatures: [Feature.extension_methods]);
213+
211214
FeatureSet get enableNnbd =>
212215
FeatureSet.forTesting(additionalFeatures: [Feature.non_nullable]);
213216

@@ -5851,6 +5854,22 @@ class C<T> {
58515854
''');
58525855
}
58535856

5857+
test_extension_documented_tripleSlash() async {
5858+
featureSet = enableExtensionMethods;
5859+
var library = await checkLibrary('''
5860+
/// aaa
5861+
/// bbbb
5862+
/// cc
5863+
extension E on int {}''');
5864+
checkElementText(library, r'''
5865+
/// aaa
5866+
/// bbbb
5867+
/// cc
5868+
extension E on int {
5869+
}
5870+
''');
5871+
}
5872+
58545873
test_field_covariant() async {
58555874
var library = await checkLibrary('''
58565875
class C {
@@ -9580,21 +9599,6 @@ int y;
95809599
''');
95819600
}
95829601

9583-
test_type_inference_fieldFormal_depends_onField() async {
9584-
var library = await checkLibrary('''
9585-
class A<T> {
9586-
var f = 0;
9587-
A(this.f);
9588-
}
9589-
''');
9590-
checkElementText(library, r'''
9591-
class A<T> {
9592-
int f;
9593-
A(int this.f);
9594-
}
9595-
''');
9596-
}
9597-
95989602
test_type_inference_field_depends_onFieldFormal() async {
95999603
var library = await checkLibrary('''
96009604
class A<T> {
@@ -9618,6 +9622,21 @@ class B {
96189622
''');
96199623
}
96209624

9625+
test_type_inference_fieldFormal_depends_onField() async {
9626+
var library = await checkLibrary('''
9627+
class A<T> {
9628+
var f = 0;
9629+
A(this.f);
9630+
}
9631+
''');
9632+
checkElementText(library, r'''
9633+
class A<T> {
9634+
int f;
9635+
A(int this.f);
9636+
}
9637+
''');
9638+
}
9639+
96219640
test_type_inference_multiplyDefinedElement() async {
96229641
addLibrarySource('/a.dart', 'class C {}');
96239642
addLibrarySource('/b.dart', 'class C {}');

0 commit comments

Comments
 (0)