Skip to content

Commit f6ef506

Browse files
committed
feat(neon_lints): add linting rule to avoid using any calls in production
Signed-off-by: Nikolas Rimikis <leptopoda@users.noreply.github.com>
1 parent e15ace2 commit f6ef506

File tree

15 files changed

+183
-2
lines changed

15 files changed

+183
-2
lines changed

melos.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ command:
3232
scripts:
3333
format: dart format --fix --line-length 120 .
3434
format:check: dart format --output=none --set-exit-if-changed --line-length 120 .
35-
analyze: dart analyze --fatal-infos .
35+
analyze: dart run custom_lint --fatal-infos .
3636
test: >
3737
melos exec --no-flutter --concurrency=1 --fail-fast --dir-exists=test -- "dart test --concurrency=$(nproc --all) --fail-fast" &&
3838
melos exec --flutter --concurrency=1 --fail-fast --dir-exists=test -- "flutter test --concurrency=$(nproc --all)"

packages/app/pubspec.lock

Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,30 @@
11
# Generated by pub
22
# See https://dart.dev/tools/pub/glossary#lockfile
33
packages:
4+
_fe_analyzer_shared:
5+
dependency: transitive
6+
description:
7+
name: _fe_analyzer_shared
8+
sha256: "0b2f2bd91ba804e53a61d757b986f89f1f9eaed5b11e4b2f5a2468d86d6c9fc7"
9+
url: "https://pub.dev"
10+
source: hosted
11+
version: "67.0.0"
12+
analyzer:
13+
dependency: transitive
14+
description:
15+
name: analyzer
16+
sha256: "37577842a27e4338429a1cbc32679d508836510b056f1eedf0c8d20e39c1383d"
17+
url: "https://pub.dev"
18+
source: hosted
19+
version: "6.4.1"
20+
analyzer_plugin:
21+
dependency: transitive
22+
description:
23+
name: analyzer_plugin
24+
sha256: "9661b30b13a685efaee9f02e5d01ed9f2b423bd889d28a304d02d704aee69161"
25+
url: "https://pub.dev"
26+
source: hosted
27+
version: "0.11.3"
428
ansicolor:
529
dependency: transitive
630
description:
@@ -129,6 +153,22 @@ packages:
129153
url: "https://pub.dev"
130154
source: hosted
131155
version: "2.0.3"
156+
ci:
157+
dependency: transitive
158+
description:
159+
name: ci
160+
sha256: "145d095ce05cddac4d797a158bc4cf3b6016d1fe63d8c3d2fbd7212590adca13"
161+
url: "https://pub.dev"
162+
source: hosted
163+
version: "0.1.0"
164+
cli_util:
165+
dependency: transitive
166+
description:
167+
name: cli_util
168+
sha256: c05b7406fdabc7a49a3929d4af76bcaccbbffcbcdcf185b082e1ae07da323d19
169+
url: "https://pub.dev"
170+
source: hosted
171+
version: "0.4.1"
132172
clock:
133173
dependency: transitive
134174
description:
@@ -201,6 +241,38 @@ packages:
201241
url: "https://pub.dev"
202242
source: hosted
203243
version: "1.0.6"
244+
custom_lint:
245+
dependency: "direct dev"
246+
description:
247+
name: custom_lint
248+
sha256: "445242371d91d2e24bd7b82e3583a2c05610094ba2d0575262484ad889c8f981"
249+
url: "https://pub.dev"
250+
source: hosted
251+
version: "0.6.2"
252+
custom_lint_builder:
253+
dependency: transitive
254+
description:
255+
name: custom_lint_builder
256+
sha256: "4c0aed2a3491096e91cf1281923ba1b6814993f16dde0fd60f697925225bbbd6"
257+
url: "https://pub.dev"
258+
source: hosted
259+
version: "0.6.2"
260+
custom_lint_core:
261+
dependency: transitive
262+
description:
263+
name: custom_lint_core
264+
sha256: ce5d6215f4e143f7780ce53f73dfa6fc503f39d2d30bef76c48be9ac1a09d9a6
265+
url: "https://pub.dev"
266+
source: hosted
267+
version: "0.6.2"
268+
dart_style:
269+
dependency: transitive
270+
description:
271+
name: dart_style
272+
sha256: "40ae61a5d43feea6d24bd22c0537a6629db858963b99b4bc1c3db80676f32368"
273+
url: "https://pub.dev"
274+
source: hosted
275+
version: "2.3.4"
204276
dbus:
205277
dependency: transitive
206278
description:
@@ -443,6 +515,14 @@ packages:
443515
url: "https://pub.dev"
444516
source: hosted
445517
version: "1.5.2"
518+
freezed_annotation:
519+
dependency: transitive
520+
description:
521+
name: freezed_annotation
522+
sha256: c3fd9336eb55a38cc1bbd79ab17573113a8deccd0ecbbf926cca3c62803b5c2d
523+
url: "https://pub.dev"
524+
source: hosted
525+
version: "2.4.1"
446526
glob:
447527
dependency: transitive
448528
description:
@@ -459,6 +539,14 @@ packages:
459539
url: "https://pub.dev"
460540
source: hosted
461541
version: "13.2.0"
542+
hotreloader:
543+
dependency: transitive
544+
description:
545+
name: hotreloader
546+
sha256: ed56fdc1f3a8ac924e717257621d09e9ec20e308ab6352a73a50a1d7a4d9158e
547+
url: "https://pub.dev"
548+
source: hosted
549+
version: "4.2.0"
462550
html:
463551
dependency: transitive
464552
description:
@@ -1423,6 +1511,14 @@ packages:
14231511
url: "https://pub.dev"
14241512
source: hosted
14251513
version: "1.1.0"
1514+
watcher:
1515+
dependency: transitive
1516+
description:
1517+
name: watcher
1518+
sha256: "3d2ad6751b3c16cf07c7fca317a1413b3f26530319181b37e3b9039b84fc01d8"
1519+
url: "https://pub.dev"
1520+
source: hosted
1521+
version: "1.1.0"
14261522
web:
14271523
dependency: transitive
14281524
description:

