New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
prefer-string-starts-ends-with
: incorrect autofix when string does not exist
#1267
Comments
I assume you mean |
@fisker no actually. The situation here is that the string ( |
Oh right. but if function didn't exists |
Correct fix for this case should be String(lang).startsWith('en') |
@fisker that fix should work, although it's not the most elegant fix, since most of the time the string casting will be unnecessary and potentially cause the reader confusion (it's not clear that it's there to solve the problem of undefined/null). So we could use that but we should definitely follow-up to use optional chaining in a year. |
Using |
No, you are wrong
|
Oh wow. I am surprised by the behavior with |
Yap, if it's not string, it convert to string first, we can remove auto fix for it or use suggestions. |
What if it's unknown value? |
I'm kind of torn on this. Ideally, an autofix should be both correct and match the intent of the original code.
There's no perfect choice. I could see arguments for each. One argument for keeping today's autofix is that it's the nicest fix and it will also fail-fast if someone uses undefined with it, so any issue should be detected quickly. |
I prefer keep the current fix, but suggest |
Like suggestions:
|
Yeah I think we're agreed on keeping the current autofix and then providing all of these as suggestions. |
Want work on it? |
Sure I'll work on it. |
Problem
If the string variable is null or undefined, the autofix will cause an exception.
Before autofix:
After autofix:
Fix
A year from now when we drop support for Node 12, we can use optional chaining to autofix this safely:
Until then, we have a few options:
prefer-starts-ends-with
: Add auto-fix #711) and no one else has complainedThe text was updated successfully, but these errors were encountered: