Skip to content

Commit ea86e0f

Browse files
scheglovCommit Queue
authored andcommitted
Elements. Return nullable 'LibraryFragment?' from 'get libraryFragment'.
We can have synthetic 'FormalParameterFragment' and 'TypeParameterFragment' that don't have any enclosing 'LibraryFragment'. Change-Id: Icc1a47c308e55b93ed6a2aad70788058e26ee803 Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/393500 Reviewed-by: Brian Wilkerson <[email protected]> Commit-Queue: Konstantin Shcheglov <[email protected]>
1 parent 3e6e13d commit ea86e0f

File tree

9 files changed

+28
-20
lines changed

9 files changed

+28
-20
lines changed

pkg/analysis_server/lib/src/lsp/handlers/code_lens/abstract_code_lens_provider.dart

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,13 +41,13 @@ abstract class AbstractCodeLensProvider
4141

4242
// Map the source onto a URI and only return this item if the client
4343
// can handle the URI.
44-
var source = fragment.libraryFragment.source;
44+
var source = fragment.libraryFragment!.source;
4545
var uri = server.uriConverter.toClientUri(source.fullName);
4646
if (!server.uriConverter.supportedSchemes.contains(uri.scheme)) {
4747
return null;
4848
}
4949

50-
var lineInfo = fragment.libraryFragment.lineInfo;
50+
var lineInfo = fragment.libraryFragment!.lineInfo;
5151
return Location(uri: uri, range: toRange(lineInfo, nameOffset, nameLength));
5252
}
5353

pkg/analysis_server/lib/src/lsp/mapping.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -506,7 +506,7 @@ lsp.Location? fragmentToLocation(
506506
return null;
507507
}
508508

509-
var libraryFragment = fragment.libraryFragment;
509+
var libraryFragment = fragment.libraryFragment!;
510510
var sourcePath = libraryFragment.source.fullName;
511511

512512
var nameOffset = fragment.nameOffset2;

