Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 8 additions & 3 deletions internal/codeguard/checks/quality/quality_defensive.go
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@
strings.HasSuffix(name, "row") ||
strings.Contains(name, "rowpart") ||
strings.HasSuffix(name, "part") ||
regexp.MustCompile(`part\d+$`).MatchString(name) ||

Check warning on line 171 in internal/codeguard/checks/quality/quality_defensive.go

View workflow job for this annotation

GitHub Actions / codeguard

[performance.regex-compile-in-loop] regular expression compiled inside a loop; compile it once before the loop or as a package-level variable. Fix: Compile the pattern once before the loop or at package/module level and reuse it.
strings.HasSuffix(name, "context") ||
strings.HasSuffix(name, "ctx") ||
strings.Contains(name, "dto") ||
Expand Down Expand Up @@ -318,8 +318,6 @@
name + " != null",
name + " == nullptr",
name + " != nullptr",
"typeof " + name + " === ",
"typeof " + name + " == ",
}
if containsAny(loweredBody, guards) {
return true
Expand Down Expand Up @@ -356,7 +354,14 @@
}

func nullableStatementUsesOnlyNullSafeOperators(statement string, name string) bool {
return containsAny(statement, []string{name + "?.", name + "?.[", name + " ??"})
if containsAny(statement, []string{name + "?.", name + "?.[", name + " ??"}) {
return true
}
// A typeof check only narrows the value on the branch controlled by that
// check. Do not treat an arbitrary typeof occurrence elsewhere in the
// function as a null guard.
quotedName := regexp.QuoteMeta(name)
return regexp.MustCompile(`typeof\s+` + quotedName + `\s*={2,3}\s*['"](?:string|number|boolean|bigint|symbol|function)['"]\s*\?[^:]*\b` + quotedName + `(?:\.|\[)`).MatchString(statement)
}

func firstUseLine(fn precisionFunction, name string) int {
Expand Down
20 changes: 20 additions & 0 deletions tests/checks/quality_precision_followup_retune_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -406,6 +406,26 @@ func TestDefensiveNullAssumptionCreditsTypeScriptNarrowing(t *testing.T) {
assertCodeQualityRuleAbsentForPath(t, report, "defensive.null-assumption", "null-narrowing.ts:16")
}

func TestDefensiveNullAssumptionRejectsUnsafeTypeofGuards(t *testing.T) {
dir := t.TempDir()
writeFile(t, filepath.Join(dir, "packages/api/src/lib/unsafe-typeof.ts"), strings.Join([]string{
"export function objectCheck(user: User | null) {",
" if (typeof user === 'object') return user.email;",
"}",
"export function lateCheck(user: User | null) {",
" const email = user.email;",
" if (typeof user === 'string') return '';",
" return email;",
"}",
"interface User { email: string }",
}, "\n"))

report := runQualityPrecisionScan(t, qualityPrecisionConfigForLanguage(dir, "typescript"))

assertCodeQualityRulePresentForPathWithMessage(t, report, "defensive.null-assumption", "unsafe-typeof.ts:2")
assertCodeQualityRulePresentForPathWithMessage(t, report, "defensive.null-assumption", "unsafe-typeof.ts:5")
}

func TestDefensiveBoundaryInputSkipsTypedInternalDTOs(t *testing.T) {
dir := t.TempDir()
writeFile(t, filepath.Join(dir, "packages/api/src/routers/internal-dtos.ts"), strings.Join([]string{
Expand Down
Loading