Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve duration parsing and error feedback #115

Merged
merged 7 commits into from
Nov 10, 2024

Conversation

Eisenwave
Copy link
Contributor

Closes #114.

I've had some issues running checks, so this is probably going to fail some lint checks, but here's the idea for now.

@Eisenwave Eisenwave force-pushed the improve-duration-handling branch 2 times, most recently from 04bf3ee to a25df20 Compare November 7, 2024 17:05
Copy link
Member

@jeremy-rifkin jeremy-rifkin left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for looking into this

Copy link
Member

@jeremy-rifkin jeremy-rifkin left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, a couple minor comments

src/utils/strings.ts Outdated Show resolved Hide resolved
src/components/moderation/moderation-common.ts Outdated Show resolved Hide resolved
const [_, n, unit] = match;
const unit_millis = millis_of_time_unit(unit);
if (unit_millis == null) {
throw new ParseError(`Invalid time unit in duration: ${unit}`);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It might be best to throw this where you return null; from millis_of_time_unit

Copy link
Contributor Author

@Eisenwave Eisenwave Nov 9, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I did consider that, and I prefer keeping it because it makes millis_of_time_unit more reusable. There's no reason why it innately needs to throw, and we could use it later to e.g. verify whether a time unit is valid with millis_of_time_unit(...) == null.

// Returns the corresponding duration in milliseconds,
// or null for permanent duration (returned if the given duration is null).
// Throws ParseError when parsing fails.
export function parse_nullable_duration(duration: string | null) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Take or leave suggestion: This can probably go away now

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There are some places that call the plain parse_duration. I think it makes sense to keep these functions separate because it isolates the "null is permanent" decision into a convenience function, making parse_duration slightly more reusable and safer.

One could run into the case where they're getting string | null from some other function where null represents an error for instance, and plugging that straight into a parse_duration(string | null) function would silence what should be a type error.

Copy link

sonarcloud bot commented Nov 9, 2024

Copy link
Member

@jeremy-rifkin jeremy-rifkin left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM thanks!

@jeremy-rifkin jeremy-rifkin merged commit 5acbbe2 into TCCPP:main Nov 10, 2024
5 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Make parse_unit more permissive
2 participants