Skip to content

Add a Format JS button to the interview editor - #752

Open
webdevbyjasmine wants to merge 1 commit into
sysprog21:mainfrom
webdevbyjasmine:feat-js-format
Open

webdevbyjasmine wants to merge 1 commit into
sysprog21:mainfrom
webdevbyjasmine:feat-js-format

Conversation

@webdevbyjasmine

@webdevbyjasmine webdevbyjasmine commented Nov 17, 2026 •

Copy link
Copy Markdown
Contributor

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

Before After
(screenshot: one-line function) (screenshot: formatted, "Formatted the code." above the code)
Code that does not parse Another 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.

Written for commit 36efd83. Summary will update on new commits.

View guided diff

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. Format JS, and Shift+Alt+F in the editor, reformat the
whole JavaScript buffer with Prettier 3.10.1, vendored and pinned like
pdf.js and imported on the first press, so the code never leaves the
browser and a candidate who never formats downloads nothing. The issue
asked for a shortcut alone, off by default; a chord can be taken by the
system or an input method before the page sees it, so the button leads,
and since each press is one explicit format, never on save or before a
run, there is no setting to opt into. Nothing changes until a candidate
presses it, and Python, C, C++ and Java are untouched: the button is
disabled there and the chord passes through, though on a Mac's
JavaScript tab Option+Shift+F now formats instead of typing a letter. A
format is one edit in the page history and takes the same input path as
typing, so one undo takes it back and highlighting, line numbers,
code_update and the test runner all see it; code that does not parse is
left as it was, with the reason and position shown above the editor.

Close sysprog21#485

Change-Id: I220d38de889beb6d8a6bcb0c06018e6627522248
Copilot AI balanced review requested due to automatic review settings November 17, 2026 06:31

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot was unable to review this pull request because the user who requested the review has insufficient remaining quota. Try requesting a review at a lower effort level.

@cubic-dev-ai cubic-dev-ai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

2 issues found across 16 files

Prompt for AI agents (unresolved issues)

Check if these issues are valid — if so, understand the root cause of each and fix them. When an issue isn't valid or won't be fixed in this PR, reply in its thread with the reason and then resolve the thread. If appropriate, use sub-agents to investigate and fix each issue separately.


<file name="tests/browser/format-shortcut.test.js">

<violation number="1" location="tests/browser/format-shortcut.test.js:414">
P2: This synchronization can let the test assert before the second formatter request finishes. In the two-press scenario, wait for the second success status (or track each request) instead of relying on the shared busy class, which is cleared by either response.</violation>
</file>

<file name="web/interview.js">

<violation number="1" location="web/interview.js:1266">
P2: `formatCode` validates only the current value, so switching JavaScript → Python → JavaScript while Prettier is pending can apply a stale result to the returned tab. Capture an editor-context generation when formatting starts and reject results from an older generation.</violation>
</file>

Reply to a comment to ask cubic a question or push back. It learns from your replies.

View guided diff | Re-trigger cubic

await requested;
await page.keyboard.type("//x");
hold.release();
await formatSettled(page);

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2: This synchronization can let the test assert before the second formatter request finishes. In the two-press scenario, wait for the second success status (or track each request) instead of relying on the shared busy class, which is cleared by either response.

Prompt for AI agents
Check if this issue is valid — if so, understand the root cause and fix it. When an issue isn't valid or won't be fixed in this PR, reply in its thread with the reason and then resolve the thread. At tests/browser/format-shortcut.test.js, line 414:

<comment>This synchronization can let the test assert before the second formatter request finishes. In the two-press scenario, wait for the second success status (or track each request) instead of relying on the shared busy class, which is cleared by either response.</comment>

<file context>
@@ -0,0 +1,769 @@
+    await requested;
+    await page.keyboard.type("//x");
+    hold.release();
+    await formatSettled(page);
+    assert.equal(await buffer(page), `${MESSY}//x`);
+    assert.equal(await status(page).textContent(), "");
</file context>

Comment thread web/interview.js
(result) => {
nodes.formatButton.classList.remove("busy");
const now = editorSnapshot(nodes.editor);
if (!formatAvailable() || editorComposition || now.value !== before.value)

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2: formatCode validates only the current value, so switching JavaScript → Python → JavaScript while Prettier is pending can apply a stale result to the returned tab. Capture an editor-context generation when formatting starts and reject results from an older generation.

Prompt for AI agents
Check if this issue is valid — if so, understand the root cause and fix it. When an issue isn't valid or won't be fixed in this PR, reply in its thread with the reason and then resolve the thread. At web/interview.js, line 1266:

<comment>`formatCode` validates only the current value, so switching JavaScript → Python → JavaScript while Prettier is pending can apply a stale result to the returned tab. Capture an editor-context generation when formatting starts and reject results from an older generation.</comment>

<file context>
@@ -1214,6 +1219,87 @@ function restoreStarter() {
+    (result) => {
+      nodes.formatButton.classList.remove("busy");
+      const now = editorSnapshot(nodes.editor);
+      if (!formatAvailable() || editorComposition || now.value !== before.value)
+        return;
+      if (now.start !== before.start || now.end !== before.end) {
</file context>

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Add an opt-in shortcut to format JavaScript code in the editor

2 participants