Skip to content

Your app looks AI-generated. Vincent Van Gogh noticed. An agent skill that finds and fixes AI-looking UI and UX in mobile apps, web apps, and websites.

fix(detect): flag Whoops in oops-error (#129) (#131)

Ahmad Ghozalic987d784e761
diff --git a/skills/unslop-anything/scripts/detect.js b/skills/unslop-anything/scripts/detect.js
index ba9e440..b355af3 100644
--- a/skills/unslop-anything/scripts/detect.js
+++ b/skills/unslop-anything/scripts/detect.js
@@ -337,6 +337,51 @@ function isTextBrush(code, index) {
return false;
}
+// True when the match at `index` sits inside a log call (console.*,
+// logger.*, Log.*, print, debugPrint, NSLog) or a `throw new ...(...)`
+// that is still open on the same line. Log calls and thrown errors never
+// reach the user, so their messages stay quiet — but a call that already
+// closed on the line does not hide visible copy after it:
+// `console.error(err); return <p>Oops!</p>;` still flags. Parens inside
+// string literals do not close the call, so a message such as
+// `console.error("a)b", "Oops")` stays quiet.
+function insideLogCall(code, index) {
+ const lineStart = code.lastIndexOf("\n", index) + 1;
+ const before = code.slice(lineStart, index);
+ const calls = /\b(?:console|logger|log|Log)\.\w+\s*\(|\b(?:print|debugPrint|NSLog)\s*\(|throw\s+new\s+\w+\s*\(/g;
+ let m;
+ while ((m = calls.exec(before))) {
+ const open = m.index + m[0].lastIndexOf("(");
+ if (callStillOpen(before, open)) return true;
+ }
+ return false;
+}
+
+// True when the `(` at `open` has no matching `)` before the end of `text`:
+// scan forward tracking paren depth, skipping string literals so a paren
+// inside a message neither closes nor opens the call.
+function callStillOpen(text, open) {
+ let depth = 0;
+ for (let i = open; i < text.length; i++) {
+ const c = text[i];
+ if (c === "\"" || c === "'" || c === "`") {
+ const quote = c;
+ i++;
+ while (i < text.length && text[i] !== quote) {
+ if (text[i] === "\\") i++;
+ i++;
+ }
+ continue;
+ }
+ if (c === "(") depth++;
+ else if (c === ")") {
+ depth--;
+ if (depth === 0) return false;
+ }
+ }
+ return depth > 0;
+}
+
// Index just past the bracket that closes the one at `open`.
function closeOf(text, open) {
const pairs = { "(": ")", "{": "}", "[": "]" };
@@ -1121,12 +1166,12 @@ const rules = [
fix: "Say what failed in the product's words and what the user can do: retry, check the connection, contact support.",
find({ code }) {
const hits = [];
- for (const m of matches(/\boops\b|something went wrong/i, code)) {
+ for (const m of matches(/\b(?:wh)?oops\b|something went wrong/i, code)) {
// Log calls and thrown errors never reach the user, so skip a match
- // whose line starts as one: console.*, logger.*, Log.*, print,
- // debugPrint, NSLog, or `throw new Error(...)`.
- const lineStart = code.lastIndexOf("\n", m.index) + 1;
- if (/\b(?:console|logger|log|Log)\.\w+\s*\(|\b(?:print|debugPrint|NSLog)\s*\(|throw\s+new\b/.test(code.slice(lineStart, m.index))) continue;
+ // inside one: console.*, logger.*, Log.*, print, debugPrint, NSLog,
+ // or `throw new Error(...)`. Only the call itself hides the match —
+ // visible copy after the call closed on the same line still flags.
+ if (insideLogCall(code, m.index)) continue;
hits.push(m.index);
}
return hits;
diff --git a/test/detect.test.js b/test/detect.test.js
index 0e293f7..5579581 100644
--- a/test/detect.test.js
+++ b/test/detect.test.js
@@ -131,6 +131,38 @@ test("oops-error ignores log calls and thrown errors", () => {
assert.deepStrictEqual(scanSource('const msg = "Oops, something went wrong";\n', "a.ts").map((f) => f.rule), ["oops-error"]);
});
+test("oops-error flags Whoops but not loops or hoops", () => {
+ // Issue #129: the issue's repro, both lines reported.
+ const src = 'export function A() {\n return <p>Oops! We could not load your orders.</p>;\n}\nexport function B() {\n return <p>Whoops! We could not load your orders.</p>;\n}\n';
+ assert.deepStrictEqual(
+ scanSource(src, "ErrorState.tsx").filter((f) => f.rule === "oops-error").map((f) => f.line),
+ [2, 5],
+ );
+ // Words that only contain the letters stay quiet.
+ assert.deepStrictEqual(scanSource('const a = "loops"; const b = "hoops";\n', "a.ts"), []);
+ assert.deepStrictEqual(scanSource('const a = "whoopsie";\n', "a.ts"), []);
+});
+
+test("oops-error flags visible copy after a log call on the same line", () => {
+ // CodeRabbit on #131: the old guard skipped every match after a log call
+ // earlier on the line, so visible copy after the call went unflagged.
+ assert.deepStrictEqual(
+ scanSource('console.error(err); return <p>Whoops!</p>;\n', "a.tsx").map((f) => f.line),
+ [1],
+ );
+ assert.deepStrictEqual(
+ scanSource('console.error("Something went wrong"); return <p>Oops!</p>;\n', "a.tsx").map((f) => f.line),
+ [1],
+ );
+ assert.deepStrictEqual(
+ scanSource('throw new Error("x"); return <p>Oops</p>;\n', "a.tsx").map((f) => f.line),
+ [1],
+ );
+ // A match still inside the call stays quiet, even with a paren in the message.
+ assert.deepStrictEqual(scanSource('console.error("a)b", "Oops");\n', "a.ts"), []);
+ assert.deepStrictEqual(scanSource('console.log("a"); console.error("Oops");\n', "a.ts"), []);
+});
+
test("ignore comments from before the rename still work", () => {
const src = '// unslopovich-ignore oops-error\nconst a = "Oops";\nconst b = "Oops"; // unslopovich-ignore\nconst c = "Oops";\n';
assert.deepStrictEqual(scanSource(src, "a.ts").map((f) => f.line), [4]);