Skip to content

Commit

Permalink
refactor: prefer asserts with message (#78)
Browse files Browse the repository at this point in the history
* refactor: prefer asserts with message

* chore(analysis): organize `analyzer.errors`
  • Loading branch information
albertms10 authored Nov 12, 2022
1 parent 113e42f commit 32c9864
Show file tree
Hide file tree
Showing 10 changed files with 41 additions and 17 deletions.
9 changes: 5 additions & 4 deletions analysis_options.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -70,10 +70,11 @@ analyzer:
- dart_code_metrics

errors:
always_put_required_named_parameters_first: ignore
always_use_package_imports: ignore
avoid_equals_and_hash_code_on_mutable_classes: ignore
prefer_asserts_with_message: ignore
public_member_api_docs: ignore
unused_element: ignore # See https://github.com/dart-lang/sdk/issues/49025

# Style decisions
always_put_required_named_parameters_first: ignore
always_use_package_imports: ignore
sort_constructors_first: ignore
unused_element: ignore
6 changes: 5 additions & 1 deletion lib/src/model/booking/booking_manager.dart
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,11 @@ class BookingManager with ChangeNotifier {
}

Duration occupiedDuration({DateTime? dateTime, DateRanger? dateRange}) {
assert(!((dateTime != null) && (dateRange != null)));
if (dateTime != null && dateRange != null) {
throw ArgumentError(
'Either dateTime or dateRange must be given, but not both.',
);
}

var runDuration = Duration.zero;

Expand Down
16 changes: 12 additions & 4 deletions lib/src/model/booking/recurring_booking.dart
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,11 @@ class RecurringBooking extends Booking {
this.repeatEvery = 1,
DateTime? recurringEndDate,
int? occurrences,
}) : assert((recurringEndDate == null) != (occurrences == null)),
}) : assert(
(recurringEndDate == null) != (occurrences == null),
'Either recurringEndDate, occurrences or none must be given, '
'but not both.',
),
_recurringEndDate = recurringEndDate,
_occurrences = occurrences;

Expand All @@ -51,7 +55,11 @@ class RecurringBooking extends Booking {
this.repeatEvery = 1,
DateTime? recurringEndDate,
int? occurrences,
}) : assert((recurringEndDate == null) != (occurrences == null)),
}) : assert(
(recurringEndDate == null) != (occurrences == null),
'Either recurringEndDate, occurrences or none must be given, '
'but not both.',
),
_recurringEndDate = recurringEndDate,
_occurrences = occurrences,
super(
Expand Down Expand Up @@ -91,7 +99,7 @@ class RecurringBooking extends Booking {
DateTime get recurringEndDate {
if (_recurringEndDate != null) return _recurringEndDate!;

assert(_occurrences != null);
assert(_occurrences != null, '_occurrences must be non-null.');

return date!.add(periodicityDuration * _occurrences!);
}
Expand All @@ -104,7 +112,7 @@ class RecurringBooking extends Booking {
int get occurrences {
if (_occurrences != null) return _occurrences!;

assert(_recurringEndDate != null);
assert(_recurringEndDate != null, '_recurringEndDate must be non-null.');

var count = 0;
var runDate = date!;
Expand Down
3 changes: 2 additions & 1 deletion lib/src/model/date/date_range.dart
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ class DateRange with DateRanger {
endDate == null ||
endDate.isAfter(startDate) ||
endDate.isAtSameMomentAs(startDate),
'endDate must be at the same moment or after startDate.',
) {
endDate ??= startDate;
}
Expand Down Expand Up @@ -56,7 +57,7 @@ mixin DateRanger {
DateTime end, {
Duration interval = const Duration(days: 1),
}) {
assert(end.isAfter(start));
assert(end.isAfter(start), 'end must be after start.');

final dates = [start];
var runDate = start;
Expand Down
6 changes: 5 additions & 1 deletion lib/src/model/date/date_range_item.dart
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,11 @@ class DateRangeItem extends Item with DateRanger {
DateTime? startDate,
DateTime? endDate,
}) : assert(
startDate == null || endDate == null || endDate.isAfter(startDate),
startDate == null ||
endDate == null ||
endDate.isAfter(startDate) ||
endDate.isAtSameMomentAs(startDate),
'endDate must be at the same moment or after startDate.',
) {
endDate ??= startDate;
}
Expand Down
2 changes: 1 addition & 1 deletion lib/utils/color_extension.dart
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ extension ColorExtension on Color {
required int samples,
double minOpacity = 0.2,
}) {
assert(samples > 0);
assert(samples > 0, 'samples must be greater than zero.');

final colorMap = SplayTreeMap<int, Color>.from({
1: withOpacity(minOpacity),
Expand Down
10 changes: 8 additions & 2 deletions lib/widgets/standalone/heat_map_calendar/heat_map_calendar.dart
Original file line number Diff line number Diff line change
Expand Up @@ -66,13 +66,19 @@ class HeatMapCalendar extends StatelessWidget {
this.lastDate,
this.highlightToday = false,
this.highlightOn,
}) : assert(firstWeekDay >= 1 && firstWeekDay <= DateTime.daysPerWeek);
}) : assert(
firstWeekDay >= 1 && firstWeekDay <= DateTime.daysPerWeek,
'firstWeekDay must be a valid week day number.',
);

/// Calculates the right amount of columns to create based on [maxWidth].
///
/// Returns the number of columns that the widget should have
int columnsToCreate(double maxWidth) {
assert(maxWidth > 2 * (squareSize + edgeSize));
assert(
maxWidth > 2 * (squareSize + edgeSize),
'maxWidth must be greater than the minimum available space.',
);

return maxWidth ~/ (squareSize + edgeSize);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ class HeatMapLegend extends StatelessWidget {
this.samples = 5,
this.lessLabel,
this.moreLabel,
}) : assert(samples > 2);
}) : assert(samples > 2, 'samples must be greater than two.');

Map<int, Color> get colorThresholds => {
1: defaultColor,
Expand Down
2 changes: 1 addition & 1 deletion lib/widgets/standalone/heat_map_calendar/utils/time.dart
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ DateTime safeAdd(DateTime dateTime, Duration duration) {
/// Creates a list of [DateTime], including all days
/// between [startDate] and [finishDate]
List<DateTime> datesBetween(DateTime startDate, DateTime finishDate) {
assert(startDate.isBefore(finishDate));
assert(finishDate.isAfter(startDate), 'finishDate must be after startDate.');

final datesList = <DateTime>[];
var aux = startDate;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ class WeekDaysLabels extends StatelessWidget {
required this.squareSize,
this.firstWeekDay = DateTime.sunday,
this.space = 4,
}) : assert(squareSize > 0);
}) : assert(squareSize > 0, 'squareSize must be greater than zero.');

@override
Widget build(BuildContext context) {
Expand Down

0 comments on commit 32c9864

Please sign in to comment.