-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge 2cce183 into dev
- Loading branch information
Showing
83 changed files
with
2,941 additions
and
1,499 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
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
83 changes: 83 additions & 0 deletions
83
pkg/analysis_server/lib/src/services/correction/dart/convert_to_flutter_style_todo.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,83 @@ | ||
// 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:_fe_analyzer_shared/src/scanner/token.dart'; | ||
import 'package:analysis_server/src/services/correction/dart/abstract_producer.dart'; | ||
import 'package:analysis_server/src/services/correction/fix.dart'; | ||
import 'package:analyzer_plugin/utilities/change_builder/change_builder_core.dart'; | ||
import 'package:analyzer_plugin/utilities/fixes/fixes.dart'; | ||
import 'package:analyzer_plugin/utilities/range_factory.dart'; | ||
import 'package:linter/src/rules/flutter_style_todos.dart'; | ||
|
||
class ConvertToFlutterStyleTodo extends ResolvedCorrectionProducer { | ||
@override | ||
bool get canBeAppliedInBulk => true; | ||
|
||
@override | ||
bool get canBeAppliedToFile => true; | ||
|
||
@override | ||
FixKind get fixKind => DartFixKind.CONVERT_TO_FLUTTER_STYLE_TODO; | ||
|
||
@override | ||
FixKind get multiFixKind => DartFixKind.CONVERT_TO_FLUTTER_STYLE_TODO_MULTI; | ||
|
||
@override | ||
Future<void> compute(ChangeBuilder builder) async { | ||
var diagnosticOffset = diagnostic?.problemMessage.offset; | ||
if (diagnosticOffset == null) return; | ||
|
||
// Find the token that follows the reported diagnostic. | ||
var token = node.beginToken; | ||
do { | ||
if (token.offset > diagnosticOffset) break; | ||
|
||
token = token.next!; | ||
} while (token != node.endToken); | ||
|
||
// Identify the right comment. | ||
Token? comment = token.precedingComments; | ||
while (comment != null) { | ||
if (comment.offset >= diagnosticOffset) break; | ||
comment = comment.next; | ||
} | ||
if (comment == null) return; | ||
|
||
var content = comment.lexeme; | ||
|
||
// Try adding a missing leading space before `TODO`. | ||
if (!content.startsWith('// ')) { | ||
content = content.replaceFirst('//', '// '); | ||
} | ||
|
||
// Try removing an unwanted space after `TODO`. | ||
if (content.length > 7 && content[7] == ' ') { | ||
content = content.replaceRange(7, 8, ''); | ||
} | ||
|
||
// Try adding a colon. | ||
var index = content.indexOf(')') + 1; | ||
if (content.length > index && !content.startsWith(':', index)) { | ||
content = content.replaceFirst(')', '):'); | ||
} | ||
|
||
// Try fixing the TODO case. | ||
if (content.startsWith('// todo')) { | ||
content = content.replaceRange(3, 7, 'TODO'); | ||
} | ||
|
||
// TODO(pq): consider adding missing user info. | ||
// Possibly inserting '(${Platform.environment['USER'] ?? Platform.environment['USERNAME']}') | ||
// (assuming the environment variable is set). | ||
|
||
// If the generated content doesn't match flutter style, don't apply it. | ||
if (!content.startsWith(FlutterStyleTodos.todoExpectedRegExp)) return; | ||
|
||
await builder.addDartFileEdit(file, (builder) { | ||
builder.addReplacement(range.token(comment as Token), (builder) { | ||
builder.write(content); | ||
}); | ||
}); | ||
} | ||
} |
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
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
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
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
119 changes: 119 additions & 0 deletions
119
pkg/analysis_server/test/src/services/correction/fix/convert_to_flutter_style_todo_test.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,119 @@ | ||
// 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/fix.dart'; | ||
import 'package:analysis_server/src/services/linter/lint_names.dart'; | ||
import 'package:analyzer/src/dart/error/todo_codes.dart'; | ||
import 'package:analyzer_plugin/utilities/fixes/fixes.dart'; | ||
import 'package:test_reflective_loader/test_reflective_loader.dart'; | ||
|
||
import 'fix_processor.dart'; | ||
|
||
void main() { | ||
defineReflectiveSuite(() { | ||
defineReflectiveTests(ConvertToFlutterStyleTodoBulkTest); | ||
defineReflectiveTests(ConvertToFlutterStyleTodoTest); | ||
}); | ||
} | ||
|
||
@reflectiveTest | ||
class ConvertToFlutterStyleTodoBulkTest extends BulkFixProcessorTest { | ||
@override | ||
String get lintCode => LintNames.flutter_style_todos; | ||
|
||
Future<void> test_singleFile() async { | ||
await resolveTestCode(''' | ||
// TODO(user) msg. | ||
void f() { | ||
// todo(user) msg. | ||
} | ||
//TODO(user): msg. | ||
void g() { } | ||
'''); | ||
await assertHasFix(''' | ||
// TODO(user): msg. | ||
void f() { | ||
// TODO(user): msg. | ||
} | ||
// TODO(user): msg. | ||
void g() { } | ||
'''); | ||
} | ||
} | ||
|
||
@reflectiveTest | ||
class ConvertToFlutterStyleTodoTest extends FixProcessorLintTest { | ||
@override | ||
FixKind get kind => DartFixKind.CONVERT_TO_FLUTTER_STYLE_TODO; | ||
|
||
@override | ||
String get lintCode => LintNames.flutter_style_todos; | ||
|
||
Future<void> test_lowerCase() async { | ||
await resolveTestCode(''' | ||
// todo(user): msg. | ||
void f() { } | ||
'''); | ||
await assertHasFix(''' | ||
// TODO(user): msg. | ||
void f() { } | ||
'''); | ||
} | ||
|
||
Future<void> test_missingColon() async { | ||
await resolveTestCode(''' | ||
// TODO(user) msg. | ||
void f() { } | ||
'''); | ||
await assertHasFix(''' | ||
// TODO(user): msg. | ||
void f() { } | ||
''', errorFilter: (e) => e.errorCode != TodoCode.TODO); | ||
} | ||
|
||
Future<void> test_missingColon_surroundingComments() async { | ||
await resolveTestCode(''' | ||
// Leading comment. | ||
// TODO(user) msg. | ||
// Trailing comment. | ||
void f() { } | ||
'''); | ||
await assertHasFix(''' | ||
// Leading comment. | ||
// TODO(user): msg. | ||
// Trailing comment. | ||
void f() { } | ||
''', errorFilter: (e) => e.errorCode != TodoCode.TODO); | ||
} | ||
|
||
Future<void> test_missingColonAndMessage() async { | ||
await resolveTestCode(''' | ||
// TODO(user) | ||
void f() {} | ||
'''); | ||
await assertNoFix(errorFilter: (e) => e.errorCode != TodoCode.TODO); | ||
} | ||
|
||
Future<void> test_missingLeadingSpace() async { | ||
await resolveTestCode(''' | ||
//TODO(user): msg. | ||
void f() {} | ||
'''); | ||
await assertHasFix(''' | ||
// TODO(user): msg. | ||
void f() {} | ||
''', errorFilter: (e) => e.errorCode != TodoCode.TODO); | ||
} | ||
|
||
Future<void> test_unwantedSpaceBeforeUser() async { | ||
await resolveTestCode(''' | ||
// TODO (user): msg. | ||
void f() {} | ||
'''); | ||
await assertHasFix(''' | ||
// TODO(user): msg. | ||
void f() {} | ||
''', errorFilter: (e) => e.errorCode != TodoCode.TODO); | ||
} | ||
} |
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
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
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
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
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
Oops, something went wrong.