Skip to content

Commit fc0e03e

Browse files
committed
feat: add base class rule
1 parent 11e1e4a commit fc0e03e

13 files changed

Lines changed: 1012 additions & 336 deletions

README.md

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ Lint rules for using [Grumpy](https://github.com/necodeIT/grumpy) architecture c
99
| [must_call_in_constructor](#must_call_in_constructor) | Require constructors to call methods annotated with @mustCallInConstructor on supertypes or mixins. | ERROR || 3 |
1010
| [abstract_classes_should_set_log_group](#abstract_classes_should_set_log_group) | Abstract classes using LogMixin must override group with their class name, or append to super.group. | ERROR || 1 |
1111
| [concrete_classes_should_set_log_tag](#concrete_classes_should_set_log_tag) | Concrete classes using LogMixin must override logTag with their class name. | ERROR || 1 |
12+
| [base_class](#base_class) | Subclasses of classes annotated with BaseClass must follow layer, naming, and file layout rules. | WARNING || 6 |
1213

1314

1415

@@ -89,3 +90,30 @@ class MyConcreteClass with LogMixin {
8990
9091
```
9192

93+
94+
95+
96+
### base_class
97+
98+
Subclasses of classes annotated with BaseClass must follow layer, naming, and file layout rules.
99+
#### Codes
100+
- `base_class_invalid_layer` (WARNING)
101+
- `base_class_missing_suffix` (WARNING)
102+
- `base_class_wrong_directory` (WARNING)
103+
- `base_class_wrong_file_name` (WARNING)
104+
- `base_class_extra_class` (WARNING)
105+
- `base_class_missing_extension` (WARNING)
106+
107+
#### Examples
108+
**✅ DO**
109+
```dart
110+
abstract class UserService extends Service {}
111+
112+
```
113+
114+
**❌ DON'T**
115+
```dart
116+
abstract class UserManager extends Service {}
117+
118+
```
119+

lib/main.dart

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import 'package:analysis_server_plugin/plugin.dart';
22
import 'package:analysis_server_plugin/registry.dart';
33
import 'package:grumpy_lints/src/rule.dart';
44
import 'package:grumpy_lints/src/rules/abstract_classes_should_set_log_group_rule.dart';
5+
import 'package:grumpy_lints/src/rules/base_class_rule.dart';
56
import 'package:grumpy_lints/src/rules/concrete_classes_should_set_log_tag_rule.dart';
67
import 'package:grumpy_lints/src/rules/must_call_in_constructor_rule.dart';
78

@@ -16,6 +17,7 @@ class GrumpyLints extends Plugin {
1617
MustCallInConstructorRule(),
1718
AbstractClassesShouldSetLogGroupRule(),
1819
ConcreteClassesShouldSetLogTagRule(),
20+
BaseClassRule(),
1921
];
2022

2123
@override

lib/src/context.dart

Lines changed: 0 additions & 277 deletions
This file was deleted.

lib/src/log.dart

Lines changed: 36 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -28,15 +28,24 @@ extension GrumpyLog on RuleContext {
2828
GrumpyLogSeverity severity,
2929
String message,
3030
) {
31-
final root = _findWorkspaceRoot(context);
32-
final file = File(_joinPath(root, logFileName));
3331
final timestamp = DateTime.now().toIso8601String();
3432
final line = '[${severity.name.toUpperCase()}] $timestamp $message';
35-
file.writeAsStringSync(
36-
'$line${Platform.lineTerminator}',
37-
mode: FileMode.append,
38-
flush: true,
39-
);
33+
if (_shouldPrintToStdout()) {
34+
_printLine(severity, line);
35+
return;
36+
}
37+
38+
final root = _findWorkspaceRoot(context);
39+
final file = File(_joinPath(root, logFileName));
40+
try {
41+
file.writeAsStringSync(
42+
'$line${Platform.lineTerminator}',
43+
mode: FileMode.append,
44+
flush: true,
45+
);
46+
} on FileSystemException {
47+
_printLine(severity, line);
48+
}
4049
}
4150

4251
static String _findWorkspaceRoot(RuleContext context) {
@@ -54,4 +63,24 @@ extension GrumpyLog on RuleContext {
5463
}
5564
return '$root$separator$file';
5665
}
66+
67+
static bool _shouldPrintToStdout() {
68+
final env = Platform.environment;
69+
if (env['GRUMPY_LINTS_LOG_STDOUT'] == '1' ||
70+
env['GRUMPY_LINTS_LOG_STDOUT']?.toLowerCase() == 'true') {
71+
return true;
72+
}
73+
return env.containsKey('DART_TEST') ||
74+
env.containsKey('FLUTTER_TEST') ||
75+
env.containsKey('UNIT_TEST');
76+
}
77+
78+
static void _printLine(GrumpyLogSeverity severity, String line) {
79+
if (severity == GrumpyLogSeverity.error ||
80+
severity == GrumpyLogSeverity.warning) {
81+
stderr.writeln(line);
82+
return;
83+
}
84+
stdout.writeln(line);
85+
}
5786
}

0 commit comments

Comments
 (0)