-
-
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
Changes from 1 commit
4bd4639
b24b4bf
66b65a4
40e9f10
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| 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.md) rule | ||
| - [prefer-message-ids](./prefer-message-ids.md) rule | ||
| 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); | ||||||||||||||||||||||
| }, | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
|
||||||||||||||||||||||
| let contextIdentifiers: Set<Node>; | |
| return { | |
| Program(ast) { | |
| contextIdentifiers = getContextIdentifiers(scopeManager, ast); | |
| }, | |
| const contextIdentifiers: Set<Node> = getContextIdentifiers(scopeManager, sourceCode.ast); | |
| return { | |
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
Uh oh!
There was an error while loading. Please reload this page.