Skip to content

Commit

Permalink
Version 3.2.0-54.0.dev
Browse files Browse the repository at this point in the history
Merge ffcb38d into dev
  • Loading branch information
Dart CI committed Aug 9, 2023
2 parents d89e4ea + ffcb38d commit ff0c128
Show file tree
Hide file tree
Showing 74 changed files with 1,227 additions and 564 deletions.
6 changes: 5 additions & 1 deletion benchmarks/MemoryCopy/dart/MemoryCopy.dart
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,11 @@ abstract class MemoryCopyBenchmark {
}
// If not, then adjust our estimate of how many iterations are needed to
// reach the minimum and try again.
rounds *= (minDuration.inMicroseconds / elapsed.inMicroseconds).ceil();
if (elapsed.inMilliseconds == 0) {
rounds *= 1000;
} else {
rounds *= (minDuration.inMicroseconds / elapsed.inMicroseconds).ceil();
}
}
}

Expand Down
9 changes: 9 additions & 0 deletions pkg/analysis_server/lib/plugin/edit/fix/fix_dart.dart
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,15 @@ import 'package:analyzer_plugin/utilities/change_builder/change_workspace.dart';
///
/// Clients may not extend, implement or mix-in this class.
abstract class DartFixContext implements FixContext {
/// Whether fixes were triggered automatically (for example by a save
/// operation).
///
/// Some fixes may be excluded when running automatically. For example
/// removing unused imports or parameters is less acceptable while the code is
/// incomplete and being worked on than when manually executing fixes ready
/// for committing.
bool get autoTriggered;

/// Return the instrumentation service used to report errors that prevent a
/// fix from being composed.
InstrumentationService get instrumentationService;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ class _FixAllOperation extends TemporaryOverlayOperation
);

