Skip to content

Commit fb3f263

Browse files
jensjohaCommit Queue
authored andcommitted
[linter] lines_longer_than_80_chars and document_ignores shouldn't read files
* document_ignores read each file once * lines_longer_than_80_chars read each file for every line > 80 chars (!). This follows the approach in https://dart-review.googlesource.com/c/sdk/+/401424 where the same pattern was identified as bad. Before this CL, if a file had, say, 1000 lines > 80 chars (and having the lint enabled), the lint would read the file - from disk - 1000 times. A somewhat saving grace is that for open files - which I guess is where it's normally run a lot - it normally reads from an overlay file and thus not from disk. Except if you are editing a part in which case the open part is an overlay, but the "parent" (which is for whatever reason also linted) is read from file. So if you have 1000 lines > 80 chars in the "parent" file, and is typing in a part file you're in for a really bad time. This CL should reduce the number of reads in the lint to 0. Change-Id: Iac6c78f50add1bc7a411c0c244a181f5ca2ea25b Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/411300 Reviewed-by: Samuel Rawlins <[email protected]> Commit-Queue: Jens Johansen <[email protected]>
1 parent f66ceaf commit fb3f263

File tree

2 files changed

+11
-5
lines changed

2 files changed

+11
-5
lines changed

pkg/linter/lib/src/rules/document_ignores.dart

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,10 @@
55
import 'package:analyzer/dart/ast/ast.dart';
66
import 'package:analyzer/dart/ast/visitor.dart';
77
import 'package:analyzer/src/ignore_comments/ignore_info.dart' // ignore: implementation_imports
8-
show CommentTokenExtension, CompilationUnitExtension, IgnoredDiagnosticComment;
8+
show
9+
CommentTokenExtension,
10+
CompilationUnitExtension,
11+
IgnoredDiagnosticComment;
912
import 'package:analyzer/src/utilities/extensions/string.dart' // ignore: implementation_imports
1013
show IntExtension;
1114

@@ -25,19 +28,21 @@ class DocumentIgnores extends LintRule {
2528
NodeLintRegistry registry,
2629
LinterContext context,
2730
) {
28-
var visitor = _Visitor(this);
31+
var visitor = _Visitor(this, context);
2932
registry.addCompilationUnit(this, visitor);
3033
}
3134
}
3235

3336
class _Visitor extends SimpleAstVisitor<void> {
3437
final LintRule rule;
38+
final LinterContext context;
3539

36-
_Visitor(this.rule);
40+
_Visitor(this.rule, this.context);
3741

3842
@override
3943
void visitCompilationUnit(CompilationUnit node) {
40-
var content = node.declaredFragment?.source.contents.data;
44+
assert(context.currentUnit?.unit == node);
45+
var content = context.currentUnit?.content;
4146
for (var comment in node.ignoreComments) {
4247
var ignoredElements = comment.ignoredElements;
4348
if (ignoredElements.isEmpty) {

pkg/linter/lib/src/rules/lines_longer_than_80_chars.dart

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,8 @@ class _Visitor extends SimpleAstVisitor<void> {
161161
var lineInfo = node.lineInfo;
162162
var lineCount = lineInfo.lineCount;
163163
var longLines = <_LineInfo>[];
164+
assert(context.currentUnit?.unit == node);
165+
var content = context.currentUnit?.content;
164166
for (var i = 0; i < lineCount; i++) {
165167
var start = lineInfo.getOffsetOfLine(i);
166168
int end;
@@ -170,7 +172,6 @@ class _Visitor extends SimpleAstVisitor<void> {
170172
end = lineInfo.getOffsetOfLine(i + 1) - 1;
171173
var length = end - start;
172174
if (length > 80) {
173-
var content = node.declaredFragment?.source.contents.data;
174175
if (content != null &&
175176
content[end] == _lf &&
176177
content[end - 1] == _cr) {

0 commit comments

Comments
 (0)