Repository navigation
Meta: use and prefer $closest from select-dom - #9325
Conversation
…sestOptional Agent-Logs-Url: https://github.com/refined-github/refined-github/sessions/7d4877cc-7fc6-4b13-8d54-0c9bdba31918 Co-authored-by: fregante <1402241+fregante@users.noreply.github.com>
$closest from select-dom
fregante
left a comment
There was a problem hiding this comment.
In some comments I mentioned using $(). I'm referring to replacing querySelector
…remove String() wrapper Agent-Logs-Url: https://github.com/refined-github/refined-github/sessions/f5656b6b-dc1b-4934-a413-dd87e3c67879 Co-authored-by: fregante <1402241+fregante@users.noreply.github.com>
| const fileLink = fileIcon | ||
| .closest('.react-directory-filename-column')! | ||
| .querySelector('a.Link--primary')!; | ||
| const fileLink = $('a.Link--primary', $closest('.react-directory-filename-column', fileIcon)); |
There was a problem hiding this comment.
Unfortunate readability downgrade, but now we get better errors 🥲
…ple lines Agent-Logs-Url: https://github.com/refined-github/refined-github/sessions/6ea377f0-4edd-4fcd-92da-500446f40af5 Co-authored-by: fregante <1402241+fregante@users.noreply.github.com>
|
@copilot Vitest is failing |
…ional instanceof check Agent-Logs-Url: https://github.com/refined-github/refined-github/sessions/00b45907-aff2-4d0b-92ab-e05740b7736a Co-authored-by: fregante <1402241+fregante@users.noreply.github.com>
There was a problem hiding this comment.
I might be biased but I think this is fantastic. Virtually every .closest() call we had was followed by !, so this addresses a real issue.
There are 3/4 instances of reduces readability like:
event
.delegateTarget
.closest('div')
.querySelector('a')becoming:
$('a', $closest('div', event.delegateTarget))but we could either accept it or revert it (in the future, if we think it's bad and not just "a bad change")
We could also wrap it this way to retain some readability, but it only works if the selectors are long
$(
'a.Link--primary',
$closest(
'.react-directory-filename-column',
event.delegateTarget
),
)|
|
I don't want to reinvent jQuery. I've gone back and forth on this kind of magic and I've settled on simple independent functions. See also: |
|
See also my other attempts at similar magic: |
|
I'll merge this after the release |
…-dom-to-v10 # Conflicts: # source/features/actionable-pr-view-file.tsx # source/features/batch-mark-files-as-viewed.tsx Co-authored-by: fregante <1402241+fregante@users.noreply.github.com>
This comment was marked as resolved.
This comment was marked as resolved.
Agent-Logs-Url: https://github.com/refined-github/refined-github/sessions/eb7aeef7-2e6a-4056-b82a-c6b9e995fb8d Co-authored-by: fregante <1402241+fregante@users.noreply.github.com>
|
@copilot run "npm run fix" |
Agent-Logs-Url: https://github.com/refined-github/refined-github/sessions/79f2b890-993d-4334-bab0-de4d6c308e5d Co-authored-by: fregante <1402241+fregante@users.noreply.github.com>
Done in b4c07ce. The only change was a formatting fix in |
Upgrades
select-domto v10 and migrates all.closest()and.querySelector()calls to the new$closest/$closestOptional/$()API.Rules:
element.closest(sel)!→$closest(sel, element)element.closest(sel)/?.closest()→$closestOptional(sel, element)element.querySelector(sel)!→$(sel, element)ESLint rule (
eslint-rules/select-dom.js)element.closest(sel)— detectsTSNonNullExpressionparent to pick$closestvs$closestOptionalESLint config (
eslint.config.js)$closestand$closestOptionalto the array-with-comments and single-element-array selectors[callee.property.name=closest](no longer needed)TSNonNullExpression > CallExpression > [name=$closest]— flags unnecessary!since$closestalready throwsSource migration (~70 files)
.closest()calls replaced viaeslint --fix.querySelector()calls on$closest(...)results replaced with$()inaction-pr-link,comments-time-machine-links,quick-file-edit,release-download-count,restore-file,status-subscription,options/feature-listselect-domimports updated in every affected filecross-deleted-pr-branches: added explicit!lastBranchActionguard before$closestcall to avoid throwing when the element is absentdom-formatters: removed now-unnecessaryString()wrapper —$closestOptionalacceptsreadonly string[]directly in v10one-click-review-submission: inlinedlegacyActionsRowintermediate variable to a single lineprevent-link-loss: wrapped multi-selector array onto multiple lines for readabilityactionable-pr-view-file: merged with main's refactor (newhandleMenuOpening/isDeletedFile/getFilePath), updated remaining.closest()calls to$closest; import expanded to multi-line by formatterbatch-mark-files-as-viewed: merged with main'shandleClickrouting structure, updated all three.closest(fileSelector)!calls (remember,batchToggle,onAltClick) to$closestTest fix (
test/setup-file.js)Elementto the linkedom globals so$closestOptional'sinstanceof Elementcheck works correctly in the Vitest environmentTest URLs
Screenshot