var changes = await processor.fixErrorsForFile(message.performance, path,
removeUnusedImports: !autoTriggered);
autoTriggered: autoTriggered);
if (changes.isEmpty) {
return success(null);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -243,7 +243,7 @@ class BulkFixProcessor {
/// diagnostics in [file] in the given [context].
Future<ChangeBuilder> fixErrorsForFile(OperationPerformanceImpl performance,
AnalysisContext context, String path,
{bool removeUnusedImports = true}) async {
{required bool autoTriggered}) async {
var pathContext = context.contextRoot.resourceProvider.pathContext;

if (file_paths.isDart(pathContext, path) && !file_paths.isGenerated(path)) {
Expand All @@ -252,8 +252,7 @@ class BulkFixProcessor {
(_) => context.currentSession.getResolvedLibrary(path),
);
if (!isCancelled && library is ResolvedLibraryResult) {
await _fixErrorsInLibrary(library,
removeUnusedImports: removeUnusedImports);
await _fixErrorsInLibrary(library, autoTriggered: autoTriggered);
}
}

Expand Down Expand Up @@ -298,7 +297,10 @@ class BulkFixProcessor {
CorrectionProducerContext context) async {
for (var generator in generators) {
var producer = generator();
if (producer.canBeAppliedInBulk) {
var shouldFix = (context.dartFixContext?.autoTriggered ?? false)
? producer.canBeAppliedAutomatically
: producer.canBeAppliedInBulk;
if (shouldFix) {
await _generateFix(context, producer, codeName);
if (isCancelled) {
return;
Expand Down Expand Up @@ -465,25 +467,31 @@ class BulkFixProcessor {
/// Use the change [builder] to create fixes for the diagnostics in the
/// library associated with the analysis [result].
Future<void> _fixErrorsInLibrary(ResolvedLibraryResult result,
{bool stopAfterFirst = false, bool removeUnusedImports = true}) async {
{bool stopAfterFirst = false, bool autoTriggered = false}) async {
var analysisOptions = result.session.analysisContext.analysisOptions;

DartFixContextImpl fixContext(
ResolvedUnitResult result, AnalysisError diagnostic) {
ResolvedUnitResult result,
AnalysisError diagnostic, {
required bool autoTriggered,
}) {
return DartFixContextImpl(
instrumentationService,
workspace,
result,
diagnostic,
autoTriggered: autoTriggered,
);
}

CorrectionProducerContext<ResolvedUnitResult>? correctionContext(
ResolvedUnitResult result, AnalysisError diagnostic) {
var overrideSet = _readOverrideSet(result);
var context =
fixContext(result, diagnostic, autoTriggered: autoTriggered);
return CorrectionProducerContext.createResolved(
applyingBulkFixes: true,
dartFixContext: fixContext(result, diagnostic),
dartFixContext: context,
diagnostic: diagnostic,
overrideSet: overrideSet,
resolvedResult: result,
Expand All @@ -499,8 +507,9 @@ class BulkFixProcessor {
for (var unitResult in result.units) {
var overrideSet = _readOverrideSet(unitResult);
for (var error in _filterErrors(analysisOptions, unitResult.errors)) {
await _fixSingleError(
fixContext(unitResult, error), unitResult, error, overrideSet);
var context =
fixContext(unitResult, error, autoTriggered: autoTriggered);
await _fixSingleError(context, unitResult, error, overrideSet);
if (isCancelled || (stopAfterFirst && changeMap.hasFixes)) {
return;
}
Expand All @@ -513,7 +522,7 @@ class BulkFixProcessor {
var definingUnit = result.units[0];
AnalysisError? directivesOrderingError;
var unusedImportErrors = <AnalysisError>[];
if (removeUnusedImports && !builder.hasEditsFor(definingUnit.path)) {
if (!autoTriggered && !builder.hasEditsFor(definingUnit.path)) {
for (var error in _filterErrors(analysisOptions, definingUnit.errors)) {
var errorCode = error.errorCode;
if (errorCode is LintCode) {
Expand Down Expand Up @@ -834,7 +843,7 @@ class IterativeBulkFixProcessor {
Future<List<SourceFileEdit>> fixErrorsForFile(
OperationPerformanceImpl performance,
String path, {
bool removeUnusedImports = true,
required bool autoTriggered,
}) async {
return performance.runAsync('IterativeBulkFixProcessor.fixErrorsForFile',
(performance) async {
Expand All @@ -850,7 +859,7 @@ class IterativeBulkFixProcessor {
'BulkFixProcessor.fixErrorsForFile pass $i',
(performance) => processor.fixErrorsForFile(
performance, context, path,
removeUnusedImports: removeUnusedImports),
autoTriggered: autoTriggered),
);

if (isCancelled) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,17 +48,36 @@ abstract class CorrectionProducer<T extends ParsedUnitResult>
/// if this producer doesn't support assists.
AssistKind? get assistKind => null;

/// Return `true` if fixes from this producer are acceptable to run
/// automatically (such as during a save operation) when code could be
/// incomplete.
///
/// By default this value matches [canBeAppliedInBulk] but may return `false`
/// for fixes that perform actions like removing unused code, which could be
/// unused only because the code is still being worked on.
bool get canBeAppliedAutomatically => canBeAppliedInBulk;

/// Return `true` if this producer can be used to fix diagnostics across
/// multiple files. Cases where this will return `false` include fixes for
/// which
/// multiple files and/or at the same time as applying fixes from other
/// producers.
///
/// This flag is used when the user has chosen to apply fixes but may not have
/// chosen to apply a specific fix (such as running `dart fix`).
///
/// Cases where this will return `false` include fixes for which
/// - the modified regions can overlap, and
/// - fixes that have not been tested to ensure that they can be used this
/// way.
bool get canBeAppliedInBulk => false;

/// Return `true` if this producer can be used to fix multiple diagnostics in
/// the same file. Cases where this will return `false` include fixes for
/// which
/// the same file.
///
/// Unlike [canBeAppliedInBulk], this flag is used to provide the option for
/// a user to fix a specific diagnostic across a file (such as a quick-fix to
/// "fix all x in this file").
///
/// Cases where this will return `false` include fixes for which
/// - the modified regions can overlap,
/// - the fix for one diagnostic would fix all diagnostics with the same code,
/// and,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,8 @@ class RemoveLeadingUnderscore extends ResolvedCorrectionProducer {
var root = node
.thisOrAncestorMatching((node) =>
node.parent is FunctionDeclaration ||
node.parent is MethodDeclaration)
node.parent is MethodDeclaration ||
node.parent is ConstructorDeclaration)
?.parent;
if (root != null) {
references = findLocalElementReferences(root, element);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@ import 'package:analyzer_plugin/utilities/fixes/fixes.dart';
import 'package:analyzer_plugin/utilities/range_factory.dart';

class RemoveUnusedCatchClause extends ResolvedCorrectionProducer {
@override
// May not be appropriate while actively coding.
bool get canBeAppliedAutomatically => false;

@override
bool get canBeAppliedInBulk => true;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@ import 'package:analyzer_plugin/utilities/fixes/fixes.dart';
import 'package:analyzer_plugin/utilities/range_factory.dart';

class RemoveUnusedCatchStack extends ResolvedCorrectionProducer {
@override
// May not be appropriate while actively coding.
bool get canBeAppliedAutomatically => false;

@override
bool get canBeAppliedInBulk => true;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@ import 'package:analyzer_plugin/utilities/fixes/fixes.dart';
import 'package:analyzer_plugin/utilities/range_factory.dart';

class RemoveUnusedParameter extends ResolvedCorrectionProducer {
@override
bool get canBeAppliedAutomatically => false;

@override
bool get canBeAppliedInBulk => true;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -636,6 +636,10 @@ CompileTimeErrorCode.EXTENSION_OVERRIDE_WITH_CASCADE:
Replace the `..` with `.`.
CompileTimeErrorCode.EXTENSION_OVERRIDE_WITHOUT_ACCESS:
status: noFix
CompileTimeErrorCode.EXTENSION_TYPE_CONSTRUCTOR_WITH_SUPER_INVOCATION:
status: needsFix
notes: |-
Remove it.
CompileTimeErrorCode.EXTENSION_TYPE_DECLARES_INSTANCE_FIELD:
status: noFix
CompileTimeErrorCode.EXTENSION_TYPE_DECLARES_MEMBER_OF_OBJECT:
Expand Down
6 changes: 5 additions & 1 deletion pkg/analysis_server/lib/src/services/correction/fix.dart
Original file line number Diff line number Diff line change
Expand Up @@ -67,8 +67,12 @@ class DartFixContextImpl implements DartFixContext {
@override
final AnalysisError error;

@override
final bool autoTriggered;

DartFixContextImpl(this.instrumentationService, this.workspace,
this.resolveResult, this.error);
this.resolveResult, this.error,
{this.autoTriggered = false});

@override
Future<Map<LibraryElement, Element>> getTopLevelDeclarations(
Expand Down
52 changes: 52 additions & 0 deletions pkg/analysis_server/test/lsp/code_actions_source_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,58 @@ void f() {
expect(edit.textDocument.version, 1);
}

Future<void> test_privateUnusedParameters_notRemovedIfSave() async {
const content = '''
class _MyClass {
int? _param;
_MyClass({
this._param,
});
}
''';

final codeAction = await expectAction(
content,
command: Commands.fixAll,
triggerKind: CodeActionTriggerKind.Automatic,
);
final command = codeAction.command!;

// We should not get an applyEdit call during the command execution because
// no edits should be produced.
final applyEditSubscription = requestsFromServer
.where((n) => n.method == Method.workspace_applyEdit)
.listen((_) => throw 'workspace/applyEdit was unexpectedly called');
final commandResponse = await executeCommand(command);
expect(commandResponse, isNull);

await pumpEventQueue();
await applyEditSubscription.cancel();
}

Future<void> test_privateUnusedParameters_removedByDefault() async {
const content = '''
class _MyClass {
int? param;
_MyClass({
this.param,
});
}
''';
const expectedContent = '''
class _MyClass {
int? param;
_MyClass();
}
''';

await verifyActionEdits(
content,
expectedContent,
command: Commands.fixAll,
);
}

Future<void> test_unavailable_outsideAnalysisRoot() async {
final otherFile = convertPath('/other/file.dart');
final content = '';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -222,6 +222,23 @@ void f() {
''');
}

Future<void> test_parameter_constructor() async {
await resolveTestCode('''
class A {
A(int _foo) {
print(_foo);
}
}
''');
await assertHasFix('''
class A {
A(int foo) {
print(foo);
}
}
''');
}

Future<void> test_parameter_function() async {
await resolveTestCode('''
void f(int _foo) {
Expand Down
7 changes: 6 additions & 1 deletion pkg/analyzer/lib/src/dart/analysis/library_analyzer.dart
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ import 'package:analyzer/src/lint/linter_visitor.dart';
import 'package:analyzer/src/services/lint.dart';
import 'package:analyzer/src/task/strong/checker.dart';
import 'package:analyzer/src/util/performance/operation_performance.dart';
import 'package:analyzer/src/utilities/extensions/version.dart';
import 'package:collection/collection.dart';
import 'package:path/path.dart' as path;

Expand Down Expand Up @@ -478,7 +479,11 @@ class LibraryAnalyzer {
var sdkVersionConstraint = _analysisOptions.sdkVersionConstraint;
if (sdkVersionConstraint != null) {
SdkConstraintVerifier verifier = SdkConstraintVerifier(
errorReporter, _libraryElement, _typeProvider, sdkVersionConstraint);
errorReporter,
_libraryElement,
_typeProvider,
sdkVersionConstraint.withoutPreRelease,
);
unit.accept(verifier);
}
}
Expand Down
1 change: 1 addition & 0 deletions pkg/analyzer/lib/src/dart/error/ffi_code.g.dart
Original file line number Diff line number Diff line change
Expand Up @@ -323,6 +323,7 @@ class FfiCode extends AnalyzerErrorCode {
"The return type of the function passed to 'NativeCallable.listener' must "
"be 'void' rather than '{0}'.",
correctionMessage: "Try changing the return type to 'void'.",
hasPublishedDocs: true,
);

/// Parameters:
Expand Down
Loading

0 comments on commit ff0c128

Please sign in to comment.