-
Notifications
You must be signed in to change notification settings - Fork 6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add "convert to int literal" to dartfix
This updates the analysis server edit.dartfix request to find and convert double literals to int literals. Under the covers it uses the prefer_int_literal lint and gracefully degrades if the lint is not found (e.g. third_party/linter needs to be rolled). Change-Id: I0236af9c19667e543541fef18836bfeeba8c67d7 Reviewed-on: https://dart-review.googlesource.com/c/81302 Reviewed-by: Brian Wilkerson <[email protected]> Commit-Queue: Dan Rubel <[email protected]>
- Loading branch information
1 parent
0009ca7
commit 4695b5f
Showing
3 changed files
with
168 additions
and
72 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
56 changes: 56 additions & 0 deletions
56
pkg/analysis_server/lib/src/edit/fix/prefer_int_literals_fix.dart
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
// 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:analyzer/analyzer.dart'; | ||
import 'package:analyzer/dart/ast/ast.dart'; | ||
import 'package:analyzer/src/dart/analysis/driver.dart'; | ||
|
||
class PreferIntLiteralsFix extends LinterFix { | ||
final literalsToConvert = <DoubleLiteral>[]; | ||
|
||
PreferIntLiteralsFix(EditDartFix dartFix) : super(dartFix); | ||
|
||
@override | ||
Future<void> applyLocalFixes(AnalysisResult result) async { | ||
while (literalsToConvert.isNotEmpty) { | ||
DoubleLiteral literal = literalsToConvert.removeLast(); | ||
AssistProcessor processor = new AssistProcessor( | ||
new EditDartFixAssistContext(dartFix, source, result.unit, literal)); | ||
List<Assist> assists = | ||
await processor.computeAssist(DartAssistKind.CONVERT_TO_INT_LITERAL); | ||
final location = dartFix.locationDescription(result, literal.offset); | ||
if (assists.isNotEmpty) { | ||
for (Assist assist in assists) { | ||
dartFix.addFix( | ||
'Replace a double literal with an int literal in $location', | ||
assist.change); | ||
} | ||
} else { | ||
// TODO(danrubel): If assists is empty, then determine why | ||
// assist could not be performed and report that in the description. | ||
dartFix.addRecommendation('Could not replace' | ||
' a double literal with an int literal in $location'); | ||
} | ||
} | ||
} | ||
|
||
@override | ||
Future<void> applyRemainingFixes() { | ||
// All fixes applied in [applyLocalFixes] | ||
return null; | ||
} | ||
|
||
@override | ||
void reportErrorForNode(ErrorCode errorCode, AstNode node, | ||
[List<Object> arguments]) { | ||
String filePath = source.fullName; | ||
if (filePath != null && dartFix.isIncluded(filePath)) { | ||
literalsToConvert.add(node); | ||
} | ||
} | ||
} |
72 changes: 72 additions & 0 deletions
72
pkg/analysis_server/lib/src/edit/fix/prefer_mixin_fix.dart
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
// 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:analyzer/analyzer.dart'; | ||
import 'package:analyzer/dart/ast/ast.dart'; | ||
import 'package:analyzer/dart/element/element.dart'; | ||
import 'package:analyzer/src/dart/analysis/driver.dart'; | ||
|
||
class PreferMixinFix extends LinterFix { | ||
final classesToConvert = new Set<Element>(); | ||
|
||
PreferMixinFix(EditDartFix dartFix) : super(dartFix); | ||
|
||
@override | ||
Future<void> applyLocalFixes(AnalysisResult result) { | ||
// All fixes applied in [applyRemainingFixes] | ||
return null; | ||
} | ||
|
||
@override | ||
Future<void> applyRemainingFixes() async { | ||
for (Element elem in classesToConvert) { | ||
await convertClassToMixin(elem); | ||
} | ||
} | ||
|
||
Future<void> convertClassToMixin(Element elem) async { | ||
AnalysisResult result = | ||
await dartFix.server.getAnalysisResult(elem.source?.fullName); | ||
|
||
for (CompilationUnitMember declaration in result.unit.declarations) { | ||
if (declaration is ClassOrMixinDeclaration && | ||
declaration.name.name == elem.name) { | ||
AssistProcessor processor = new AssistProcessor( | ||
new EditDartFixAssistContext( | ||
dartFix, elem.source, result.unit, declaration.name)); | ||
List<Assist> assists = await processor | ||
.computeAssist(DartAssistKind.CONVERT_CLASS_TO_MIXIN); | ||
final location = dartFix.locationDescription(result, elem.nameOffset); | ||
if (assists.isNotEmpty) { | ||
for (Assist assist in assists) { | ||
dartFix.addFix( | ||
'Convert ${elem.displayName} to a mixin in $location', | ||
assist.change); | ||
} | ||
} else { | ||
// TODO(danrubel): If assists is empty, then determine why | ||
// assist could not be performed and report that in the description. | ||
dartFix.addRecommendation( | ||
'Could not convert ${elem.displayName} to a mixin' | ||
' because the class contains a constructor in $location'); | ||
} | ||
} | ||
} | ||
} | ||
|
||
@override | ||
void reportErrorForNode(ErrorCode errorCode, AstNode node, | ||
[List<Object> arguments]) { | ||
TypeName type = node; | ||
Element element = type.name.staticElement; | ||
String filePath = element.source?.fullName; | ||
if (filePath != null && dartFix.isIncluded(filePath)) { | ||
classesToConvert.add(element); | ||
} | ||
} | ||
} |