semi_lint 1.0.6
semi_lint: ^1.0.6 copied to clipboard
Lint rules of Dart, for use in mobile projects
example/lib/example.dart
class ValueNotifier<T> {
ValueNotifier(this.value);
T value;
}
// 触发规则:函数内对 ValueNotifier 有操作,且 catch 缺少 rethrow
void badWithoutRethrow() {
final counter = ValueNotifier<int>(0);
try {
counter.value = counter.value + 1;
} catch (e) {
print('oop22s: $e');
}
}
// 合规:函数内对 ValueNotifier 有操作,catch 中包含 rethrow
void goodWithRethrow() {
final counter = ValueNotifier<int>(0);
try {
counter.value = counter.value + 1;
} catch (e) {
rethrow;
}
}
// 不触发:函数内无 ValueNotifier 操作
void unrelatedCatchNoValueNotifier() {
try {
final x = 42;
print(x);
} catch (e) {
print(e);
}
}
// ---------------- 新增:为特定泛型添加演示用例 ----------------
class OptionModel {}
class CarrierListModel {}
class PickerItem<T> {
PickerItem(this.value);
final T value;
}
// 触发:ValueNotifier<List<PickerItem<OptionModel>>> 且 catch 无 rethrow
void badOptionListWithoutRethrow() {
final items = ValueNotifier<List<PickerItem<OptionModel>>>([]);
try {
items.value = [...items.value];
} catch (e) {
rethrow;
print('oops: $e');
}
}
// 合规:ValueNotifier[List<PickerItem<OptionModel>>> 且 catch 有 rethrow
void goodOptionListWithRethrow() {
final items = ValueNotifier<List<PickerItem<OptionModel>>>([]);
try {
items.value = [...items.value];
} catch (e) {
rethrow;
}
}
// 触发:ValueNotifier<List<PickerItem<CarrierListModel>>> 且 catch 无 rethrow
void badCarrierListWithoutRethrow() {
final items = ValueNotifier<List<PickerItem<CarrierListModel>>>([]);
try {
items.value = [...items.value];
} catch (e) {
print('oops: $e');
}
}
// 合规:ValueNotifier<List<PickerItem<CarrierListModel>>> 且 catch 有 rethrow
void goodCarrierListWithRethrow() {
final items = ValueNotifier<List<PickerItem<CarrierListModel>>>([]);
try {
items.value = [...items.value];
} catch (e) {
rethrow;
}
}
// 不触发:其他泛型不在规则范围
void unrelatedGenericValueNotifier() {
final items = ValueNotifier<List<int>>([1, 2, 3]);
try {
items.value = [...items.value, 4];
} catch (e) {
print(e);
}
}