Skip to content

feat: implement prefer-import/assert-strict rule - #553

Merged
aladdin-add merged 3 commits into
eslint-community:masterfrom
baevm:prefer-import/assert-strict
Aug 8, 2026
Merged

feat: implement prefer-import/assert-strict rule#553
aladdin-add merged 3 commits into
eslint-community:masterfrom
baevm:prefer-import/assert-strict

Conversation

@baevm

@baevm baevm commented Jul 11, 2026

Copy link
Copy Markdown

What is the purpose of this pull request?

this PR implements prefer-import/assert-strict rule from issue #59. I reused some tests from biome rule and also added some new ones.

What changes did you make? (Give an overview)

implemented prefer-import/assert-strict rule

Related Issues

closes #59

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

I used rule name suggested in the issue, but maybe it should be renamed to something like assert/prefer-strict, since more rules related to node assert have been suggested? Im also open to changing whether this rule should be recommended and whether it should provide an autofix

Copilot AI review requested due to automatic review settings July 11, 2026 16:36

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

This PR adds a new ESLint rule n/prefer-import/assert-strict to discourage importing node:assert (legacy assertion mode) and prefer node:assert/strict, including documentation and test coverage.

Changes:

  • Implement prefer-import/assert-strict rule with autofix support.
  • Add RuleTester suite covering import/export/require/dynamic import cases.
  • Document and register the rule in README.md and lib/all-rules.js.

Reviewed changes

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

Show a summary per file
File Description
lib/rules/prefer-import/assert-strict.js Implements the new rule + fixer logic (needs adjustment for safety in mixed strict import/destructure cases).
tests/lib/rules/prefer-import/assert-strict.js Adds tests for valid/invalid patterns (some expected autofixes are currently invalid).
docs/rules/prefer-import/assert-strict.md Adds rule documentation page.
lib/all-rules.js Registers the new rule in the rule map.
README.md Adds the rule to the rules list table.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment on lines +95 to +117
/**
* @param {import('estree').Node | null | undefined} node
* @param {import('eslint').Rule.RuleContext} context
*/
function checkSource(node, context) {
if (
node?.type !== "Literal" ||
typeof node.value !== "string" ||
node.value !== "node:assert"
) {
return
}

context.report({
node,
messageId,
fix(fixer) {
const raw = context.sourceCode.getText(node)
const quote = raw[0]
return fixer.replaceText(node, `${quote}node:assert/strict${quote}`)
},
})
}

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

Yeah, I removed the autofixer. Its harder to implement safely than I thought 😫

Comment on lines +76 to +93
/**
* @param {import('estree').CallExpression & import('eslint').Rule.NodeParentExtension} node
* @returns {boolean}
*/
function requiresOnlyStrict(node) {
const { parent } = node

if (parent.type === "MemberExpression" && parent.object === node) {
return accessesOnlyStrict(parent)
}
if (parent.type === "VariableDeclarator" && parent.init === node) {
return accessesOnlyStrict(parent.id)
}
if (parent.type === "AssignmentExpression" && parent.right === node) {
return accessesOnlyStrict(parent.left)
}
return false
}

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

Comment on lines +137 to +162
CallExpression(node) {
if (
node.callee.type === "Identifier" &&
node.callee.name === "require" &&
!requiresOnlyStrict(node)
) {
checkSource(node.arguments[0], context)
}
},
ExportAllDeclaration(node) {
checkSource(node.source, context)
},
ExportNamedDeclaration(node) {
if (!exportsOnlyStrict(node)) {
checkSource(node.source, context)
}
},
ImportDeclaration(node) {
if (!importsOnlyStrict(node)) {
checkSource(node.source, context)
}
},
ImportExpression(node) {
checkSource(node.source, context)
},
}

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

Comment on lines +54 to +58
{
code: 'import assert, { strict } from "node:assert";',
output: 'import assert, { strict } from "node:assert/strict";',
errors: [{ messageId: "preferAssertStrict" }],
},

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

Comment on lines +59 to +63
{
code: 'import { strict, equal } from "node:assert";',
output: 'import { strict, equal } from "node:assert/strict";',
errors: [{ messageId: "preferAssertStrict" }],
},

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

Comment on lines +69 to +73
{
code: 'export { strict, equal } from "node:assert";',
output: 'export { strict, equal } from "node:assert/strict";',
errors: [{ messageId: "preferAssertStrict" }],
},

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

Comment on lines +99 to +103
{
code: 'const { strict, equal } = require("node:assert");',
output: 'const { strict, equal } = require("node:assert/strict");',
errors: [{ messageId: "preferAssertStrict" }],
},

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

@baevm
baevm force-pushed the prefer-import/assert-strict branch from c05051b to 0b1f9bc Compare July 11, 2026 16:50
@aladdin-add

Copy link
Copy Markdown

Could you please resolve the conflict? Thanks!

@baevm
baevm force-pushed the prefer-import/assert-strict branch from 0b1f9bc to b6e2714 Compare August 7, 2026 11:05
@baevm

baevm commented Aug 7, 2026

Copy link
Copy Markdown
Author

Could you please resolve the conflict? Thanks!

Resolved

@aladdin-add
aladdin-add merged commit d7afbed into eslint-community:master Aug 8, 2026
12 checks passed
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.

prefer-import/assert-strict: new rule to prefer import 'node:assert/strict'

3 participants