eslint-plugin-unicorn: `prefer-string-starts-ends-with`: incorrect autofix when string does not exist
Problem
If the string variable is null or undefined, the autofix will cause an exception.
Before autofix:
/^en/.test(lang)
// Returns `false` if `lang` is `undefined` / `null`
After autofix:
lang.startsWith('en')
// Uncaught TypeError: Cannot read property 'startsWith' of undefined
Fix
A year from now when we drop support for Node 12, we can use optional chaining to autofix this safely:
lang?.startsWith('en')
Until then, we have a few options:
- Do nothing since the autofixer has been around or a year (#711) and no one else has complained
- Temporarily turn the autofixer into an automated suggestion until we can use the optional chaining autofix
- Warn about this situation in the rule doc
About this issue
- Original URL
- State: closed
- Created 3 years ago
- Comments: 16
Sure I’ll work on it.