Skip to content
This repository was archived by the owner on Jul 16, 2023. It is now read-only.

Commit ec6df45

Browse files
committed
chore: renew New Line Before Return rule
1 parent 144421d commit ec6df45

File tree

5 files changed

+57
-51
lines changed

5 files changed

+57
-51
lines changed

lib/src/obsoleted/rules_factory.dart

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import '../rules/double_literal_format/double_literal_format.dart';
2+
import '../rules/newline_before_return/newline_before_return.dart';
23
import '../rules/rule.dart';
34
import 'rules/avoid_late_keyword.dart';
45
import 'rules/avoid_non_null_assertion_rule.dart';
@@ -9,7 +10,6 @@ import 'rules/binary_expression_operand_order_rule.dart';
910
import 'rules/component_annotation_arguments_ordering.dart';
1011
import 'rules/member_ordering.dart';
1112
import 'rules/member_ordering_extended/member_ordering_extended_rule.dart';
12-
import 'rules/newline_before_return.dart';
1313
import 'rules/no_boolean_literal_compare_rule.dart';
1414
import 'rules/no_empty_block.dart';
1515
import 'rules/no_equal_arguments.dart';

lib/src/obsoleted/rules/newline_before_return.dart renamed to lib/src/rules/newline_before_return/newline_before_return.dart

+36-43
Original file line numberDiff line numberDiff line change
@@ -5,40 +5,49 @@ import 'package:analyzer/dart/ast/visitor.dart';
55
import 'package:analyzer/source/line_info.dart';
66

77
import '../../models/issue.dart';
8+
import '../../models/rule_documentation.dart';
89
import '../../models/severity.dart';
910
import '../../utils/node_utils.dart';
1011
import '../../utils/rule_utils.dart';
11-
import 'obsolete_rule.dart';
12+
import '../rule.dart';
1213

13-
// Inspired by TSLint (https://palantir.github.io/tslint/rules/newline-before-return/)
14+
part 'return_statement_visitor.dart';
1415

