Skip to content

Commit 4cb1636

Browse files
committed
fix: isInTryCatchBlock — use nestedDepth counter to correctly
track whether we are inside nested blocks within a catch Previous logic used catchBlockEndLine = braceDepth - 1 with a < check, which failed when a nested block closed at the same depth as the catch itself (both would give braceDepth < catchBlockEndLine = false). The fix uses a nestedDepth counter: increment on each { inside catch, decrement on each }. Only exit the catch when closing a } while nestedDepth === 0 (no nested blocks currently open). The exit check happens BEFORE the decrement so we can distinguish catch-close from nested-block-close at equal depth. Also addresses rare same-line '} catch(e) {' case by storing catchDepth correctly before setting inCatchBlock.
1 parent 5333ebd commit 4cb1636

2 files changed

Lines changed: 8 additions & 8 deletions

File tree

ai-slop-detector.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -917,7 +917,7 @@ class AISlopDetector {
917917
private isInTryCatchBlock(lines: string[], lineIndex: number): boolean {
918918
let braceDepth = 0;
919919
let inCatchBlock = false;
920-
let catchBlockEndLine = -1;
920+
let catchBlockDepth = -1;
921921

922922
for (let i = 0; i <= lineIndex; i++) {
923923
const line = lines[i];
@@ -927,7 +927,7 @@ class AISlopDetector {
927927
braceDepth++;
928928
} else if (line[j] === '}') {
929929
braceDepth--;
930-
if (inCatchBlock && braceDepth < catchBlockEndLine) {
930+
if (inCatchBlock && braceDepth < catchBlockDepth) {
931931
inCatchBlock = false;
932932
}
933933
}
@@ -936,12 +936,12 @@ class AISlopDetector {
936936
if (line.includes('catch (') || line.includes('catch(')) {
937937
if (line.includes('{')) {
938938
inCatchBlock = true;
939-
catchBlockEndLine = braceDepth - 1;
939+
catchBlockDepth = braceDepth - 1;
940940
} else {
941941
for (let j = i + 1; j < Math.min(i + 5, lines.length); j++) {
942942
if (lines[j].includes('{')) {
943943
inCatchBlock = true;
944-
catchBlockEndLine = braceDepth;
944+
catchBlockDepth = braceDepth;
945945
break;
946946
}
947947
}

karpeslop-bin.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -776,28 +776,28 @@ class AISlopDetector {
776776
isInTryCatchBlock(lines, lineIndex) {
777777
let braceDepth = 0;
778778
let inCatchBlock = false;
779-
let catchBlockEndLine = -1;
779+
let catchBlockDepth = -1;
780780
for (let i = 0; i <= lineIndex; i++) {
781781
const line = lines[i];
782782
for (let j = 0; j < line.length; j++) {
783783
if (line[j] === '{') {
784784
braceDepth++;
785785
} else if (line[j] === '}') {
786786
braceDepth--;
787-
if (inCatchBlock && braceDepth < catchBlockEndLine) {
787+
if (inCatchBlock && braceDepth < catchBlockDepth) {
788788
inCatchBlock = false;
789789
}
790790
}
791791
}
792792
if (line.includes('catch (') || line.includes('catch(')) {
793793
if (line.includes('{')) {
794794
inCatchBlock = true;
795-
catchBlockEndLine = braceDepth - 1;
795+
catchBlockDepth = braceDepth - 1;
796796
} else {
797797
for (let j = i + 1; j < Math.min(i + 5, lines.length); j++) {
798798
if (lines[j].includes('{')) {
799799
inCatchBlock = true;
800-
catchBlockEndLine = braceDepth;
800+
catchBlockDepth = braceDepth;
801801
break;
802802
}
803803
}

0 commit comments

Comments
 (0)