-
-
Notifications
You must be signed in to change notification settings - Fork 26
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-i…
…ds` (#254) * feat: add new rules `no-missing-message-ids` and `no-unused-message-ids` * handle variable messages object keys in no-{missing,unused}-message-ids and add messageId to messages
- Loading branch information
Showing
10 changed files
with
1,392 additions
and
17 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,97 @@ | ||
'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 the messageId "{{messageId}}".', | ||
}, | ||
}, | ||
|
||
create(context) { | ||
const sourceCode = context.getSourceCode(); | ||
const { scopeManager } = sourceCode; | ||
const ruleInfo = utils.getRuleInfo(sourceCode); | ||
|
||
const messagesNode = utils.getMessagesNode(ruleInfo, scopeManager); | ||
|
||
let contextIdentifiers; | ||
|
||
if (!messagesNode || messagesNode.type !== 'ObjectExpression') { | ||
// If we can't find `meta.messages`, disable the rule. | ||
return {}; | ||
} | ||
|
||
return { | ||
Program(ast) { | ||
contextIdentifiers = utils.getContextIdentifiers(scopeManager, ast); | ||
}, | ||
|
||
CallExpression(node) { | ||
// Check for messageId properties used in known calls to context.report(); | ||
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); | ||
|
||
// Look for any possible string values we found for this messageId. | ||
values.forEach((val) => { | ||
if ( | ||
val.type === 'Literal' && | ||
val.value !== null && | ||
val.value !== '' && | ||
!utils.getMessageIdNodeById( | ||
val.value, | ||
ruleInfo, | ||
scopeManager, | ||
context.getScope() | ||
) | ||
) | ||
// Couldn't find this messageId in `meta.messages`. | ||
context.report({ | ||
node: val, | ||
messageId: 'missingMessage', | ||
data: { | ||
messageId: 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,120 @@ | ||
'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: 'The messageId "{{messageId}}" 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) { | ||
// If we can't find `meta.messages`, disable the rule. | ||
return {}; | ||
} | ||
|
||
return { | ||
Program(ast) { | ||
contextIdentifiers = utils.getContextIdentifiers(scopeManager, ast); | ||
}, | ||
|
||
'Program:exit'() { | ||
if (shouldPerformUnusedCheck) { | ||
const messageIdNodesUnused = messageIdNodes.filter( | ||
(node) => | ||
!messageIdsUsed.has(utils.getKeyName(node, context.getScope())) | ||
); | ||
|
||
// Report any messageIds that were never used. | ||
for (const messageIdNode of messageIdNodesUnused) { | ||
context.report({ | ||
node: messageIdNode, | ||
messageId: 'unusedMessage', | ||
data: { | ||
messageId: utils.getKeyName(messageIdNode, context.getScope()), | ||
}, | ||
}); | ||
} | ||
} | ||
}, | ||
|
||
CallExpression(node) { | ||
// Check for messageId properties used in known calls to context.report(); | ||
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') | ||
) { | ||
// When a dynamic messageId is used and we can't detect its value, disable the rule to avoid false positives. | ||
shouldPerformUnusedCheck = false; | ||
} | ||
values.forEach((val) => messageIdsUsed.add(val.value)); | ||
} | ||
} | ||
}, | ||
|
||
Property(node) { | ||
// In order to reduce false positives, we will also check for messageId properties anywhere in the file. | ||
// This is helpful especially in the event that helper functions are used for reporting violations. | ||
if (node.key.type === 'Identifier' && node.key.name === 'messageId') { | ||
const values = | ||
node.value.type === 'Literal' | ||
? [node.value] | ||
: utils.findPossibleVariableValues(node.value, scopeManager); | ||
if ( | ||
values.length === 0 || | ||
values.some((val) => val.type !== 'Literal') | ||
) { | ||
// When a dynamic messageId is used and we can't detect its value, disable the rule to avoid false positives. | ||
shouldPerformUnusedCheck = false; | ||
} | ||
values.forEach((val) => messageIdsUsed.add(val.value)); | ||
} | ||
}, | ||
}; | ||
}, | ||
}; |
Oops, something went wrong.