From 8db494cc3bc35ff66b5f098abadd246407c74731 Mon Sep 17 00:00:00 2001 From: Nikolas Rimikis Date: Sat, 24 Feb 2024 19:17:50 +0100 Subject: [PATCH] feat(neon_lints): add linting rule to avoid using any calls in production Signed-off-by: Nikolas Rimikis --- melos.yaml | 2 +- packages/app/pubspec.lock | 96 +++++++++++++++++++ packages/app/pubspec.yaml | 1 + packages/neon/neon_dashboard/pubspec.yaml | 1 + packages/neon/neon_files/pubspec.yaml | 1 + packages/neon/neon_news/pubspec.yaml | 1 + packages/neon/neon_notes/pubspec.yaml | 1 + packages/neon/neon_notifications/pubspec.yaml | 1 + packages/neon_framework/pubspec.yaml | 1 + packages/neon_lints/analysis_options.yaml | 2 +- packages/neon_lints/lib/flutter.yaml | 3 + packages/neon_lints/lib/neon_lints.dart | 16 ++++ .../lib/src/amend_model_suffix.dart | 41 ++++++++ packages/neon_lints/pubspec.yaml | 5 + pubspec.yaml | 4 +- 15 files changed, 173 insertions(+), 3 deletions(-) mode change 120000 => 100644 packages/neon_lints/analysis_options.yaml create mode 100644 packages/neon_lints/lib/neon_lints.dart create mode 100644 packages/neon_lints/lib/src/amend_model_suffix.dart diff --git a/melos.yaml b/melos.yaml index b0c912acbf5..88a6bc31169 100644 --- a/melos.yaml +++ b/melos.yaml @@ -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)" diff --git a/packages/app/pubspec.lock b/packages/app/pubspec.lock index 978506e4b9a..e6eafb5b195 100644 --- a/packages/app/pubspec.lock +++ b/packages/app/pubspec.lock @@ -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: @@ -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: @@ -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: @@ -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: @@ -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: @@ -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: diff --git a/packages/app/pubspec.yaml b/packages/app/pubspec.yaml index 7560a42d2f8..0cc26711cec 100644 --- a/packages/app/pubspec.yaml +++ b/packages/app/pubspec.yaml @@ -41,6 +41,7 @@ dependencies: vector_graphics: any dev_dependencies: + custom_lint: ^0.6.2 neon_lints: git: url: https://github.com/nextcloud/neon diff --git a/packages/neon/neon_dashboard/pubspec.yaml b/packages/neon/neon_dashboard/pubspec.yaml index b1612424f6a..85142669233 100644 --- a/packages/neon/neon_dashboard/pubspec.yaml +++ b/packages/neon/neon_dashboard/pubspec.yaml @@ -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 diff --git a/packages/neon/neon_files/pubspec.yaml b/packages/neon/neon_files/pubspec.yaml index 926430548a0..fae04c770ac 100644 --- a/packages/neon/neon_files/pubspec.yaml +++ b/packages/neon/neon_files/pubspec.yaml @@ -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: diff --git a/packages/neon/neon_news/pubspec.yaml b/packages/neon/neon_news/pubspec.yaml index 3ed9bb6af3a..393d65d4099 100644 --- a/packages/neon/neon_news/pubspec.yaml +++ b/packages/neon/neon_news/pubspec.yaml @@ -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: diff --git a/packages/neon/neon_notes/pubspec.yaml b/packages/neon/neon_notes/pubspec.yaml index 0094174b2fa..051e73b6ec2 100644 --- a/packages/neon/neon_notes/pubspec.yaml +++ b/packages/neon/neon_notes/pubspec.yaml @@ -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: diff --git a/packages/neon/neon_notifications/pubspec.yaml b/packages/neon/neon_notifications/pubspec.yaml index 2704420304a..b4757d8cdba 100644 --- a/packages/neon/neon_notifications/pubspec.yaml +++ b/packages/neon/neon_notifications/pubspec.yaml @@ -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: diff --git a/packages/neon_framework/pubspec.yaml b/packages/neon_framework/pubspec.yaml index 7d0549e5442..cd9d875a8de 100644 --- a/packages/neon_framework/pubspec.yaml +++ b/packages/neon_framework/pubspec.yaml @@ -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 diff --git a/packages/neon_lints/analysis_options.yaml b/packages/neon_lints/analysis_options.yaml deleted file mode 120000 index 38b1608a9a2..00000000000 --- a/packages/neon_lints/analysis_options.yaml +++ /dev/null @@ -1 +0,0 @@ -lib/dart.yaml \ No newline at end of file diff --git a/packages/neon_lints/analysis_options.yaml b/packages/neon_lints/analysis_options.yaml new file mode 100644 index 00000000000..4db3c296b81 --- /dev/null +++ b/packages/neon_lints/analysis_options.yaml @@ -0,0 +1 @@ +include: package:neon_lints/dart.yaml diff --git a/packages/neon_lints/lib/flutter.yaml b/packages/neon_lints/lib/flutter.yaml index 65e82b6ca37..803e0429be7 100644 --- a/packages/neon_lints/lib/flutter.yaml +++ b/packages/neon_lints/lib/flutter.yaml @@ -1,5 +1,8 @@ include: package:neon_lints/src/base.yaml +analyzer: + plugins: + - custom_lint linter: rules: avoid_print: true diff --git a/packages/neon_lints/lib/neon_lints.dart b/packages/neon_lints/lib/neon_lints.dart new file mode 100644 index 00000000000..a50e83341a2 --- /dev/null +++ b/packages/neon_lints/lib/neon_lints.dart @@ -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 getLintRules(CustomLintConfigs _) => const [ + AvoidDebugPrint(), + ]; +} diff --git a/packages/neon_lints/lib/src/amend_model_suffix.dart b/packages/neon_lints/lib/src/amend_model_suffix.dart new file mode 100644 index 00000000000..916bde9d1df --- /dev/null +++ b/packages/neon_lints/lib/src/amend_model_suffix.dart @@ -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); + } + }); + } +} diff --git a/packages/neon_lints/pubspec.yaml b/packages/neon_lints/pubspec.yaml index c44a2ebd8fa..814bf77f9a0 100644 --- a/packages/neon_lints/pubspec.yaml +++ b/packages/neon_lints/pubspec.yaml @@ -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 diff --git a/pubspec.yaml b/pubspec.yaml index fcca74f7932..0e45330c263 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -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