Files
oh-my-pi/packages/coding-agent/src/discovery/builtin-rules/ts-redundant-clear-guard.md
T
can1357 a84da2a61e feat(coding-agent): added AST-condition matching for interrupt flow rule handling
- Added astCondition to rule frontmatter parsing and rule metadata, with AST-grep normalization.
- Updated TTSR bucketing so astCondition-only rules are treated as interruptible matches.
- Added ts-redundant-clear-guard as a built-in JS/TS tool rule for guarded clear* calls.
- Added AST snapshot matching in agent sessions with per-stream cache throttling and cleanup.
2026-06-08 14:57:17 +02:00

2.8 KiB

description, scope, interruptMode, astCondition
description scope interruptMode astCondition
Do not guard clearTimeout/clearInterval/clearImmediate with a truthiness or null/undefined check — they accept null and undefined tool:edit(*.{ts,tsx,js,jsx,mts,cts,mjs,cjs}), tool:write(*.{ts,tsx,js,jsx,mts,cts,mjs,cjs}) never
if ($X) clearTimeout($X)
if ($X) { clearTimeout($X) }
if ($X) clearInterval($X)
if ($X) { clearInterval($X) }
if ($X) clearImmediate($X)
if ($X) { clearImmediate($X) }
if ($X !== null) clearTimeout($X)
if ($X !== null) { clearTimeout($X) }
if ($X !== null) clearInterval($X)
if ($X !== null) { clearInterval($X) }
if ($X !== null) clearImmediate($X)
if ($X !== null) { clearImmediate($X) }
if ($X != null) clearTimeout($X)
if ($X != null) { clearTimeout($X) }
if ($X != null) clearInterval($X)
if ($X != null) { clearInterval($X) }
if ($X != null) clearImmediate($X)
if ($X != null) { clearImmediate($X) }
if ($X !== undefined) clearTimeout($X)
if ($X !== undefined) { clearTimeout($X) }
if ($X !== undefined) clearInterval($X)
if ($X !== undefined) { clearInterval($X) }
if ($X !== undefined) clearImmediate($X)
if ($X !== undefined) { clearImmediate($X) }
if ($X != undefined) clearTimeout($X)
if ($X != undefined) { clearTimeout($X) }
if ($X != undefined) clearInterval($X)
if ($X != undefined) { clearInterval($X) }
if ($X != undefined) clearImmediate($X)
if ($X != undefined) { clearImmediate($X) }

Do not guard clearTimeout / clearInterval / clearImmediate with a truthiness or null/undefined check. Per the WHATWG/Node timers spec these functions are no-ops when handed null, undefined, or any value that doesn't correspond to a live timer. The guard adds a redundant branch that the reader must still reason about.

Why it's wrong

  • The branch can never change behavior — clearing a missing/null/undefined handle does nothing.
  • Extra branches inflate the code and hide the one line that matters.
  • It signals a misunderstanding of the timer API to future readers.

Avoid

if (this.timer) clearTimeout(this.timer);
if (handle !== null) clearInterval(handle);
if (id != undefined) {
	clearImmediate(id);
}

Use

clearTimeout(this.timer);
clearInterval(handle);
clearImmediate(id);

When a guard is warranted

Keep the check only when the body does more than clear — e.g. it also reassigns the handle or runs other cleanup:

if (this.timer) {
	clearTimeout(this.timer);
	this.timer = undefined; // extra work → guard is not purely redundant
}

This rule only fires when the clear call is the sole statement in the guarded branch, so those legitimate cases are left alone.