Meta: Warn contributors against using ?. indiscriminately#9197
Conversation
Agent-Logs-Url: https://github.com/refined-github/refined-github/sessions/ea867e40-ec76-44bd-87ac-7b8a37f901e0 Co-authored-by: fregante <[email protected]>
|
@copilot no the rule should block ?. in favor of !. If ?. is justified, people will have to disable the rule with an explanation of why the property might be missing. The explanation cannot be "it might change in the future" as that's exactly when we want the code to fail. I did not mention querySelector at all, the rule applies to all code indiscriminately |
querySelector()?. in favor of $optional()…al chaining Co-authored-by: Copilot <[email protected]> Co-authored-by: fregante <[email protected]>
Understood — the rule should block all |
?. optional chaining in favor of !.
…nal=true] rule, use ESLint suppressions file for existing violations Agent-Logs-Url: https://github.com/refined-github/refined-github/sessions/390235c0-198f-43ec-96ca-07e298d76711 Co-authored-by: fregante <[email protected]>
Done in 0be0731. Reverted all |
…l and suppress via eslint-suppressions.json Agent-Logs-Url: https://github.com/refined-github/refined-github/sessions/16b19256-e785-48b5-96bc-a60dcb2426b9 Co-authored-by: fregante <[email protected]>
|
@copilot create a custom plugin and rule instead. This way the exceptions will include the name of the rule instead of the "no-restricted-syntax" |
|
@SunsetTechuila I know the rule will be kinda noisy, but I want to explore what it means for the repo. I don't want to have to manually ask each PR if ?. matches our expectations regarding failing code. I wrote that long comment enough times already |
…ed-github/no-optional-chaining rule Agent-Logs-Url: https://github.com/refined-github/refined-github/sessions/5b545f27-9b4d-480d-961e-999fb49b5f97 Co-authored-by: fregante <[email protected]>
…e suppressions Agent-Logs-Url: https://github.com/refined-github/refined-github/sessions/9a1f7e53-5fc3-46a2-9f3b-452e48c9f8a8 Co-authored-by: fregante <[email protected]>
Done in fb6195a:
|
| }, | ||
| { | ||
| plugins: { | ||
| 'refined-github': refinedGithubPlugin, |
There was a problem hiding this comment.
I can't believe it's so easy to create custom plugins/rules now 😍 it used to be a PITA before flat config. This opens the floodgates to all kinds of rules that would be too complex for no-restricted-syntax.
|
|
||
| context.report({ | ||
| node, | ||
| message: 'Use `!.` instead of `?.`. If the value can be nullish, disable this rule with a comment explaining why.', |
There was a problem hiding this comment.
Before merge: this message needs to be extended to explain what we expect from the comment. Example: describe in what scenario the value can CURRENTLY be null. If you cannot CURRENTLY find a nullish value, use !. instead.
There was a problem hiding this comment.
Updated in 625f1b8 — the error message now reads: "Use !. instead of ?.. Add a comment on the same or preceding line describing in which scenario the value can CURRENTLY be null. If you cannot find such a scenario, use !. instead."
It's not worth it at all |
Just check this diff. How many of those disable comments are really necessary? I'd say that barely any. Instead of sweeping the errors under the carpet with suppressions, try to add a comment for every optional chaining use explaining why the property can be nullish and why it's safe to continue execution in that case. |
Some is better than none. We can target those later.
The observer actually has an integrated warning that's logged when it cannot find the element. Unfortunately we never enabled by default because it would need an "optional" setting for cases where the selector doesn't match every single page in And again, some is better than none. Yours is whataboutism.
True. This is an exploratory PR
We have 4 options:
The first one is a lot of work that requires time I might not have right now. |
The point was that this "some" is too small a part of "all" to justify its cost |
I'm sure you'll see after just 15 instances how little value this rule has |
I opened the commit where copilot added the comments and can immediately see a bunch of useless chaining: None of those are necessary, repository.issue.nodes exists on repos with issues disabled too. Only 2 out of 10 question marks are valid
That looks like an error and it should use !.
Valid, ok with comment
Valid, ok with already-existing comment
Valid, ok with comment I went halfway through the commit and saw a bunch of incorrect uses, as well as many valid ones. I think it's 20/80 for this, but even for valid ones I would certainly appreciate a comment explaining when we're expected to find the element. You're right in saying it will be noisy, but it is doing its job. We can restrict the rule to |
|
…error message, fix bugs-tab.tsx, prune suppressions Agent-Logs-Url: https://github.com/refined-github/refined-github/sessions/ee596444-031d-4660-835d-e39500a5e0c0 Co-authored-by: fregante <[email protected]>
Done in 625f1b8:
|
?. optional chaining in favor of !.?. optional chaining in source/features/ in favor of !.
That's the whole point of suppressions btw, for us to gradually remove them |
?. optional chaining in source/features/ in favor of !.?. indiscriminately
|
Let's give it a go |
The repo had no rule preventing the use of
?.optional chaining. This PR adds a customrefined-github/no-optional-chainingESLint rule scoped tosource/features/**that blocks?.property access in favor of!.. When?.is genuinely justified, developers can document it with a comment on the same or preceding line, or add a suppression entry.Changes
eslint.config.js: defines an inlinerefinedGithubPluginwith ano-optional-chainingrule, registered asrefined-github/no-optional-chaining: 'error', scoped tosource/features/**only. The rule handles allMemberExpression[optional=true]nodes with two behaviors:$()call, it reports a more specific message: "Either use$optional()with?.or$()without.$()will throw when the element is not found." — previously this was a separateno-restricted-syntaxentry, now consolidated into the custom plugin.!.instead of?.. Add a comment on the same or preceding line describing in which scenario the value can CURRENTLY be null. If you cannot find such a scenario, use!.instead."?.whose immediately preceding line starts with//or ends with*/, or whose same line contains a trailing//comment, is exempt — developers can document why a value may be nullish with a plain comment instead of a suppression entry.source/features/bugs-tab.tsx: updatedApiResponsetype to remove unnecessary optional markers onissues,labels, andnodes(always present in the API response). Replacedrepository?.issues?.totalCountandrepository?.labels?.nodes?.find(...)with direct non-optional access. Remaininglabel?.usages are kept with explanatory comments sincelabelmay genuinely beundefined.eslint-suppressions.json: regenerated using ESLint v9's suppression feature. All entries referencerefined-github/no-optional-chainingby name. After scoping the rule tosource/features/, adding both comment exceptions, and fixingbugs-tab.tsx, the file was pruned to 55 suppressions across 37 files (down from the initial 139/77). New code must use!., add an explanatory comment next to or before the?., or add a suppression entry with justification.Test URLs
Screenshot