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

Commit 2684de2

Browse files
authored
Merge 4aaf567 into 187b3cd
2 parents 187b3cd + 4aaf567 commit 2684de2

File tree

5 files changed

+3
-106
lines changed

5 files changed

+3
-106
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
* feat: add static code diagnostic [`prefer-define-hero-tag`](https://dcm.dev/docs/individuals/rules/common/prefer-define-hero-tag).
88
* chore: restrict `analyzer` version to `>=5.1.0 <5.8.0`.
99
* feat: add static code diagnostic [`avoid-substring`](https://dcm.dev/docs/individuals/rules/common/avoid-substring).
10+
* fix: correctly track prefixes usage for check-unused-code.
1011

1112
## 5.6.0
1213

lib/src/analyzers/unused_code_analyzer/models/file_elements_usage.dart

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,8 @@
11
import 'package:analyzer/dart/element/element.dart';
22

3-
import 'prefix_element_usage.dart';
4-
53
/// A container with information about used imports prefixes and used imported
64
/// elements.
75
class FileElementsUsage {
8-
/// The map of referenced prefix elements and the elements that they prefix.
9-
final Map<PrefixElement, PrefixElementUsage> prefixMap = {};
10-
116
/// The set of referenced top-level elements.
127
final Set<Element> elements = {};
138

@@ -19,7 +14,6 @@ class FileElementsUsage {
1914
final Map<Set<String>, Set<Element>> conditionalElements = {};
2015

2116
void merge(FileElementsUsage other) {
22-
prefixMap.addAll(other.prefixMap);
2317
elements.addAll(other.elements);
2418
usedExtensions.addAll(other.usedExtensions);
2519
exports.addAll(other.exports);

lib/src/analyzers/unused_code_analyzer/models/prefix_element_usage.dart

Lines changed: 0 additions & 17 deletions
This file was deleted.

lib/src/analyzers/unused_code_analyzer/unused_code_analyzer.dart

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -220,15 +220,7 @@ class UnusedCodeAnalyzer {
220220
!codeUsages.elements
221221
.any((usedElement) => _isUsed(usedElement, element)) &&
222222
!codeUsages.usedExtensions
223-
.any((usedElement) => _isUsed(usedElement, element)) &&
224-
!codeUsages.prefixMap.values.any((usage) =>
225-
usage.paths.contains(path) &&
226-
usage.elements.any(
227-
(usedElement) =>
228-
_isUsed(usedElement, element) ||
229-
(usedElement.name == element.name &&
230-
usedElement.kind == element.kind),
231-
));
223+
.any((usedElement) => _isUsed(usedElement, element));
232224

233225
UnusedCodeIssue _createUnusedCodeIssue(
234226
ElementImpl element,

lib/src/analyzers/unused_code_analyzer/used_code_visitor.dart

Lines changed: 1 addition & 74 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ import 'package:collection/collection.dart';
77

88
import '../../utils/flutter_types_utils.dart';
99
import 'models/file_elements_usage.dart';
10-
import 'models/prefix_element_usage.dart';
1110

1211
// Copied from https://github.com/dart-lang/sdk/blob/main/pkg/analyzer/lib/src/error/imports_verifier.dart#L15
1312

@@ -124,40 +123,6 @@ class UsedCodeVisitor extends RecursiveAstVisitor<void> {
124123
}
125124
}
126125

127-
/// If the given [identifier] is prefixed with a [PrefixElement], fill the
128-
/// corresponding `UsedImportedElements.prefixMap` entry and return `true`.
129-
bool _recordPrefixMap(SimpleIdentifier identifier, Element element) {
130-
bool recordIfTargetIsPrefixElement(Expression? target) {
131-
if (target is SimpleIdentifier) {
132-
final targetElement = target.staticElement;
133-
if (targetElement is PrefixElement) {
134-
fileElementsUsage.prefixMap
135-
.putIfAbsent(
136-
targetElement,
137-
() => PrefixElementUsage(_getPrefixUsagePaths(target), {}),
138-
)
139-
.add(element);
140-
141-
return true;
142-
}
143-
}
144-
145-
return false;
146-
}
147-
148-
final parent = identifier.parent;
149-
150-
if (parent is MethodInvocation && parent.methodName == identifier) {
151-
return recordIfTargetIsPrefixElement(parent.target);
152-
}
153-
154-
if (parent is PrefixedIdentifier && parent.identifier == identifier) {
155-
return recordIfTargetIsPrefixElement(parent.prefix);
156-
}
157-
158-
return false;
159-
}
160-
161126
bool _recordConditionalElement(Element element) {
162127
// ignore: deprecated_member_use
163128
final elementPath = element.enclosingElement3?.source?.fullName;
@@ -194,7 +159,7 @@ class UsedCodeVisitor extends RecursiveAstVisitor<void> {
194159
}
195160

196161
void _visitIdentifier(SimpleIdentifier identifier, Element? element) {
197-
if (element == null) {
162+
if (element == null || element is PrefixElement) {
198163
return;
199164
}
200165

@@ -214,11 +179,6 @@ class UsedCodeVisitor extends RecursiveAstVisitor<void> {
214179
return;
215180
}
216181

217-
// Record `importPrefix.identifier` into 'prefixMap'.
218-
if (_recordPrefixMap(identifier, element)) {
219-
return;
220-
}
221-
222182
// ignore: deprecated_member_use
223183
final enclosingElement = element.enclosingElement3;
224184
if (enclosingElement is CompilationUnitElement) {
@@ -227,11 +187,6 @@ class UsedCodeVisitor extends RecursiveAstVisitor<void> {
227187
_recordUsedExtension(enclosingElement);
228188

229189
return;
230-
} else if (element is PrefixElement) {
231-
fileElementsUsage.prefixMap.putIfAbsent(
232-
element,
233-
() => PrefixElementUsage(_getPrefixUsagePaths(identifier), {}),
234-
);
235190
} else if (element is MultiplyDefinedElement) {
236191
// If the element is multiply defined then call this method recursively
237192
// for each of the conflicting elements.
@@ -244,34 +199,6 @@ class UsedCodeVisitor extends RecursiveAstVisitor<void> {
244199
}
245200
}
246201

247-
Iterable<String> _getPrefixUsagePaths(SimpleIdentifier target) {
248-
final root = target.root;
249-
250-
if (root is! CompilationUnit) {
251-
return [];
252-
}
253-
254-
return root.directives.fold<List<String>>([], (previousValue, directive) {
255-
if (directive is ImportDirective &&
256-
directive.prefix?.name == target.name) {
257-
// ignore: deprecated_member_use
258-
final path = directive.element2?.importedLibrary?.source.fullName;
259-
if (path != null) {
260-
previousValue.add(path);
261-
}
262-
263-
for (final config in directive.configurations) {
264-
final uri = config.resolvedUri;
265-
if (uri is DirectiveUriWithSource) {
266-
previousValue.add(uri.source.fullName);
267-
}
268-
}
269-
}
270-
271-
return previousValue;
272-
});
273-
}
274-
275202
bool _isVariableDeclarationInitializer(
276203
AstNode? target,
277204
SimpleIdentifier identifier,

0 commit comments

Comments
 (0)