question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

`prefer-string-starts-ends-with`: incorrect autofix when string does not exist

See original GitHub issue

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:

  1. Do nothing since the autofixer has been around or a year (#711) and no one else has complained
  2. Temporarily turn the autofixer into an automated suggestion until we can use the optional chaining autofix
  3. Warn about this situation in the rule doc

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:16

github_iconTop GitHub Comments

2reactions
bmishcommented, May 14, 2021

Sure I’ll work on it.

0reactions
fiskercommented, May 14, 2021

Want work on it?

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found