fix(main): hoist let bindings to top of start() to fix TDZ on anchorError

The applyMode() function reads/writes anchorError, lastSnapshot, and other
let-bound state. Function declarations are hoisted, so applyMode() can fire
from inside the dialog.mount() / applyMode() call sequence at the top of
start() — but the let bindings themselves are not initialized until
execution reaches their declaration line, which came later.

When prefs.mode === 'anchored' and findAnchorElement() returns null, the
new 'anchor missed' branch writes to anchorError and calls render(). Both
access anchorError before its let binding is initialized, throwing
ReferenceError: Cannot access 'anchorError' before initialization.

Move all four let declarations (lastSnapshot, lastAttr, lastDelta,
anchorError) to the top of start(), before dialog.mount() and applyMode().
Function declarations are unaffected — they are hoisted regardless.
This commit is contained in:
Claude
2026-06-01 22:22:29 -05:00
parent 578736a492
commit 501c6746eb
+20 -5
View File
@@ -35,12 +35,29 @@ function findAnchorElement() {
}
function start() {
try {
const store = new Store({
storage: localStorage,
onWarn: (m) => console.warn(m),
});
const prefs = store.getPrefs();
// State that applyMode() and render() may touch on first call.
// Declared up-front to avoid TDZ ReferenceError if applyMode()'s
// anchor-miss branch fires before the natural declaration point.
let lastSnapshot = null;
let lastAttr = null;
let lastDelta = 0;
let anchorError = null;
// One-time migration: dialog now defaults to bottom-left, so reset any
// previously-saved position from the bottom-right era.
if (prefs.pos && (prefs.pos.x !== 0 || prefs.pos.y !== 0)) {
console.info('[tat] resetting dialog position to new bottom-left default');
prefs.pos = { x: 0, y: 0 };
store.setPos(prefs.pos);
}
const dialog = new Dialog({
onTargetChange: (v) => {
const attr = currentAttribute()?.attr;
@@ -60,11 +77,6 @@ function start() {
dialog.mount({ initialMode: prefs.mode, initialPos: prefs.pos });
applyMode();
let lastSnapshot = null;
let lastAttr = null;
let lastDelta = 0;
let anchorError = null;
function snapshot() {
const a = currentAttribute();
if (!a) {
@@ -142,6 +154,9 @@ function start() {
},
onParseFail: (url) => console.warn('[tat] could not parse train response from', url),
});
} catch (e) {
console.error('[tat] failed to start:', e);
}
}
if (location.hash === '#tat-test') {