Skip to content

Commit 1fcbb12

Browse files
refactor: replace if(el) el.method() guards with optional chaining (#331)
Use el?.method() in place of if (el) el.method() for method-call guards across popup.js, injector.js, and detector.js. Property assignments are left as-is since ?.= is not supported. - popup.js: errRow?.classList.remove/toggle, el?.addEventListener - injector.js: panel?.remove, body?.setAttribute/removeAttribute - detector.js: observer?.disconnect + unconditional observer = null Minimum Chrome version is 111, so optional chaining is fully supported. Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com> Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
1 parent 47c6560 commit 1fcbb12

3 files changed

Lines changed: 8 additions & 10 deletions

File tree

content/detector.js

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -844,10 +844,8 @@ const TradeDetector = (() => {
844844
* Safe to call even if `observe()` was never called.
845845
*/
846846
function disconnect() {
847-
if (observer) {
848-
observer.disconnect();
849-
observer = null;
850-
}
847+
observer?.disconnect();
848+
observer = null;
851849
if (inputListener) {
852850
document.removeEventListener('input', inputListener, true);
853851
inputListener = null;

content/injector.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -384,7 +384,7 @@ const MarginInjector = (() => {
384384
*/
385385
function remove() {
386386
const panel = getPanel();
387-
if (panel) panel.remove();
387+
panel?.remove();
388388
}
389389

390390
/**
@@ -433,7 +433,7 @@ const MarginInjector = (() => {
433433
// stale or empty values (e.g. '--') that would confuse AT when the error alert
434434
// fires. Mirrors the aria-hidden="true" set by showLoading(); only updatePanel()
435435
// should expose body content by removing the attribute.
436-
if (body) body.setAttribute('aria-hidden', 'true');
436+
body?.setAttribute('aria-hidden', 'true');
437437
if (error) {
438438
if (errorText) errorText.textContent = msg || 'Unknown error';
439439
if (retryBtn) retryBtn.style.display = canRetry ? 'inline-block' : 'none';
@@ -468,7 +468,7 @@ const MarginInjector = (() => {
468468
// display/opacity of body, loading spinner, and error row are driven by
469469
// data-fmc-state CSS rules — no inline style manipulation needed here.
470470
const { body, errorText: errorTextEl, creditDebit: creditDebitEl, creditDebitLabel, delta: deltaEl, cash: cashEl, buyingPower: bpEl, debugLog: debugLogEl, debugBtn: debugBtnEl } = getPanelElements(panel);
471-
if (body) body.removeAttribute('aria-hidden');
471+
body?.removeAttribute('aria-hidden');
472472
// Clear stale error text — mirrors the same guard in showLoading(). When the
473473
// extension later re-enters error state from result state, the role="alert" row
474474
// must be empty as it enters the accessibility tree to avoid double-announcement.

popup/popup.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@
104104
if (calcEl) calcEl.textContent = '--';
105105
if (callsEl) callsEl.textContent = '0';
106106
if (errEl) errEl.textContent = '';
107-
if (errRow) errRow.classList.remove('has-error');
107+
errRow?.classList.remove('has-error');
108108
return;
109109
}
110110

@@ -126,7 +126,7 @@
126126
// when it enters the accessibility tree — improves screen-reader announcement
127127
// reliability when the row transitions from visually-hidden to visible.
128128
if (errEl) errEl.textContent = status.lastError || '';
129-
if (errRow) errRow.classList.toggle('has-error', !!status.lastError);
129+
errRow?.classList.toggle('has-error', !!status.lastError);
130130
}
131131

132132
// --- Settings ---
@@ -280,7 +280,7 @@
280280
// Settings change handlers — use getSettingsEls() so IDs stay in one place
281281
const { enabled: enabledEl, threshold: thresholdEl, debounce: debounceEl } = getSettingsEls();
282282
for (const el of [enabledEl, thresholdEl, debounceEl]) {
283-
if (el) el.addEventListener('change', saveSettings);
283+
el?.addEventListener('change', saveSettings);
284284
}
285285

286286
// Settings toggle

0 commit comments

Comments
 (0)