|
| 1 | +'use strict'; |
| 2 | + |
| 3 | +const utils = require('../utils'); |
| 4 | + |
| 5 | +// ------------------------------------------------------------------------------ |
| 6 | +// Rule Definition |
| 7 | +// ------------------------------------------------------------------------------ |
| 8 | + |
| 9 | +/** @type {import('eslint').Rule.RuleModule} */ |
| 10 | +module.exports = { |
| 11 | + meta: { |
| 12 | + type: 'problem', |
| 13 | + docs: { |
| 14 | + description: 'disallow unused `messageId`s in `meta.messages`', |
| 15 | + category: 'Rules', |
| 16 | + recommended: false, |
| 17 | + url: 'https://github.com/not-an-aardvark/eslint-plugin-eslint-plugin/tree/HEAD/docs/rules/no-unused-message-ids.md', |
| 18 | + }, |
| 19 | + fixable: null, |
| 20 | + schema: [], |
| 21 | + messages: { |
| 22 | + unusedMessage: 'This message is never used.', |
| 23 | + }, |
| 24 | + }, |
| 25 | + |
| 26 | + create(context) { |
| 27 | + const sourceCode = context.getSourceCode(); |
| 28 | + const { scopeManager } = sourceCode; |
| 29 | + const info = utils.getRuleInfo(sourceCode); |
| 30 | + |
| 31 | + const messageIdsUsed = new Set(); |
| 32 | + let contextIdentifiers; |
| 33 | + let shouldPerformUnusedCheck = true; |
| 34 | + |
| 35 | + const messageIdNodes = utils.getMessageIdNodes(info, scopeManager); |
| 36 | + if (!messageIdNodes) { |
| 37 | + // If we can't find `meta.messages`, disable the rule. |
| 38 | + return {}; |
| 39 | + } |
| 40 | + |
| 41 | + // ---------------------------------------------------------------------- |
| 42 | + // Public |
| 43 | + // ---------------------------------------------------------------------- |
| 44 | + |
| 45 | + return { |
| 46 | + Program(ast) { |
| 47 | + contextIdentifiers = utils.getContextIdentifiers(scopeManager, ast); |
| 48 | + }, |
| 49 | + |
| 50 | + 'Program:exit'() { |
| 51 | + if (shouldPerformUnusedCheck) { |
| 52 | + for (const messageIdNode of messageIdNodes.filter( |
| 53 | + (node) => !messageIdsUsed.has(node.key.name) |
| 54 | + )) { |
| 55 | + context.report({ |
| 56 | + node: messageIdNode, |
| 57 | + messageId: 'unusedMessage', |
| 58 | + }); |
| 59 | + } |
| 60 | + } |
| 61 | + }, |
| 62 | + |
| 63 | + CallExpression(node) { |
| 64 | + // Check for messageId properties use in known calls to context.report(); |
| 65 | + if ( |
| 66 | + node.callee.type === 'MemberExpression' && |
| 67 | + contextIdentifiers.has(node.callee.object) && |
| 68 | + node.callee.property.type === 'Identifier' && |
| 69 | + node.callee.property.name === 'report' |
| 70 | + ) { |
| 71 | + const reportInfo = utils.getReportInfo(node.arguments, context); |
| 72 | + if (!reportInfo) { |
| 73 | + return; |
| 74 | + } |
| 75 | + |
| 76 | + const reportMessagesAndDataArray = |
| 77 | + utils.collectReportViolationAndSuggestionData(reportInfo); |
| 78 | + |
| 79 | + for (const { messageId } of reportMessagesAndDataArray.filter( |
| 80 | + (obj) => obj.messageId |
| 81 | + )) { |
| 82 | + const values = |
| 83 | + messageId.type === 'Literal' |
| 84 | + ? [messageId] |
| 85 | + : utils.findPossibleVariableValues(messageId, scopeManager); |
| 86 | + if ( |
| 87 | + values.length === 0 || |
| 88 | + values.some((val) => val.type !== 'Literal') |
| 89 | + ) { |
| 90 | + // When a dynamic messageId is used and we can't detect its value, disable the rule to avoid false positives. |
| 91 | + shouldPerformUnusedCheck = false; |
| 92 | + } |
| 93 | + values.forEach((val) => messageIdsUsed.add(val.value)); |
| 94 | + } |
| 95 | + } |
| 96 | + }, |
| 97 | + |
| 98 | + Property(node) { |
| 99 | + // In order to reduce false positives, we will also check for messageId properties anywhere in the file. |
| 100 | + // This is helpful especially in the event that helper functions are used for reporting violations. |
| 101 | + if (node.key.type === 'Identifier' && node.key.name === 'messageId') { |
| 102 | + const values = |
| 103 | + node.value.type === 'Literal' |
| 104 | + ? [node.value] |
| 105 | + : utils.findPossibleVariableValues(node.value, scopeManager); |
| 106 | + if ( |
| 107 | + values.length === 0 || |
| 108 | + values.some((val) => val.type !== 'Literal') |
| 109 | + ) { |
| 110 | + // When a dynamic messageId is used and we can't detect its value, disable the rule to avoid false positives. |
| 111 | + shouldPerformUnusedCheck = false; |
| 112 | + } |
| 113 | + values.forEach((val) => messageIdsUsed.add(val.value)); |
| 114 | + } |
| 115 | + }, |
| 116 | + }; |
| 117 | + }, |
| 118 | +}; |
0 commit comments