-
-
Notifications
You must be signed in to change notification settings - Fork 33
feat: add new rule no-matching-violation-suggest-message-ids
#567
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
aladdin-add
merged 4 commits into
eslint-community:main
from
StyleShit:feat/368-no-matching-violation-suggest-message-ids
Nov 10, 2025
Merged
Changes from 2 commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
4bd4639
feat: add new rule `no-matching-violation-suggest-message-ids`
StyleShit b24b4bf
Update docs/rules/no-matching-violation-suggest-message-ids.md
StyleShit 66b65a4
refactor: use sourceCode.ast directly
StyleShit 40e9f10
Merge branch 'feat/368-no-matching-violation-suggest-message-ids' of …
StyleShit File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,69 @@ | ||
| # Require suggestions to have different `messageId` than their parent report (`eslint-plugin/no-matching-violation-suggest-message-ids`) | ||
|
|
||
| <!-- end auto-generated rule header --> | ||
|
|
||
| When providing fix suggestions to a reported problem, it's important to have an actionable `messageId` for each suggestion rather than reusing the same `messageId` as the main report. | ||
|
|
||
| ## Rule Details | ||
|
|
||
| Examples of **incorrect** code for this rule: | ||
|
|
||
| ```js | ||
| /* eslint eslint-plugin/no-matching-violation-suggest-message-ids: error */ | ||
|
|
||
| module.exports = { | ||
| meta: { messages: { notAllowed: '`DebuggerStatement`s are not allowed' } }, | ||
|
|
||
| create(context) { | ||
| return { | ||
| DebuggerStatement(node) { | ||
| context.report({ | ||
| node, | ||
| messageId: 'notAllowed', | ||
| suggest: [{ messageId: 'notAllowed' }], | ||
| }); | ||
| }, | ||
| }; | ||
| }, | ||
| }; | ||
| ``` | ||
|
|
||
| Examples of **correct** code for this rule: | ||
|
|
||
| ```js | ||
| /* eslint eslint-plugin/no-matching-violation-suggest-message-ids: error */ | ||
|
|
||
| module.exports = { | ||
| meta: { | ||
| messages: { | ||
| notAllowed: '`DebuggerStatement`s are not allowed', | ||
| remove: 'Remove the debugger statement', | ||
| }, | ||
| }, | ||
|
|
||
| create(context) { | ||
| return { | ||
| DebuggerStatement(node) { | ||
| context.report({ | ||
| node, | ||
| messageId: 'notAllowed', | ||
| suggest: [ | ||
| { | ||
| messageId: 'remove', | ||
| fix: (fixer) => fixer.remove(node), | ||
| }, | ||
| ], | ||
| }); | ||
| }, | ||
| }; | ||
| }, | ||
| }; | ||
| ``` | ||
|
|
||
| ## Further Reading | ||
|
|
||
| - [ESLint rule docs: Providing Suggestions](https://eslint.org/docs/latest/extend/custom-rules#providing-suggestions) | ||
| - [ESLint rule docs: Suggestion `messageId`s](https://eslint.org/docs/latest/extend/custom-rules#suggestion-messageids) | ||
| - [no-missing-message-ids](./no-missing-message-ids.md) rule | ||
| - [no-unused-message-ids](./no-unused-message-ids.md) rule | ||
| - [prefer-message-ids](./prefer-message-ids.md) rule |
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,128 @@ | ||
| import type { Rule, Scope } from 'eslint'; | ||
| import type { Node, CallExpression, MemberExpression } from 'estree'; | ||
|
|
||
| import { | ||
| collectReportViolationAndSuggestionData, | ||
| findPossibleVariableValues, | ||
| getContextIdentifiers, | ||
| getReportInfo, | ||
| getRuleInfo, | ||
| isStringLiteral, | ||
| } from '../utils.ts'; | ||
| import type { StringLiteral, ViolationAndSuppressionData } from '../types.ts'; | ||
|
|
||
| const rule: Rule.RuleModule = { | ||
| meta: { | ||
| type: 'suggestion', | ||
| docs: { | ||
| recommended: false, | ||
| description: | ||
| 'require suggestions to have different `messageId` than their parent report', | ||
| category: 'Rules', | ||
| url: 'https://github.com/eslint-community/eslint-plugin-eslint-plugin/tree/HEAD/docs/rules/no-matching-violation-suggest-message-ids.md', | ||
| }, | ||
| schema: [], | ||
| messages: { | ||
| matchingMessageId: | ||
| 'Suggestion messageId "{{messageId}}" should not match its parent report messageId.', | ||
| }, | ||
| }, | ||
|
|
||
| create(context) { | ||
| const sourceCode = context.sourceCode; | ||
| const { scopeManager } = sourceCode; | ||
| const ruleInfo = getRuleInfo(sourceCode); | ||
| if (!ruleInfo) { | ||
| return {}; | ||
| } | ||
|
|
||
| let contextIdentifiers: Set<Node>; | ||
|
|
||
| return { | ||
| Program(ast) { | ||
| contextIdentifiers = getContextIdentifiers(scopeManager, ast); | ||
| }, | ||
|
|
||
| 'CallExpression:has(>MemberExpression[property.name="report"])'( | ||
| _node: Rule.Node, | ||
| ) { | ||
| const node = _node as CallExpression & { callee: MemberExpression }; | ||
|
|
||
| if (!contextIdentifiers.has(node.callee.object)) { | ||
| return; | ||
| } | ||
|
|
||
| const reportInfo = getReportInfo(node, context); | ||
|
|
||
| if (!reportInfo) { | ||
| return; | ||
| } | ||
|
|
||
| const [report, ...suggestions] = | ||
| collectReportViolationAndSuggestionData(reportInfo).map((obj) => ({ | ||
| ...obj, | ||
| literalMessageIds: messageIdToLiteralValues( | ||
| obj.messageId, | ||
| scopeManager, | ||
| ), | ||
| })); | ||
|
|
||
| if (report.literalMessageIds.length === 0 || suggestions.length === 0) { | ||
| return; | ||
| } | ||
|
|
||
| const reportContainsMessageId = (messageId: StringLiteral) => { | ||
| return report.literalMessageIds.some( | ||
| (reportMessageId) => messageId.value === reportMessageId.value, | ||
| ); | ||
| }; | ||
|
|
||
| suggestions.forEach((suggestion) => { | ||
| const matchingMessageId = suggestion.literalMessageIds.find( | ||
| reportContainsMessageId, | ||
| ); | ||
|
|
||
| if (matchingMessageId) { | ||
| context.report({ | ||
| messageId: 'matchingMessageId', | ||
| node: matchingMessageId, | ||
| data: { | ||
| messageId: matchingMessageId.value, | ||
| }, | ||
| }); | ||
| } | ||
| }); | ||
| }, | ||
| }; | ||
| }, | ||
| }; | ||
|
|
||
| function messageIdToLiteralValues( | ||
| messageId: ViolationAndSuppressionData['messageId'], | ||
| scopeManager: Scope.ScopeManager, | ||
| ): StringLiteral[] { | ||
| if (!messageId) { | ||
| return []; | ||
| } | ||
|
|
||
| if (isStringLiteral(messageId)) { | ||
| return [messageId]; | ||
| } | ||
|
|
||
| if (messageId.type === 'ConditionalExpression') { | ||
| return [ | ||
| ...messageIdToLiteralValues(messageId.consequent, scopeManager), | ||
| ...messageIdToLiteralValues(messageId.alternate, scopeManager), | ||
| ]; | ||
| } | ||
|
|
||
| if (messageId.type === 'Identifier') { | ||
| return findPossibleVariableValues(messageId, scopeManager).filter( | ||
| isStringLiteral, | ||
| ); | ||
| } | ||
|
|
||
| return []; | ||
| } | ||
|
|
||
| export default rule; | ||
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
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
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
you can just get the ast directly.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Cool, thanks!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done