Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions lib/analysis_options.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -288,3 +288,6 @@ linter:
- use_test_throws_matchers
- valid_regexps
- void_checks

formatter:
trailing_commas: preserve
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ class ExcludedIdentifiersListParameter {

/// Returns whether the target node should be ignored during analysis.
bool shouldIgnore(Declaration node) {
final declarationName = node.declaredElement?.name;
final declarationName = node.declaredFragment?.name2;

final excludedItem = exclude.firstWhereOrNull(
(e) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -159,11 +159,10 @@ your `debugPrint` call in a `!kReleaseMode` check.""",
case PrefixedIdentifier():
final prefix = node.prefix.name;
name = node.name.replaceAll('$prefix.', '');
sourcePath = node.staticElement?.librarySource?.uri.toString() ?? '';

sourcePath = node.element?.library2?.uri.toString() ?? '';
case SimpleIdentifier():
name = node.name;
sourcePath = node.staticElement?.librarySource?.uri.toString() ?? '';
sourcePath = node.element?.library2?.uri.toString() ?? '';

default:
return false;
Expand Down Expand Up @@ -194,10 +193,10 @@ your `debugPrint` call in a `!kReleaseMode` check.""",
final prefix = node.prefix.name;

name = node.name.replaceAll('$prefix.', '');
sourcePath = node.staticElement?.librarySource?.uri.toString() ?? '';
sourcePath = node.element?.library2?.uri.toString() ?? '';
case SimpleIdentifier():
name = node.name;
sourcePath = node.staticElement?.librarySource?.uri.toString() ?? '';
sourcePath = node.element?.library2?.uri.toString() ?? '';
default:
return false;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import 'package:analyzer/dart/ast/ast.dart';
import 'package:analyzer/dart/ast/visitor.dart';
import 'package:analyzer/dart/element/element.dart';
import 'package:analyzer/dart/element/element2.dart';
import 'package:solid_lints/src/lints/avoid_final_with_getter/visitors/getter_variable_visitor.dart';

/// A visitor that checks for final private fields with getters.
Expand All @@ -16,9 +16,11 @@ class AvoidFinalWithGetterVisitor extends RecursiveAstVisitor<void> {
if (node
case MethodDeclaration(
isGetter: true,
declaredElement: ExecutableElement(
isAbstract: false,
isPublic: true,
declaredFragment: ExecutableFragment(
element: ExecutableElement2(
isAbstract: false,
isPublic: true,
)
)
)) {
final visitor = GetterVariableVisitor(node);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import 'package:analyzer/dart/ast/ast.dart';
import 'package:analyzer/dart/ast/visitor.dart';
import 'package:analyzer/dart/element/element.dart';
import 'package:analyzer/dart/element/element2.dart';

/// A visitor that checks the association of the getter with
/// the final private variable
Expand All @@ -17,11 +17,11 @@ class GetterVariableVisitor extends RecursiveAstVisitor<void> {

@override
void visitVariableDeclaration(VariableDeclaration node) {
if (node
case VariableDeclaration(
isFinal: true,
declaredElement: VariableElement(id: final id, isPrivate: true)
) when id == _getterId) {
final element = node.declaredFragment?.element;
if (element != null &&
element.isPrivate &&
element.isFinal &&
element.id == _getterId) {
_variable = node;
}

Expand All @@ -30,17 +30,17 @@ class GetterVariableVisitor extends RecursiveAstVisitor<void> {
}

extension on MethodDeclaration {
int? get getterReferenceId => switch (body) {
ExpressionFunctionBody(
expression: SimpleIdentifier(
staticElement: Element(
declaration: PropertyAccessorElement(
variable2: PropertyInducingElement(:final id)
)
)
)
) =>
id,
_ => null,
};
int? get getterReferenceId {
if (body is! ExpressionFunctionBody) return null;

final expression = (body as ExpressionFunctionBody).expression;
if (expression is SimpleIdentifier) {
final element = expression.element;
if (element is PropertyAccessorElement2) {
return element.variable3?.id;
}
}

return null;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ class AvoidGlobalStateRule extends SolidLintRule {
extension on VariableDeclaration {
bool get isMutable => !isFinal && !isConst;

bool get isPrivate => declaredElement?.isPrivate ?? false;
bool get isPrivate => declaredElement2?.isPrivate ?? false;

bool get isPublicMutable => isMutable && !isPrivate;
}
4 changes: 2 additions & 2 deletions lib/src/lints/avoid_late_keyword/avoid_late_keyword_rule.dart
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ class AvoidLateKeywordRule extends SolidLintRule<AvoidLateKeywordParameters> {
}

bool _shouldLint(VariableDeclaration node) {
final isLateDeclaration = node.declaredElement?.isLate ?? false;
final isLateDeclaration = node.isLate;
if (!isLateDeclaration) return false;

final hasIgnoredType = _hasIgnoredType(node);
Expand All @@ -99,7 +99,7 @@ class AvoidLateKeywordRule extends SolidLintRule<AvoidLateKeywordParameters> {
final ignoredTypes = config.parameters.ignoredTypes.toSet();
if (ignoredTypes.isEmpty) return false;

final variableType = node.declaredElement?.type;
final variableType = node.declaredFragment?.element.type;
if (variableType == null) return false;

return variableType.hasIgnoredType(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ class AvoidReturningWidgetsRule
/// This lint rule represents
/// the error whether we return a widget.
static const lintName = 'avoid_returning_widgets';
static const _override = 'override';

AvoidReturningWidgetsRule._(super.config);

Expand Down Expand Up @@ -97,7 +98,16 @@ class AvoidReturningWidgetsRule

final isIgnored = config.parameters.exclude.shouldIgnore(node);

final isOverriden = node.declaredElement?.hasOverride ?? false;
final isOverriden = switch (node) {
FunctionDeclaration(:final functionExpression) =>
functionExpression.parent is MethodDeclaration &&
(functionExpression.parent! as MethodDeclaration)
.metadata
.any((m) => m.name.name == _override),
MethodDeclaration(:final metadata) =>
metadata.any((m) => m.name.name == _override),
_ => false,
};

if (isWidgetReturned && !isOverriden && !isIgnored) {
reporter.atNode(node, code);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ class AvoidUnrelatedTypeAssertionsVisitor extends RecursiveAstVisitor<void> {
? objectType.typeArguments.first
: objectType;

if ((correctObjectType.element == castedType.element) ||
if ((correctObjectType.element3 == castedType.element3) ||
castedType is DynamicType ||
correctObjectType is DynamicType ||
_isObjectAndEnum(correctObjectType, castedType)) {
Expand All @@ -106,7 +106,7 @@ class AvoidUnrelatedTypeAssertionsVisitor extends RecursiveAstVisitor<void> {

if (correctObjectType is InterfaceType) {
return correctObjectType.allSupertypes
.firstWhereOrNull((value) => value.element == castedType.element);
.firstWhereOrNull((value) => value.element3 == castedType.element3);
}

return null;
Expand Down Expand Up @@ -144,5 +144,5 @@ class AvoidUnrelatedTypeAssertionsVisitor extends RecursiveAstVisitor<void> {

bool _isObjectAndEnum(DartType objectType, DartType castedType) =>
objectType.isDartCoreObject &&
castedType.element?.kind == ElementKind.ENUM;
castedType.element3?.kind == ElementKind.ENUM;
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@

import 'package:analyzer/dart/ast/ast.dart';
import 'package:analyzer/dart/ast/visitor.dart';
import 'package:analyzer/dart/element/element.dart';
import 'package:analyzer/dart/element/element2.dart';
import 'package:collection/collection.dart';
import 'package:solid_lints/src/utils/node_utils.dart';
import 'package:solid_lints/src/utils/parameter_utils.dart';
Expand Down Expand Up @@ -129,7 +129,7 @@ class AvoidUnusedParametersVisitor extends RecursiveAstVisitor<void> {
for (final parameter in parameters) {
final name = parameter.name;
final isPresentInAll = allIdentifierElements.contains(
parameter.declaredElement,
parameter.declaredFragment?.element.baseElement.nonSynthetic2,
);

/// Variables declared and initialized as 'Foo(this.param)'
Expand Down Expand Up @@ -172,13 +172,13 @@ class AvoidUnusedParametersVisitor extends RecursiveAstVisitor<void> {
}

class _IdentifiersVisitor extends RecursiveAstVisitor<void> {
final elements = <Element>{};
final elements = <Element2>{};

@override
void visitSimpleIdentifier(SimpleIdentifier node) {
super.visitSimpleIdentifier(node);

final element = node.staticElement;
final element = node.element;
if (element != null) {
elements.add(element);
}
Expand All @@ -195,7 +195,7 @@ class _InvocationsVisitor extends RecursiveAstVisitor<void> {
@override
void visitSimpleIdentifier(SimpleIdentifier node) {
if (node.name == methodName &&
node.staticElement is MethodElement &&
node.element is MethodElement2 &&
node.parent is ArgumentList) {
hasTearOffInvocations = true;
}
Expand Down
49 changes: 25 additions & 24 deletions lib/src/lints/avoid_using_api/avoid_using_api_linter.dart
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import 'package:analyzer/dart/ast/ast.dart';
import 'package:analyzer/dart/element/element.dart';
import 'package:analyzer/dart/element/element2.dart';
import 'package:analyzer/error/listener.dart';
import 'package:collection/collection.dart';
import 'package:custom_lint_builder/custom_lint_builder.dart';
Expand Down Expand Up @@ -94,8 +94,10 @@ class AvoidUsingApiLinter {
return;
}

switch (node.staticElement) {
case FunctionElement() || PropertyAccessorElement():
switch (node.element) {
case LocalFunctionElement() ||
TopLevelFunctionElement() ||
PropertyAccessorElement2():
reporter.atNode(node, entryCode);
}
});
Expand All @@ -108,13 +110,13 @@ class AvoidUsingApiLinter {
String source,
) {
context.registry.addVariableDeclaration((node) {
final typeName = node.declaredElement?.type.element?.name;
final typeName = node.declaredElement2?.type.element3?.name3;
if (typeName != className) {
return;
}

final sourcePath =
node.declaredElement?.type.element?.librarySource?.uri.toString();
node.declaredElement2?.type.element3?.library2?.uri.toString();
if (sourcePath == null || !_matchesSource(sourcePath, source)) {
return;
}
Expand All @@ -136,25 +138,24 @@ class AvoidUsingApiLinter {
case InstanceCreationExpression(:final staticType?):
case SimpleIdentifier(:final staticType?):
final parentSourcePath =
staticType.element?.librarySource?.uri.toString() ??
node.sourceUrl;
staticType.element3?.library2?.uri.toString() ?? node.sourceUrl;
if (parentSourcePath == null ||
!_matchesSource(parentSourcePath, source)) {
return;
}

final parentTypeName = staticType.element?.name;
final parentTypeName = staticType.element3?.name3;
if (parentTypeName != className) {
return;
}
case SimpleIdentifier(:final staticElement?):
case SimpleIdentifier(:final element?):
final parentSourcePath =
staticElement.librarySource?.uri.toString() ?? node.sourceUrl;
element.library2?.uri.toString() ?? node.sourceUrl;
if (parentSourcePath == null ||
!_matchesSource(parentSourcePath, source)) {
return;
}
final parentElementName = staticElement.name;
final parentElementName = element.name3;
if (parentElementName != className) {
return;
}
Expand Down Expand Up @@ -214,7 +215,7 @@ class AvoidUsingApiLinter {
switch (entityBeforeNode) {
case InstanceCreationExpression(:final staticType?):
case SimpleIdentifier(:final staticType?):
final parentTypeName = staticType.element?.name;
final parentTypeName = staticType.element3?.name3;
if (parentTypeName != className) {
return;
}
Expand All @@ -223,8 +224,8 @@ class AvoidUsingApiLinter {
node.parent ?? node,
entryCode,
);
case SimpleIdentifier(:final staticElement?):
final parentElementName = staticElement.name;
case SimpleIdentifier(:final element?):
final parentElementName = element.name3;
if (parentElementName != className) {
return;
}
Expand All @@ -233,8 +234,8 @@ class AvoidUsingApiLinter {
node.parent ?? node,
entryCode,
);
case NamedType(:final element?):
final parentTypeName = element.name;
case NamedType(:final element2?):
final parentTypeName = element2.name3;
if (parentTypeName != className) {
return;
}
Expand All @@ -250,13 +251,13 @@ class AvoidUsingApiLinter {
final methodName = node.methodName.name;
if (methodName != identifier) return;

final enclosingElement = node.methodName.staticElement?.enclosingElement3;
if (enclosingElement is! ExtensionElement ||
enclosingElement.name != className) {
final enclosingElement = node.methodName.element?.enclosingElement2;
if (enclosingElement is! ExtensionElement2 ||
enclosingElement.name3 != className) {
return;
}

final sourcePath = enclosingElement.librarySource.uri.toString();
final sourcePath = enclosingElement.library2.uri.toString();
if (!_matchesSource(sourcePath, source)) {
return;
}
Expand All @@ -268,13 +269,13 @@ class AvoidUsingApiLinter {
final propertyName = node.identifier.name;
if (propertyName != identifier) return;

final enclosingElement = node.identifier.staticElement?.enclosingElement3;
if (enclosingElement is! ExtensionElement ||
enclosingElement.name != className) {
final enclosingElement = node.identifier.element?.enclosingElement2;
if (enclosingElement is! ExtensionElement2 ||
enclosingElement.name3 != className) {
return;
}

final sourcePath = enclosingElement.librarySource.uri.toString();
final sourcePath = enclosingElement.library2.uri.toString();
if (!_matchesSource(sourcePath, source)) return;

reporter.atNode(node.identifier, entryCode);
Expand Down
4 changes: 2 additions & 2 deletions lib/src/lints/avoid_using_api/avoid_using_api_utils.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import 'package:analyzer/dart/ast/ast.dart';
extension SimpleIdentifierParentSourceExtension on SimpleIdentifier {
/// Returns library of the node
String? get sourceUrl {
final parentSource = staticElement?.librarySource;
final parentSource = element?.library2;
final parentSourceName = parentSource?.uri.toString();
return parentSourceName;
}
Expand All @@ -14,7 +14,7 @@ extension SimpleIdentifierParentSourceExtension on SimpleIdentifier {
extension NamedTypeParentSourceExtension on NamedType {
/// Returns library of the node
String? get sourceUrl {
final parentSource = element?.librarySource;
final parentSource = element2?.library2;
final parentSourceName = parentSource?.uri.toString();
return parentSourceName;
}
Expand Down
Loading