Skip to content

[eslint-miner] Add require-null-check-with-typeof-object rule - #76738

Merged
pelikhan merged 4 commits into
mainfrom
eslint-miner/require-null-check-with-typeof-object-5d66d6ad90c21897
Nov 11, 2026
Merged

pelikhan merged 4 commits into
mainfrom
eslint-miner/require-null-check-with-typeof-object-5d66d6ad90c21897

Conversation

@github-actions

Copy link
Copy Markdown
Contributor

New rule: gh-aw-custom/require-null-check-with-typeof-object

Flags typeof x === "object" (also !==, ==, !=) when x is not also checked for null/undefined/truthiness in the same logical expression or earlier in the same function. typeof null === "object", so a null value passes and later property access throws.

Steering issue: #76732 (no steer comments found).

Research evidence

Window: 2026-10-28 to 2026-11-11 (14 days), repo github/gh-aw.

Rationale

The scan found 15 unguarded typeof ... === "object" sites in non-test .cjs files (e.g. pick_experiment.cjs:196/215 and load_experiment_state_from_repo.cjs:36/48 where parsed.counts / entry.counts may be null in a persisted state file; finish_work_queue_claim.cjs:530 Object.values(result.claims) on null; report_markdown.cjs:200/320/473; add_reaction_and_edit_comment.cjs:185/198). Not stylistic: null input there yields a TypeError or silently accepts a corrupt snapshot. No existing rule covers it.

Low false-positive design: only identifier/non-computed member-chain subjects; any null/undefined comparison, truthiness test, Boolean(x), !x, or logical-chain use of the same subject (before or after) suppresses the report, including an earlier guard in the enclosing function. Registered as warn, so existing code is not made to fail.

Changes

  • eslint-factory/src/rules/require-null-check-with-typeof-object.ts (+ test)
  • registered in src/index.ts, enabled in eslint.config.cjs, documented in README.md

Validation (from eslint-factory)

  • npm ci: exit 0
  • npm run build: exit 0
  • npm test -- src/rules/require-null-check-with-typeof-object.test.ts: 3 passed
  • npm test (full): 78 files / 726 tests passed
  • npm run lint:setup-js: exit 0, 1088 warnings and 0 errors in total; 15 are from the new rule. The rest are pre-existing warnings from other rules.

Generated by ESLint Miner · copilot · routed: sonnet55 high · ~105 AIC · ⌖ ~2 AIC · ⊞ 4.6K · ◷
Claim finished

  • expires on Nov 17, 2026, 7:38 PM UTC-08:00

github-actions Bot and others added 2 commits November 11, 2026 03:38
Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
Co-authored-by: Peli de Halleux <pelikhan@users.noreply.github.com>
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Changes recommended

The rule incorrectly accepts guards in nested functions and strict undefined comparisons as null protection, suppressing valid warnings for code that can throw on null.

2 open issues
What changed in this PR

Adds a custom ESLint rule to detect object-type checks that lack null guards, helping prevent null-related failures in actions/setup/js.

Changes:

  • Implements detection for typeof object comparisons and nearby null or truthiness checks.
  • Registers the rule and enables it at warning level.
  • Adds rule tests and documentation with usage examples.
File Description
eslint-factory/​src/​rules/​require-null-check-with-typeof-object.ts Implements comparison detection and guard matching.
eslint-factory/​src/​rules/​require-null-check-with-typeof-object.test.ts Tests flagged comparisons and accepted guards.
eslint-factory/​src/​index.ts Registers the rule in the plugin.
eslint-factory/​README.md Documents the rule and examples.
eslint-factory/​eslint.config.cjs Enables the rule as a warning.

Review effort: Balanced (auto)

Comment on lines +36 to +39
if (node.type === AST_NODE_TYPES.BinaryExpression && EQUALITY_OPERATORS.has(node.operator)) {
const other = isNullish(node.right) ? node.left : isNullish(node.left) ? node.right : null;
return other !== null && subjectText(other) === text;
}

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed in 8344ab8: strict undefined comparisons no longer count as null guards; only loose ==/!= against undefined do. Added regression coverage and clarified the README.

