Add a Format JS button to the interview editor

WE
19m ago
+1327-7

Candidates who move code around while thinking out loud end up with indentation that no longer shows the structure, and the editor had no way to tidy it. This adds a Format JS button beside Restore, and Shift+Alt+F in the editor, that reformat the whole JavaScript buffer with Prettier 3.10.1.

Before and after

BeforeAfter
(screenshot: one-line function)(screenshot: formatted, "Formatted the code." above the code)
Code that does not parseAnother language
(screenshot: "Formatting failed: Assigning to rvalue at line 3, column 3. The code was not changed.")(screenshot: Format JS disabled on the Python tab)

Departures from the issue

  • A toolbar button. The issue proposed a shortcut with no button. A chord can be taken by the operating system (Alt+Shift switches the input language on Windows), an input method or another application before the page sees it, and the page cannot tell. The button is the primary control; Shift+Alt+F stays as a secondary path, named with aria-keyshortcuts on the editor on the JavaScript tab.
  • No default-off setting. Each press is one explicit format, never on save or before a run, so there is no mode to opt into. Nothing changes until a candidate presses it. One consequence: on a Mac's JavaScript tab, Option+Shift+F formats instead of typing Ï. Python, C, C++ and Java are unchanged; the button is disabled there and the chord passes through.
  • A status line under the toolbar. It reports each result and stays while the candidate types, so the editor does not jump under the caret; the next format, a tab switch, Restore or the end of coding clears it.

Both Practice and live interviews get it, as decided on the issue.

How it works

  • Formatter. Prettier 3.10.1 standalone.mjs with the estree and acorn plugins, vendored through FETCH and pinned in SHA256SUMS like pdf.js, MIT, listed in THIRD-PARTY-NOTICES.md with Prettier's own notices fetched beside it. 460,497 bytes (about 136 KB gzipped), imported on the first press, so a candidate who never formats downloads nothing and the code never leaves the browser. No eval or new Function, so the Content-Security-Policy is unchanged. Acorn accepts standard ECMAScript only and does not repair incomplete code; the known exceptions are JSX and a top-level return, which stay as unrunnable after formatting as before. Options are Prettier's defaults except the indentation, which follows the lobby's choice.
  • One edit. A result goes through the existing applyEditorEdit, so one undo takes it back and highlighting, line numbers, code_update and the test runner see it as they see typing.
  • Stale results. Formatting is asynchronous. A result lands only over the buffer it was made from: an edit, a tab switch, an IME composition or the end of coding in the meantime drops it, and a caret moved without editing formats again from the new caret. A second press needs no queue for the same reason. A failure leaves the buffer, the selection and the history unchanged and names the reason with its line and column.
  • Caret. formatWithCursor maps the caret; a selection collapses to where its start went.

Tradeoffs to check

  • One more toolbar button moves the one-row wrap point: around 768px and 1152px viewports, Run tests drops to a second row (measured; 1280px and phone widths keep their layout). Any new toolbar button does this.
  • Prettier runs on the main thread; a 37,779-character buffer formatted in 245 ms locally.
  • A load that never settles leaves the button in its working look until reload; there is no load timeout.

Validation

  • ./scripts/test.sh in a clean worktree at this head: every lane passed except review-memory-tests (local gh lacks attestation flags) and git-hooks (commentflow, shfmt and ruff not installed), both environment, outside this diff. Skipped: Java runs (no javac), one ffmpeg test, cargo-audit, cargo-deny, actionlint, shellcheck.
  • tests/browser/editor-format.test.js, 33 Node tests: before/after fixtures and their idempotence (comments, template literals, a directive, a long line, the ASI case), empty buffers, runtime equivalence of six programs before and after, incomplete code refused with its position, the known exceptions, caret mapping, the three indentation choices, a failed load and its retry, and the keystroke predicate.
  • tests/browser/format-shortcut.test.js, 18 Chromium tests on the interview page: the button per tab, three rounds of undo/redo with overlay and gutter checked, a parse error, a blocked download, an edit, a tab switch, a caret move, a locked editor and a composition while loading, a held chord, the macOS spelling, untouched Ctrl+/ and typing, Run tests and code_update receiving the formatted buffer, and the pressed look. Each guard in the result path was removed in turn and its test failed.
  • Not checked: Firefox and Safari, shortcut conflicts on macOS and Windows, screen readers, forced colors.

Close #485


Summary by cubic

Adds a Format JS button and Shift+Alt+F shortcut to the interview editor that reformats the whole JavaScript buffer with Prettier 3.10.1, imported on first use so candidates who never format download nothing. The result lands as one undoable edit through the existing edit path, so highlighting, line numbers, Run tests and interviewer updates see it as they see typing. Code that doesn't parse is left as-is, with the error and position shown above the editor.

  • Python, C, C++ and Java are unaffected: the button is disabled and the chord passes through; on a Mac's JavaScript tab, Option+Shift+F now formats instead of typing a character.
  • Prettier is vendored and pinned by checksum like pdf.js and runs entirely in the browser.