eslint-plugin-unicorn icon indicating copy to clipboard operation
eslint-plugin-unicorn copied to clipboard

Rule proposal: cron-comment

Open mmkal opened this issue 2 years ago • 3 comments

Description

Cron expressions are used in lots of places, but they're hard to read and misunderstanding them can be quite problematic.

This lint rule could detect cron expressions, and require the line above have a comment describing the expression accurately

Fail

export const someJobConfig = {
  schedule: {cron: '0 * * * *'}
}
export const someOtherJobConfig = {
  schedule: {cron: '*/15 8-18 * * 1-5'}
}

Pass

export const someJobConfig = {
  // Every hour
  schedule: {cron: '0 * * * *'}
}
export const someOtherJobConfig = {
  // Every 15 minutes, between 08:00 AM and 06:59 PM, Monday through Friday
  schedule: {cron: '*/15 8-18 * * 1-5'}
}

Additional Info

The library construe can parse these expressions and produce a human-readable equivalent. Violations could be auto-fixable.

If it could support yaml as well, it'd work with github actions workflows etc.

mmkal avatar Jun 02 '23 00:06 mmkal

IMHO, a better solution would be to use a library that lets you write it using a fluent and human-friendly API which then gets compiled down to a cron expression. Basically, the inverse.

sindresorhus avatar May 08 '24 22:05 sindresorhus

Also, a problem with a comment like that is that it's easy for it to get outdated.

sindresorhus avatar May 08 '24 22:05 sindresorhus

Also, a problem with a comment like that is that it's easy for it to get outdated.

The lint rule would make sure the comment stayed up to date and accurate. It'd be a lint error if the cron expression changed without updating the comment.

the inverse

Agreed about a fluent api being ideal, but realistically there will still be cron expressions used, especially in places where it's not practical to add such a library as a dependency for whatever reason. The nice thing about a lint rule is it's only a dev dependency.

The YAML use case is the clearest one to see - cron expressions are used in GitHub actions yaml, as well kubernetes resource files, and lots of other declarative orchestration configs. Those can't use js libraries, but they can use eslint with an appropriate processor.

mmkal avatar May 09 '24 12:05 mmkal