-
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.
[analysis server] add fix for dangling_library_doc_comments
Change-Id: I5bd3ce04fc85a698983cbbd04bab61fedadfca6c Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/272481 Reviewed-by: Brian Wilkerson <[email protected]> Commit-Queue: Samuel Rawlins <[email protected]>
- Loading branch information
Showing
7 changed files
with
284 additions
and
1 deletion.
There are no files selected for viewing
127 changes: 127 additions & 0 deletions
127
...alysis_server/lib/src/services/correction/dart/move_doc_comment_to_library_directive.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,127 @@ | ||
// Copyright (c) 2022, 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/dart/analysis/features.dart'; | ||
import 'package:analyzer/dart/ast/ast.dart'; | ||
import 'package:analyzer/source/line_info.dart'; | ||
import 'package:analyzer/source/source_range.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:collection/collection.dart'; | ||
|
||
class MoveDocCommentToLibraryDirective extends CorrectionProducer { | ||
@override | ||
FixKind get fixKind => DartFixKind.MOVE_DOC_COMMENT_TO_LIBRARY_DIRECTIVE; | ||
|
||
@override | ||
Future<void> compute(ChangeBuilder builder) async { | ||
var comment = node.thisOrAncestorOfType<Comment>(); | ||
if (comment == null) { | ||
return; | ||
} | ||
var compilationUnit = comment.root; | ||
if (compilationUnit is! CompilationUnit) { | ||
return; | ||
} | ||
|
||
var firstDirective = compilationUnit.directives.firstOrNull; | ||
if (firstDirective is LibraryDirective) { | ||
await _moveToExistingLibraryDirective(builder, comment, firstDirective); | ||
} else if (libraryElement.featureSet.isEnabled(Feature.unnamedLibraries)) { | ||
await _moveToNewLibraryDirective(builder, comment, compilationUnit); | ||
} | ||
|
||
// If the library doesn't support unnamed libraries, then we cannot add | ||
// a new library directive; we don't know what to name it. | ||
} | ||
|
||
Future<void> _moveToExistingLibraryDirective(ChangeBuilder builder, | ||
Comment comment, LibraryDirective libraryDirective) async { | ||
// Just move the annotation to the existing library directive. | ||
var commentRange = utils.getLinesRange(range.node(comment)); | ||
await builder.addDartFileEdit(file, (builder) { | ||
builder.addDeletion(commentRange); | ||
var commentText = utils.getRangeText(commentRange); | ||
builder.addSimpleInsertion( | ||
libraryDirective.firstTokenAfterCommentAndMetadata.offset, | ||
commentText); | ||
}); | ||
} | ||
|
||
Future<void> _moveToNewLibraryDirective(ChangeBuilder builder, | ||
Comment comment, CompilationUnit compilationUnit) async { | ||
var commentRange = _rangeOfFirstBlock(comment, compilationUnit.lineInfo); | ||
|
||
// Create a new, unnamed library directive, and move the comment to just | ||
// above the directive. | ||
var token = compilationUnit.beginToken; | ||
|
||
if (token.type == TokenType.SCRIPT_TAG) { | ||
// TODO(srawlins): Handle this case. | ||
return; | ||
} | ||
|
||
if (token.precedingComments == comment.beginToken) { | ||
// Do not "move" the comment. Just slip a library directive below it. | ||
await builder.addDartFileEdit(file, (builder) { | ||
builder.addSimpleInsertion(commentRange.end, 'library;$eol'); | ||
}); | ||
return; | ||
} | ||
|
||
int insertionOffset; | ||
String prefix; | ||
Token? commentOnFirstToken = token.precedingComments; | ||
if (commentOnFirstToken != null) { | ||
while (commentOnFirstToken!.next != null) { | ||
commentOnFirstToken = commentOnFirstToken.next!; | ||
|
||
if (commentOnFirstToken == comment.beginToken) { | ||
// Do not "move" the comment. Just slip a library directive below it. | ||
await builder.addDartFileEdit(file, (builder) { | ||
builder.addSimpleInsertion(commentRange.end, 'library;$eol$eol'); | ||
}); | ||
return; | ||
} | ||
} | ||
// `token` is now the last of the leading comments (perhaps a Copyight | ||
// notice, a Dart language version, etc.) | ||
insertionOffset = commentOnFirstToken.end; | ||
prefix = '$eol$eol'; | ||
} else { | ||
insertionOffset = 0; | ||
prefix = ''; | ||
} | ||
|
||
await builder.addDartFileEdit(file, (builder) { | ||
builder.addDeletion(commentRange); | ||
var commentText = utils.getRangeText(commentRange); | ||
builder.addSimpleInsertion( | ||
insertionOffset, '$prefix${commentText}library;$eol$eol'); | ||
}); | ||
} | ||
|
||
/// The range of the first "block" in [comment]. | ||
/// | ||
/// A [Comment] can contain blank lines (even an end-of-line comment, and an | ||
/// end-of-line doc comment). But for the purpose of this fix, we interpret | ||
/// only the first "block" or "paragraph" of text as what was intented to be | ||
/// the library comment. | ||
SourceRange _rangeOfFirstBlock(Comment comment, LineInfo lineInfo) { | ||
for (var token in comment.tokens) { | ||
var next = token.next; | ||
if (next != null && | ||
lineInfo.getLocation(next.offset).lineNumber > | ||
lineInfo.getLocation(token.end).lineNumber + 1) { | ||
// There is a blank line. Interpret this as two separate doc comments. | ||
return utils.getLinesRange(range.startEnd(comment.tokens.first, token)); | ||
} | ||
} | ||
return utils.getLinesRange(range.node(comment)); | ||
} | ||
} |
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
142 changes: 142 additions & 0 deletions
142
...s_server/test/src/services/correction/fix/move_doc_comment_to_library_directive_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,142 @@ | ||
// Copyright (c) 2022, 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_plugin/utilities/fixes/fixes.dart'; | ||
import 'package:test_reflective_loader/test_reflective_loader.dart'; | ||
|
||
import 'fix_processor.dart'; | ||
|
||
void main() { | ||
defineReflectiveSuite(() { | ||
defineReflectiveTests(MoveDocCommentToLibraryDirectiveTest); | ||
}); | ||
} | ||
|
||
@reflectiveTest | ||
class MoveDocCommentToLibraryDirectiveTest extends FixProcessorLintTest { | ||
@override | ||
FixKind get kind => DartFixKind.MOVE_DOC_COMMENT_TO_LIBRARY_DIRECTIVE; | ||
|
||
@override | ||
String get lintCode => LintNames.dangling_library_doc_comments; | ||
|
||
Future<void> test_commentsAreFirst() async { | ||
await resolveTestCode(''' | ||
// Comment 1. | ||
// Comment 2. | ||
/// Doc comment. | ||
import 'dart:async'; | ||
void f(Completer c) {} | ||
'''); | ||
await assertHasFix(''' | ||
// Comment 1. | ||
// Comment 2. | ||
/// Doc comment. | ||
library; | ||
import 'dart:async'; | ||
void f(Completer c) {} | ||
'''); | ||
} | ||
|
||
Future<void> test_commentsAreFirst_andAnnotations() async { | ||
await resolveTestCode(''' | ||
// Comment 1. | ||
// Comment 2. | ||
@deprecated | ||
/// Doc comment. | ||
import 'dart:async'; | ||
void f(Completer c) {} | ||
'''); | ||
// TODO(srawlins): Fix the 4 newlines below; should be 2. | ||
await assertHasFix(''' | ||
// Comment 1. | ||
// Comment 2. | ||
/// Doc comment. | ||
library; | ||
@deprecated | ||
import 'dart:async'; | ||
void f(Completer c) {} | ||
'''); | ||
} | ||
|
||
Future<void> test_docCommentIsFirst_aboveDeclaration() async { | ||
await resolveTestCode(''' | ||
/// Doc comment. | ||
void f() {} | ||
'''); | ||
await assertHasFix(''' | ||
/// Doc comment. | ||
library; | ||
void f() {} | ||
'''); | ||
} | ||
|
||
Future<void> test_docCommentIsFirst_aboveDeclarationWithComment() async { | ||
await resolveTestCode(''' | ||
/// Library comment. | ||
// Regular comment. | ||
class C {} | ||
'''); | ||
await assertHasFix(''' | ||
/// Library comment. | ||
library; | ||
// Regular comment. | ||
class C {} | ||
'''); | ||
} | ||
|
||
Future<void> test_docCommentIsFirst_aboveDeclarationWithDocComment() async { | ||
await resolveTestCode(''' | ||
/// Library comment. | ||
/// Class comment. | ||
class C {} | ||
'''); | ||
await assertHasFix(''' | ||
/// Library comment. | ||
library; | ||
/// Class comment. | ||
class C {} | ||
'''); | ||
} | ||
|
||
Future<void> test_docCommentIsFirst_aboveDirective() async { | ||
await resolveTestCode(''' | ||
/// Doc comment. | ||
import 'dart:async'; | ||
void f(Completer c) {} | ||
'''); | ||
await assertHasFix(''' | ||
/// Doc comment. | ||
library; | ||
import 'dart:async'; | ||
void f(Completer c) {} | ||
'''); | ||
} | ||
} |
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