Skip to content

Commit 9a67968

Browse files
DanTupCommit Queue
authored andcommitted
[analyzer] Simplify documentationCommentOrNull and metadataOrEmpty
Change-Id: I8b903292b635714d9a7f52e7a212c6354ae6a8c4 Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/391404 Reviewed-by: Samuel Rawlins <[email protected]> Reviewed-by: Brian Wilkerson <[email protected]> Commit-Queue: Brian Wilkerson <[email protected]>
1 parent ef04f5d commit 9a67968

File tree

3 files changed

+27
-15
lines changed

3 files changed

+27
-15
lines changed

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

Lines changed: 18 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -10710,29 +10710,32 @@ class _Sentinel {
1071010710
}
1071110711

1071210712
extension on Fragment {
10713+
/// The content of the documentation comment (including delimiters) for this
10714+
/// element or fragment.
10715+
///
10716+
/// If the receiver is an element that has fragments, the comment will be a
10717+
/// concatenation of the comments from all of the fragments.
10718+
///
10719+
/// Returns `null` if the receiver does not have or does not support
10720+
/// documentation.
1071310721
String? get documentationCommentOrNull {
10714-
// TODO(brianwilkerson): I think that all fragments are annotatable. If
10715-
// that's true then this getter isn't necessary and should be removed.
1071610722
return switch (this) {
10717-
LibraryFragment(:var documentationComment) => documentationComment,
10718-
TypeDefiningFragment(:var documentationComment) => documentationComment,
10719-
TypeParameterizedFragment(:var documentationComment) =>
10720-
documentationComment,
10721-
FormalParameterFragment(:var documentationComment) =>
10722-
documentationComment,
10723+
Annotatable(:var documentationComment) => documentationComment,
1072310724
_ => null,
1072410725
};
1072510726
}
1072610727

10728+
/// The metadata associated with the element or fragment.
10729+
///
10730+
/// If the receiver is an element that has fragments, the list will include
10731+
/// all of the metadata from all of the fragments.
10732+
///
10733+
/// The list will be empty if the receiver does not have any metadata, does
10734+
/// not support metadata, or if the library containing this element has not
10735+
/// yet been fully resolved.
1072710736
Metadata get metadataOrEmpty {
10728-
// TODO(brianwilkerson): I think that all fragments are annotatable. If
10729-
// that's true then this getter isn't necessary and should be removed.
1073010737
return switch (this) {
10731-
LibraryFragment(:var metadata2) => metadata2,
10732-
PropertyInducingFragment(:var metadata2) => metadata2,
10733-
TypeDefiningFragment(:var metadata2) => metadata2,
10734-
TypeParameterizedFragment(:var metadata2) => metadata2,
10735-
FormalParameterFragment(:var metadata2) => metadata2,
10738+
Annotatable(:var metadata2) => metadata2,
1073610739
_ => MetadataImpl(-1, const [], () => null),
1073710740
};
1073810741
}

pkg/analyzer/test/src/summary/elements/offsets_test.dart

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4078,11 +4078,13 @@ library
40784078
topLevelVariables
40794079
hasDocComment
40804080
firstFragment: <testLibraryFragment>::@topLevelVariable::hasDocComment
4081+
documentationComment: /// Comment 1.\n/// Comment 2.
40814082
type: int
40824083
getter: <testLibraryFragment>::@getter::hasDocComment#element
40834084
setter: <testLibraryFragment>::@setter::hasDocComment#element
40844085
hasDocComment2
40854086
firstFragment: <testLibraryFragment>::@topLevelVariable::hasDocComment2
4087+
documentationComment: /// Comment 1.\n/// Comment 2.
40864088
type: int
40874089
getter: <testLibraryFragment>::@getter::hasDocComment2#element
40884090
setter: <testLibraryFragment>::@setter::hasDocComment2#element
@@ -4124,6 +4126,7 @@ library
41244126
setter: <testLibraryFragment>::@setter::hasAnnotation2#element
41254127
annotationThenComment
41264128
firstFragment: <testLibraryFragment>::@topLevelVariable::annotationThenComment
4129+
documentationComment: /// Comment 1.\n/// Comment 2.
41274130
metadata
41284131
Annotation
41294132
atSign: @ @112
@@ -4142,6 +4145,7 @@ library
41424145
setter: <testLibraryFragment>::@setter::annotationThenComment#element
41434146
annotationThenComment2
41444147
firstFragment: <testLibraryFragment>::@topLevelVariable::annotationThenComment2
4148+
documentationComment: /// Comment 1.\n/// Comment 2.
41454149
metadata
41464150
Annotation
41474151
atSign: @ @112
@@ -4160,6 +4164,7 @@ library
41604164
setter: <testLibraryFragment>::@setter::annotationThenComment2#element
41614165
commentThenAnnotation
41624166
firstFragment: <testLibraryFragment>::@topLevelVariable::commentThenAnnotation
4167+
documentationComment: /// Comment 1.\n/// Comment 2.
41634168
metadata
41644169
Annotation
41654170
atSign: @ @234
@@ -4178,6 +4183,7 @@ library
41784183
setter: <testLibraryFragment>::@setter::commentThenAnnotation#element
41794184
commentThenAnnotation2
41804185
firstFragment: <testLibraryFragment>::@topLevelVariable::commentThenAnnotation2
4186+
documentationComment: /// Comment 1.\n/// Comment 2.
41814187
metadata
41824188
Annotation
41834189
atSign: @ @234
@@ -4196,6 +4202,7 @@ library
41964202
setter: <testLibraryFragment>::@setter::commentThenAnnotation2#element
41974203
commentAroundAnnotation
41984204
firstFragment: <testLibraryFragment>::@topLevelVariable::commentAroundAnnotation
4205+
documentationComment: /// Comment 2.
41994206
metadata
42004207
Annotation
42014208
atSign: @ @311
@@ -4214,6 +4221,7 @@ library
42144221
setter: <testLibraryFragment>::@setter::commentAroundAnnotation#element
42154222
commentAroundAnnotation2
42164223
firstFragment: <testLibraryFragment>::@topLevelVariable::commentAroundAnnotation2
4224+
documentationComment: /// Comment 2.
42174225
metadata
42184226
Annotation
42194227
atSign: @ @311

pkg/analyzer/test/src/summary/elements/top_level_variable_test.dart

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1583,6 +1583,7 @@ library
15831583
topLevelVariables
15841584
x
15851585
firstFragment: <testLibraryFragment>::@topLevelVariable::x
1586+
documentationComment: /**\n * Docs\n */
15861587
type: dynamic
15871588
getter: <testLibraryFragment>::@getter::x#element
15881589
setter: <testLibraryFragment>::@setter::x#element

0 commit comments

Comments
 (0)