blob: 2972f498e5b269030dec1a7182ea596574330544 [file] [log] [blame]
// Copyright (c) 2018, 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/plugin/edit/assist/assist_core.dart';
import 'package:analysis_server/src/edit/edit_dartfix.dart';
import 'package:analysis_server/src/services/correction/assist.dart';
import 'package:analysis_server/src/services/correction/assist_internal.dart';
import 'package:analysis_server/src/services/correction/change_workspace.dart';
import 'package:analyzer/dart/analysis/results.dart';
import 'package:analyzer/dart/ast/ast.dart';
import 'package:analyzer/dart/element/element.dart';
import 'package:analyzer/error/error.dart';
class PreferMixinFix extends LinterFix {
final classesToConvert = new Set<Element>();
PreferMixinFix(EditDartFix dartFix) : super(dartFix);
Future<void> applyLocalFixes(ResolvedUnitResult result) {
// All fixes applied in [applyRemainingFixes]
return null;
Future<void> applyRemainingFixes() async {
for (Element elem in classesToConvert) {
await convertClassToMixin(elem);
Future<void> convertClassToMixin(Element elem) async {
ResolvedUnitResult result =
await dartFix.server.getResolvedUnit(elem.source?.fullName);
for (CompilationUnitMember declaration in result.unit.declarations) {
if (declaration is ClassOrMixinDeclaration && == {
AssistProcessor processor = new AssistProcessor(
new DartAssistContextImpl(
List<Assist> assists = await processor
final location =
dartFix.locationFor(result, elem.nameOffset, elem.nameLength);
if (assists.isNotEmpty) {
for (Assist assist in assists) {
dartFix.addSourceChange('Convert ${elem.displayName} to a mixin',
location, assist.change);
} else {
// TODO(danrubel): If assists is empty, then determine why
// assist could not be performed and report that in the description.
'Could not convert ${elem.displayName} to a mixin'
' because the class contains a constructor',
void reportErrorForNode(ErrorCode errorCode, AstNode node,
[List<Object> arguments]) {
TypeName type = node;
Element element =;
String filePath = element.source?.fullName;
if (filePath != null && dartFix.isIncluded(filePath)) {