-
Notifications
You must be signed in to change notification settings - Fork 1
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: Add assists for macro documentation comments #78
Merged
+345
−0
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
53155cc
feat: Add assists for macro documentation comments
naipaka e7dbe49
doc: Update README with new macro documentation assists
naipaka 6326111
feat: Prevent wrapping of existing macro template documentation comments
naipaka 0446d4c
Merge branch 'main' into add-dartdoc-macros-assist
naipaka e20145a
Merge branch 'main' into add-dartdoc-macros-assist
naipaka a835697
feat: Remove unnecessary documentation check in WrapWithMacroTemplate…
naipaka File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,6 +6,7 @@ | |
"bools", | ||
"combinators", | ||
"diffscrape", | ||
"endtemplate", | ||
"gocolly", | ||
"interps", | ||
"ints", | ||
|
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
88 changes: 88 additions & 0 deletions
88
packages/altive_lints/lib/src/assists/add_macro_document_comments.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,88 @@ | ||
import 'package:analyzer/dart/ast/ast.dart'; | ||
import 'package:analyzer/source/source_range.dart'; | ||
import 'package:custom_lint_builder/custom_lint_builder.dart'; | ||
|
||
/// {@template altive_lints.AddMacroDocumentComment} | ||
/// An assist to add macro template documentation comments to function | ||
/// or constructor declarations. | ||
/// | ||
/// This assist helps maintain consistent documentation across | ||
/// the entire codebase by adding macro template comments | ||
/// to each function or constructor. These comments can be used | ||
/// for documentation generation or by other tools. | ||
/// | ||
/// Macro template comments follow this format: | ||
/// | ||
/// ```dart | ||
/// /// {[@]macro packageName.functionName} | ||
/// ``` | ||
/// | ||
/// Example: | ||
/// | ||
/// Before: | ||
/// ```dart | ||
/// void myFunction() { | ||
/// // Function implementation | ||
/// } | ||
/// ``` | ||
/// | ||
/// After applying the assist: | ||
/// ```dart | ||
/// /// {[@]macro my_package.myFunction} | ||
/// void myFunction() { | ||
/// // Function implementation | ||
/// } | ||
/// ``` | ||
/// | ||
/// {@endtemplate} | ||
class AddMacroDocumentComment extends DartAssist { | ||
/// {@macro altive_lints.AddMacroDocumentComment} | ||
AddMacroDocumentComment(); | ||
|
||
@override | ||
void run( | ||
CustomLintResolver resolver, | ||
ChangeReporter reporter, | ||
CustomLintContext context, | ||
SourceRange target, | ||
) { | ||
context.registry.addFunctionDeclaration((node) { | ||
_processNode(node, target, reporter, context); | ||
}); | ||
|
||
context.registry.addConstructorDeclaration((node) { | ||
_processNode(node, target, reporter, context); | ||
}); | ||
} | ||
|
||
void _processNode( | ||
AstNode node, | ||
SourceRange target, | ||
ChangeReporter reporter, | ||
CustomLintContext context, | ||
) { | ||
if (!target.intersects(node.sourceRange)) { | ||
return; | ||
} | ||
|
||
final changeBuilder = reporter.createChangeBuilder( | ||
message: 'Add macro documentation comment', | ||
priority: 20, | ||
); | ||
|
||
final packageName = context.pubspec.name; | ||
var name = ''; | ||
|
||
if (node is FunctionDeclaration) { | ||
name = node.name.lexeme; | ||
} else if (node is ConstructorDeclaration) { | ||
name = node.returnType.name; | ||
} | ||
|
||
final macroComment = '/// {@macro $packageName.$name}'; | ||
|
||
changeBuilder.addDartFileEdit((builder) { | ||
builder.addSimpleInsertion(node.offset, '$macroComment\n'); | ||
}); | ||
} | ||
} |
80 changes: 80 additions & 0 deletions
80
packages/altive_lints/lib/src/assists/add_macro_template_document_comment.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,80 @@ | ||
import 'package:analyzer/source/source_range.dart'; | ||
import 'package:custom_lint_builder/custom_lint_builder.dart'; | ||
|
||
/// {@template altive_lints.AddMacroTemplateDocumentComment} | ||
/// A Dart assist that adds a macro template documentation comment to a class | ||
/// declaration if it does not already have one. | ||
/// | ||
/// This assist helps in maintaining consistent documentation across the | ||
/// codebase by ensuring that each class has a macro template comment, which | ||
/// can be used for generating documentation or for other tooling purposes. | ||
/// | ||
/// The macro template comment follows the format: | ||
/// | ||
/// ```dart | ||
/// /// {[@]template packageName.className} | ||
/// /// | ||
/// /// {[@]endtemplate} | ||
/// ``` | ||
/// | ||
/// Example usage: | ||
/// | ||
/// Before: | ||
/// ```dart | ||
/// class MyClass { | ||
/// // Class implementation | ||
/// } | ||
/// ``` | ||
/// | ||
/// After running the assist: | ||
/// ```dart | ||
/// /// {[@]template my_package.MyClass} | ||
/// /// | ||
/// /// {[@]endtemplate} | ||
/// class MyClass { | ||
/// // Class implementation | ||
/// } | ||
/// ``` | ||
/// | ||
/// {@endtemplate} | ||
class AddMacroTemplateDocumentComment extends DartAssist { | ||
/// {@macro altive_lints.AddMacroTemplateDocumentComment} | ||
AddMacroTemplateDocumentComment(); | ||
|
||
@override | ||
void run( | ||
CustomLintResolver resolver, | ||
ChangeReporter reporter, | ||
CustomLintContext context, | ||
SourceRange target, | ||
) { | ||
context.registry.addClassDeclaration((node) { | ||
if (!target.intersects(node.sourceRange)) { | ||
return; | ||
} | ||
|
||
final docComment = node.documentationComment; | ||
if (docComment != null) { | ||
return; | ||
} | ||
|
||
final changeBuilder = reporter.createChangeBuilder( | ||
message: 'Add macro template documentation comment', | ||
priority: 20, | ||
); | ||
|
||
final packageName = context.pubspec.name; | ||
final className = node.name.lexeme; | ||
|
||
final template = [ | ||
'/// {@template $packageName.$className}', | ||
'/// ', | ||
'/// {@endtemplate}', | ||
].join('\n'); | ||
|
||
changeBuilder.addDartFileEdit((builder) { | ||
builder.addSimpleInsertion(node.offset, '$template\n'); | ||
}); | ||
}); | ||
} | ||
} |
87 changes: 87 additions & 0 deletions
87
packages/altive_lints/lib/src/assists/wrap_with_macro_template_document_comment.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,87 @@ | ||
import 'package:analyzer/dart/ast/ast.dart'; | ||
import 'package:analyzer/source/source_range.dart'; | ||
import 'package:custom_lint_builder/custom_lint_builder.dart'; | ||
|
||
/// {@template altive_lints.WrapWithMacroTemplateDocumentComment} | ||
/// A Dart assist that wraps an existing documentation comment with a macro | ||
/// template comment. | ||
/// | ||
/// This assist helps in maintaining consistent documentation across the | ||
/// codebase by ensuring that each documentation comment is wrapped with a | ||
/// macro template, which can be used for generating documentation or for | ||
/// other tooling purposes. | ||
/// | ||
/// The macro template comment follows the format: | ||
/// | ||
/// ```dart | ||
/// /// {[@]template packageName.className} | ||
/// /// Existing documentation comment. | ||
/// /// {[@]endtemplate} | ||
/// ``` | ||
/// | ||
/// Example usage: | ||
/// | ||
/// Before: | ||
/// ```dart | ||
/// /// This is a class. | ||
/// class MyClass { | ||
/// // Class implementation | ||
/// } | ||
/// ``` | ||
/// | ||
/// After running the assist: | ||
/// ```dart | ||
/// /// {[@]template my_package.MyClass} | ||
/// /// This is a class. | ||
/// /// {[@]endtemplate} | ||
/// class MyClass { | ||
/// // Class implementation | ||
/// } | ||
/// ``` | ||
/// | ||
/// {@endtemplate} | ||
class WrapWithMacroTemplateDocumentComment extends DartAssist { | ||
/// {@macro altive_lints.WrapWithMacroTemplateDocumentComment} | ||
WrapWithMacroTemplateDocumentComment(); | ||
|
||
@override | ||
void run( | ||
CustomLintResolver resolver, | ||
ChangeReporter reporter, | ||
CustomLintContext context, | ||
SourceRange target, | ||
) { | ||
context.registry.addComment((node) { | ||
if (!target.intersects(node.sourceRange)) { | ||
return; | ||
} | ||
|
||
final currentComment = node.tokens.join(); | ||
if (currentComment.contains('{@template') && | ||
currentComment.contains('{@endtemplate}')) { | ||
return; | ||
} | ||
|
||
final changeBuilder = reporter.createChangeBuilder( | ||
message: 'Wrap with macro template documentation comment', | ||
priority: 20, | ||
); | ||
|
||
final packageName = context.pubspec.name; | ||
final classNode = node.parent; | ||
var className = ''; | ||
if (classNode is ClassDeclaration) { | ||
className = classNode.name.lexeme; | ||
} | ||
|
||
final templateStart = '/// {@template $packageName.$className}'; | ||
const templateEnd = '/// {@endtemplate}'; | ||
|
||
changeBuilder.addDartFileEdit((builder) { | ||
builder | ||
..addSimpleInsertion(node.offset, '$templateStart\n') | ||
..addSimpleInsertion(node.end, '\n$templateEnd'); | ||
}); | ||
}); | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In the example, using
@endtemplate
as is would be interpreted as the end of the template, so I’ve enclosed it in[]
to avoid this!