Skip to content

Commit 55f09ef

Browse files
committed
fix: isInTryCatchBlock properly tracks catch scope with nestedDepth and handles same-line } catch { patterns
- Added nestedDepth counter to track blocks inside catch block - Added pendingExit flag for proper exit detection - Fixed same-line } catch { pattern by scanning ahead to find catch's closing brace - Changed exit condition from braceDepth < catchBlockDepth to <= to properly exit - All 9 test cases now pass including: empty catch, nested functions in catch, same-line catch, and post-catch console.error
1 parent 4cb1636 commit 55f09ef

File tree

2 files changed

+108
-10
lines changed

2 files changed

+108
-10
lines changed

ai-slop-detector.ts

Lines changed: 55 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -918,30 +918,80 @@ class AISlopDetector {
918918
let braceDepth = 0;
919919
let inCatchBlock = false;
920920
let catchBlockDepth = -1;
921+
let nestedDepth = 0;
922+
let pendingExit = false;
921923

922924
for (let i = 0; i <= lineIndex; i++) {
923925
const line = lines[i];
926+
const hasCatch = line.includes('catch (') || line.includes('catch(');
927+
const catchOnSameLineAsCloseBrace = hasCatch && line.trim().startsWith('}');
924928

925929
for (let j = 0; j < line.length; j++) {
926930
if (line[j] === '{') {
931+
if (inCatchBlock && !catchOnSameLineAsCloseBrace) {
932+
if (braceDepth >= catchBlockDepth) {
933+
nestedDepth++;
934+
}
935+
}
927936
braceDepth++;
928937
} else if (line[j] === '}') {
929938
braceDepth--;
930-
if (inCatchBlock && braceDepth < catchBlockDepth) {
931-
inCatchBlock = false;
939+
if (inCatchBlock) {
940+
if (nestedDepth > 0) {
941+
nestedDepth--;
942+
if (nestedDepth === 0) {
943+
pendingExit = true;
944+
}
945+
} else if (braceDepth <= catchBlockDepth) {
946+
inCatchBlock = false;
947+
nestedDepth = 0;
948+
pendingExit = false;
949+
}
932950
}
933951
}
934952
}
935953

936-
if (line.includes('catch (') || line.includes('catch(')) {
954+
if (pendingExit) {
955+
pendingExit = false;
956+
}
957+
958+
if (hasCatch) {
937959
if (line.includes('{')) {
938-
inCatchBlock = true;
939-
catchBlockDepth = braceDepth - 1;
960+
if (catchOnSameLineAsCloseBrace) {
961+
const closeBraceIdx = line.indexOf('}');
962+
const catchIdx = line.indexOf('catch');
963+
const openBraceIdx = line.indexOf('{', catchIdx);
964+
if (closeBraceIdx !== -1 && closeBraceIdx < catchIdx && openBraceIdx > catchIdx) {
965+
braceDepth--;
966+
}
967+
for (let j = 0; j < openBraceIdx; j++) {
968+
if (line[j] === '{') braceDepth++;
969+
}
970+
for (let j = openBraceIdx + 1; j < line.length; j++) {
971+
if (line[j] === '{') nestedDepth++;
972+
else if (line[j] === '}') {
973+
nestedDepth--;
974+
if (nestedDepth === 0) {
975+
pendingExit = true;
976+
}
977+
}
978+
}
979+
inCatchBlock = true;
980+
catchBlockDepth = braceDepth;
981+
nestedDepth = 0;
982+
} else {
983+
inCatchBlock = true;
984+
catchBlockDepth = braceDepth - 1;
985+
nestedDepth = 0;
986+
pendingExit = false;
987+
}
940988
} else {
941989
for (let j = i + 1; j < Math.min(i + 5, lines.length); j++) {
942990
if (lines[j].includes('{')) {
943991
inCatchBlock = true;
944992
catchBlockDepth = braceDepth;
993+
nestedDepth = 0;
994+
pendingExit = false;
945995
break;
946996
}
947997
}

karpeslop-bin.js

Lines changed: 53 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -777,27 +777,75 @@ class AISlopDetector {
777777
let braceDepth = 0;
778778
let inCatchBlock = false;
779779
let catchBlockDepth = -1;
780+
let nestedDepth = 0;
781+
let pendingExit = false;
780782
for (let i = 0; i <= lineIndex; i++) {
781783
const line = lines[i];
784+
const hasCatch = line.includes('catch (') || line.includes('catch(');
785+
const catchOnSameLineAsCloseBrace = hasCatch && line.trim().startsWith('}');
782786
for (let j = 0; j < line.length; j++) {
783787
if (line[j] === '{') {
788+
if (inCatchBlock && !catchOnSameLineAsCloseBrace) {
789+
if (braceDepth >= catchBlockDepth) {
790+
nestedDepth++;
791+
}
792+
}
784793
braceDepth++;
785794
} else if (line[j] === '}') {
786795
braceDepth--;
787-
if (inCatchBlock && braceDepth < catchBlockDepth) {
788-
inCatchBlock = false;
796+
if (inCatchBlock) {
797+
if (nestedDepth > 0) {
798+
nestedDepth--;
799+
if (nestedDepth === 0) {
800+
pendingExit = true;
801+
}
802+
} else if (braceDepth <= catchBlockDepth) {
803+
inCatchBlock = false;
804+
nestedDepth = 0;
805+
pendingExit = false;
806+
}
789807
}
790808
}
791809
}
792-
if (line.includes('catch (') || line.includes('catch(')) {
810+
if (pendingExit) {
811+
pendingExit = false;
812+
}
813+
if (hasCatch) {
793814
if (line.includes('{')) {
794-
inCatchBlock = true;
795-
catchBlockDepth = braceDepth - 1;
815+
if (catchOnSameLineAsCloseBrace) {
816+
const closeBraceIdx = line.indexOf('}');
817+
const catchIdx = line.indexOf('catch');
818+
const openBraceIdx = line.indexOf('{', catchIdx);
819+
if (closeBraceIdx !== -1 && closeBraceIdx < catchIdx && openBraceIdx > catchIdx) {
820+
braceDepth--;
821+
}
822+
for (let j = 0; j < openBraceIdx; j++) {
823+
if (line[j] === '{') braceDepth++;
824+
}
825+
for (let j = openBraceIdx + 1; j < line.length; j++) {
826+
if (line[j] === '{') nestedDepth++;else if (line[j] === '}') {
827+
nestedDepth--;
828+
if (nestedDepth === 0) {
829+
pendingExit = true;
830+
}
831+
}
832+
}
833+
inCatchBlock = true;
834+
catchBlockDepth = braceDepth;
835+
nestedDepth = 0;
836+
} else {
837+
inCatchBlock = true;
838+
catchBlockDepth = braceDepth - 1;
839+
nestedDepth = 0;
840+
pendingExit = false;
841+
}
796842
} else {
797843
for (let j = i + 1; j < Math.min(i + 5, lines.length); j++) {
798844
if (lines[j].includes('{')) {
799845
inCatchBlock = true;
800846
catchBlockDepth = braceDepth;
847+
nestedDepth = 0;
848+
pendingExit = false;
801849
break;
802850
}
803851
}

0 commit comments

Comments
 (0)