-
-
Notifications
You must be signed in to change notification settings - Fork 30
/
Copy pathno-unused-placeholders.js
127 lines (114 loc) · 4.32 KB
/
no-unused-placeholders.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
/**
* @fileoverview Disallow unused placeholders in rule report messages
* @author 薛定谔的猫<[email protected]>
*/
'use strict';
const utils = require('../utils');
const { getStaticValue } = require('@eslint-community/eslint-utils');
// ------------------------------------------------------------------------------
// Rule Definition
// ------------------------------------------------------------------------------
/** @type {import('eslint').Rule.RuleModule} */
module.exports = {
meta: {
type: 'problem',
docs: {
description: 'Disallow unused placeholders in rule report messages',
category: 'Rules',
recommended: true,
url: 'https://github.com/eslint-community/eslint-plugin-eslint-plugin/tree/HEAD/docs/rules/no-unused-placeholders.md',
},
fixable: null,
schema: [],
messages: {
placeholderUnused:
'The placeholder {{{{unusedKey}}}} is unused (does not exist in the actual message).',
},
},
create(context) {
const sourceCode = context.sourceCode || context.getSourceCode(); // TODO: just use context.sourceCode when dropping eslint < v9
const { scopeManager } = sourceCode;
let contextIdentifiers;
const ruleInfo = utils.getRuleInfo(sourceCode);
if (!ruleInfo) {
return {};
}
const messagesNode = utils.getMessagesNode(ruleInfo, scopeManager);
return {
Program(ast) {
contextIdentifiers = utils.getContextIdentifiers(scopeManager, ast);
},
CallExpression(node) {
const scope = sourceCode.getScope?.(node) || context.getScope(); // TODO: just use sourceCode.getScope() when we drop support for ESLint < 9.0.0
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, context);
if (!reportInfo) {
return;
}
const reportMessagesAndDataArray =
utils.collectReportViolationAndSuggestionData(reportInfo);
if (messagesNode) {
// Check for any potential instances where we can use the messageId to fill in the message for convenience.
reportMessagesAndDataArray.forEach((obj) => {
if (
!obj.message &&
obj.messageId &&
obj.messageId.type === 'Literal' &&
typeof obj.messageId.value === 'string'
) {
const correspondingMessage = utils.getMessageIdNodeById(
obj.messageId.value,
ruleInfo,
scopeManager,
scope,
);
if (correspondingMessage) {
obj.message = correspondingMessage.value;
}
}
});
}
for (const { message, data } of reportMessagesAndDataArray.filter(
(obj) => obj.message,
)) {
const messageStaticValue = getStaticValue(message, scope);
if (
((message.type === 'Literal' &&
typeof message.value === 'string') ||
(messageStaticValue &&
typeof messageStaticValue.value === 'string')) &&
data &&
data.type === 'ObjectExpression'
) {
const messageValue = message.value || messageStaticValue.value;
// https://github.com/eslint/eslint/blob/2874d75ed8decf363006db25aac2d5f8991bd969/lib/linter.js#L986
const PLACEHOLDER_MATCHER = /{{\s*([^{}]+?)\s*}}/g;
const placeholdersInMessage = new Set();
messageValue.replaceAll(
PLACEHOLDER_MATCHER,
(fullMatch, term) => {
placeholdersInMessage.add(term);
},
);
data.properties.forEach((prop) => {
const key = utils.getKeyName(prop);
if (!placeholdersInMessage.has(key)) {
context.report({
node: prop,
messageId: 'placeholderUnused',
data: { unusedKey: key },
});
}
});
}
}
}
},
};
},
};