Skip to content

Commit

Permalink
feat(neon_lints): add linting rule to avoid using any calls in produc…
Browse files Browse the repository at this point in the history
…tion

Signed-off-by: Nikolas Rimikis <leptopoda@users.noreply.github.com>
  • Loading branch information
Leptopoda committed Feb 26, 2024
1 parent d9d9c23 commit 8db494c
Show file tree
Hide file tree
Showing 15 changed files with 173 additions and 3 deletions.
2 changes: 1 addition & 1 deletion melos.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ command:
scripts:
format: dart format --fix --line-length 120 .
format:check: dart format --output=none --set-exit-if-changed --line-length 120 .
analyze: dart analyze --fatal-infos .
analyze: dart run custom_lint --fatal-infos .
test: >
melos exec --no-flutter --concurrency=1 --fail-fast --dir-exists=test -- "dart test --concurrency=$(nproc --all) --fail-fast" &&
melos exec --flutter --concurrency=1 --fail-fast --dir-exists=test -- "flutter test --concurrency=$(nproc --all)"
Expand Down
96 changes: 96 additions & 0 deletions packages/app/pubspec.lock
Original file line number Diff line number Diff line change
@@ -1,6 +1,30 @@
# Generated by pub
# See https://dart.dev/tools/pub/glossary#lockfile
packages:
_fe_analyzer_shared:
dependency: transitive
description:
name: _fe_analyzer_shared
sha256: "0b2f2bd91ba804e53a61d757b986f89f1f9eaed5b11e4b2f5a2468d86d6c9fc7"
url: "https://pub.dev"
source: hosted
version: "67.0.0"
analyzer:
dependency: transitive
description:
name: analyzer
sha256: "37577842a27e4338429a1cbc32679d508836510b056f1eedf0c8d20e39c1383d"
url: "https://pub.dev"
source: hosted
version: "6.4.1"
analyzer_plugin:
dependency: transitive
description:
name: analyzer_plugin
sha256: "9661b30b13a685efaee9f02e5d01ed9f2b423bd889d28a304d02d704aee69161"
url: "https://pub.dev"
source: hosted
version: "0.11.3"
ansicolor:
dependency: transitive
description:
Expand Down Expand Up @@ -129,6 +153,22 @@ packages:
url: "https://pub.dev"
source: hosted
version: "2.0.3"
ci:
dependency: transitive
description:
name: ci
sha256: "145d095ce05cddac4d797a158bc4cf3b6016d1fe63d8c3d2fbd7212590adca13"
url: "https://pub.dev"
source: hosted
version: "0.1.0"
cli_util:
dependency: transitive
description:
name: cli_util
sha256: c05b7406fdabc7a49a3929d4af76bcaccbbffcbcdcf185b082e1ae07da323d19
url: "https://pub.dev"
source: hosted
version: "0.4.1"
clock:
dependency: transitive
description:
Expand Down Expand Up @@ -201,6 +241,38 @@ packages:
url: "https://pub.dev"
source: hosted
version: "1.0.6"
custom_lint:
dependency: "direct dev"
description:
name: custom_lint
sha256: "445242371d91d2e24bd7b82e3583a2c05610094ba2d0575262484ad889c8f981"
url: "https://pub.dev"
source: hosted
version: "0.6.2"
custom_lint_builder:
dependency: transitive
description:
name: custom_lint_builder
sha256: "4c0aed2a3491096e91cf1281923ba1b6814993f16dde0fd60f697925225bbbd6"
url: "https://pub.dev"
source: hosted
version: "0.6.2"
custom_lint_core:
dependency: transitive
description:
name: custom_lint_core
sha256: ce5d6215f4e143f7780ce53f73dfa6fc503f39d2d30bef76c48be9ac1a09d9a6
url: "https://pub.dev"
source: hosted
version: "0.6.2"
dart_style:
dependency: transitive
description:
name: dart_style
sha256: "40ae61a5d43feea6d24bd22c0537a6629db858963b99b4bc1c3db80676f32368"
url: "https://pub.dev"
source: hosted
version: "2.3.4"
dbus:
dependency: transitive
description:
Expand Down Expand Up @@ -443,6 +515,14 @@ packages:
url: "https://pub.dev"
source: hosted
version: "1.5.2"
freezed_annotation:
dependency: transitive
description:
name: freezed_annotation
sha256: c3fd9336eb55a38cc1bbd79ab17573113a8deccd0ecbbf926cca3c62803b5c2d
url: "https://pub.dev"
source: hosted
version: "2.4.1"
glob:
dependency: transitive
description:
Expand All @@ -459,6 +539,14 @@ packages:
url: "https://pub.dev"
source: hosted
version: "13.2.0"
hotreloader:
dependency: transitive
description:
name: hotreloader
sha256: ed56fdc1f3a8ac924e717257621d09e9ec20e308ab6352a73a50a1d7a4d9158e
url: "https://pub.dev"
source: hosted
version: "4.2.0"
html:
dependency: transitive
description:
Expand Down Expand Up @@ -1423,6 +1511,14 @@ packages:
url: "https://pub.dev"
source: hosted
version: "1.1.0"
watcher:
dependency: transitive
description:
name: watcher
sha256: "3d2ad6751b3c16cf07c7fca317a1413b3f26530319181b37e3b9039b84fc01d8"
url: "https://pub.dev"
source: hosted
version: "1.1.0"
web:
dependency: transitive
description:
Expand Down
1 change: 1 addition & 0 deletions packages/app/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ dependencies:
vector_graphics: any