packages/app/pubspec.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ dependencies:
4141
vector_graphics: any
4242

4343
dev_dependencies:
44+
custom_lint: ^0.6.2
4445
neon_lints:
4546
git:
4647
url: https://github.com/nextcloud/neon

packages/neon/neon_dashboard/pubspec.yaml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ dependencies:
1414
sdk: flutter
1515
go_router: ^13.0.0
1616
intersperse: ^2.0.0
17+
logging: ^1.0.0
1718
meta: ^1.0.0
1819
neon_framework:
1920
git:
@@ -24,6 +25,7 @@ dependencies:
2425

2526
dev_dependencies:
2627
build_runner: ^2.4.8
28+
custom_lint: ^0.6.2
2729
flutter_test:
2830
sdk: flutter
2931
go_router_builder: ^2.4.1

packages/neon/neon_files/pubspec.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ dependencies:
4040

4141
dev_dependencies:
4242
build_runner: ^2.4.8
43+
custom_lint: ^0.6.2
4344
go_router_builder: ^2.4.1
4445
neon_lints:
4546
git:

packages/neon/neon_news/pubspec.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ dependencies:
3232

3333
dev_dependencies:
3434
build_runner: ^2.4.8
35+
custom_lint: ^0.6.2
3536
go_router_builder: ^2.4.1
3637
neon_lints:
3738
git:

packages/neon/neon_notes/pubspec.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ dependencies:
3131

3232
dev_dependencies:
3333
build_runner: ^2.4.8
34+
custom_lint: ^0.6.2
3435
go_router_builder: ^2.4.1
3536
neon_lints:
3637
git:

packages/neon/neon_notifications/pubspec.yaml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ dependencies:
1515
flutter_material_design_icons: ^1.0.0
1616
go_router: ^13.0.0
1717
intl: ^0.18.0
18+
logging: ^1.0.0
1819
meta: ^1.0.0
1920
neon_framework:
2021
git:
@@ -25,6 +26,7 @@ dependencies:
2526

2627
dev_dependencies:
2728
build_runner: ^2.4.8
29+
custom_lint: ^0.6.2
2830
go_router_builder: ^2.4.1
2931
neon_lints:
3032
git:

packages/neon/neon_talk/pubspec.yaml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ dependencies:
1313
flutter_localizations:
1414
sdk: flutter
1515
go_router: ^13.0.0
16+
logging: ^1.0.0
1617
meta: ^1.0.0
1718
neon_framework:
1819
git:
@@ -23,6 +24,7 @@ dependencies:
2324