const stack: TSESTree.Node[] = [root];
while (stack.length > 0) {
const node = stack.pop()!;
if (node === skip) continue;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed in 8344ab8: earlier guards are limited to top-level early-exit checks in the nearest function, so nested-function guards cannot suppress the outer report. Added the regression case.

@pelikhan
pelikhan marked this pull request as ready for review November 11, 2026 08:29
@github-actions

github-actions Bot commented Nov 11, 2026 •

Copy link
Copy Markdown
Contributor Author

✅ Test Quality Sentinel completed test quality analysis.

Test Quality Sentinel

Outcome: reported_no_action

The agent completed and reported no action. A no-op output alone does not verify that all required work was completed or that publication was possible.

Agent messages are omitted from this comment. The category uses structured run status, not interpretation of agent prose.

Generated from Test Quality Sentinel · routed: haiku55 xhigh · ~2 AIC · ⌖ ~2 AIC · ◇ ~1 AIC · ⊞ 7.2K · ◷

🧪 Test quality analysis by Test Quality Sentinel

@github-actions

github-actions Bot commented Nov 11, 2026 •

Copy link
Copy Markdown
Contributor Author

✅ Design Decision Gate 🏗️ completed the design decision gate check. See the comment below for the result and any generated ADR draft.

Design Decision Gate 🏗️

Outcome: reported_no_action

The agent completed and reported no action. A no-op output alone does not verify that all required work was completed or that publication was possible.

Agent messages are omitted from this comment. The category uses structured run status, not interpretation of agent prose.

Generated from Design Decision Gate 🏗️ · routed: haiku55 xhigh · ~1 AIC · ⌖ ~2 AIC · ◇ ~1 AIC · ⊞ 8.5K · ◷

🏗️ ADR gate enforced by Design Decision Gate 🏗️

@github-actions

github-actions Bot commented Nov 11, 2026 •

Copy link
Copy Markdown
Contributor Author

✅ PR Code Quality Reviewer completed the code quality review.

PR Code Quality Reviewer

Outcome: reported_action

The agent completed and reported one or more actions. The no-op output is a completion signal, not a claim that no action was taken.

Agent messages are omitted from this comment. The category uses structured run status, not interpretation of agent prose.

Generated from PR Code Quality Reviewer · gpt56 · ~3 AIC · ⌖ ~1 AIC · ◇ ~1 AIC · ⊞ 1.6K · ◷

🔎 Code quality review by PR Code Quality Reviewer

@github-actions

github-actions Bot commented Nov 11, 2026 •

Copy link
Copy Markdown
Contributor Author

🧠 Matt Pocock Skills Reviewer has completed the skills-based review. ✅

Matt Pocock Skills Reviewer

Outcome: reported_action

The agent completed and reported one or more actions. The no-op output is a completion signal, not a claim that no action was taken.

Agent messages are omitted from this comment. The category uses structured run status, not interpretation of agent prose.

Generated from Matt Pocock Skills Reviewer · routed: haiku55 xhigh · ~6 AIC · ⌖ ~2 AIC · ⊞ 6.6K · ◷

🧠 Reviewed using Matt Pocock's skills by Matt Pocock Skills Reviewer

@github-actions

github-actions Bot commented Nov 11, 2026 •

Copy link
Copy Markdown
Contributor Author

✅ Ponytail Reviewer completed successfully!

Ponytail Reviewer

Outcome: reported_no_action

The agent completed and reported no action. A no-op output alone does not verify that all required work was completed or that publication was possible.

Agent messages are omitted from this comment. The category uses structured run status, not interpretation of agent prose.

Generated from Ponytail Reviewer · routed: gpt61 medium · ~4 AIC · ⌖ ~1 AIC · ◇ ~1 AIC · ⊞ 181 · ◷

Generated by Ponytail Reviewer for #76738

@github-actions

Copy link
Copy Markdown
Contributor Author

Comment Memory

Peek at saved memory (pr-code-quality-reviewer)
reviewed_at: 2026-11-11T08:36:00Z
review_event: REQUEST_CHANGES
top_themes: [inequality false positive, unsound logical guards, incorrect module-scope guard search]
files_reviewed: [eslint-factory/src/rules/require-null-check-with-typeof-object.ts, eslint-factory/src/rules/require-null-check-with-typeof-object.test.ts, eslint-factory/README.md, eslint-factory/eslint.config.cjs, eslint-factory/src/index.ts]
comment_count: 3

Note

This comment is managed by comment memory.

Expand the saved memory block to view or edit the persistent context for this thread.
Edit only the text inside the backtick fences; workflow metadata and the footer are regenerated automatically.

Learn more about comment memory

🔎 Code quality review by PR Code Quality Reviewer · copilot · gpt50 · ~3 AIC · ⌖ ~1 AIC · ⊞ 1.6K · ◷
Comment /review to run again

@github-actions github-actions Bot left a comment

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Found three correctness issues in the new rule: it warns on typeof x !== "object" even though that predicate excludes null, treats arbitrary logical-expression operands as null guards, and lets unrelated earlier module-scope guards suppress findings. The inline comments describe each case and requested regression coverage.

🔎 Code quality review by PR Code Quality Reviewer · copilot · gpt50 · ~3 AIC · ⌖ ~1 AIC · ⊞ 1.6K
Comment /review to run again


/** Matches `typeof <subject> <op> "object"` in either operand order. */
function matchTypeofObject(node: TSESTree.BinaryExpression): { text: string; operator: string } | null {
if (!EQUALITY_OPERATORS.has(node.operator)) return null;

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

!==/!= are safe for this specific null concern: since typeof null === "object", typeof value !== "object" is false when value is null, so null cannot pass this condition. Requiring a null guard here creates false-positive warnings for a predicate that already excludes null. Please restrict this rule to ===/== (or otherwise handle the inequality semantics), and add a regression test for typeof value !== "object".

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed in 8344ab8: the rule now reports only typeof value === "object" and ==; !==/!= comparisons are excluded and covered by tests.


const guard = (candidate: TSESTree.Node) => isNullGuard(candidate, match.text);
if (someNode(outermostLogicalChain(node), node, Infinity, guard)) return;
if (someNode(enclosingFunctionOrProgram(node), node, node.range[0], guard)) return;

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

At module scope this searches the entire prefix of Program, so an unrelated earlier guard can suppress a later warning. For example, function helper(x) { if (x) return; } followed by const ok = typeof x === "object"; is treated as guarded even though the helper never establishes anything about the later x (and the same name may be a different binding). This contradicts the documented same-function scope. Please avoid using the whole Program as the earlier-guard scope, or use scope/binding-aware analysis.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed in 8344ab8: module scope is no longer searched for earlier guards. Only a nullish early-exit in the nearest function can suppress a later report; added a module-scope regression.

if (subjectText(node) !== text) return false;
const parent = node.parent;
if (!parent) return false;
if (parent.type === AST_NODE_TYPES.LogicalExpression) return true;

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This treats any direct child of a logical expression as a null guard, including the value operand in const result = condition && value;. That expression does not establish that value is non-null; condition may be true while value is null, and a later typeof value === "object" can still pass before property access. Restrict suppression to a condition that actually tests the subject (for example the left operand in a short-circuit guard), and add a regression case for value-position operands.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed in 8344ab8: suppression now requires a subject-specific guard in a conjunction; unrelated logical operands no longer count. Added the value-position regression, and OR expressions are not treated as null guards.

@github-actions github-actions Bot left a comment

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Skills-Based Review 🧠

Applied /tdd and /grill-with-docs. Requesting changes: the rule has confirmed false negatives, and the null-guard semantics in the code and README do not yet agree.

📋 Key Themes & Highlights

Key Themes

  • Guard suppression is too loose: someNode accepts any truthiness or null check in the enclosing function, even when it does not dominate the typeof site (inline comment on line 110). Two existing threads (lines 39 and 74) cover the !== undefined and nested-function cases.
  • Docs and implementation disagree: the README says "earlier in the same function", which describes the loose behaviour rather than an intended contract. Pick one and make both match.
  • Test gaps: the valid cases do not include a non-dominating earlier guard, so the suppression path is untested.

Positive Highlights

  • ✅ Clear separation of matchTypeofObject, isNullGuard, and someNode, which keeps the traversal easy to follow.
  • ✅ Operand-order and optional-chaining handling are covered by tests.
  • ✅ Scope is tight: a warn severity avoids breaking existing code.

Verification Note

The sandbox could not install eslint-factory dependencies (npm registry TLS error), so I could not run the test suite. Findings come from tracing the code paths, not from executing the tests. Please run the three new cases once they are added.

@copilot please address the review comments above.

🧠 Reviewed using Matt Pocock's skills by Matt Pocock Skills Reviewer · copilot · routed: haiku55 xhigh · ~6 AIC · ⌖ ~2 AIC · ⊞ 6.6K
Comment /matt to run again


const guard = (candidate: TSESTree.Node) => isNullGuard(candidate, match.text);
if (someNode(outermostLogicalChain(node), node, Infinity, guard)) return;
if (someNode(enclosingFunctionOrProgram(node), node, node.range[0], guard)) return;

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[/tdd] A truthiness check anywhere earlier in the function suppresses the report, even when it does not gate the typeof site. function f(value) { if (value) log("set"); return typeof value === "object" ? Object.keys(value) : []; } is not flagged, yet value === null still reaches Object.keys(null) and throws.

Either require the guard to dominate the site (for example an early-exit if (!value) return), or document the looser behaviour explicitly in the README. Either way, add a failing test first.

💡 Suggested test
{
  code: `function f(value) { if (value) log("set"); return typeof value === "object" ? Object.keys(value) : []; }`,
  errors: [{ message: messageFor("value") }],
},

The README (around line 1356) currently says "earlier in the same function", which describes the loose behaviour. The docs and implementation should agree on the intended semantics.

@copilot please address this.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed in 8344ab8: non-dominating truthiness checks no longer suppress reports; earlier guards count only when a top-level if exits on nullish/falsy values. Added the suggested regression and updated the README.

@pelikhan

Copy link
Copy Markdown
Collaborator

@copilot run pr-finisher skill

Co-authored-by: pelikhan <4175913+pelikhan@users.noreply.github.com>

Copilot AI commented Nov 11, 2026

Copy link
Copy Markdown
Contributor

@copilot run pr-finisher skill

The finisher pass is complete. Commit 8344ab8 fixes the rule and adds regressions; local lint, build, and the full ESLint-factory suite pass. PR mergeability is clean; CI predates this push and needs a maintainer re-trigger.

Co-authored-by: pelikhan <4175913+pelikhan@users.noreply.github.com>
Copilot AI requested a review from pelikhan November 11, 2026 18:33
@pelikhan
pelikhan merged commit 3ffc002 into main Nov 11, 2026
13 checks passed
@pelikhan
pelikhan deleted the eslint-miner/require-null-check-with-typeof-object-5d66d6ad90c21897 branch November 11, 2026 19:54
@github-actions

Copy link
Copy Markdown
Contributor Author

🎉 This pull request is included in a new release.

Release: v1.2.3

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

automation cookie Issue Monster Loves Cookies! eslint

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants