Skip to content
This repository was archived by the owner on Nov 20, 2024. It is now read-only.

Commit 0cc2e29

Browse files
authored
improve pubspec state caching (#2642)
1 parent e5c8fe9 commit 0cc2e29

File tree

1 file changed

+39
-33
lines changed

1 file changed

+39
-33
lines changed

lib/src/rules/avoid_web_libraries_in_flutter.dart

Lines changed: 39 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,9 @@ YamlMap _parseYaml(String content) {
4141
}
4242

4343
class AvoidWebLibrariesInFlutter extends LintRule implements NodeLintRule {
44+
/// Cache of most recent analysis root to parsed "hasFlutter" state.
45+
static final Map<String, bool> _rootHasFlutterCache = {};
46+
4447
AvoidWebLibrariesInFlutter()
4548
: super(
4649
name: 'avoid_web_libraries_in_flutter',
@@ -49,34 +52,14 @@ class AvoidWebLibrariesInFlutter extends LintRule implements NodeLintRule {
4952
maturity: Maturity.stable,
5053
group: Group.errors);
5154

52-
@override
53-
void registerNodeProcessors(
54-
NodeLintRegistry registry, LinterContext context) {
55-
var visitor = _Visitor(this);
56-
registry.addCompilationUnit(this, visitor);
57-
registry.addImportDirective(this, visitor);
58-
}
59-
}
60-
61-
class _Visitor extends SimpleAstVisitor<void> {
62-
File? pubspecFile;
63-
64-
final LintRule rule;
65-
bool? _shouldValidateUri;
66-
67-
_Visitor(this.rule);
68-
69-
bool get shouldValidateUri => _shouldValidateUri ??= checkForValidation();
70-
71-
bool checkForValidation() {
72-
var file = pubspecFile;
73-
if (file == null) {
55+
bool hasFlutterDep(File? pubspec) {
56+
if (pubspec == null) {
7457
return false;
7558
}
7659

7760
YamlMap parsedPubspec;
7861
try {
79-
var content = file.readAsStringSync();
62+
var content = pubspec.readAsStringSync();
8063
parsedPubspec = _parseYaml(content);
8164
// ignore: avoid_catches_without_on_clauses
8265
} catch (_) {
@@ -95,25 +78,48 @@ class _Visitor extends SimpleAstVisitor<void> {
9578
null;
9679
}
9780

81+
@override
82+
void registerNodeProcessors(
83+
NodeLintRegistry registry, LinterContext context) {
84+
bool hasFlutter(String root) {
85+
var hasFlutter = _rootHasFlutterCache[root];
86+
if (hasFlutter == null) {
87+
// clear previous cache
88+
_rootHasFlutterCache.clear();
89+
var pubspecFile = locatePubspecFile(context.currentUnit.unit);
90+
hasFlutter = hasFlutterDep(pubspecFile);
91+
_rootHasFlutterCache[root] = hasFlutter;
92+
}
93+
return hasFlutter;
94+
}
95+
96+
var root = context.package?.root;
97+
if (root != null) {
98+
if (hasFlutter(root)) {
99+
var visitor = _Visitor(this);
100+
registry.addImportDirective(this, visitor);
101+
}
102+
}
103+
}
104+
}
105+
106+
class _Visitor extends SimpleAstVisitor<void> {
107+
final LintRule rule;
108+
109+
_Visitor(this.rule);
110+
98111
bool isWebUri(String uri) {
99112
var uriLength = uri.length;
100113
return (uriLength == 9 && uri == 'dart:html') ||
101114
(uriLength == 7 && uri == 'dart:js') ||
102115
(uriLength == 12 && uri == 'dart:js_util');
103116
}
104117

105-
@override
106-
void visitCompilationUnit(CompilationUnit node) {
107-
pubspecFile = locatePubspecFile(node);
108-
}
109-
110118
@override
111119
void visitImportDirective(ImportDirective node) {
112-
if (shouldValidateUri) {
113-
var uriString = node.uri.stringValue;
114-
if (uriString != null && isWebUri(uriString)) {
115-
rule.reportLint(node);
116-
}
120+
var uriString = node.uri.stringValue;
121+
if (uriString != null && isWebUri(uriString)) {
122+
rule.reportLint(node);
117123
}
118124
}
119125
}

0 commit comments

Comments
 (0)