forked from eslint-community/eslint-plugin-eslint-plugin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathno-missing-placeholders.js
85 lines (75 loc) · 2.85 KB
/
no-missing-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
/**
* @fileoverview Disallow missing placeholders in rule report messages
* @author Teddy Katz
*/
'use strict';
const utils = require('../utils');
const { getStaticValue } = require('eslint-utils');
// ------------------------------------------------------------------------------
// Rule Definition
// ------------------------------------------------------------------------------
module.exports = {
meta: {
type: 'problem',
docs: {
description: 'disallow missing placeholders in rule report messages',
category: 'Rules',
recommended: true,
},
fixable: null,
schema: [],
messages: {
placeholderDoesNotExist: 'The placeholder {{{{missingKey}}}} does not exist.',
},
},
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) {
return;
}
const reportMessagesAndDataArray = utils.collectReportViolationAndSuggestionData(reportInfo).filter(obj => obj.message);
for (const { message, data } of reportMessagesAndDataArray) {
const messageStaticValue = getStaticValue(message, context.getScope());
if (
(
(message.type === 'Literal' && typeof message.value === 'string') ||
(messageStaticValue && typeof messageStaticValue.value === 'string')
) &&
(!data || data.type === 'ObjectExpression')
) {
// Same regex as the one ESLint uses
// https://github.com/eslint/eslint/blob/e5446449d93668ccbdb79d78cc69f165ce4fde07/lib/eslint.js#L990
const PLACEHOLDER_MATCHER = /{{\s*([^{}]+?)\s*}}/g;
let match;
while ((match = PLACEHOLDER_MATCHER.exec(message.value || messageStaticValue.value))) { // eslint-disable-line no-extra-parens
const matchingProperty = data &&
data.properties.find(prop => utils.getKeyName(prop) === match[1]);
if (!matchingProperty) {
context.report({
node: message,
messageId: 'placeholderDoesNotExist',
data: { missingKey: match[1] },
});
}
}
}
}
}
},
};
},
};