@@ -5,40 +5,49 @@ import 'package:analyzer/dart/ast/visitor.dart';
5
5
import 'package:analyzer/source/line_info.dart' ;
6
6
7
7
import '../../models/issue.dart' ;
8
+ import '../../models/rule_documentation.dart' ;
8
9
import '../../models/severity.dart' ;
9
10
import '../../utils/node_utils.dart' ;
10
11
import '../../utils/rule_utils.dart' ;
11
- import 'obsolete_rule .dart' ;
12
+ import '../rule .dart' ;
12
13
13
- // Inspired by TSLint (https://palantir.github.io/tslint/rules/newline-before-return/)
14
+ part 'return_statement_visitor.dart' ;
14
15
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
+ );
17
22
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' ;
19
27
20
28
NewlineBeforeReturnRule ({Map <String , Object > config = const {}})
21
29
: super (
22
30
id: ruleId,
31
+ documentation: _documentation,
23
32
severity: readSeverity (config, Severity .style),
24
33
excludes: readExcludes (config),
25
34
);
26
35
27
36
@override
28
37
Iterable <Issue > check (ResolvedUnitResult source) {
29
- final _visitor = _Visitor ();
38
+ final visitor = _Visitor ();
30
39
31
- source.unit? .visitChildren (_visitor );
40
+ source.unit? .visitChildren (visitor );
32
41
33
- return _visitor .statements
42
+ return visitor .statements
34
43
// return statement is in a block
35
- .where ((statement) =>
36
- statement.parent != null && statement.parent is Block )
44
+ .where ((statement) => statement.parent is Block )
37
45
// return statement isn't first token in a block
38
46
.where ((statement) =>
39
47
statement.returnKeyword.previous != statement.parent? .beginToken)
40
48
.where ((statement) {
41
49
final lineInfo = source.unit! .lineInfo! ;
50
+
42
51
final previousTokenLine = lineInfo
43
52
.getLocation (statement.returnKeyword.previous! .end)
44
53
.lineNumber;
@@ -52,48 +61,32 @@ class NewlineBeforeReturnRule extends ObsoleteRule {
52
61
})
53
62
.map ((statement) => createIssue (
54
63
rule: this ,
55
- location: nodeLocation (
56
- node: statement,
57
- source: source,
58
- withCommentOrMetadata: true ,
59
- ),
64
+ location: nodeLocation (node: statement, source: source),
60
65
message: _failure,
61
66
))
62
67
.toList (growable: false );
63
68
}
69
+ }
64
70
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;
75
73
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;
77
80
}
78
81
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;
87
83
}
88
84
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;
98
89
}
90
+
91
+ return latestCommentToken;
99
92
}
0 commit comments