sindresorhus / eslint-plugin-unicorn

More than 100 powerful ESLint rules
MIT License
4.04k stars 359 forks source link

Rule proposal: cron-comment #2147

Open mmkal opened 1 year ago

mmkal commented 1 year ago

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.

sindresorhus commented 2 months ago

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 commented 2 months ago

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

mmkal commented 2 months ago

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.