forked from not-an-aardvark/eslint-rule-composer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
rule-composer.js
228 lines (211 loc) · 6.96 KB
/
rule-composer.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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
/**
* Translates a multi-argument context.report() call into a single object argument call
* @param {...*} arguments A list of arguments passed to `context.report`
* @returns {MessageDescriptor} A normalized object containing report information
*/
function normalizeMultiArgReportCall() {
// If there is one argument, it is considered to be a new-style call already.
if (arguments.length === 1) {
return arguments[0];
}
// If the second argument is a string, the arguments are interpreted as [node, message, data, fix].
if (typeof arguments[1] === 'string') {
return {
node: arguments[0],
message: arguments[1],
data: arguments[2],
fix: arguments[3],
};
}
// Otherwise, the arguments are interpreted as [node, loc, message, data, fix].
return {
node: arguments[0],
loc: arguments[1],
message: arguments[2],
data: arguments[3],
fix: arguments[4],
};
}
/**
* Normalizes a MessageDescriptor to always have a `loc` with `start` and `end` properties
* @param {MessageDescriptor} descriptor A descriptor for the report from a rule.
* @returns {{start: Location, end: (Location|null)}} An updated location that infers the `start` and `end` properties
* from the `node` of the original descriptor, or infers the `start` from the `loc` of the original descriptor.
*/
function normalizeReportLoc(descriptor) {
if (descriptor.loc) {
if (descriptor.loc.start) {
return descriptor.loc;
}
return { start: descriptor.loc, end: null };
}
return descriptor.node.loc;
}
/**
* Interpolates data placeholders in report messages
* @param {MessageDescriptor} descriptor The report message descriptor.
* @param {Object} messageIds Message IDs from rule metadata
* @returns {{message: string, data: Object}} The interpolated message and data for the descriptor
*/
function normalizeMessagePlaceholders(descriptor, messageIds) {
const message = typeof descriptor.messageId === 'string' ? messageIds[descriptor.messageId] : descriptor.message;
if (!descriptor.data) {
return {
message,
data: typeof descriptor.messageId === 'string' ? {} : null,
};
}
const normalizedData = Object.create(null);
const interpolatedMessage = message.replace(/\{\{\s*([^{}]+?)\s*\}\}/g, (fullMatch, term) => {
if (term in descriptor.data) {
normalizedData[term] = descriptor.data[term];
return descriptor.data[term];
}
return fullMatch;
});
return {
message: interpolatedMessage,
data: Object.freeze(normalizedData),
};
}
function getRuleMeta(rule) {
return typeof rule === 'object' && rule.meta && typeof rule.meta === 'object'
? rule.meta
: {};
}
function getMessageIds(rule) {
const meta = getRuleMeta(rule);
return meta.messages && typeof rule.meta.messages === 'object'
? meta.messages
: {};
}
function getReportNormalizer(rule) {
const messageIds = getMessageIds(rule);
return function normalizeReport() {
const descriptor = normalizeMultiArgReportCall.apply(null, arguments);
const interpolatedMessageAndData = normalizeMessagePlaceholders(descriptor, messageIds);
return {
node: descriptor.node,
message: interpolatedMessageAndData.message,
messageId: typeof descriptor.messageId === 'string' ? descriptor.messageId : null,
data: typeof descriptor.messageId === 'string' ? interpolatedMessageAndData.data : null,
loc: normalizeReportLoc(descriptor),
fix: descriptor.fix,
};
};
}
function getRuleCreateFunc(rule) {
return typeof rule === 'function' ? rule : rule.create;
}
function removeMessageIfMessageIdPresent(reportDescriptor) {
const newDescriptor = { ...reportDescriptor };
if (typeof reportDescriptor.messageId === 'string' && typeof reportDescriptor.message === 'string') {
delete newDescriptor.message;
}
return newDescriptor;
}
module.exports = Object.freeze({
filterReports(rule, predicate) {
return Object.freeze({
create(context) {
const filename = context.getFilename();
const sourceCode = context.sourceCode;
const settings = context.settings;
const options = context.options;
return getRuleCreateFunc(rule)(
Object.freeze(
Object.create(
context,
{
report: {
enumerable: true,
value() {
const reportDescriptor = getReportNormalizer(rule).apply(null, arguments);
if (predicate(reportDescriptor, {
sourceCode, settings, options, filename,
})) {
context.report(removeMessageIfMessageIdPresent(reportDescriptor));
}
},
},
},
),
),
);
},
schema: rule.schema,
meta: getRuleMeta(rule),
});
},
mapReports(rule, iteratee) {
return Object.freeze({
create(context) {
const filename = context.getFilename();
const sourceCode = context.sourceCode;
const settings = context.settings;
const options = context.options;
return getRuleCreateFunc(rule)(
Object.freeze(
Object.create(
context,
{
report: {
enumerable: true,
value() {
context.report(
removeMessageIfMessageIdPresent(
iteratee(
getReportNormalizer(rule).apply(null, arguments),
{
sourceCode, settings, options, filename,
},
),
),
);
},
},
},
),
),
);
},
schema: rule.schema,
meta: getRuleMeta(rule),
});
},
joinReports(rules) {
return Object.freeze({
create(context) {
return rules
.map((rule) => getRuleCreateFunc(rule)(context))
.reduce(
(allListeners, ruleListeners) => Object.keys(ruleListeners).reduce(
(combinedListeners, key) => {
const currentListener = combinedListeners[key];
const ruleListener = ruleListeners[key];
if (currentListener) {
return {
...combinedListeners,
[key]() {
currentListener.apply(null, arguments);
ruleListener.apply(null, arguments);
},
};
}
return { ...combinedListeners, [key]: ruleListener };
},
allListeners,
),
Object.create(null),
);
},
meta: Object.freeze({
messages: Object.assign.apply(
null,
[Object.create(null)].concat(rules.map(getMessageIds)),
),
fixable: 'code',
}),
});
},
});