Migrate some additional libraries and their tests

Change-Id: I3aae14488fb5def205788a60b27d33211c88d330
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/193889
Reviewed-by: Konstantin Shcheglov <scheglov@google.com>
Commit-Queue: Brian Wilkerson <brianwilkerson@google.com>
This commit is contained in:
Brian Wilkerson 2021-04-02 20:02:22 +00:00 committed by commit-bot@chromium.org
parent a1697525c1
commit 744fa0ba2d
9 changed files with 81 additions and 87 deletions

View file

@ -2,8 +2,6 @@
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.
// @dart = 2.9
import 'package:analysis_server/src/utilities/strings.dart';
import 'package:analyzer/dart/ast/ast.dart';
import 'package:analyzer/dart/ast/token.dart';
@ -16,17 +14,22 @@ import 'package:analyzer_plugin/protocol/protocol_common.dart'
/// Organizer of imports (and other directives) in the [unit].
class ImportOrganizer {
final String initialCode;
final CompilationUnit unit;
final List<AnalysisError> errors;
final bool removeUnused;
String code;
String endOfLine;
bool hasUnresolvedIdentifierError;
String endOfLine = '\n';
bool hasUnresolvedIdentifierError = false;
ImportOrganizer(this.initialCode, this.unit, this.errors,
{this.removeUnused = true}) {
code = initialCode;
{this.removeUnused = true})
: code = initialCode {
endOfLine = getEOL(code);
hasUnresolvedIdentifierError = errors.any((error) {
return error.errorCode.isUnresolvedIdentifier;
@ -60,7 +63,7 @@ class ImportOrganizer {
/// Organize all [Directive]s.
void _organizeDirectives() {
var lineInfo = unit.lineInfo;
var lineInfo = unit.lineInfo ?? LineInfo.fromContent(code);
var hasLibraryDirective = false;
var directives = <_DirectiveInfo>[];
for (var directive in unit.directives) {
@ -77,34 +80,34 @@ class ImportOrganizer {
final trailingComment =
getTrailingComment(unit, directive, lineInfo, end);
String leadingCommentText;
String? leadingCommentText;
if (leadingComment != null) {
leadingCommentText =
code.substring(leadingComment.offset, directive.offset);
offset = leadingComment.offset;
}
String trailingCommentText;
String? trailingCommentText;
if (trailingComment != null) {
trailingCommentText =
code.substring(directive.end, trailingComment.end);
end = trailingComment.end;
}
String documentationText;
if (directive.documentationComment != null) {
String? documentationText;
var documentationComment = directive.documentationComment;
if (documentationComment != null) {
documentationText = code.substring(
directive.documentationComment.offset,
directive.documentationComment.end);
documentationComment.offset, documentationComment.end);
}
String annotationText;
if (directive.metadata.beginToken != null) {
annotationText = code.substring(
directive.metadata.beginToken.offset,
directive.metadata.endToken.end);
String? annotationText;
var beginToken = directive.metadata.beginToken;
var endToken = directive.metadata.endToken;
if (beginToken != null && endToken != null) {
annotationText = code.substring(beginToken.offset, endToken.end);
}
var text = code.substring(
directive.firstTokenAfterCommentAndMetadata.offset,
directive.end);
var uriContent = directive.uri.stringValue;
var uriContent = directive.uri.stringValue ?? '';
directives.add(
_DirectiveInfo(
directive,
@ -131,7 +134,7 @@ class ImportOrganizer {
// Without a library directive, the library comment is the comment of the
// first directive.
_DirectiveInfo libraryDocumentationDirective;
_DirectiveInfo? libraryDocumentationDirective;
if (!hasLibraryDirective && directives.isNotEmpty) {
libraryDocumentationDirective = directives.first;
}
@ -142,7 +145,8 @@ class ImportOrganizer {
String directivesCode;
{
var sb = StringBuffer();
if (libraryDocumentationDirective?.documentationText != null) {
if (libraryDocumentationDirective != null &&
libraryDocumentationDirective.documentationText != null) {
sb.write(libraryDocumentationDirective.documentationText);
sb.write(endOfLine);
}
@ -185,7 +189,7 @@ class ImportOrganizer {
code = beforeDirectives + directivesCode + afterDirectives;
}
static _DirectivePriority getDirectivePriority(UriBasedDirective directive) {
static _DirectivePriority? getDirectivePriority(UriBasedDirective directive) {
var uriContent = directive.uri.stringValue ?? '';
if (directive is ImportDirective) {
if (uriContent.startsWith('dart:')) {
@ -230,28 +234,30 @@ class ImportOrganizer {
/// Leading comments for the first directive in a file are considered library
/// comments and not returned unless they contain blank lines, in which case
/// only the last part of the comment will be returned.
static Token getLeadingComment(
static Token? getLeadingComment(
CompilationUnit unit, UriBasedDirective directive, LineInfo lineInfo) {
if (directive.beginToken.precedingComments == null) {
return null;
}
var firstComment = directive.beginToken.precedingComments;
Token? firstComment = directive.beginToken.precedingComments;
var comment = firstComment;
var nextComment = comment?.next;
// Don't connect comments that have a blank line between them
while (comment.next != null) {
while (comment != null && nextComment != null) {
var currentLine = lineInfo.getLocation(comment.offset).lineNumber;
var nextLine = lineInfo.getLocation(comment.next.offset).lineNumber;
var nextLine = lineInfo.getLocation(nextComment.offset).lineNumber;
if (nextLine - currentLine > 1) {
firstComment = comment.next;
firstComment = nextComment;
}
comment = comment.next;
comment = nextComment;
nextComment = comment.next;
}
// Check if the comment is the first comment in the document
if (firstComment != unit.beginToken.precedingComments) {
var previousDirectiveLine =
lineInfo.getLocation(directive.beginToken.previous.end).lineNumber;
lineInfo.getLocation(directive.beginToken.previous!.end).lineNumber;
// Skip over any comments on the same line as the previous directive
// as they will be attached to the end of it.
@ -271,10 +277,10 @@ class ImportOrganizer {
///
/// To be considered a trailing comment, the comment must be on the same line
/// as the directive.
static Token getTrailingComment(CompilationUnit unit,
static Token? getTrailingComment(CompilationUnit unit,
UriBasedDirective directive, LineInfo lineInfo, int end) {
var line = lineInfo.getLocation(end).lineNumber;
Token comment = directive.endToken.next.precedingComments;
Token? comment = directive.endToken.next!.precedingComments;
while (comment != null) {
if (lineInfo.getLocation(comment.offset).lineNumber == line) {
return comment;
@ -288,11 +294,11 @@ class ImportOrganizer {
class _DirectiveInfo implements Comparable<_DirectiveInfo> {
final UriBasedDirective directive;
final _DirectivePriority priority;
final String leadingCommentText;
final String documentationText;
final String annotationText;
final String? leadingCommentText;
final String? documentationText;
final String? annotationText;
final String uri;
final String trailingCommentText;
final String? trailingCommentText;
/// The offset of the first token, usually the keyword but may include leading comments.
final int offset;

View file

@ -2,8 +2,6 @@
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.
// @dart = 2.9
import 'package:analysis_server/src/services/correction/organize_imports.dart';
import 'package:analysis_server/src/utilities/strings.dart';
import 'package:analyzer/dart/ast/ast.dart';
@ -44,12 +42,14 @@ class MemberSorter {
];
final String initialCode;
final CompilationUnit unit;
String code;
String endOfLine;
MemberSorter(this.initialCode, this.unit) {
code = initialCode;
final CompilationUnit unit;
String code;
String endOfLine = '\n';
MemberSorter(this.initialCode, this.unit) : code = initialCode {
endOfLine = getEOL(code);
}
@ -108,17 +108,18 @@ class MemberSorter {
} else {
name = nameNode.name;
}
}
if (member is FieldDeclaration) {
} else if (member is FieldDeclaration) {
var fieldDeclaration = member;
List<VariableDeclaration> fields = fieldDeclaration.fields.variables;
if (fields.isNotEmpty) {
kind = _MemberKind.CLASS_FIELD;
isStatic = fieldDeclaration.isStatic;
name = fields[0].name.name;
} else {
// Don't sort members if there are errors in the code.
return;
}
}
if (member is MethodDeclaration) {
} else if (member is MethodDeclaration) {
var method = member;
isStatic = method.isStatic;
name = method.name.name;
@ -131,14 +132,14 @@ class MemberSorter {
} else {
kind = _MemberKind.CLASS_METHOD;
}
} else {
throw StateError('Unsupported class of member: ${member.runtimeType}');
}
if (name != null) {
var item = _PriorityItem.forName(isStatic, name, kind);
var offset = member.offset;
var length = member.length;
var text = code.substring(offset, offset + length);
members.add(_MemberInfo(item, name, offset, length, text));
}
var item = _PriorityItem.forName(isStatic, name, kind);
var offset = member.offset;
var length = member.length;
var text = code.substring(offset, offset + length);
members.add(_MemberInfo(item, name, offset, length, text));
}
// do sort
_sortAndReorderMembers(members);
@ -203,15 +204,18 @@ class MemberSorter {
kind = _MemberKind.UNIT_VARIABLE;
}
name = variables[0].name.name;
} else {
// Don't sort members if there are errors in the code.
return;
}
} else {
throw StateError('Unsupported class of member: ${member.runtimeType}');
}
if (name != null) {
var item = _PriorityItem.forName(false, name, kind);
var offset = member.offset;
var length = member.length;
var text = code.substring(offset, offset + length);
members.add(_MemberInfo(item, name, offset, length, text));
}
var item = _PriorityItem.forName(false, name, kind);
var offset = member.offset;
var length = member.length;
var text = code.substring(offset, offset + length);
members.add(_MemberInfo(item, name, offset, length, text));
}
// do sort
_sortAndReorderMembers(members);

View file

@ -2,8 +2,6 @@
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.
// @dart = 2.9
import 'dart:io';
/// A class that can return memory and cpu usage information for a given
@ -11,12 +9,12 @@ import 'dart:io';
abstract class ProcessProfiler {
ProcessProfiler._();
Future<UsageInfo> getProcessUsage(int processId);
Future<UsageInfo?> getProcessUsage(int processId);
/// Return a [ProcessProfiler] instance suitable for the current host
/// platform. This can return `null` if we're not able to gather memory and
/// cpu information for the current platform.
static ProcessProfiler getProfilerForPlatform() {
static ProcessProfiler? getProfilerForPlatform() {
if (Platform.isLinux || Platform.isMacOS) {
return _PosixProcessProfiler();
}
@ -48,7 +46,7 @@ class _PosixProcessProfiler extends ProcessProfiler {
_PosixProcessProfiler() : super._();
@override
Future<UsageInfo> getProcessUsage(int processId) {
Future<UsageInfo?> getProcessUsage(int processId) {
try {
// Execution time is typically 2-4ms.
var future =
@ -65,7 +63,7 @@ class _PosixProcessProfiler extends ProcessProfiler {
}
}
UsageInfo _parse(String psResults) {
UsageInfo? _parse(String psResults) {
try {
// " 0.0 378940"
var line = psResults.split('\n').first.trim();

View file

@ -2,8 +2,6 @@
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.
// @dart = 2.9
import 'package:analysis_server/src/services/correction/organize_imports.dart';
import 'package:analyzer/error/error.dart';
import 'package:analyzer_plugin/protocol/protocol_common.dart'
@ -21,7 +19,7 @@ void main() {
@reflectiveTest
class OrganizeDirectivesTest extends AbstractSingleUnitTest {
List<AnalysisError> testErrors;
late List<AnalysisError> testErrors;
Future<void> test_docComment_beforeDirective_hasUnresolvedIdentifier() async {
await _computeUnitAndErrors(r'''
@ -584,7 +582,7 @@ import 'package:b/a.dart';''');
Future<void> _computeUnitAndErrors(String code) async {
addTestSource(code);
var result = await session.getResolvedUnit(testFile);
testUnit = result.unit;
testUnit = result.unit!;
testErrors = result.errors;
}
}

View file

@ -2,8 +2,6 @@
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.
// @dart = 2.9
import 'package:analysis_server/src/services/correction/sort_members.dart';
import 'package:analyzer_plugin/protocol/protocol_common.dart';
import 'package:test/test.dart';

View file

@ -2,8 +2,6 @@
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.
// @dart = 2.9
import 'dart:io';
import 'package:analysis_server/src/utilities/profiling.dart';
@ -21,10 +19,9 @@ void main() {
});
test('getProcessUsage', () async {
var profiler = ProcessProfiler.getProfilerForPlatform();
var info = await profiler.getProcessUsage(pid);
var profiler = ProcessProfiler.getProfilerForPlatform()!;
var info = (await profiler.getProcessUsage(pid))!;
expect(info, isNotNull);
expect(info.cpuPercentage, greaterThanOrEqualTo(0.0));
expect(info.memoryKB, greaterThanOrEqualTo(0));
});

View file

@ -2,8 +2,6 @@
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.
// @dart = 2.9
import 'package:test_reflective_loader/test_reflective_loader.dart';
import 'flutter_test.dart' as flutter_test;

View file

@ -2,8 +2,6 @@
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.
// @dart = 2.9
import 'package:analysis_server/src/services/correction/sort_members.dart';
import 'package:analyzer/dart/analysis/analysis_context_collection.dart';
import 'package:analyzer/dart/analysis/results.dart';
@ -11,7 +9,6 @@ import 'package:analyzer/dart/analysis/session.dart';
import 'package:analyzer/file_system/file_system.dart';
import 'package:analyzer/file_system/physical_file_system.dart';
import 'package:analyzer_utilities/package_root.dart';
import 'package:meta/meta.dart';
import 'package:test/test.dart';
void main() {
@ -33,8 +30,8 @@ void main() {
}
void buildTests({
@required String packagePath,
@required List<String> excludedPaths,
required String packagePath,
required List<String> excludedPaths,
}) {
var provider = PhysicalResourceProvider.INSTANCE;
var pkgRootPath = provider.pathContext.normalize(packageRoot);

View file

@ -2,8 +2,6 @@
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.
// @dart = 2.9
import 'package:analyzer/file_system/file_system.dart';
import 'package:analyzer/file_system/physical_file_system.dart';
import 'package:analyzer_utilities/package_root.dart' as package_root;
@ -20,7 +18,7 @@ void main() {
}
class _VerifyTests extends VerifyTests {
_VerifyTests(String testDirPath, {List<String> excludedPaths})
_VerifyTests(String testDirPath, {List<String>? excludedPaths})
: super(testDirPath, excludedPaths: excludedPaths);
@override