pkg/analysis_server/lib/src/protocol_server.dart

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -456,7 +456,7 @@ Location _locationForArgs(
456456

457457
/// Creates a new [Location].
458458
Location _locationForArgs2(engine.Fragment fragment, engine.SourceRange range) {
459-
var lineInfo = fragment.libraryFragment.lineInfo;
459+
var lineInfo = fragment.libraryFragment!.lineInfo;
460460

461461
var startLocation = lineInfo.getLocation(range.offset);
462462
var endLocation = lineInfo.getLocation(range.end);
@@ -467,7 +467,7 @@ Location _locationForArgs2(engine.Fragment fragment, engine.SourceRange range) {
467467
var endColumn = endLocation.columnNumber;
468468

469469
return Location(
470-
fragment.libraryFragment.source.fullName,
470+
fragment.libraryFragment!.source.fullName,
471471
range.offset,
472472
range.length,
473473
startLine,

pkg/analysis_server/lib/src/services/correction/dart/create_method.dart

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -163,15 +163,15 @@ class CreateMethod extends ResolvedCorrectionProducer {
163163
target.element?.kind == ElementKind.MIXIN;
164164
}
165165
// Use different utils.
166-
var targetPath = targetFragment.libraryFragment.source.fullName;
166+
var targetPath = targetFragment.libraryFragment!.source.fullName;
167167
var targetResolveResult = await unitResult.session.getResolvedUnit(
168168
targetPath,
169169
);
170170
if (targetResolveResult is! ResolvedUnitResult) {
171171
return;
172172
}
173173
}
174-
var targetSource = targetFragment?.libraryFragment.source;
174+
var targetSource = targetFragment?.libraryFragment!.source;
175175
if (targetSource == null) {
176176
return;
177177
}

pkg/analyzer/lib/dart/element/element2.dart

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -646,6 +646,9 @@ abstract class ExecutableFragment implements FunctionTypedFragment {
646646
/// Whether the body is marked as being synchronous.
647647
bool get isSynchronous;
648648

649+
@override
650+
LibraryFragment get libraryFragment;
651+
649652
@override
650653
ExecutableFragment? get nextFragment;
651654

@@ -929,7 +932,7 @@ abstract class Fragment {
929932
/// The library fragment that contains this fragment.
930933
///
931934
/// This will be the fragment itself if it is a library fragment.
932-
LibraryFragment get libraryFragment;
935+
LibraryFragment? get libraryFragment;
933936

934937
/// The name of the fragment.
935938
///
@@ -1157,6 +1160,9 @@ abstract class InstanceFragment
11571160
/// If `true`, the declaration has the explicit `augment` modifier.
11581161
bool get isAugmentation;
11591162

1163+
@override
1164+
LibraryFragment get libraryFragment;
1165+
11601166
/// The methods declared in this fragment.
11611167
List<MethodFragment> get methods2;
11621168

@@ -2126,6 +2132,9 @@ abstract class PropertyInducingFragment
21262132
// TODO(brianwilkerson): Should synthetic elements have a fragment?
21272133
bool get isSynthetic;
21282134

2135+
@override
2136+
LibraryFragment get libraryFragment;
2137+
21292138
@override
21302139
PropertyInducingFragment? get nextFragment;
21312140

pkg/analyzer/lib/src/dart/analysis/session_helper.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ class AnalysisSessionHelper {
6262
/// that is not a part of a library.
6363
Future<ElementDeclarationResult?> getElementDeclaration2(
6464
Fragment fragment) async {
65-
var libraryPath = fragment.libraryFragment.source.fullName;
65+
var libraryPath = fragment.libraryFragment!.source.fullName;
6666
var resolvedLibrary = await _getResolvedLibrary(libraryPath);
6767
return resolvedLibrary?.getElementDeclaration2(fragment);
6868
}

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

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8547,8 +8547,8 @@ class ParameterElementImpl extends VariableElementImpl
85478547
ElementKind get kind => ElementKind.PARAMETER;
85488548

85498549
@override
8550-
LibraryFragment get libraryFragment =>
8551-
thisOrAncestorOfType<CompilationUnitElementImpl>() as LibraryFragment;
8550+
LibraryFragment? get libraryFragment =>
8551+
thisOrAncestorOfType<CompilationUnitElementImpl>();
85528552

85538553
@override
85548554
// TODO(augmentations): Support chaining between the fragments.
@@ -9386,7 +9386,7 @@ abstract class PropertyInducingElementImpl
93869386

93879387
@override
93889388
LibraryFragment get libraryFragment =>
9389-
thisOrAncestorOfType<CompilationUnitElement>() as LibraryFragment;
9389+
thisOrAncestorOfType<CompilationUnitElementImpl>()!;
93909390

93919391
@override
93929392
PropertyInducingFragment? get nextFragment =>
@@ -10301,8 +10301,8 @@ class TypeParameterElementImpl extends ElementImpl
1030110301
ElementKind get kind => ElementKind.TYPE_PARAMETER;
1030210302

1030310303
@override
10304-
LibraryFragment get libraryFragment =>
10305-
thisOrAncestorOfType<CompilationUnitElementImpl>() as LibraryFragment;
10304+
LibraryFragment? get libraryFragment =>
10305+
thisOrAncestorOfType<CompilationUnitElementImpl>();
1030610306

1030710307
@override
1030810308
String get name {

pkg/analyzer/lib/src/lint/linter.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -334,7 +334,7 @@ class LintRuleUnitContext {
334334

335335
/// The library fragment representing the compilation unit.
336336
@experimental
337-
LibraryFragment get libraryFragment => unit as LibraryFragment;
337+
LibraryFragment get libraryFragment => unit.declaredFragment!;
338338
}
339339

340340
/// An error listener that only records whether any constant related errors have

pkg/linter/lib/src/rules/analyzer_use_new_elements.dart

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,17 +20,16 @@ bool _isOldModelElement(Element2? element) {
2020
return false;
2121
}
2222

23-
// Skip synthetic formal parameters.
24-
if (element is FormalParameterElement && element.enclosingElement2 == null) {
25-
return false;
26-
}
27-
2823
var firstFragment = element.firstFragment;
2924
if (firstFragment == null) {
3025
return false;
3126
}
3227

3328
var libraryFragment = firstFragment.libraryFragment;
29+
if (libraryFragment == null) {
30+
return false;
31+
}
32+
3433
var uriStr = libraryFragment.source.uri.toString();
3534

3635
if (element is InstanceElement2) {

0 commit comments

Comments
 (0)