Skip to content

Commit

Permalink
Merge pull request #110 from The-Redhat/fix_analyzer_6.0_errors
Browse files Browse the repository at this point in the history
fix analyzer 6.0 errors
  • Loading branch information
santitigaga authored Apr 5, 2024
2 parents 5679d0e + 09e9b20 commit 2629852
Show file tree
Hide file tree
Showing 9 changed files with 58 additions and 31 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,7 @@ abstract class IntlBaseVisitor extends GeneralizingAstVisitor<void> {

@override
void visitFieldDeclaration(FieldDeclaration node) {
// ignore: deprecated_member_use
if (node.fields.type?.as<NamedType>()?.name.name != 'String') {
if (node.fields.type?.as<NamedType>()?.name2.lexeme != 'String') {
return;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,7 @@ class _Visitor extends RecursiveAstVisitor<void> {
void visitVariableDeclaration(VariableDeclaration node) {
super.visitVariableDeclaration(node);

// ignore: deprecated_member_use
if (node.declaredElement?.enclosingElement3 is CompilationUnitElement) {
if (node.declaredElement?.enclosingElement is CompilationUnitElement) {
if (_isNodeValid(node)) {
_declarations.add(node);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ class _Visitor extends RecursiveAstVisitor<void> {
allowInitialized && node.initializer != null;

bool _hasIgnoredType(VariableDeclaration node) => ignoredTypes.contains(
// ignore: deprecated_member_use
node.declaredElement2?.type.getDisplayString(withNullability: false),
node.declaredElement?.type.getDisplayString(withNullability: false),
);
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,5 @@ class _Visitor extends RecursiveAstVisitor<void> {

bool _hasObjectType(TypeAnnotation? type) =>
type?.type?.isDartCoreObject ??
// ignore: deprecated_member_use
(type is NamedType && type.name.name == 'Object');
(type is NamedType && type.name2.lexeme == 'Object');
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,7 @@ class InvocationsVisitor extends RecursiveAstVisitor<void> {
void visitExportDirective(ExportDirective node) {
super.visitExportDirective(node);

// ignore: deprecated_member_use
final uri = node.element2?.uri;
final uri = node.element?.uri;
if (uri is DirectiveUriWithSource) {
invocationsUsages.exports.add(uri.source.fullName);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,14 @@ class FileElementsUsage {

final Map<Set<String>, Set<Element>> conditionalElements = {};

/// The map of referenced prefix elements and the elements that they prefix.
final Map<PrefixElement, List<Element>> prefixMap = {};

void merge(FileElementsUsage other) {
elements.addAll(other.elements);
usedExtensions.addAll(other.usedExtensions);
exports.addAll(other.exports);
conditionalElements.addAll(other.conditionalElements);
prefixMap.addAll(other.prefixMap);
}
}
53 changes: 43 additions & 10 deletions lib/src/analyzers/unused_code_analyzer/used_code_visitor.dart
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,7 @@ class UsedCodeVisitor extends RecursiveAstVisitor<void> {

return (uri is DirectiveUriWithSource) ? uri.source.fullName : null;
}).whereNotNull();
// ignore: deprecated_member_use
final mainImport = node.element2?.importedLibrary?.source.fullName;
final mainImport = node.element?.importedLibrary?.source.fullName;

final allPaths = {if (mainImport != null) mainImport, ...paths};

Expand All @@ -42,8 +41,7 @@ class UsedCodeVisitor extends RecursiveAstVisitor<void> {
void visitExportDirective(ExportDirective node) {
super.visitExportDirective(node);

// ignore: deprecated_member_use
final path = node.element2?.exportedLibrary?.source.fullName;
final path = node.element?.exportedLibrary?.source.fullName;
if (path != null) {
fileElementsUsage.exports.add(path);
}
Expand Down Expand Up @@ -97,6 +95,44 @@ class UsedCodeVisitor extends RecursiveAstVisitor<void> {
_visitIdentifier(node, node.staticElement);
}

@override
void visitNamedType(NamedType node) {
_recordPrefixedElement(node.importPrefix, node.element);
super.visitNamedType(node);
}

void _recordPrefixedElement(
ImportPrefixReference? importPrefix,
Element? element,
) {
if (element is MultiplyDefinedElement) {
for (final component in element.conflictingElements) {
_recordPrefixedElement(importPrefix, component);

return;
}
}

// Invalid code can use `importPrefix` as a named type;
if (element is PrefixElement) {
fileElementsUsage.prefixMap[element] ??= [];

return;
}

if (importPrefix != null) {
final prefixElement = importPrefix.element;
if (prefixElement is PrefixElement) {
final map = fileElementsUsage.prefixMap[prefixElement] ??= [];
if (element != null) {
map.add(element);
}
}
} else if (element != null) {
_recordUsedElement(element);
}
}

void _recordAssignmentTarget(
CompoundAssignmentExpression node,
Expression target,
Expand All @@ -115,17 +151,15 @@ class UsedCodeVisitor extends RecursiveAstVisitor<void> {

void _recordIfExtensionMember(Element? element) {
if (element != null) {
// ignore: deprecated_member_use
final enclosingElement = element.enclosingElement3;
final enclosingElement = element.enclosingElement;
if (enclosingElement is ExtensionElement) {
_recordUsedExtension(enclosingElement);
}
}
}

bool _recordConditionalElement(Element element) {
// ignore: deprecated_member_use
final elementPath = element.enclosingElement3?.source?.fullName;
final elementPath = element.enclosingElement?.source?.fullName;
if (elementPath == null) {
return false;
}
Expand Down Expand Up @@ -179,8 +213,7 @@ class UsedCodeVisitor extends RecursiveAstVisitor<void> {
return;
}

// ignore: deprecated_member_use
final enclosingElement = element.enclosingElement3;
final enclosingElement = element.enclosingElement;
if (enclosingElement is CompilationUnitElement) {
_recordUsedElement(element);
} else if (enclosingElement is ExtensionElement) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,18 +49,15 @@ class UnusedFilesVisitor extends GeneralizingAstVisitor<void> {

String? _getAbsolutePath(UriBasedDirective node) {
if (node is ImportDirective) {
// ignore: deprecated_member_use
return node.element2?.importedLibrary?.source.fullName;
return node.element?.importedLibrary?.source.fullName;
}

if (node is ExportDirective) {
// ignore: deprecated_member_use
return node.element2?.exportedLibrary?.source.fullName;
return node.element?.exportedLibrary?.source.fullName;
}

if (node is PartDirective) {
// ignore: deprecated_member_use
final uri = node.element2?.uri;
final uri = node.element?.uri;
if (uri is DirectiveUriWithSource) {
return uri.source.fullName;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -107,8 +107,7 @@ class UnusedL10nVisitor extends RecursiveAstVisitor<void> {

bool _matchExtension(Expression? target) =>
target is PrefixedIdentifier &&
// ignore: deprecated_member_use
target.staticElement?.enclosingElement3 is ExtensionElement;
target.staticElement?.enclosingElement is ExtensionElement;

bool _matchStaticGetter(Expression? target) =>
target is PrefixedIdentifier &&
Expand Down Expand Up @@ -146,15 +145,14 @@ class UnusedL10nVisitor extends RecursiveAstVisitor<void> {
) {
final staticElement =
// ignore: deprecated_member_use
target.constructorName.staticElement?.enclosingElement3;
target.constructorName.staticElement?.enclosingElement;

_tryAddInvocation(staticElement, name);
}

void _addMemberInvocationOnAccessor(SimpleIdentifier target, String name) {
final staticElement =
// ignore: deprecated_member_use
target.staticElement?.enclosingElement3 as ExtensionElement;
target.staticElement?.enclosingElement as ExtensionElement;

for (final element in staticElement.accessors) {
if (_classPattern.hasMatch(element.returnType.toString())) {
Expand Down

0 comments on commit 2629852

Please sign in to comment.