Skip to content

Commit

Permalink
Version 3.1.0-197.0.dev
Browse files Browse the repository at this point in the history
Merge ecec8e7 into dev
  • Loading branch information
Dart CI committed Jun 12, 2023
2 parents 0c4f3ae + ecec8e7 commit a4611bd
Show file tree
Hide file tree
Showing 17 changed files with 276 additions and 239 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@ import 'package:analysis_server/src/services/correction/util.dart';
import 'package:analysis_server/src/utilities/extensions/object.dart';
import 'package:analyzer/dart/analysis/features.dart';
import 'package:analyzer/dart/ast/ast.dart';
import 'package:analyzer/dart/element/element.dart';
import 'package:analyzer/dart/element/nullability_suffix.dart';
import 'package:analyzer/dart/element/type.dart';
import 'package:analyzer_plugin/utilities/change_builder/change_builder_core.dart';
import 'package:analyzer_plugin/utilities/change_builder/change_builder_dart.dart';
import 'package:analyzer_plugin/utilities/fixes/fixes.dart';
Expand Down Expand Up @@ -84,6 +86,7 @@ class CreateConstructorForFinalFields extends CorrectionProducer {
final fixContext = _FixContext(
builder: builder,
containerName: className,
superType: superType,
location: targetLocation,
variableLists: variableLists,
);
Expand Down Expand Up @@ -115,6 +118,7 @@ class CreateConstructorForFinalFields extends CorrectionProducer {
fixContext: _FixContext(
builder: builder,
containerName: enumName,
superType: null,
location: targetLocation,
variableLists: variableLists,
),
Expand Down Expand Up @@ -230,22 +234,40 @@ class CreateConstructorForFinalFields extends CorrectionProducer {
switch (_style) {
case _Style.requiredNamed:
builder.write('{');
fields.forEachIndexed((index, field) {
if (index > 0) {
var hasWritten = false;
final superNamed = fixContext.superNamed;
if (superNamed != null) {
for (final formalParameter in superNamed) {
if (hasWritten) {
builder.write(', ');
}
if (formalParameter.isRequiredNamed) {
builder.write('required ');
}
builder.write('super.');
builder.write(formalParameter.name);
hasWritten = true;
}
}
for (final field in fields) {
if (hasWritten) {
builder.write(', ');
}
builder.write('required this.');
builder.write(field.name);
});
hasWritten = true;
}
builder.write('}');
case _Style.requiredPositional:
fields.forEachIndexed((index, field) {
if (index > 0) {
var hasWritten = false;
for (final field in fields) {
if (hasWritten) {
builder.write(', ');
}
builder.write('this.');
builder.write(field.name);
});
hasWritten = true;
}
}
builder.write(');');
builder.write(location.suffix);
Expand Down Expand Up @@ -301,15 +323,27 @@ class _Field {
class _FixContext {
final ChangeBuilder builder;
final String containerName;
final InterfaceType? superType;
final InsertionLocation location;
final List<VariableDeclarationList> variableLists;

_FixContext({
required this.builder,
required this.containerName,
required this.superType,
required this.location,
required this.variableLists,
});

List<ParameterElement>? get superNamed {
final superConstructor = superType?.constructors.singleOrNull;
if (superConstructor != null) {
final superAll = superConstructor.parameters;
final superNamed = superAll.where((e) => e.isNamed).toList();
return superNamed.length == superAll.length ? superNamed : null;
}
return null;
}
}

enum _Style {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,7 @@ class RemoveLeadingUnderscore extends CorrectionProducer {
final node = this.node;
final Token? nameToken;
final Element? element;
if (node is DeclaredSimpleIdentifier) {
nameToken = node.token;
element = node.staticElement;
} else if (node is SimpleIdentifier) {
if (node is SimpleIdentifier) {
nameToken = node.token;
element = node.staticElement;
} else if (node is FormalParameter) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,72 @@ class Test {
''');
}

Future<void> test_class_hasSuperClass_withOptionalNamed() async {
await resolveTestCode('''
class A {
final int? f11;
final int? f12;
A({this.f11, this.f12})
}
class B extends A {
final int f21;
final int f22;
}
''');
await assertHasFix('''
class A {
final int? f11;
final int? f12;
A({this.f11, this.f12})
}
class B extends A {
final int f21;
final int f22;
B({super.f11, super.f12, required this.f21, required this.f22});
}
''', errorFilter: (error) {
return error.message.contains("'f21'");
});
}

Future<void> test_class_hasSuperClass_withRequiredNamed() async {
await resolveTestCode('''
class A {
final int f11;
final int f12;
A({required this.f11, required this.f12})
}
class B extends A {
final int f21;
final int f22;
}
''');
await assertHasFix('''
class A {
final int f11;
final int f12;
A({required this.f11, required this.f12})
}
class B extends A {
final int f21;
final int f22;
B({required super.f11, required super.f12, required this.f21, required this.f22});
}
''', errorFilter: (error) {
return error.message.contains("'f21'");
});
}

Future<void> test_class_lint_sortConstructorsFirst() async {
createAnalysisOptionsFile(lints: [LintNames.sort_constructors_first]);
await resolveTestCode('''
Expand Down
72 changes: 32 additions & 40 deletions pkg/analyzer/lib/src/dart/analysis/driver.dart
Original file line number Diff line number Diff line change
Expand Up @@ -772,12 +772,10 @@ class AnalysisDriver implements AnalysisDriverGeneric {
}

var fileOr = _fsState.getFileForUri(uriObj);
return fileOr.map(
(file) async {
if (file == null) {
return CannotResolveUriResult();
}

switch (fileOr) {
case null:
return CannotResolveUriResult();
case UriResolutionFile(:final file):
final kind = file.kind;
if (kind is LibraryFileKind) {
} else if (kind is AugmentationFileKind) {
Expand All @@ -802,14 +800,12 @@ class AnalysisDriver implements AnalysisDriverGeneric {

// Should not happen.
return UnspecifiedInvalidResult();
},
(externalLibrary) async {
final uri = externalLibrary.source.uri;
case UriResolutionExternalLibrary(:final source):
final uri = source.uri;
// TODO(scheglov) Check if the source is not for library.
var element = libraryContext.getLibraryElement(uri);
return LibraryElementResultImpl(element);
},
);
}
}

/// Return a [ParsedLibraryResult] for the library with the given [path].
Expand Down Expand Up @@ -854,17 +850,14 @@ class AnalysisDriver implements AnalysisDriverGeneric {
/// Return a [ParsedLibraryResult] for the library with the given [uri].
SomeParsedLibraryResult getParsedLibraryByUri(Uri uri) {
var fileOr = _fsState.getFileForUri(uri);
return fileOr.map(
(file) {
if (file == null) {
return CannotResolveUriResult();
}
switch (fileOr) {
case null:
return CannotResolveUriResult();
case UriResolutionFile(:final file):
return getParsedLibrary(file.path);
},
(externalLibrary) {
case UriResolutionExternalLibrary():
return UriOfExternalLibraryResult();
},
);
}
}

/// Return a [Future] that completes with a [ResolvedLibraryResult] for the
Expand Down Expand Up @@ -922,19 +915,16 @@ class AnalysisDriver implements AnalysisDriverGeneric {
/// state (including new states of the files previously reported using
/// [changeFile]), prior to the next time the analysis state transitions
/// to "idle".
Future<SomeResolvedLibraryResult> getResolvedLibraryByUri(Uri uri) {
Future<SomeResolvedLibraryResult> getResolvedLibraryByUri(Uri uri) async {
var fileOr = _fsState.getFileForUri(uri);
return fileOr.map(
(file) async {
if (file == null) {
return CannotResolveUriResult();
}
switch (fileOr) {
case null:
return CannotResolveUriResult();
case UriResolutionFile(:final file):
return getResolvedLibrary(file.path);
},
(externalLibrary) async {
case UriResolutionExternalLibrary():
return UriOfExternalLibraryResult();
},
);
}
}

/// Return a [Future] that completes with a [SomeResolvedUnitResult] for the
Expand Down Expand Up @@ -1631,13 +1621,14 @@ class AnalysisDriver implements AnalysisDriverGeneric {
}
_hasDartCoreDiscovered = true;

_fsState.getFileForUri(uriCache.parse('dart:core')).map(
(file) {
final kind = file?.kind as LibraryFileKind;
final dartCoreUri = uriCache.parse('dart:core');
final dartCoreResolution = _fsState.getFileForUri(dartCoreUri);
if (dartCoreResolution is UriResolutionFile) {
final kind = dartCoreResolution.file.kind;
if (kind is LibraryFileKind) {
kind.discoverReferencedFiles();
},
(externalLibrary) {},
);
}
}
}

void _discoverLibraries() {
Expand Down Expand Up @@ -1758,10 +1749,11 @@ class AnalysisDriver implements AnalysisDriverGeneric {
bool _hasLibraryByUri(String uriStr) {
var uri = uriCache.parse(uriStr);
var fileOr = _fsState.getFileForUri(uri);
return fileOr.map(
(file) => file != null && file.exists,
(_) => true,
);
return switch (fileOr) {
null => false,
UriResolutionFile(:final file) => file.exists,
UriResolutionExternalLibrary() => true,
};
}

bool _isAbsolutePath(String path) {
Expand Down
Loading

0 comments on commit a4611bd

Please sign in to comment.