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
Rule proposal: prefer-optional-chaining
#429
Comments
@jorgegonzalez |
It's in Stage 3 and already landed in Babel and TypeScript 🤷♂ |
why close? I think its great, and already using |
Big 👍, but we have to wait for ESLint to add support for it in their parser first. |
Optional chaining is now stage 4! |
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
Support for optional chaining was added in ESLint 7.5.0. Also, Interesting fact: quite a few of TypeScript rules can be used in JavaScript projects, see https://github.com/EvgenyOrekhov/eslint-config-hardcore#hardcorets-for-js. |
prefer-optional-chaining
Why not close this since @typescript-eslint/prefer-optional-chain exists? |
As a side note, I recently discovered this project: https://github.com/coderaiser/putout. It has an ESLint plugin, and it provides dozens of autofixes (some of them are quite advanced), including |
If safe, it could be added there, why write a whole rule from scratch?
But as you mentioned it could be used on JS files as well. Edit: I tried putout on Refined GitHub and mostly it just broke code and dropped whole lines of very clear code without any reason. I wouldn't trust it. Astounding 90% unwanted/breaking change rate. |
Seems like ESLint will add this rule at some point, so there's no point in adding it here. eslint/eslint#13430 |
There is a reason that the You can only detect these cases with a type-aware lint rule. For example: function foo(arg) {
return arg && arg.foo;
} Should this be converted into an optional chain? Seems like it... until you add more information about the expected types: function foo(arg: false | { foo: string }) {
return arg && arg.foo;
}
foo(false);
foo({ foo: 'a' }); Now it is no longer correct to make this an optional chain. Is this code a weird pattern? For sure. There are numerous other examples that you could dig out of the issues filed on https://github.com/typescript-eslint/typescript-eslint. [1] To clarify for people - an ESLint autofixer must be safe - meaning it mustn't break the build or change runtime semantics of code. This is why the The |
Yeah in this case the correct auto fix would be |
Copied from #2230 Failfoo === undefined ? foo : foo.bar // Autofix to `foo?.bar`
foo == undefined ? undefined : foo.bar // Autofix to `foo?.bar`
foo == null ? undefined : foo.bar // Autofix to `foo?.bar` Passfoo == null ? foo : bar
foo == undefined ? foo : bar
// Technically these can change the return type from `null` to `undefined`. So I'm not sure if they should pass or fail with suggestion
foo == undefined ? foo : foo.bar // Suggests `foo?.bar`
foo == null ? foo : foo.bar // Suggests `foo?.bar` Additional Infohttps://github.com/sindresorhus/eslint-plugin-unicorn/blob/v46.0.1/docs/rules/no-negated-condition.md already takes cares of the
|
https://typescript-eslint.io/rules/prefer-optional-chain/ Note that since this issue in v6 of our plugin we did a massive refactoring of the rule. It now uses type information because users found that there were simply too many false positives. By using type information it means the rule can also offer safe autofixes. My 2c: this issue should be closed because there's no way that this plugin can provide a syntax-only rule that offers reports without false positives or an autofixer that won't change runtime behaviour. |
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Optional_chaining
The text was updated successfully, but these errors were encountered: