Skip to content

feat: add require-assert-message rule - #565

Open
electrohyun wants to merge 2 commits into
eslint-community:masterfrom
electrohyun:feat/require-assert-message
Open

feat: add require-assert-message rule#565
electrohyun wants to merge 2 commits into
eslint-community:masterfrom
electrohyun:feat/require-assert-message

Conversation

@electrohyun

@electrohyun electrohyun commented Aug 9, 2026

Copy link
Copy Markdown

Disclosure: I'm a participant of open source contribution program OSSCA

Important

This PR currently supports only node:assert and node:assert/strict; bare assert and assert/strict specifiers are not supported.
I would appreciate feedback on whether this scope is appropriate before merging.

To avoid false positives, the rule conservatively skips all calls through a CommonJS assertion binding if that binding is reassigned after initialization. This can also skip calls that appear before the reassignment. It does not currently exclude property mutations such as assert.ok = mock.

What is the purpose of this pull request?

This PR adds n/require-assert-message, a rule that requires messages for Node.js assert calls.

Without a message, assertion failures fall back to Node.js's default AssertionError message, which can make the reason for a failure harder to identify. The rule reports missing or obviously empty messages for assertion APIs whose message position can be determined statically.

What changes did you make? (Give an overview)

  • Added n/require-assert-message.
    • Tracks node:assert, node:assert/strict, and the strict export from node:assert.
    • Checks assert(), assert.ok(), equality assertions, match(), doesNotMatch(), and partialDeepStrictEqual().
    • Reports a missing message, global undefined, a void expression, an empty string, and an empty template literal.
  • Excluded throws, doesNotThrow, rejects, and doesNotReject because their optional error argument makes the message position ambiguous.
  • Excluded ifError, which has no message parameter, and fail, which is commonly used to mark unreachable code.
  • To avoid false positives, conservatively skips all calls through a CommonJS assertion binding if that binding is reassigned after initialization. This can also skip calls that appear before the reassignment.
  • Added rule tests and documentation, and updated the generated rule list in the README.

Related Issues

refs #322

Is there anything you'd like reviewers to focus on?

  1. Bare specifier scope
  • The rule currently supports only node:assert and node:assert/strict. Should it also support bare assert and assert/strict specifiers to align with existing plugin rules?
  1. Property mutation policy
  • Reassigned bindings are excluded:

    let assert = require("node:assert");
    assert = mock;
    assert.ok(user); // Not reported.
  • The current binding-reassignment policy is intentionally conservative and may also create false negatives for calls before the reassignment.

  • However, property mutations are not currently excluded:

    const assert = require("node:assert");
    assert.ok = mock;
    assert.ok(user); // May still be reported.
  • This is an uncommon case. Please let me know whether it should also be excluded, or whether limiting the rule to binding reassignment is the right scope.

@aladdin-add
aladdin-add requested review from aladdin-add and a balanced review from Copilot August 25, 2026 19:42

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Pull request overview

Adds n/require-assert-message to enforce meaningful messages for supported Node.js assertion APIs.

Changes:

  • Implements assertion reference tracking and message validation.
  • Adds comprehensive rule tests and documentation.
  • Registers the rule and updates the generated rule list.

Reviewed changes

Copilot reviewed 5 out of 5 changed files in this pull request and generated 2 comments.

Show a summary per file
File Description
lib/rules/require-assert-message.js Implements the new rule.
lib/all-rules.js Registers the rule.
tests/lib/rules/require-assert-message.js Adds rule test cases.
docs/rules/require-assert-message.md Documents behavior and scope.
README.md Adds the rule to the rule list.

💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.

Comment on lines +139 to +142
} else if (
isUndefined(message, context) ||
isEmptyString(message)
) {
continue
}

const message = node.arguments[messageIndex]
@electrohyun

Copy link
Copy Markdown
Author

Applied the Copilot review feedback and renamed isEmptyString to isEmptyMessage since it now also checks for null.

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants