forked from eslint-community/eslint-plugin-eslint-plugin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathno-unused-placeholders.js
82 lines (72 loc) · 2.68 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
/**
* @fileoverview Disallow unused placeholders in rule report messages
* @author 薛定谔的猫<[email protected]>
*/
'use strict';
const utils = require('../utils');
const { getStaticValue } = require('eslint-utils');
// ------------------------------------------------------------------------------
// Rule Definition
// ------------------------------------------------------------------------------
module.exports = {
meta: {
docs: {
description: 'disallow unused placeholders in rule report messages',
category: 'Rules',
recommended: true,
},
type: 'problem',
fixable: null,
schema: [],
},
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;
}
const messageStaticValue = getStaticValue(reportInfo.message, context.getScope());
if (
(
(reportInfo.message.type === 'Literal' && typeof reportInfo.message.value === 'string') ||
(messageStaticValue && typeof messageStaticValue.value === 'string')
) &&
reportInfo.data &&
reportInfo.data.type === 'ObjectExpression'
) {
const message = reportInfo.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();
message.replace(PLACEHOLDER_MATCHER, (fullMatch, term) => {
placeholdersInMessage.add(term);
});
reportInfo.data.properties.forEach(prop => {
const key = utils.getKeyName(prop);
if (!placeholdersInMessage.has(key)) {
context.report({
node: reportInfo.message,
message: 'The placeholder {{{{unusedKey}}}} is unused.',
data: { unusedKey: key },
});
}
});
}
}
},
};
},
};