Skip to content

Commit

Permalink
[element model] migrate avoid_print
Browse files Browse the repository at this point in the history
Bug: dart-lang/linter#5099
Change-Id: Ia2f290d27d195f3a2857ec64a13a2387421a0bb1
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/388182
Auto-Submit: Phil Quitslund <[email protected]>
Reviewed-by: Brian Wilkerson <[email protected]>
Commit-Queue: Brian Wilkerson <[email protected]>
  • Loading branch information
pq authored and Commit Queue committed Oct 4, 2024
1 parent 4e0fe81 commit b9c886e
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 9 deletions.
4 changes: 4 additions & 0 deletions pkg/linter/lib/src/extensions.dart
Original file line number Diff line number Diff line change
Expand Up @@ -557,6 +557,10 @@ extension InterfaceTypeExtension on InterfaceType {
}
}

extension LibraryElementExtension2 on LibraryElement2? {
Uri? get uri => this?.library2.firstFragment.source.uri;
}

extension LinterContextExtension on LinterContext {
/// Whether the given [feature] is enabled in this linter context.
bool isEnabled(Feature feature) =>
Expand Down
15 changes: 6 additions & 9 deletions pkg/linter/lib/src/rules/avoid_print.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,9 @@

import 'package:analyzer/dart/ast/ast.dart';
import 'package:analyzer/dart/ast/visitor.dart';
import 'package:analyzer/dart/element/element.dart';

import '../analyzer.dart';
import '../ast.dart';
import '../extensions.dart';
import '../linter_lint_codes.dart';
import '../util/flutter_utils.dart';

Expand Down Expand Up @@ -38,7 +37,8 @@ class _Visitor extends SimpleAstVisitor {

@override
void visitMethodInvocation(MethodInvocation node) {
if (node.methodName.staticElement.isDartCorePrint && !_isDebugOnly(node)) {
if ((node.methodName.element?.isDartCorePrint ?? false) &&
!_isDebugOnly(node)) {
rule.reportLint(node.methodName);
}

Expand All @@ -51,8 +51,7 @@ class _Visitor extends SimpleAstVisitor {
var parent = node.parent;
if (parent is IfStatement && node == parent.thenStatement) {
var condition = parent.expression;
if (condition is SimpleIdentifier &&
isKDebugMode(condition.staticElement)) {
if (condition is SimpleIdentifier && isKDebugMode2(condition.element)) {
return true;
}
} else if (parent is FunctionBody) {
Expand All @@ -65,10 +64,8 @@ class _Visitor extends SimpleAstVisitor {

void _validateArgument(Expression expression) {
if (expression is SimpleIdentifier) {
var element = expression.staticElement;
if (element is FunctionElement &&
element.name == 'print' &&
element.library.isDartCore) {
var element = expression.element;
if (element.isDartCorePrint) {
rule.reportLint(expression);
}
}
Expand Down
8 changes: 8 additions & 0 deletions pkg/linter/lib/src/util/flutter_utils.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
// BSD-style license that can be found in the LICENSE file.

import 'package:analyzer/dart/element/element.dart';
import 'package:analyzer/dart/element/element2.dart';
import 'package:analyzer/dart/element/nullability_suffix.dart';
import 'package:analyzer/dart/element/type.dart';

Expand Down Expand Up @@ -37,6 +38,8 @@ bool isExactWidgetTypeSizedBox(DartType? type) =>

bool isKDebugMode(Element? element) => _flutter.isKDebugMode(element);

bool isKDebugMode2(Element2? element) => _flutter.isKDebugMode2(element);

bool isState(InterfaceElement element) => _flutter.isState(element);

bool isStatefulWidget(ClassElement? element) =>
Expand Down Expand Up @@ -127,6 +130,11 @@ class _Flutter {
element.name == 'kDebugMode' &&
element.source?.uri == _uriFoundation;

bool isKDebugMode2(Element2? element) =>
element != null &&
element.name == 'kDebugMode' &&
element.library2.uri == _uriFoundation;

bool isState(InterfaceElement element) =>
isExactly(element, _nameState, _uriFramework) ||
element.allSupertypes
Expand Down

0 comments on commit b9c886e

Please sign in to comment.