`prefer-string-starts-ends-with`: incorrect autofix when string does not exist
See original GitHub issueProblem
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
Issue Analytics
- State:
- Created 2 years ago
- Comments:16
Top Results From Across the Web
"Running save participants" hangs indefinitely on VSCode ...
I tried removing the deprecated "eslint.autoFixOnSave": true and there was no change. This issue does not occur if I revert back to VSCode...
Read more >Auto Fix is enabled by default. Use the single string form
When I configure my vscode with eslint & prettier, I met a problem in .settings.json file with error ...
Read more >consistent-type-exports | typescript-eslint
TypeScript allows specifying a type keyword on exports to indicate that the export exists only in the type system, not at runtime.
Read more >@typescript-eslint/eslint-plugin | Yarn - Package Manager
All notable changes to this project will be documented in this file. ... ast-spec: correct some incorrect ast types (#6257 (0f3f645); eslint-plugin: ...
Read more >sindresorhus/eslint-plugin-unicorn (Raised $2856.00)
autofix `unicorn/prefer-array-some` can corrupt code easily ... `prefer-string-starts-ends-with`: incorrect autofix when string does not exist.
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Sure I’ll work on it.
Want work on it?