forked from eslint-community/eslint-plugin-eslint-plugin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathreport-message-format.js
138 lines (124 loc) · 4.43 KB
/
report-message-format.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
128
129
130
131
132
133
134
135
136
137
138
/**
* @fileoverview enforce a consistent format for rule report messages
* @author Teddy Katz
*/
'use strict';
const { getStaticValue } = require('@eslint-community/eslint-utils');
const utils = require('../utils');
// ------------------------------------------------------------------------------
// Rule Definition
// ------------------------------------------------------------------------------
/** @type {import('eslint').Rule.RuleModule} */
module.exports = {
meta: {
type: 'suggestion',
docs: {
description: 'enforce a consistent format for rule report messages',
category: 'Rules',
recommended: false,
url: 'https://github.com/eslint-community/eslint-plugin-eslint-plugin/tree/HEAD/docs/rules/report-message-format.md',
},
fixable: null,
schema: [
{
description: 'Format that all report messages must match.',
type: 'string',
},
],
messages: {
noMatch: "Report message does not match the pattern '{{pattern}}'.",
},
},
create(context) {
const pattern = new RegExp(context.options[0] || '');
let contextIdentifiers;
/**
* Report a message node if it doesn't match the given formatting
* @param {ASTNode} message The message AST node
* @returns {void}
*/
function processMessageNode(message, scope) {
const staticValue = getStaticValue(message, scope);
if (
(message.type === 'Literal' &&
typeof message.value === 'string' &&
!pattern.test(message.value)) ||
(message.type === 'TemplateLiteral' &&
message.quasis.length === 1 &&
!pattern.test(message.quasis[0].value.cooked)) ||
(staticValue && !pattern.test(staticValue.value))
) {
context.report({
node: message,
messageId: 'noMatch',
data: { pattern: context.options[0] || '' },
});
}
}
const sourceCode = context.sourceCode || context.getSourceCode(); // TODO: just use context.sourceCode when dropping eslint < v9
const ruleInfo = utils.getRuleInfo(sourceCode);
if (!ruleInfo) {
return {};
}
// ----------------------------------------------------------------------
// Public
// ----------------------------------------------------------------------
return {
Program(ast) {
const scope = sourceCode.getScope?.(ast) || context.getScope(); // TODO: just use sourceCode.getScope() when we drop support for ESLint < v9.0.0
contextIdentifiers = utils.getContextIdentifiers(
sourceCode.scopeManager,
ast,
);
const messagesObject =
ruleInfo &&
ruleInfo.meta &&
ruleInfo.meta.type === 'ObjectExpression' &&
ruleInfo.meta.properties.find(
(prop) =>
prop.type === 'Property' && utils.getKeyName(prop) === 'messages',
);
if (
!messagesObject ||
messagesObject.value.type !== 'ObjectExpression'
) {
return;
}
messagesObject.value.properties
.filter((prop) => prop.type === 'Property')
.map((prop) => prop.value)
.forEach((it) => processMessageNode(it, scope));
},
CallExpression(node) {
const scope = sourceCode.getScope?.(node) || context.getScope(); // TODO: just use sourceCode.getScope() when we drop support for ESLint < v9.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);
const message = reportInfo && reportInfo.message;
const suggest = reportInfo && reportInfo.suggest;
if (message) {
processMessageNode(message, scope);
}
if (suggest && suggest.type === 'ArrayExpression') {
suggest.elements
.flatMap((obj) =>
obj.type === 'ObjectExpression' ? obj.properties : [],
)
.filter(
(prop) =>
prop.type === 'Property' &&
prop.key.type === 'Identifier' &&
prop.key.name === 'message',
)
.map((prop) => prop.value)
.forEach((it) => processMessageNode(it, scope));
}
}
},
};
},
};