15-
class NewlineBeforeReturnRule extends ObsoleteRule {
16-
static const String ruleId = 'newline-before-return';
16+
// Inspired by ESLint (https://eslint.org/docs/rules/newline-before-return)
17+
18+
const _documentation = RuleDocumentation(
19+
name: 'New Line Before Return',
20+
brief: 'Enforces blank line between statements and return in a block',
21+
);
1722

18-
static const _failure = 'Missing blank line before return';
23+
const _failure = 'Missing blank line before return';
24+
25+
class NewlineBeforeReturnRule extends Rule {
26+
static const String ruleId = 'newline-before-return';
1927

2028
NewlineBeforeReturnRule({Map<String, Object> config = const {}})
2129
: super(
2230
id: ruleId,
31+
documentation: _documentation,
2332
severity: readSeverity(config, Severity.style),
2433
excludes: readExcludes(config),
2534
);
2635

2736
@override
2837
Iterable<Issue> check(ResolvedUnitResult source) {
29-
final _visitor = _Visitor();
38+
final visitor = _Visitor();
3039

31-
source.unit?.visitChildren(_visitor);
40+
source.unit?.visitChildren(visitor);
3241

33-
return _visitor.statements
42+
return visitor.statements
3443
// return statement is in a block
35-
.where((statement) =>
36-
statement.parent != null && statement.parent is Block)
44+
.where((statement) => statement.parent is Block)
3745
// return statement isn't first token in a block
3846
.where((statement) =>
3947
statement.returnKeyword.previous != statement.parent?.beginToken)
4048
.where((statement) {
4149
final lineInfo = source.unit!.lineInfo!;
50+
4251
final previousTokenLine = lineInfo
4352
.getLocation(statement.returnKeyword.previous!.end)
4453
.lineNumber;
@@ -52,48 +61,32 @@ class NewlineBeforeReturnRule extends ObsoleteRule {
5261
})
5362
.map((statement) => createIssue(
5463
rule: this,
55-
location: nodeLocation(
56-
node: statement,
57-
source: source,
58-
withCommentOrMetadata: true,
59-
),
64+
location: nodeLocation(node: statement, source: source),
6065
message: _failure,
6166
))
6267
.toList(growable: false);
6368
}
69+
}
6470

65-
Token _optimalToken(Token token, LineInfo lineInfo) {
66-
var optimalToken = token;
67-
68-
var commentToken = _latestCommentToken(token);
69-
while (commentToken != null &&
70-
lineInfo.getLocation(commentToken.end).lineNumber + 1 >=
71-
lineInfo.getLocation(optimalToken.offset).lineNumber) {
72-
optimalToken = commentToken;
73-
commentToken = commentToken.previous;
74-
}
71+
Token _optimalToken(Token token, LineInfo lineInfo) {
72+
var optimalToken = token;
7573

76-
return optimalToken;
74+
var commentToken = _latestCommentToken(token);
75+
while (commentToken != null &&
76+
lineInfo.getLocation(commentToken.end).lineNumber + 1 >=
77+
lineInfo.getLocation(optimalToken.offset).lineNumber) {
78+
optimalToken = commentToken;
79+
commentToken = commentToken.previous;
7780
}
7881

79-
Token? _latestCommentToken(Token token) {
80-
Token? latestCommentToken = token.precedingComments;
81-
while (latestCommentToken?.next != null) {
82-
latestCommentToken = latestCommentToken?.next;
83-
}
84-
85-
return latestCommentToken;
86-
}
82+
return optimalToken;
8783
}
8884

89-
class _Visitor extends RecursiveAstVisitor<void> {
90-
final _statements = <ReturnStatement>[];
91-
92-
Iterable<ReturnStatement> get statements => _statements;
93-
94-
@override
95-
void visitReturnStatement(ReturnStatement node) {
96-
super.visitReturnStatement(node);
97-
_statements.add(node);
85+
Token? _latestCommentToken(Token token) {
86+
Token? latestCommentToken = token.precedingComments;
87+
while (latestCommentToken?.next != null) {
88+
latestCommentToken = latestCommentToken?.next;
9889
}
90+
91+
return latestCommentToken;
9992
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
part of 'newline_before_return.dart';
2+
3+
class _Visitor extends RecursiveAstVisitor<void> {
4+
final _statements = <ReturnStatement>[];
5+
6+
Iterable<ReturnStatement> get statements => _statements;
7+
8+
@override
9+
void visitReturnStatement(ReturnStatement node) {
10+
super.visitReturnStatement(node);
11+
_statements.add(node);
12+
}
13+
}

test/obsoleted/rules/newline_before_return/examples/example.dart renamed to test/resources/newline_before_return_example.dart

+1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
// ignore_for_file: always_put_control_body_on_new_line, newline-before-return
12
int simpleFunction() {
23
var a = 4;
34

+6-7
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,11 @@
11
@TestOn('vm')
22
import 'package:dart_code_metrics/src/models/severity.dart';
3-
import 'package:dart_code_metrics/src/obsoleted/rules/newline_before_return.dart';
3+
import 'package:dart_code_metrics/src/rules/newline_before_return/newline_before_return.dart';
44
import 'package:test/test.dart';
55

6-
import '../../../helpers/rule_test_helper.dart';
6+
import '../helpers/rule_test_helper.dart';
77

8-
const _examplePath =
9-
'test/obsoleted/rules/newline_before_return/examples/example.dart';
8+
const _examplePath = 'test/resources/newline_before_return_example.dart';
109

1110
void main() {
1211
group('NewlineBeforeReturnRule', () {
@@ -27,10 +26,10 @@ void main() {
2726

2827
RuleTestHelper.verifyIssues(
2928
issues: issues,
30-
startOffsets: [177, 898, 1060],
31-
startLines: [12, 57, 69],
29+
startOffsets: [256, 977, 1139],
30+
startLines: [13, 58, 70],
3231
startColumns: [5, 5, 5],
33-
endOffsets: [190, 911, 1073],
32+
endOffsets: [269, 990, 1152],
3433
locationTexts: [
3534
'return a + 1;',
3635
'return a + 2;',

0 commit comments

Comments
 (0)