|
| 1 | +import { describe, it, expect } from "vitest"; |
| 2 | +import { readFileSync, readdirSync, statSync } from "fs"; |
| 3 | +import { resolve, join } from "path"; |
| 4 | + |
| 5 | +/** |
| 6 | + * Regression test for Sentry MEMO-5: "Cannot use method in read-only mode." |
| 7 | + * |
| 8 | + * Lexical's dispatchCommand() triggers command listeners synchronously. If a |
| 9 | + * listener mutates the editor state (e.g. $toggleLink calling splitText) and |
| 10 | + * dispatchCommand was called outside editor.update(), the mutation can execute |
| 11 | + * in a read-only context — especially when an editorState.read() is active on |
| 12 | + * the call stack. |
| 13 | + * |
| 14 | + * This test scans editor source files for bare editor.dispatchCommand() calls |
| 15 | + * that are NOT wrapped in editor.update(). Commands that mutate state (like |
| 16 | + * TOGGLE_LINK_COMMAND) must be dispatched inside editor.update(). |
| 17 | + */ |
| 18 | + |
| 19 | +/** Recursively collect .ts/.tsx files under a directory */ |
| 20 | +function collectSourceFiles(dir: string): string[] { |
| 21 | + const files: string[] = []; |
| 22 | + for (const entry of readdirSync(dir)) { |
| 23 | + const full = join(dir, entry); |
| 24 | + if (statSync(full).isDirectory()) { |
| 25 | + files.push(...collectSourceFiles(full)); |
| 26 | + } else if (/\.tsx?$/.test(entry) && !entry.includes(".test.")) { |
| 27 | + files.push(full); |
| 28 | + } |
| 29 | + } |
| 30 | + return files; |
| 31 | +} |
| 32 | + |
| 33 | +/** |
| 34 | + * Commands known to mutate editor state when handled by their default |
| 35 | + * @lexical/react plugin listeners. |
| 36 | + */ |
| 37 | +const MUTATING_COMMANDS = [ |
| 38 | + "TOGGLE_LINK_COMMAND", |
| 39 | + "INSERT_PARAGRAPH_COMMAND", |
| 40 | + "INSERT_LINE_BREAK_COMMAND", |
| 41 | + "DELETE_CHARACTER_COMMAND", |
| 42 | + "DELETE_WORD_COMMAND", |
| 43 | + "DELETE_LINE_COMMAND", |
| 44 | + "FORMAT_TEXT_COMMAND", |
| 45 | + "FORMAT_ELEMENT_COMMAND", |
| 46 | + "INSERT_TAB_COMMAND", |
| 47 | + "INDENT_CONTENT_COMMAND", |
| 48 | + "OUTDENT_CONTENT_COMMAND", |
| 49 | + "REMOVE_TEXT_COMMAND", |
| 50 | + "PASTE_COMMAND", |
| 51 | + "CUT_COMMAND", |
| 52 | +]; |
| 53 | + |
| 54 | +describe("Lexical dispatchCommand safety — mutating commands inside editor.update()", () => { |
| 55 | + const editorDir = resolve(__dirname); |
| 56 | + const sourceFiles = collectSourceFiles(editorDir); |
| 57 | + |
| 58 | + it("mutating commands are not dispatched outside editor.update()", () => { |
| 59 | + const violations: string[] = []; |
| 60 | + const commandPattern = new RegExp( |
| 61 | + `editor\\.dispatchCommand\\(\\s*(${MUTATING_COMMANDS.join("|")})`, |
| 62 | + "g" |
| 63 | + ); |
| 64 | + |
| 65 | + for (const filePath of sourceFiles) { |
| 66 | + const content = readFileSync(filePath, "utf-8"); |
| 67 | + const lines = content.split("\n"); |
| 68 | + |
| 69 | + for (let i = 0; i < lines.length; i++) { |
| 70 | + const match = lines[i].match(commandPattern); |
| 71 | + if (!match) continue; |
| 72 | + |
| 73 | + // Check if editor.update( appears on the same line before dispatchCommand |
| 74 | + const colIdx = lines[i].indexOf("editor.dispatchCommand"); |
| 75 | + const linePrefix = lines[i].substring(0, colIdx); |
| 76 | + if (/editor\.update\s*\(/.test(linePrefix)) continue; |
| 77 | + |
| 78 | + // Walk backwards through preceding lines to find an enclosing |
| 79 | + // editor.update() block. Track brace/paren depth to stay within |
| 80 | + // the same scope. |
| 81 | + let insideUpdate = false; |
| 82 | + let braceDepth = 0; |
| 83 | + |
| 84 | + for (let j = i - 1; j >= 0 && j >= i - 30; j--) { |
| 85 | + const line = lines[j]; |
| 86 | + |
| 87 | + for (let c = line.length - 1; c >= 0; c--) { |
| 88 | + if (line[c] === "}" || line[c] === ")") braceDepth++; |
| 89 | + if (line[c] === "{" || line[c] === "(") braceDepth--; |
| 90 | + } |
| 91 | + |
| 92 | + if (braceDepth < 0 && /editor\.update\s*\(/.test(line)) { |
| 93 | + insideUpdate = true; |
| 94 | + break; |
| 95 | + } |
| 96 | + // If we've exited the enclosing scope, stop |
| 97 | + if (braceDepth > 2) break; |
| 98 | + } |
| 99 | + |
| 100 | + if (!insideUpdate) { |
| 101 | + const relative = filePath.replace( |
| 102 | + resolve(__dirname, "../..") + "/", |
| 103 | + "" |
| 104 | + ); |
| 105 | + violations.push( |
| 106 | + `${relative}:${i + 1}: ${match[0]} — must be inside editor.update()` |
| 107 | + ); |
| 108 | + } |
| 109 | + } |
| 110 | + } |
| 111 | + |
| 112 | + expect( |
| 113 | + violations, |
| 114 | + "Mutating Lexical commands must be dispatched inside editor.update() to " + |
| 115 | + "avoid read-only mode errors. See Sentry MEMO-5.\n\nViolations:\n" + |
| 116 | + violations.join("\n") |
| 117 | + ).toHaveLength(0); |
| 118 | + }); |
| 119 | +}); |
0 commit comments