-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Add
prefer_space_between_elements
rule (#33)
- Loading branch information
Showing
3 changed files
with
134 additions
and
0 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
111 changes: 111 additions & 0 deletions
111
packages/altive_lints/lib/src/lints/prefer_space_between_elements.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,111 @@ | ||
import 'package:analyzer/dart/ast/ast.dart'; | ||
import 'package:analyzer/error/listener.dart'; | ||
import 'package:analyzer/source/line_info.dart'; | ||
import 'package:custom_lint_builder/custom_lint_builder.dart'; | ||
|
||
/// A `prefer_space_between_elements` rule that enforces | ||
/// spacing conventions within class definitions by requiring | ||
/// a blank line between the constructor and fields, | ||
/// and between the constructor and the build method. | ||
/// | ||
/// Proper spacing enhances code readability and organization, | ||
/// making it easier to visually distinguish between | ||
/// different sections of a class. | ||
/// | ||
/// ### Example | ||
/// | ||
/// #### BAD: | ||
/// | ||
/// ```dart | ||
/// class MyWidget extends StatelessWidget { | ||
/// final String title; | ||
/// MyWidget(this.title); | ||
/// @override | ||
/// Widget build(BuildContext context) { | ||
/// return Text(title); | ||
/// } | ||
/// } | ||
/// ``` | ||
/// | ||
/// #### GOOD: | ||
/// | ||
/// ```dart | ||
/// class MyWidget extends StatelessWidget { | ||
/// final String title; | ||
/// | ||
/// MyWidget(this.title); | ||
/// | ||
/// @override | ||
/// Widget build(BuildContext context) { | ||
/// return Text(title); | ||
/// } | ||
/// } | ||
/// ``` | ||
class PreferSpaceBetweenElements extends DartLintRule { | ||
const PreferSpaceBetweenElements() : super(code: _code); | ||
|
||
static const _code = LintCode( | ||
name: 'prefer_space_between_elements', | ||
problemMessage: | ||
'Ensure there is a blank line between constructor and fields, ' | ||
'and between constructor and build method.', | ||
); | ||
|
||
@override | ||
void run( | ||
CustomLintResolver resolver, | ||
ErrorReporter reporter, | ||
CustomLintContext context, | ||
) { | ||
context.registry.addClassDeclaration((node) { | ||
final lineInfo = resolver.lineInfo; | ||
final members = node.members; | ||
for (var i = 0; i < members.length - 1; i++) { | ||
final currentMember = members[i]; | ||
final nextMember = members[i + 1]; | ||
|
||
// No blank line between constructor and build method. | ||
if (currentMember is ConstructorDeclaration && | ||
nextMember is MethodDeclaration && | ||
nextMember.name.lexeme == 'build') { | ||
if (!_hasBlankLineBetween(currentMember, nextMember, lineInfo)) { | ||
reporter.reportErrorForNode(code, nextMember); | ||
} | ||
} | ||
|
||
// No blank line between fields and constructor. | ||
if (currentMember is FieldDeclaration && | ||
nextMember is ConstructorDeclaration) { | ||
if (!_hasBlankLineBetween(currentMember, nextMember, lineInfo)) { | ||
reporter.reportErrorForNode(code, nextMember); | ||
} | ||
} | ||
|
||
// No blank line between constructor and fields. | ||
if (currentMember is ConstructorDeclaration && | ||
nextMember is FieldDeclaration) { | ||
if (!_hasBlankLineBetween(currentMember, nextMember, lineInfo)) { | ||
reporter.reportErrorForNode(code, nextMember); | ||
} | ||
} | ||
|
||
// No blank line between constructor and fields. | ||
if (currentMember is FieldDeclaration && | ||
nextMember is MethodDeclaration && | ||
nextMember.name.lexeme == 'build') { | ||
if (!_hasBlankLineBetween(currentMember, nextMember, lineInfo)) { | ||
reporter.reportErrorForNode(code, nextMember); | ||
} | ||
} | ||
} | ||
}); | ||
} | ||
|
||
/// Returns `true` if there is a blank line between [first] and [second]. | ||
bool _hasBlankLineBetween(AstNode first, AstNode second, LineInfo lineInfo) { | ||
final firstEndLine = lineInfo.getLocation(first.endToken.end).lineNumber; | ||
final secondStartLine = | ||
lineInfo.getLocation(second.beginToken.offset).lineNumber; | ||
return (secondStartLine - firstEndLine) > 1; | ||
} | ||
} |
21 changes: 21 additions & 0 deletions
21
packages/altive_lints/lint_test/lints/prefer_space_between_elements.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,21 @@ | ||
import 'package:flutter/material.dart'; | ||
|
||
class MyWidget extends StatelessWidget { | ||
const MyWidget({super.key}); // expect_lint: prefer_space_between_elements | ||
@override | ||
Widget build(BuildContext context) { | ||
return const Placeholder(); | ||
} | ||
} | ||
|
||
class MyWidget2 extends StatelessWidget { | ||
const MyWidget2({ | ||
super.key, | ||
required this.id, | ||
}); // expect_lint: prefer_space_between_elements | ||
final String id; // expect_lint: prefer_space_between_elements | ||
@override | ||
Widget build(BuildContext context) { | ||
return const Placeholder(); | ||
} | ||
} |