Skip to content

Commit

Permalink
Look for the checks all the way up the tree; check for !kReleaseMode …
Browse files Browse the repository at this point in the history
…instead
  • Loading branch information
sufftea committed Apr 29, 2024
1 parent 8324d8c commit 8955830
Show file tree
Hide file tree
Showing 5 changed files with 92 additions and 36 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -100,18 +100,29 @@ class AvoidDebugPrintInReleaseRule extends SolidLintRule {
final funcModel =
AvoidDebugPrintInReleaseFuncModel.parseExpression(identifier);

if (funcModel.isDebugPrint && !_checkIsDebugMode(node)) {
if (funcModel.isDebugPrint) {
if (node.thisOrAncestorMatching(
(node) {
if (node is IfStatement) {
final check =
AvoidDebugPrintInReleaseCheckModel.parseExpression(
node.expression,
);

return check.isNotRelease;
}

return false;
},
) !=
null) {
return;
}

reporter.reportErrorForNode(code, node);
}
}

bool _checkIsDebugMode(AstNode node) {
final statement = node.thisOrAncestorOfType<IfStatement>()?.expression;
final debugModel =
AvoidDebugPrintInReleaseDebugModel.parseExpression(statement);
return debugModel.isDebugMode;
}

/// Returns null if doesn't have right operand
SyntacticEntity? _getRightOperand(List<SyntacticEntity> entities) {
/// Example var t = 15; 15 is in 3d position
Expand Down
Original file line number Diff line number Diff line change
@@ -1,44 +1,50 @@
import 'package:analyzer/dart/ast/ast.dart';
import 'package:analyzer/dart/ast/token.dart';

/// A class used to parse function expression
class AvoidDebugPrintInReleaseDebugModel {
class AvoidDebugPrintInReleaseCheckModel {
/// Wheather the statement is prefixed with `!`
final bool negated;

/// Function name
final String name;

/// Function's source path
final String sourcePath;

/// A class used to parse function expression
const AvoidDebugPrintInReleaseDebugModel({
const AvoidDebugPrintInReleaseCheckModel({
required this.negated,
required this.name,
required this.sourcePath,
});

/// A constructor that parses identifier into [name] and [sourcePath]
factory AvoidDebugPrintInReleaseDebugModel.parseExpression(
AstNode? identifier,
factory AvoidDebugPrintInReleaseCheckModel.parseExpression(
Expression? expression,
) {
switch (identifier) {
case PrefixedIdentifier():
final prefix = identifier.prefix.name;
return AvoidDebugPrintInReleaseDebugModel(
name: identifier.name.replaceAll('$prefix.', ''),
sourcePath:
identifier.staticElement?.librarySource?.uri.toString() ?? '',
switch (expression?.childEntities.toList()) {
case [
final Token token,
final Identifier identifier,
]:
return AvoidDebugPrintInReleaseCheckModel._parseIdentifier(
identifier: identifier,
negated: token.type == TokenType.BANG,
);
case SimpleIdentifier():
return AvoidDebugPrintInReleaseDebugModel(
name: identifier.name,
sourcePath:
identifier.staticElement?.librarySource?.uri.toString() ?? '',
case [final Identifier identifier]:
return AvoidDebugPrintInReleaseCheckModel._parseIdentifier(
identifier: identifier,
negated: false,
);
default:
return AvoidDebugPrintInReleaseDebugModel._empty();
return AvoidDebugPrintInReleaseCheckModel._empty();
}
}

factory AvoidDebugPrintInReleaseDebugModel._empty() {
return const AvoidDebugPrintInReleaseDebugModel(
factory AvoidDebugPrintInReleaseCheckModel._empty() {
return const AvoidDebugPrintInReleaseCheckModel(
negated: false,
name: '',
sourcePath: '',
);
Expand All @@ -47,19 +53,44 @@ class AvoidDebugPrintInReleaseDebugModel {
static const String _expectedPath =
'package:flutter/src/foundation/constants.dart';

static const String _kDebugMode = 'kDebugMode';
static const String _kReleaseMode = 'kReleaseMode';

/// Whether the function has the same source library as kDebugMode
/// Whether the expression has the same source library as kDebugMode
bool get hasTheSameSource => _expectedPath == sourcePath;

/// Whether the function has the same name as kDebugMode
bool get hasSameName => _kDebugMode == name;
/// Whether the expression has the same name as kReleaseMode
bool get hasSameName => _kReleaseMode == name;

/// The complete check if the statement is the `kDebugMode` identifier.
bool get isDebugMode => hasSameName && hasTheSameSource;
/// Whether the expression checks ifthe mode is not release.
bool get isNotRelease => negated && hasSameName && hasTheSameSource;

@override
String toString() {
return '$name, $sourcePath';
}

factory AvoidDebugPrintInReleaseCheckModel._parseIdentifier({
required Identifier identifier,
required bool negated,
}) {
switch (identifier) {
case PrefixedIdentifier():
final prefix = identifier.prefix.name;
return AvoidDebugPrintInReleaseCheckModel(
negated: negated,
name: identifier.name.replaceAll('$prefix.', ''),
sourcePath:
identifier.staticElement?.librarySource?.uri.toString() ?? '',
);
case SimpleIdentifier():
return AvoidDebugPrintInReleaseCheckModel(
negated: negated,
name: identifier.name,
sourcePath:
identifier.staticElement?.librarySource?.uri.toString() ?? '',
);
default:
return AvoidDebugPrintInReleaseCheckModel._empty();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ class AvoidDebugPrintInReleaseFuncModel {
/// Ehether the function has the same name as debugPrint
bool get hasSameName => _debugPrint == name;

/// The complete check if the expression of is the debugPrint function
/// The final check if the statement is the debugPrint function
bool get isDebugPrint => hasSameName && hasTheSameSource;

@override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ void avoidDebugPrintTest() {

debugPrint;

if (f.kDebugMode) {
if (!f.kReleaseMode) {
f.debugPrint('');

final test = f.debugPrint;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ void avoidDebugPrintTest() {

someOtherFunction();

if (kDebugMode) {
if (!kReleaseMode) {
debugPrint('');

final test = debugPrint;
Expand All @@ -36,6 +36,20 @@ void avoidDebugPrintTest() {
final test3 = debugPrint('');

someOtherFunction();

if (true) {
debugPrint('');

final test = debugPrint;

var test2;

test2 = debugPrint;

test.call('test');

final test3 = debugPrint('');
}
}
}

Expand Down

0 comments on commit 8955830

Please sign in to comment.