dev_dependencies:
custom_lint: ^0.6.2
neon_lints:
git:
url: https://github.com/nextcloud/neon
Expand Down
1 change: 1 addition & 0 deletions packages/neon/neon_dashboard/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ dependencies:

dev_dependencies:
build_runner: ^2.4.8
custom_lint: ^0.6.2
flutter_test:
sdk: flutter
go_router_builder: ^2.4.1
Expand Down
1 change: 1 addition & 0 deletions packages/neon/neon_files/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ dependencies:

dev_dependencies:
build_runner: ^2.4.8
custom_lint: ^0.6.2
go_router_builder: ^2.4.1
neon_lints:
git:
Expand Down
1 change: 1 addition & 0 deletions packages/neon/neon_news/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ dependencies:

dev_dependencies:
build_runner: ^2.4.8
custom_lint: ^0.6.2
go_router_builder: ^2.4.1
neon_lints:
git:
Expand Down
1 change: 1 addition & 0 deletions packages/neon/neon_notes/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ dependencies:

dev_dependencies:
build_runner: ^2.4.8
custom_lint: ^0.6.2
go_router_builder: ^2.4.1
neon_lints:
git:
Expand Down
1 change: 1 addition & 0 deletions packages/neon/neon_notifications/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ dependencies:

dev_dependencies:
build_runner: ^2.4.8
custom_lint: ^0.6.2
go_router_builder: ^2.4.1
neon_lints:
git:
Expand Down
1 change: 1 addition & 0 deletions packages/neon_framework/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ dependencies:

dev_dependencies:
build_runner: ^2.4.8
custom_lint: ^0.6.2
flutter_test:
sdk: flutter
go_router_builder: ^2.4.1
Expand Down
1 change: 0 additions & 1 deletion packages/neon_lints/analysis_options.yaml

This file was deleted.

1 change: 1 addition & 0 deletions packages/neon_lints/analysis_options.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
include: package:neon_lints/dart.yaml
3 changes: 3 additions & 0 deletions packages/neon_lints/lib/flutter.yaml
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
include: package:neon_lints/src/base.yaml

analyzer:
plugins:
- custom_lint
linter:
rules:
avoid_print: true
16 changes: 16 additions & 0 deletions packages/neon_lints/lib/neon_lints.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import 'package:custom_lint_builder/custom_lint_builder.dart';
import 'package:neon_lints/src/amend_model_suffix.dart';

/// Registers the neon lints plugin.
///
/// Do **not** use this dierectly.
/// This method is applied used when enabling the `custom_lint` in the
/// `analysis_options.yaml` file.
PluginBase createPlugin() => _NeonLintsPlugin();

class _NeonLintsPlugin extends PluginBase {
@override
List<LintRule> getLintRules(CustomLintConfigs _) => const [
AvoidDebugPrint(),
];
}
41 changes: 41 additions & 0 deletions packages/neon_lints/lib/src/amend_model_suffix.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import 'package:analyzer/dart/ast/ast.dart';
import 'package:analyzer/error/listener.dart';
import 'package:custom_lint_builder/custom_lint_builder.dart';

/// Lint rule checking that no print methods from `flutter:framework` are used.
///
/// Similar to the analyzer rule `avoid_print`.
/// Instead use a a logging framework like `https://pub.dev/packages/logging`.
///
/// ```dart
/// debugPrint('message'); // lint
/// debugPrintStack(stackTrace: stackTrace); // lint
/// debugPrintSynchronously('message'); // lint
/// debugPrintThrottled('message'); // lint
/// ```
final class AvoidDebugPrint extends DartLintRule {
/// @nodoc
const AvoidDebugPrint() : super(code: _code);

static const _code = LintCode(
name: 'avoid_debug_print',
problemMessage: "Don't invoke 'debugPrint' and 'debugPrintStack' in production code.",
correctionMessage: 'Try using a logging framework.',
);

static const String _debugPrint = 'debugPrint';

@override
void run(
CustomLintResolver resolver,
ErrorReporter reporter,
CustomLintContext context,
) {
context.registry.addInvocationExpression((node) {
final element = node.function;
if (element case Identifier(:final name) when name.startsWith(_debugPrint)) {
reporter.reportErrorForToken(code, node.beginToken);
}
});
}
}
5 changes: 5 additions & 0 deletions packages/neon_lints/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,10 @@ publish_to: none
environment:
sdk: '>=3.1.0 <4.0.0'

dependencies:
analyzer: ^6.4.1
analyzer_plugin: ^0.11.3
custom_lint_builder: ^0.6.2

dev_dependencies:
lint_maker: ^0.2.1
4 changes: 3 additions & 1 deletion pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@ name: neon_workspace
publish_to: none

environment:
sdk: '>=3.1.0 <4.0.0'
sdk: '>=3.2.0 <4.0.0'

dev_dependencies:
commitlint_cli: ^0.7.1
fvm: ^3.0.11
husky: ^0.1.7
melos: ^4.1.0
custom_lint: ^0.6.2

0 comments on commit 8db494c

Please sign in to comment.