-
-
Notifications
You must be signed in to change notification settings - Fork 26.9k
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
no-unused-expressions warning for optional chaining #8107
Comments
It looks like those features just aren't currently supported by eslint? From #8003 it looks like we support using them with typescript, but until eslint lands support I'm not sure there's a solution. Take this with a grain of salt because I wasn't involved in landing those features. |
I knew it looked like an eslint error, but why are eslint errors preventing the page from rendering? I believe in linting but I'm not sure I can see a good reason for preventing compiling if there's a lint problem. Until they add support, how can we disable this rule for CRA? Your link is broken btw, correct link: eslint/eslint#12642 |
I think that you can't on a project level; we only support overriding with inline comments. You might be better off just not using optional chaining (unless you're using typescript) until a fix for this lands in eslint. I'm sorry, I know that isn't a great answer. |
Sorry, I should've mentioned this previously - I am using TypeScript. How can I make it work in that case? |
You can only use it in typescript files. Using a fresh install with const foo = {} as any;
foo.unknown?.(); Just to be clear, I can reproduce in js files. |
Support already exists, you need to enable typescript-eslint/typescript-eslint#1241 (comment) I tried when I enable typescript variant but keep regular one and sadly, it will complain. Only when that regular rule is explicitly disabled, it works correctly.
Oh I see it's been already done: #8003 |
Just wanted to add, that you need TS < 3.7 for this to work, if that wasn't clear yet. |
@josias-r What? :) Optional chaining is a feature of TS 3.7. Whatever is working for you, might be because you have Babel somewhere in there 🤷♂ |
@FredyC sorry meant to say TS 3.7 or higher. I have no special babel setup in my project. react-scripts 3.3.0 and prettier is installed and I can use optional chaining without problems in TSX files. |
I think this issue can be closed now because |
I’m going to treat this as the tracking issue for optional chaining in non-typescript files, since that still isn’t supported afaik. |
It looks like there's a workaround by using eslint-plugin-babel if anyone wants to try that out? |
I tried using eslint-plugin-babel but I was still getting the no-unused-expression error when running |
How about this solution: babel/eslint-plugin-babel#185 (comment)? |
@RiZKiT Yes I tried that solution, and I'm still seeing the error when running
I still get the error when running
The line in question:
|
For Typescript, FredyC's answer is the solution. Thanks! |
@mofojed You're also supposed to use |
Seems like this should be working now for Typescript according to this issue (eslint/eslint#12822) but it's still not working for me so I probably need to update one of my dependencies, just don't want to go through the list right now. Update: Just double checked that I have Update 2: After looking at the answer from @FredyC, I added the following to my eslint config and now I'm all set:
|
after upgrading typescript-eslint to latest version (v4.1.0) today, my app build also starts to fail with
|
Since updating to TS 4 & @typescript-eslint/* 4.1 this linting error seems to be impossible to stop when using optional chaining.
|
Same here, no matter what I do, @typescript-eslint/no-unused-expressions keeps popping up |
Also encountering an issue where optional-chaining typescript is throwing false-positives at build-time. Introduced after updating react-scripts (3.4.3), eslint, and typescript packages. Settings via my .eslintrc file seem to run perfectly fine with eslint CLI. However, when I run react-scripts build I encounter the error @koriner mentioned at the first sight of an optional-chain. I attempted to disable |
@Sleepful It seems like it doesn't work for me, my .eslintrc:
I get this error:
EDIT for some strange reason, if I remove everything from my .eslintrc, it just works, not sure what's causing the issue but I'll investigate. Maybe |
Running into the same issue, but clearing .eslintrc didn't help. @cglacet any luck with the investigation? |
@TurboLion no, sorry. |
I've managed to mitigate the problem this way: .eslintrc.json "extends": ["react-app", "eslint:recommended"],
"parser": "@typescript-eslint/parser",
"parserOptions": {
"project": "tsconfig.json",
"sourceType": "module"
},
"plugins": ["react", "@typescript-eslint", "@typescript-eslint/eslint-plugin", "prefer-arrow"],
"rules": {
"no-unused-expressions": "off",
"@typescript-eslint/no-unused-expressions": "error"
} package.json
remove |
Tried this too (+ every other suggestion in this thread) but still can't get past the same no-unused-expressions error. |
@scottdevito |
TLDR: Other than upgrading to 4.x, I think that most people's issues are because they are installing eslint plugins themselves, which is a good thing if you want to start improving code quality in your repo! But once you do that, you'll probably find yourself in the abyss since CRA doesn't let you turn off their implementation of eslint. Part of the reason why you might be struggling to find out why your rule-overrides aren't working, is because CRA...wait for it... will cache your lint config! (noted in #9154 and #9007), making it even harder to debug where the issue lies with the convoluted mess of "who's really in control, and what version of each plugin is actually being used?". While debugging, make sure you purge that cache in between runs:
Also note the undocumented (maybe it was at some point, but their docs aren't versioned so 🤷♂️ ) env var My solution actually involves doing something you shouldn't do, and it'll probably bite me in the butt: Setting
And then changing my production build from using If I only list my upgrades You might not run into this issue if you aren't checking in your All of this headache could be avoided if there was just an option to let people turn off the linter, at least during a production build. Next on my plate: convincing my team that we will be okay if we |
this issue seems to be fixed in react-scripts@4.0.0 |
it works for me, thank u |
Describe the bug
I just upgrade
react-scripts
to 3.3.0. I am able to use nullish coalescing, but when I attempt to use optional chaining, I can't compile as I get this error:The line in question is:
Did you try recovering your dependencies?
No, I don't have time at the moment. If this might help I'll try it.
Which terms did you search for in User Guide?
I searched for optional chaining, no-unused-expressions, eslint optional chaining.
Environment
Steps to reproduce
npm start
.Expected behavior
I expected the app to compile and for the code to function like:
Actual behavior
The app failed to compile and gave the above message.
The text was updated successfully, but these errors were encountered: