Repository navigation
Add a Format JS button to the interview editor - #752
webdevbyjasmine wants to merge 1 commit into
Conversation
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
There was a problem hiding this comment.
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); |
There was a problem hiding this comment.
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>
| (result) => { | ||
| nodes.formatButton.classList.remove("busy"); | ||
| const now = editorSnapshot(nodes.editor); | ||
| if (!formatAvailable() || editorComposition || now.value !== before.value) |
There was a problem hiding this comment.
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>
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
Departures from the issue
aria-keyshortcutson the editor on the JavaScript tab.Ï. Python, C, C++ and Java are unchanged; the button is disabled there and the chord passes through.Both Practice and live interviews get it, as decided on the issue.
How it works
standalone.mjswith the estree and acorn plugins, vendored throughFETCHand pinned inSHA256SUMSlike pdf.js, MIT, listed inTHIRD-PARTY-NOTICES.mdwith 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. Noevalornew 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-levelreturn, which stay as unrunnable after formatting as before. Options are Prettier's defaults except the indentation, which follows the lobby's choice.applyEditorEdit, so one undo takes it back and highlighting, line numbers,code_updateand the test runner see it as they see typing.formatWithCursormaps the caret; a selection collapses to where its start went.Tradeoffs to check
Validation
./scripts/test.shin a clean worktree at this head: every lane passed exceptreview-memory-tests(localghlacks attestation flags) andgit-hooks(commentflow,shfmtandruffnot installed), both environment, outside this diff. Skipped: Java runs (nojavac), 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 andcode_updatereceiving the formatted buffer, and the pressed look. Each guard in the result path was removed in turn and its test failed.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.
Written for commit 36efd83. Summary will update on new commits.