Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

I166 #170

Merged
merged 7 commits into from
May 2, 2024
Merged

I166 #170

Show file tree
Hide file tree
Changes from 6 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: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@
- Add a rule prefer_guard_clause for reversing nested if statements (https://github.com/solid-software/solid_lints/issues/91)
- add exclude params support to avoid_returning_widgets rule (https://github.com/solid-software/solid_lints/issues/131)
- add quick fix to avoid_final_with_getter (https://github.com/solid-software/solid_lints/pull/164)

- Renamed `avoid_debug_print` to `avoid_debug_print_in_release`
- Now, `avoid_debug_print_in_release` doesn't trigger if the `debugPrint` call is wrapped in a `!kReleaseMode` check.
sufftea marked this conversation as resolved.
Show resolved Hide resolved

## 0.1.5

Expand Down
2 changes: 1 addition & 1 deletion example/analysis_options.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ custom_lint:
- avoid_unnecessary_setstate
- double_literal_format
- avoid_unnecessary_type_assertions
- avoid_debug_print
- avoid_debug_print_in_release
- avoid_using_api:
severity: info
entries:
Expand Down
2 changes: 1 addition & 1 deletion lib/analysis_options.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ custom_lint:
- avoid_unnecessary_type_casts
- avoid_unrelated_type_assertions
- avoid_unused_parameters
- avoid_debug_print
- avoid_debug_print_in_release
- avoid_final_with_getter

- cyclomatic_complexity:
Expand Down
4 changes: 2 additions & 2 deletions lib/solid_lints.dart
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
library solid_metrics;

import 'package:custom_lint_builder/custom_lint_builder.dart';
import 'package:solid_lints/src/lints/avoid_debug_print/avoid_debug_print_rule.dart';
import 'package:solid_lints/src/lints/avoid_debug_print_in_release/avoid_debug_print_in_release_rule.dart';
import 'package:solid_lints/src/lints/avoid_final_with_getter/avoid_final_with_getter_rule.dart';
import 'package:solid_lints/src/lints/avoid_global_state/avoid_global_state_rule.dart';
import 'package:solid_lints/src/lints/avoid_late_keyword/avoid_late_keyword_rule.dart';
Expand Down Expand Up @@ -62,7 +62,7 @@ class _SolidLints extends PluginBase {
PreferLastRule.createRule(configs),
PreferMatchFileNameRule.createRule(configs),
ProperSuperCallsRule.createRule(configs),
AvoidDebugPrint.createRule(configs),
AvoidDebugPrintInReleaseRule.createRule(configs),
PreferEarlyReturnRule.createRule(configs),
AvoidFinalWithGetterRule.createRule(configs),
];
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import 'package:analyzer/dart/ast/ast.dart';
import 'package:analyzer/dart/ast/syntactic_entity.dart';
import 'package:analyzer/dart/ast/token.dart';
import 'package:analyzer/error/listener.dart';
import 'package:custom_lint_builder/custom_lint_builder.dart';
import 'package:solid_lints/src/lints/avoid_debug_print/models/avoid_debug_print_func_model.dart';
import 'package:solid_lints/src/models/rule_config.dart';
import 'package:solid_lints/src/models/solid_lint_rule.dart';

/// A `avoid_debug_print` rule which forbids calling or referencing
sufftea marked this conversation as resolved.
Show resolved Hide resolved
/// debugPrint function from flutter/foundation.
/// debugPrint function in release mode.
sufftea marked this conversation as resolved.
Show resolved Hide resolved
///
/// See more here: https://github.com/flutter/flutter/issues/147141
///
Expand All @@ -25,29 +25,35 @@ import 'package:solid_lints/src/models/solid_lint_rule.dart';
/// #### GOOD:
///
/// ```dart
/// if (kDebugMode) {
/// if (!kReleaseMode) {
/// debugPrint('');
/// }
/// ```
///
///
class AvoidDebugPrint extends SolidLintRule {
class AvoidDebugPrintInReleaseRule extends SolidLintRule {
sufftea marked this conversation as resolved.
Show resolved Hide resolved
/// The [LintCode] of this lint rule that represents
/// the error when debugPrint is called
static const lintName = 'avoid_debug_print';
static const lintName = 'avoid_debug_print_in_release';

AvoidDebugPrint._(super.config);
static const String _kReleaseModePath =
'package:flutter/src/foundation/constants.dart';
static const String _kReleaseModeName = 'kReleaseMode';
static const _debugPrintPath = 'package:flutter/src/foundation/print.dart';
static const _debugPrintName = 'debugPrint';

/// Creates a new instance of [AvoidDebugPrint]
AvoidDebugPrintInReleaseRule._(super.config);

/// Creates a new instance of [AvoidDebugPrintInReleaseRule]
/// based on the lint configuration.
factory AvoidDebugPrint.createRule(CustomLintConfigs configs) {
factory AvoidDebugPrintInReleaseRule.createRule(CustomLintConfigs configs) {
final rule = RuleConfig(
configs: configs,
name: lintName,
problemMessage: (_) => "Avoid using 'debugPrint'",
problemMessage: (_) => "Avoid using 'debugPrint' in release mode",
sufftea marked this conversation as resolved.
Show resolved Hide resolved
);

return AvoidDebugPrint._(rule);
return AvoidDebugPrintInReleaseRule._(rule);
}

@override
Expand All @@ -62,6 +68,7 @@ class AvoidDebugPrint extends SolidLintRule {
if (func is! Identifier) {
return;
}

_checkIdentifier(
identifier: func,
node: node,
Expand Down Expand Up @@ -95,11 +102,25 @@ class AvoidDebugPrint extends SolidLintRule {
required AstNode node,
required ErrorReporter reporter,
}) {
final funcModel = AvoidDebugPrintFuncModel.parseExpression(identifier);
if (!_isDebugPrintNode(identifier)) {
return;
}

final debugCheck = node.thisOrAncestorMatching(
(node) {
if (node is IfStatement) {
return _isNotReleaseCheck(node.expression);
}

if (funcModel.hasSameName && funcModel.hasTheSameSource) {
reporter.reportErrorForNode(code, node);
return false;
},
);

if (debugCheck != null) {
return;
}

reporter.reportErrorForNode(code, node);
}

/// Returns null if doesn't have right operand
Expand Down Expand Up @@ -128,4 +149,57 @@ class AvoidDebugPrint extends SolidLintRule {
reporter: reporter,
);
}

bool _isDebugPrintNode(Identifier node) {
final String name;
final String sourcePath;
switch (node) {
case PrefixedIdentifier():
final prefix = node.prefix.name;
name = node.name.replaceAll('$prefix.', '');
sourcePath = node.staticElement?.librarySource?.uri.toString() ?? '';

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

default:
return false;
}

return name == _debugPrintName && sourcePath == _debugPrintPath;
}

bool _isNotReleaseCheck(Expression node) {
if (node.childEntities.toList()
case [
final Token token,
final Identifier identifier,
]) {
return token.type == TokenType.BANG &&
_isReleaseModeIdentifier(identifier);
}

return false;
}

bool _isReleaseModeIdentifier(Identifier node) {
final String name;
final String sourcePath;

switch (node) {
case PrefixedIdentifier():
final prefix = node.prefix.name;

name = node.name.replaceAll('$prefix.', '');
sourcePath = node.staticElement?.librarySource?.uri.toString() ?? '';
case SimpleIdentifier():
name = node.name;
sourcePath = node.staticElement?.librarySource?.uri.toString() ?? '';
default:
return false;
}

return name == _kReleaseModeName && sourcePath == _kReleaseModePath;
}
}
2 changes: 1 addition & 1 deletion lint_test/analysis_options.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ custom_lint:
- newline_before_return
- no_empty_block
- no_equal_then_else
- avoid_debug_print
- avoid_debug_print_in_release
- prefer_early_return
- member_ordering:
alphabetize: true
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
// ignore_for_file: unused_local_variable

import 'package:flutter/foundation.dart' as f;

/// Test the avoid_debug_print_in_release
void avoidDebugPrintTest() {
// expect_lint: avoid_debug_print_in_release
f.debugPrint('');

// expect_lint: avoid_debug_print_in_release
final test = f.debugPrint;

test('test');

// expect_lint: avoid_debug_print_in_release
final test2 = f.debugPrint('');

debugPrint();

debugPrint;

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

final test = f.debugPrint;

var test2;

test2 = debugPrint;

test.call('test');

final test3 = f.debugPrint('');

if (true) {
f.debugPrint('');

final test = f.debugPrint;

var test2;

test2 = debugPrint;

test.call('test');

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

void debugPrint() {
return;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
// ignore_for_file: unused_local_variable

import 'package:flutter/foundation.dart';

/// Test the avoid_debug_print_in_release
void avoidDebugPrintTest() {
// expect_lint: avoid_debug_print_in_release
debugPrint('');

// expect_lint: avoid_debug_print_in_release
final test = debugPrint;

var test2;

// expect_lint: avoid_debug_print_in_release
test2 = debugPrint;

test.call('test');

// expect_lint: avoid_debug_print_in_release
final test3 = debugPrint('');

someOtherFunction();

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

final test = debugPrint;

var test2;

test2 = debugPrint;

test.call('test');

final test3 = debugPrint('');

someOtherFunction();

if (true) {
debugPrint('');

final test = debugPrint;

var test2;

test2 = debugPrint;

test.call('test');

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

void someOtherFunction() {
print('iii');
}
Loading
Loading