feat: implement prefer-import/assert-strict rule - #553
Merged
aladdin-add merged 3 commits intoAug 8, 2026
Conversation
There was a problem hiding this comment.
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-strictrule with autofix support. - Add RuleTester suite covering import/export/require/dynamic import cases.
- Document and register the rule in
README.mdandlib/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}`) | ||
| }, | ||
| }) | ||
| } |
Author
There was a problem hiding this comment.
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 | ||
| } |
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) | ||
| }, | ||
| } |
Comment on lines
+54
to
+58
| { | ||
| code: 'import assert, { strict } from "node:assert";', | ||
| output: 'import assert, { strict } from "node:assert/strict";', | ||
| errors: [{ messageId: "preferAssertStrict" }], | ||
| }, |
Comment on lines
+59
to
+63
| { | ||
| code: 'import { strict, equal } from "node:assert";', | ||
| output: 'import { strict, equal } from "node:assert/strict";', | ||
| errors: [{ messageId: "preferAssertStrict" }], | ||
| }, |
Comment on lines
+69
to
+73
| { | ||
| code: 'export { strict, equal } from "node:assert";', | ||
| output: 'export { strict, equal } from "node:assert/strict";', | ||
| errors: [{ messageId: "preferAssertStrict" }], | ||
| }, |
Comment on lines
+99
to
+103
| { | ||
| code: 'const { strict, equal } = require("node:assert");', | ||
| output: 'const { strict, equal } = require("node:assert/strict");', | ||
| errors: [{ messageId: "preferAssertStrict" }], | ||
| }, |
baevm
force-pushed
the
prefer-import/assert-strict
branch
from
July 11, 2026 16:50
c05051b to
0b1f9bc
Compare
|
Could you please resolve the conflict? Thanks! |
baevm
force-pushed
the
prefer-import/assert-strict
branch
from
August 7, 2026 11:05
0b1f9bc to
b6e2714
Compare
Author
Resolved |
aladdin-add
approved these changes
Aug 8, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What is the purpose of this pull request?
this PR implements
prefer-import/assert-strictrule 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-strictruleRelated 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 recommendedand whether it should provide an autofix