-
-
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.
feat: add new rules
no-missing-message-ids
and no-unused-message-ids
- Loading branch information
Showing
10 changed files
with
1,065 additions
and
0 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,59 @@ | ||
# Disallow `messageId`s that are missing from `meta.messages` (no-missing-message-ids) | ||
|
||
When using `meta.messages` and `messageId` to report rule violations, it's possible to mistakenly use a `messageId` that doesn't exist in `meta.messages`. | ||
|
||
## Rule Details | ||
|
||
Examples of **incorrect** code for this rule: | ||
|
||
```js | ||
/* eslint eslint-plugin/no-missing-message-ids: error */ | ||
|
||
module.exports = { | ||
meta: { | ||
messages: { | ||
foo: 'hello world', | ||
}, | ||
}, | ||
create(context) { | ||
return { | ||
CallExpression(node) { | ||
context.report({ | ||
node, | ||
messageId: 'abc', | ||
}); | ||
}, | ||
}; | ||
}, | ||
}; | ||
``` | ||
|
||
Examples of **correct** code for this rule: | ||
|
||
```js | ||
/* eslint eslint-plugin/no-missing-message-ids: error */ | ||
|
||
module.exports = { | ||
meta: { | ||
messages: { | ||
foo: 'hello world', | ||
}, | ||
}, | ||
create(context) { | ||
return { | ||
CallExpression(node) { | ||
context.report({ | ||
node, | ||
messageId: 'foo', | ||
}); | ||
}, | ||
}; | ||
}, | ||
}; | ||
``` | ||
|
||
## Further Reading | ||
|
||
* [messageIds API](https://eslint.org/docs/developer-guide/working-with-rules#messageids) | ||
* [no-unused-message-ids](./no-unused-message-ids.md) rule | ||
* [prefer-message-ids](./prefer-message-ids.md) rule |
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,60 @@ | ||
# Disallow unused `messageId`s in `meta.messages` (no-unused-message-ids) | ||
|
||
When using `meta.messages` and `messageId` to report rule violations, it's possible to mistakenly leave a message in `meta.messages` that is never used. | ||
|
||
## Rule Details | ||
|
||
Examples of **incorrect** code for this rule: | ||
|
||
```js | ||
/* eslint eslint-plugin/no-unused-message-ids: error */ | ||
|
||
module.exports = { | ||
meta: { | ||
messages: { | ||
foo: 'hello world', | ||
bar: 'lorem ipsum', // this message is never used | ||
}, | ||
}, | ||
create(context) { | ||
return { | ||
CallExpression(node) { | ||
context.report({ | ||
node, | ||
messageId: 'foo', | ||
}); | ||
}, | ||
}; | ||
}, | ||
}; | ||
``` | ||
|
||
Examples of **correct** code for this rule: | ||
|
||
```js | ||
/* eslint eslint-plugin/no-unused-message-ids: error */ | ||
|
||
module.exports = { | ||
meta: { | ||
messages: { | ||
foo: 'hello world', | ||
}, | ||
}, | ||
create(context) { | ||
return { | ||
CallExpression(node) { | ||
context.report({ | ||
node, | ||
messageId: 'foo', | ||
}); | ||
}, | ||
}; | ||
}, | ||
}; | ||
``` | ||
|
||
## Further Reading | ||
|
||
* [messageIds API](https://eslint.org/docs/developer-guide/working-with-rules#messageids) | ||
* [no-missing-message-ids](./no-missing-message-ids.md) rule | ||
* [prefer-message-ids](./prefer-message-ids.md) rule |
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,89 @@ | ||
'use strict'; | ||
|
||
const utils = require('../utils'); | ||
|
||
// ------------------------------------------------------------------------------ | ||
// Rule Definition | ||
// ------------------------------------------------------------------------------ | ||
|
||
/** @type {import('eslint').Rule.RuleModule} */ | ||
module.exports = { | ||
meta: { | ||
type: 'problem', | ||
docs: { | ||
description: | ||
'disallow `messageId`s that are missing from `meta.messages`', | ||
category: 'Rules', | ||
recommended: false, | ||
url: 'https://github.com/not-an-aardvark/eslint-plugin-eslint-plugin/tree/HEAD/docs/rules/no-missing-message-ids.md', | ||
}, | ||
fixable: null, | ||
schema: [], | ||
messages: { | ||
missingMessage: '`meta.messages` is missing this `messageId`.', | ||
}, | ||
}, | ||
|
||
create(context) { | ||
const sourceCode = context.getSourceCode(); | ||
const { scopeManager } = sourceCode; | ||
const ruleInfo = utils.getRuleInfo(sourceCode); | ||
|
||
const messagesNode = utils.getMessagesNode(ruleInfo, scopeManager); | ||
|
||
let contextIdentifiers; | ||
|
||
// ---------------------------------------------------------------------- | ||
// Public | ||
// ---------------------------------------------------------------------- | ||
|
||
if (!messagesNode || messagesNode.type !== 'ObjectExpression') { | ||
return {}; | ||
} | ||
|
||
return { | ||
Program(ast) { | ||
contextIdentifiers = utils.getContextIdentifiers(scopeManager, 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) { | ||
return; | ||
} | ||
|
||
const reportMessagesAndDataArray = | ||
utils.collectReportViolationAndSuggestionData(reportInfo); | ||
|
||
for (const { messageId } of reportMessagesAndDataArray.filter( | ||
(obj) => obj.messageId | ||
)) { | ||
const values = | ||
messageId.type === 'Literal' | ||
? [messageId] | ||
: utils.findPossibleVariableValues(messageId, scopeManager); | ||
|
||
values.forEach((val) => { | ||
if ( | ||
val.type === 'Literal' && | ||
val.value !== null && | ||
val.value !== '' && | ||
!utils.getMessageIdNodeById(val.value, ruleInfo, scopeManager) | ||
) | ||
context.report({ | ||
node: val, | ||
messageId: 'missingMessage', | ||
}); | ||
}); | ||
} | ||
} | ||
}, | ||
}; | ||
}, | ||
}; |
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,98 @@ | ||
'use strict'; | ||
|
||
const utils = require('../utils'); | ||
|
||
// ------------------------------------------------------------------------------ | ||
// Rule Definition | ||
// ------------------------------------------------------------------------------ | ||
|
||
/** @type {import('eslint').Rule.RuleModule} */ | ||
module.exports = { | ||
meta: { | ||
type: 'problem', | ||
docs: { | ||
description: 'disallow unused `messageId`s in `meta.messages`', | ||
category: 'Rules', | ||
recommended: false, | ||
url: 'https://github.com/not-an-aardvark/eslint-plugin-eslint-plugin/tree/HEAD/docs/rules/no-unused-message-ids.md', | ||
}, | ||
fixable: null, | ||
schema: [], | ||
messages: { | ||
unusedMessage: 'This message is never used.', | ||
}, | ||
}, | ||
|
||
create(context) { | ||
const sourceCode = context.getSourceCode(); | ||
const { scopeManager } = sourceCode; | ||
const info = utils.getRuleInfo(sourceCode); | ||
|
||
const messageIdsUsed = new Set(); | ||
let contextIdentifiers; | ||
let shouldPerformUnusedCheck = true; | ||
|
||
const messageIdNodes = utils.getMessageIdNodes(info, scopeManager); | ||
if (!messageIdNodes) { | ||
return {}; | ||
} | ||
|
||
// ---------------------------------------------------------------------- | ||
// Public | ||
// ---------------------------------------------------------------------- | ||
|
||
return { | ||
Program(ast) { | ||
contextIdentifiers = utils.getContextIdentifiers(scopeManager, ast); | ||
}, | ||
|
||
'Program:exit'() { | ||
if (shouldPerformUnusedCheck) { | ||
for (const messageIdNode of messageIdNodes.filter( | ||
(node) => !messageIdsUsed.has(node.key.name) | ||
)) { | ||
context.report({ | ||
node: messageIdNode, | ||
messageId: 'unusedMessage', | ||
}); | ||
} | ||
} | ||
}, | ||
|
||
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) { | ||
return; | ||
} | ||
|
||
const reportMessagesAndDataArray = | ||
utils.collectReportViolationAndSuggestionData(reportInfo); | ||
|
||
for (const { messageId } of reportMessagesAndDataArray.filter( | ||
(obj) => obj.messageId | ||
)) { | ||
const values = | ||
messageId.type === 'Literal' | ||
? [messageId] | ||
: utils.findPossibleVariableValues(messageId, scopeManager); | ||
if ( | ||
values.length === 0 || | ||
values.some((val) => val.type !== 'Literal') | ||
) { | ||
shouldPerformUnusedCheck = false; | ||
} | ||
values.forEach((val) => { | ||
messageIdsUsed.add(val.value); | ||
}); | ||
} | ||
} | ||
}, | ||
}; | ||
}, | ||
}; |
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.