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

fix: change elements equality check to overcome incorrect libs resolution #891

Merged
merged 3 commits into from Jun 20, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Expand Up @@ -8,6 +8,7 @@
* fix: improve context root included files calculation.
* feat: add [`avoid-banned-imports`](https://dartcodemetrics.dev/docs/rules/common/avoid-banned-imports) rule
* fix: resolve package with imported analysis options.
* fix: change elements equality check to overcome incorrect libs resolution.
* feat: add configuration to [`prefer-extracting-callbacks`](https://dartcodemetrics.dev/docs/rules/flutter/prefer-extracting-callbacks)
* feat: improve [`checkstyle`](https://dartcodemetrics.dev/docs/cli/analyze#checkstyle) report, added metrics entries.

Expand Down
24 changes: 22 additions & 2 deletions lib/src/analyzers/unused_code_analyzer/unused_code_analyzer.dart
Expand Up @@ -169,8 +169,28 @@ class UnusedCodeAnalyzer {
}

bool _isUsed(Element usedElement, Element element) =>
element == usedElement ||
element is PropertyInducingElement && element.getter == usedElement;
_isEqualElements(usedElement, element) ||
element is PropertyInducingElement &&
_isEqualElements(usedElement, element.getter);

bool _isEqualElements(Element left, Element? right) {
if (left == right) {
return true;
}

final usedLibrary = left.library;
final declaredSource = right?.librarySource;

// This is a hack to fix incorrect libraries resolution.
// Should be removed after new analyzer version is available.
// see: https://github.com/dart-lang/sdk/issues/49182
return usedLibrary != null &&
declaredSource != null &&
left.name == right?.name &&
usedLibrary.units
.map((unit) => unit.source.fullName)
.contains(declaredSource.fullName);
}

bool _isUnused(
FileElementsUsage codeUsages,
Expand Down
@@ -0,0 +1 @@
export 'src/usage.dart';
@@ -0,0 +1,7 @@
part of 'resources.dart';

class AppIcons {
AppIcons._();

static const String authError = 'assets/icons/auth_error.svg';
}
@@ -0,0 +1,7 @@
part of 'resources.dart';

class AppStrings {
AppStrings._();

static const String title = 'Application title';
}
@@ -0,0 +1,2 @@
part 'app_icons.dart';
part 'app_strings.dart';
6 changes: 6 additions & 0 deletions test/resources/unused_code_analyzer/library/src/usage.dart
@@ -0,0 +1,6 @@
import '../resources/resources.dart';

/// Checks if you are awesome. Spoiler: you are.
class Awesome {
bool get isAwesome => AppStrings.title.isNotEmpty;
}
Expand Up @@ -36,7 +36,7 @@ void main() {
});

test('should report 3 files and not report excluded file', () {
expect(result, hasLength(3));
expect(result, hasLength(4));
});

test('should analyze not used files', () async {
Expand Down Expand Up @@ -139,6 +139,20 @@ void main() {
expect(eightsIssue.location.line, 171);
expect(eightsIssue.location.column, 1);
});

test('should analyze elements from incorrectly parsed library', () {
final report = result.firstWhere(
(report) => report.path.endsWith('app_icons.dart'),
);

expect(report.issues, hasLength(1));

final firstIssue = report.issues.first;
expect(firstIssue.declarationName, 'AppIcons');
expect(firstIssue.declarationType, 'class');
expect(firstIssue.location.line, 3);
expect(firstIssue.location.column, 1);
});
});

test('should return a reporter', () {
Expand Down