2425
dev_dependencies:
2526
build_runner: ^2.4.8
27+
custom_lint: ^0.6.2
2628
flutter_test:
2729
sdk: flutter
2830
go_router_builder: ^2.4.1

packages/neon_framework/pubspec.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@ dependencies:
6464

6565
dev_dependencies:
6666
build_runner: ^2.4.8
67+
custom_lint: ^0.6.2
6768
flutter_test:
6869
sdk: flutter
6970
go_router_builder: ^2.4.1

packages/neon_lints/lib/flutter.yaml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
include: package:neon_lints/src/base.yaml
22

3+
analyzer:
4+
plugins:
5+
- custom_lint
36
linter:
47
rules:
58
avoid_print: true
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import 'package:custom_lint_builder/custom_lint_builder.dart';
2+
import 'package:neon_lints/src/avoid_debug_print.dart';
3+
4+
/// Registers the neon lints plugin.
5+
///
6+
/// Do **not** use this directly.
7+
/// This method is used when enabling the `custom_lint` in the
8+
/// `analysis_options.yaml` file.
9+
PluginBase createPlugin() => _NeonLintsPlugin();
10+
11+
class _NeonLintsPlugin extends PluginBase {
12+
@override
13+
List<LintRule> getLintRules(CustomLintConfigs _) => const [
14+
AvoidDebugPrint(),
15+
];
16+
}
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
import 'package:analyzer/dart/ast/ast.dart';
2+
import 'package:analyzer/error/listener.dart';
3+
import 'package:custom_lint_builder/custom_lint_builder.dart';
4+
5+
/// Lint rule checking that no print methods from `flutter:framework` are used.
6+
///
7+
/// Similar to the analyzer rule `avoid_print`.
8+
/// Use `https://pub.dev/packages/logging` instead.
9+
///
10+
/// ```dart
11+
/// debugPrint('message'); // lint
12+
/// debugPrintStack(stackTrace: stackTrace); // lint
13+
/// debugPrintSynchronously('message'); // lint
14+
/// debugPrintThrottled('message'); // lint
15+
/// ```
16+
///
17+
/// Refer to `https://stackoverflow.com/questions/2031163/when-to-use-the-different-log-levels/64806781#64806781`
18+
/// if unsure about the logging level to use.
19+
final class AvoidDebugPrint extends DartLintRule {
20+
/// @nodoc
21+
const AvoidDebugPrint() : super(code: _code);
22+
23+
static const _code = LintCode(
24+
name: 'avoid_debug_print',
25+
problemMessage: "Do not invoke 'debugPrint' and 'debugPrintStack' in production code.",
26+
correctionMessage: '''
27+
Use 'package:logging' instead.
28+
Refer to https://stackoverflow.com/questions/2031163/when-to-use-the-different-log-levels/64806781#64806781
29+
if unsure about the logging level to use.
30+
''',
31+
);
32+
33+
static const String _debugPrint = 'debugPrint';
34+
35+
@override
36+
void run(
37+
CustomLintResolver resolver,
38+
ErrorReporter reporter,
39+
CustomLintContext context,
40+
) {
41+
context.registry.addInvocationExpression((node) {
42+
final element = node.function;
43+
if (element case Identifier(:final name) when name.startsWith(_debugPrint)) {
44+
reporter.reportErrorForToken(code, node.beginToken);
45+
}
46+
});
47+
}
48+
}

packages/neon_lints/pubspec.yaml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,5 +6,10 @@ publish_to: none
66
environment:
77
sdk: '>=3.1.0 <4.0.0'
88

9+
dependencies:
10+
analyzer: ^6.4.1
11+
analyzer_plugin: ^0.11.3
12+
custom_lint_builder: ^0.6.2
13+
914
dev_dependencies:
1015
lint_maker: ^0.2.1

pubspec.yaml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,11 @@ name: neon_workspace
22
publish_to: none
33

44
environment:
5-
sdk: '>=3.1.0 <4.0.0'
5+
sdk: '>=3.2.0 <4.0.0'
6+
67
dev_dependencies:
78
commitlint_cli: ^0.7.1
89
fvm: ^3.0.12
910
husky: ^0.1.7
1011
melos: ^4.1.0
12+
custom_lint: ^0.6.2

0 commit comments

Comments
 (0)