-
-
Notifications
You must be signed in to change notification settings - Fork 29
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
New: Add new rule
prefer-message-ids
(#170)
- Loading branch information
Showing
24 changed files
with
298 additions
and
56 deletions.
There are no files selected for viewing
This file contains 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 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,57 @@ | ||
# Require using `messageId` instead of `message` to report rule violations (prefer-message-ids) | ||
|
||
When reporting a rule violation, it's preferred to provide the violation message with the `messageId` property instead of the `message` property. Message IDs provide the following benefits: | ||
|
||
* Rule violation messages can be stored in a central `meta.messages` object for convenient management | ||
* Rule violation messages do not need to be repeated in both the rule file and rule test file | ||
|
||
## Rule Details | ||
|
||
This rule catches usages of the `message` property when reporting a rule violation. | ||
|
||
Examples of **incorrect** code for this rule: | ||
|
||
```js | ||
/* eslint eslint-plugin/prefer-message-ids: error */ | ||
|
||
module.exports = { | ||
create (context) { | ||
return { | ||
CallExpression (node) { | ||
context.report({ | ||
node, | ||
message: 'Some error message.', | ||
}); | ||
}, | ||
}; | ||
}, | ||
}; | ||
``` | ||
|
||
Examples of **correct** code for this rule: | ||
|
||
```js | ||
/* eslint eslint-plugin/prefer-message-ids: error */ | ||
|
||
module.exports = { | ||
meta: { | ||
messages: { | ||
someMessageId: 'Some error message', | ||
}, | ||
}, | ||
create (context) { | ||
return { | ||
CallExpression (node) { | ||
context.report({ | ||
node, | ||
messageId: 'someMessageId', | ||
}); | ||
}, | ||
}; | ||
}, | ||
}; | ||
``` | ||
|
||
## Further Reading | ||
|
||
* [messageIds API](https://eslint.org/docs/developer-guide/working-with-rules#messageids) |
This file contains 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 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 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 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 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 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 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 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,54 @@ | ||
'use strict'; | ||
|
||
const utils = require('../utils'); | ||
|
||
// ------------------------------------------------------------------------------ | ||
// Rule Definition | ||
// ------------------------------------------------------------------------------ | ||
|
||
module.exports = { | ||
meta: { | ||
type: 'problem', | ||
docs: { | ||
description: 'require using `messageId` instead of `message` to report rule violations', | ||
category: 'Rules', | ||
recommended: false, | ||
}, | ||
fixable: null, | ||
schema: [], | ||
messages: { | ||
foundMessage: 'Use `messageId` instead of `message`.', | ||
}, | ||
}, | ||
|
||
create (context) { | ||
let contextIdentifiers; | ||
|
||
// ---------------------------------------------------------------------- | ||
// Public | ||
// ---------------------------------------------------------------------- | ||
|
||
return { | ||
Program (ast) { | ||
contextIdentifiers = utils.getContextIdentifiers(context, ast); | ||
}, | ||
CallExpression (node) { | ||
if ( | ||
node.callee.type === 'MemberExpression' && | ||
contextIdentifiers.has(node.callee.object) && | ||
node.callee.property.type === 'Identifier' && node.callee.property.name === 'report' | ||
) { | ||
const reportInfo = utils.getReportInfo(node.arguments, context); | ||
if (!reportInfo || !reportInfo.message) { | ||
return; | ||
} | ||
|
||
context.report({ | ||
node: reportInfo.message.parent, | ||
messageId: 'foundMessage', | ||
}); | ||
} | ||
}, | ||
}; | ||
}, | ||
}; |
This file contains 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 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 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 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 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 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.