Skip to content

Commit

Permalink
Version 3.1.0-34.0.dev
Browse files Browse the repository at this point in the history
Merge 45efccb into dev
  • Loading branch information
Dart CI committed Apr 22, 2023
2 parents 978d38d + 45efccb commit fd4797c
Show file tree
Hide file tree
Showing 58 changed files with 291 additions and 564 deletions.
2 changes: 1 addition & 1 deletion DEPS
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ vars = {
"http_parser_rev": "bbe37dd228ec59f58a73df4b328ef747757165c7",
"intl_rev": "a958db01c90d041f0cd357d1732ae9813b620ee0",
"json_rpc_2_rev": "5da270592006e4d43fd5a6ac736829f955881240",
"linter_rev": "2212d95ec16f8c243e3fb9e8cd7d2328c914a0f4", # disable rev_sdk_deps.dart
"linter_rev": "e36813b302817c7a4aaa39480d1654502c786b14", # disable rev_sdk_deps.dart
"lints_rev": "f09399a2574eb4e3f485881cf977fca72628f443",
"logging_rev": "787030a2b3d0d5d53ce57f1c7dc74f27ecb07b0b",
"markdown_rev": "d437c85188806fe2bfa4f3616159300ba9dc6d2a",
Expand Down
5 changes: 5 additions & 0 deletions pkg/analysis_server/lib/src/services/correction/assist.dart
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,11 @@ class DartAssistKind {
DartAssistKindPriority.DEFAULT,
"Convert to 'if-case' statement",
);
static const CONVERT_TO_IF_CASE_STATEMENT_CHAIN = AssistKind(
'dart.assist.convert.ifCaseStatementChain',
DartAssistKindPriority.DEFAULT,
"Convert to 'if-case' statement chain",
);
static const CONVERT_TO_IF_ELEMENT = AssistKind(
'dart.assist.convert.toIfElement',
DartAssistKindPriority.DEFAULT,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ import 'package:analysis_server/src/services/correction/dart/convert_to_expressi
import 'package:analysis_server/src/services/correction/dart/convert_to_field_parameter.dart';
import 'package:analysis_server/src/services/correction/dart/convert_to_generic_function_syntax.dart';
import 'package:analysis_server/src/services/correction/dart/convert_to_if_case_statement.dart';
import 'package:analysis_server/src/services/correction/dart/convert_to_if_case_statement_chain.dart';
import 'package:analysis_server/src/services/correction/dart/convert_to_int_literal.dart';
import 'package:analysis_server/src/services/correction/dart/convert_to_map_literal.dart';
import 'package:analysis_server/src/services/correction/dart/convert_to_multiline_string.dart';
Expand Down Expand Up @@ -116,6 +117,7 @@ class AssistProcessor extends BaseProcessor {
ConvertToFieldParameter.new,
ConvertToGenericFunctionSyntax.new,
ConvertToIfCaseStatement.new,
ConvertToIfCaseStatementChain.new,
ConvertToIntLiteral.new,
ConvertToMapLiteral.new,
ConvertToMultilineString.new,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
// Copyright (c) 2023, the Dart project authors. Please see the AUTHORS file
// 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.

import 'package:analysis_server/src/services/correction/assist.dart';
import 'package:analysis_server/src/services/correction/dart/abstract_producer.dart';
import 'package:analyzer/dart/ast/ast.dart';
import 'package:analyzer_plugin/utilities/assist/assist.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/range_factory.dart';

class ConvertToIfCaseStatementChain extends CorrectionProducer {
@override
AssistKind get assistKind =>
DartAssistKind.CONVERT_TO_IF_CASE_STATEMENT_CHAIN;

@override
Future<void> compute(ChangeBuilder builder) async {
final switchStatement = node;
if (switchStatement is! SwitchStatement) {
return;
}

final ifIndent = utils.getLinePrefix(switchStatement.offset);
final expressionCode = utils.getNodeText(switchStatement.expression);

final switchPatternCases = <SwitchPatternCase>[];
SwitchDefault? defaultCase;
for (final member in switchStatement.members) {
switch (member) {
case SwitchPatternCase():
switchPatternCases.add(member);
case SwitchDefault():
defaultCase = member;
default:
return;
}
}

await builder.addDartFileEdit(file, (builder) {
builder.addReplacement(range.node(switchStatement), (builder) {
var isFirst = true;
for (final case_ in switchPatternCases) {
if (isFirst) {
isFirst = false;
} else {
builder.write(' else ');
}
final patternCode = utils.getNodeText(case_.guardedPattern);
builder.writeln('if ($expressionCode case $patternCode) {');
_writeStatements(
builder: builder,
blockIndent: ifIndent,
statements: case_.statements,
);
builder.write('$ifIndent}');
}
if (defaultCase case final defaultCase?) {
builder.writeln(' else {');
_writeStatements(
builder: builder,
blockIndent: ifIndent,
statements: defaultCase.statements,
);
builder.write('$ifIndent}');
}
});
});
}

void _writeStatements({
required DartEditBuilder builder,
required List<Statement> statements,
required String blockIndent,
}) {
final range = utils.getLinesRangeStatements(statements);

final firstIndent = utils.getLinePrefix(statements.first.offset);
final singleIndent = utils.getIndent(1);

final code = utils.replaceSourceRangeIndent(
range,
firstIndent,
blockIndent + singleIndent,
);
builder.write(code);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
// Copyright (c) 2023, the Dart project authors. Please see the AUTHORS file
// 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.

import 'package:analysis_server/src/services/correction/assist.dart';
import 'package:analyzer_plugin/utilities/assist/assist.dart';
import 'package:test_reflective_loader/test_reflective_loader.dart';

import 'assist_processor.dart';

void main() {
defineReflectiveSuite(() {
defineReflectiveTests(ConvertToIfCaseStatementChainTest);
});
}

@reflectiveTest
class ConvertToIfCaseStatementChainTest extends AssistProcessorTest {
@override
AssistKind get kind => DartAssistKind.CONVERT_TO_IF_CASE_STATEMENT_CHAIN;

Future<void> test_noDefault() async {
await resolveTestCode('''
void f(Object? x) {
switch (x) {
case int():
0;
case double():
1;
}
}
''');
await assertHasAssistAt('switch', '''
void f(Object? x) {
if (x case int()) {
0;
} else if (x case double()) {
1;
}
}
''');
}

Future<void> test_withDefault() async {
await resolveTestCode('''
void f(Object? x) {
switch (x) {
case int():
0;
case double():
1;
default:
2;
}
}
''');
await assertHasAssistAt('switch', '''
void f(Object? x) {
if (x case int()) {
0;
} else if (x case double()) {
1;
} else {
2;
}
}
''');
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ import 'convert_to_double_quoted_string_test.dart'
as convert_to_double_quoted_string;
import 'convert_to_field_parameter_test.dart' as convert_to_field_parameter;
import 'convert_to_for_element_test.dart' as convert_to_for_element;
import 'convert_to_if_case_statement_chain_test.dart'
as convert_to_if_case_statement_chain;
import 'convert_to_if_case_statement_test.dart' as convert_to_if_case_statement;
import 'convert_to_if_element_test.dart' as convert_to_if_element;
import 'convert_to_int_literal_test.dart' as convert_to_int_literal;
Expand Down Expand Up @@ -119,6 +121,7 @@ void main() {
convert_to_double_quoted_string.main();
convert_to_field_parameter.main();
convert_to_for_element.main();
convert_to_if_case_statement_chain.main();
convert_to_if_case_statement.main();
convert_to_if_element.main();
convert_to_int_literal.main();
Expand Down
8 changes: 0 additions & 8 deletions pkg/compiler/lib/src/common/elements.dart
Original file line number Diff line number Diff line change
Expand Up @@ -1107,14 +1107,6 @@ class KCommonElements extends CommonElements {
class JCommonElements extends CommonElements {
JCommonElements(super.dartTypes, super.env);

/// Returns `true` if [element] is the unnamed constructor of `List`.
///
/// This will not resolve the constructor if it hasn't been seen yet during
/// compilation.
bool isUnnamedListConstructor(ConstructorEntity element) =>
(element.name == '' && element.enclosingClass == listClass) ||
(element.name == 'list' && element.enclosingClass == jsArrayClass);

/// Returns `true` if [element] is the named constructor of `List`,
/// e.g. `List.of`.
///
Expand Down
12 changes: 7 additions & 5 deletions pkg/compiler/lib/src/dart2js.dart
Original file line number Diff line number Diff line change
Expand Up @@ -1020,13 +1020,15 @@ Future<api.CompilationResult> compile(List<String> argv,
String? summary;
switch (readStrategy) {
case ReadStrategy.fromDart:
inputName = inputDillUri != null ? 'kernel bytes' : 'characters Dart';
inputSize = inputProvider.dartCharactersRead;
final sourceCharCount =
_formatCharacterCount(inputProvider.sourceBytesFromDill);
inputName = 'input bytes ($sourceCharCount characters source)';
inputSize = inputProvider.bytesRead;
summary = 'Dart file $input ';
break;
case ReadStrategy.fromClosedWorld:
inputName = 'bytes data';
inputSize = inputProvider.dartCharactersRead;
inputSize = inputProvider.bytesRead;
String dataInput =
fe.relativizeUri(Uri.base, readClosedWorldUri!, Platform.isWindows);
summary = 'Data files $input and $dataInput ';
Expand All @@ -1035,7 +1037,7 @@ Future<api.CompilationResult> compile(List<String> argv,
_fail("Must read from closed world and data.");
case ReadStrategy.fromDataAndClosedWorld:
inputName = 'bytes data';
inputSize = inputProvider.dartCharactersRead;
inputSize = inputProvider.bytesRead;
String worldInput =
fe.relativizeUri(Uri.base, readClosedWorldUri!, Platform.isWindows);
String dataInput =
Expand All @@ -1048,7 +1050,7 @@ Future<api.CompilationResult> compile(List<String> argv,
_fail("Must read from closed world, data, and codegen");
case ReadStrategy.fromCodegenAndClosedWorldAndData:
inputName = 'bytes data';
inputSize = inputProvider.dartCharactersRead;
inputSize = inputProvider.bytesRead;
String worldInput =
fe.relativizeUri(Uri.base, readClosedWorldUri!, Platform.isWindows);
String dataInput =
Expand Down
18 changes: 0 additions & 18 deletions pkg/compiler/lib/src/inferrer/builder.dart
Original file line number Diff line number Diff line change
Expand Up @@ -1385,24 +1385,6 @@ class KernelTypeGraphBuilder extends ir.Visitor<TypeInformation?>

var commonElements = _elementMap.commonElements;

if (commonElements.isUnnamedListConstructor(constructor)) {
// We have `new List(...)`.
if (arguments.positional.isEmpty && arguments.named.isEmpty) {
// We have `new List()`.
return _inferrer.concreteTypes.putIfAbsent(
node,
() => _types.allocateList(_types.growableListType, node,
_analyzedMember, _types.nonNullEmpty(), 0));
} else {
// We have `new List(len)`.
final length = _findLength(arguments);
return _inferrer.concreteTypes.putIfAbsent(
node,
() => _types.allocateList(_types.fixedListType, node,
_analyzedMember, _types.nullType, length));
}
}

if (commonElements.isNamedListConstructor('filled', constructor)) {
// We have something like `List.filled(len, fill)`.
final length = _findLength(arguments);
Expand Down
18 changes: 0 additions & 18 deletions pkg/compiler/lib/src/inferrer_experimental/builder.dart
Original file line number Diff line number Diff line change
Expand Up @@ -1385,24 +1385,6 @@ class KernelTypeGraphBuilder extends ir.Visitor<TypeInformation?>

var commonElements = _elementMap.commonElements;

if (commonElements.isUnnamedListConstructor(constructor)) {
// We have `new List(...)`.
if (arguments.positional.isEmpty && arguments.named.isEmpty) {
// We have `new List()`.
return _inferrer.concreteTypes.putIfAbsent(
node,
() => _types.allocateList(_types.growableListType, node,
_analyzedMember, _types.nonNullEmpty(), 0));
} else {
// We have `new List(len)`.
final length = _findLength(arguments);
return _inferrer.concreteTypes.putIfAbsent(
node,
() => _types.allocateList(_types.fixedListType, node,
_analyzedMember, _types.nullType, length));
}
}

if (commonElements.isNamedListConstructor('filled', constructor)) {
// We have something like `List.filled(len, fill)`.
final length = _findLength(arguments);
Expand Down
6 changes: 4 additions & 2 deletions pkg/compiler/lib/src/source_file_provider.dart
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@ abstract class SourceFileByteReader {
abstract class SourceFileProvider implements api.CompilerInput {
bool isWindows = (Platform.operatingSystem == 'windows');
Uri cwd = Uri.base;
int dartCharactersRead = 0;
int bytesRead = 0;
int sourceBytesFromDill = 0;
SourceFileByteReader byteReader;
final Set<Uri> _registeredUris = {};
final Map<Uri, Uri> _mappedUris = {};
Expand Down Expand Up @@ -63,6 +64,7 @@ abstract class SourceFileProvider implements api.CompilerInput {
if (!disableByteCache) {
_byteCache[resourceUri] = source;
}
sourceBytesFromDill += source.length;
}

/// Registers the URI and returns true if the URI is new.
Expand All @@ -83,7 +85,7 @@ abstract class SourceFileProvider implements api.CompilerInput {
throw "Error reading '${relativizeUri(resourceUri)}' $detail";
}
if (registerUri(resourceUri)) {
dartCharactersRead += source.length;
bytesRead += source.length;
}
if (resourceUri != uri) {
registerUri(uri);
Expand Down
Loading

0 comments on commit fd4797c

Please sign in to comment.