| 1 |
'use strict'; |
| 2 |
|
| 3 |
/** |
| 4 |
* Translates a multi-argument context.report() call into a single object argument call |
| 5 |
* @param {...*} arguments A list of arguments passed to `context.report` |
| 6 |
* @returns {MessageDescriptor} A normalized object containing report information |
| 7 |
*/ |
| 8 |
function normalizeMultiArgReportCall() { |
| 9 |
// If there is one argument, it is considered to be a new-style call already. |
| 10 |
if (arguments.length === 1) { |
| 11 |
return arguments[0]; |
| 12 |
} |
| 13 |
|
| 14 |
// If the second argument is a string, the arguments are interpreted as [node, message, data, fix]. |
| 15 |
if (typeof arguments[1] === 'string') { |
| 16 |
return { |
| 17 |
node: arguments[0], |
| 18 |
message: arguments[1], |
| 19 |
data: arguments[2], |
| 20 |
fix: arguments[3], |
| 21 |
}; |
| 22 |
} |
| 23 |
|
| 24 |
// Otherwise, the arguments are interpreted as [node, loc, message, data, fix]. |
| 25 |
return { |
| 26 |
node: arguments[0], |
| 27 |
loc: arguments[1], |
| 28 |
message: arguments[2], |
| 29 |
data: arguments[3], |
| 30 |
fix: arguments[4], |
| 31 |
}; |
| 32 |
} |
| 33 |
|
| 34 |
/** |
| 35 |
* Normalizes a MessageDescriptor to always have a `loc` with `start` and `end` properties |
| 36 |
* @param {MessageDescriptor} descriptor A descriptor for the report from a rule. |
| 37 |
* @returns {{start: Location, end: (Location|null)}} An updated location that infers the `start` and `end` properties |
| 38 |
* from the `node` of the original descriptor, or infers the `start` from the `loc` of the original descriptor. |
| 39 |
*/ |
| 40 |
function normalizeReportLoc(descriptor) { |
| 41 |
if (descriptor.loc) { |
| 42 |
if (descriptor.loc.start) { |
| 43 |
return descriptor.loc; |
| 44 |
} |
| 45 |
return { start: descriptor.loc, end: null }; |
| 46 |
} |
| 47 |
return descriptor.node.loc; |
| 48 |
} |
| 49 |
|
| 50 |
|
| 51 |
/** |
| 52 |
* Interpolates data placeholders in report messages |
| 53 |
* @param {MessageDescriptor} descriptor The report message descriptor. |
| 54 |
* @param {Object} messageIds Message IDs from rule metadata |
| 55 |
* @returns {{message: string, data: Object}} The interpolated message and data for the descriptor |
| 56 |
*/ |
| 57 |
function normalizeMessagePlaceholders(descriptor, messageIds) { |
| 58 |
const message = typeof descriptor.messageId === 'string' ? messageIds[descriptor.messageId] : descriptor.message; |
| 59 |
if (!descriptor.data) { |
| 60 |
return { |
| 61 |
message, |
| 62 |
data: typeof descriptor.messageId === 'string' ? {} : null, |
| 63 |
}; |
| 64 |
} |
| 65 |
|
| 66 |
const normalizedData = Object.create(null); |
| 67 |
const interpolatedMessage = message.replace(/\{\{\s*([^{}]+?)\s*\}\}/g, (fullMatch, term) => { |
| 68 |
if (term in descriptor.data) { |
| 69 |
normalizedData[term] = descriptor.data[term]; |
| 70 |
return descriptor.data[term]; |
| 71 |
} |
| 72 |
|
| 73 |
return fullMatch; |
| 74 |
}); |
| 75 |
|
| 76 |
return { |
| 77 |
message: interpolatedMessage, |
| 78 |
data: Object.freeze(normalizedData), |
| 79 |
}; |
| 80 |
} |
| 81 |
|
| 82 |
function getRuleMeta(rule) { |
| 83 |
return typeof rule === 'object' && rule.meta && typeof rule.meta === 'object' |
| 84 |
? rule.meta |
| 85 |
: {}; |
| 86 |
} |
| 87 |
|
| 88 |
function getMessageIds(rule) { |
| 89 |
const meta = getRuleMeta(rule); |
| 90 |
return meta.messages && typeof rule.meta.messages === 'object' |
| 91 |
? meta.messages |
| 92 |
: {}; |
| 93 |
} |
| 94 |
|
| 95 |
function getReportNormalizer(rule) { |
| 96 |
const messageIds = getMessageIds(rule); |
| 97 |
|
| 98 |
return function normalizeReport() { |
| 99 |
const descriptor = normalizeMultiArgReportCall.apply(null, arguments); |
| 100 |
const interpolatedMessageAndData = normalizeMessagePlaceholders(descriptor, messageIds); |
| 101 |
|
| 102 |
return { |
| 103 |
node: descriptor.node, |
| 104 |
message: interpolatedMessageAndData.message, |
| 105 |
messageId: typeof descriptor.messageId === 'string' ? descriptor.messageId : null, |
| 106 |
data: typeof descriptor.messageId === 'string' ? interpolatedMessageAndData.data : null, |
| 107 |
loc: normalizeReportLoc(descriptor), |
| 108 |
fix: descriptor.fix, |
| 109 |
}; |
| 110 |
}; |
| 111 |
} |
| 112 |
|
| 113 |
function getRuleCreateFunc(rule) { |
| 114 |
return typeof rule === 'function' ? rule : rule.create; |
| 115 |
} |
| 116 |
|
| 117 |
function removeMessageIfMessageIdPresent(reportDescriptor) { |
| 118 |
const newDescriptor = Object.assign({}, reportDescriptor); |
| 119 |
|
| 120 |
if (typeof reportDescriptor.messageId === 'string' && typeof reportDescriptor.message === 'string') { |
| 121 |
delete newDescriptor.message; |
| 122 |
} |
| 123 |
|
| 124 |
return newDescriptor; |
| 125 |
} |
| 126 |
|
| 127 |
module.exports = Object.freeze({ |
| 128 |
filterReports(rule, predicate) { |
| 129 |
return Object.freeze({ |
| 130 |
create(context) { |
| 131 |
const filename = context.getFilename(); |
| 132 |
const sourceCode = context.getSourceCode(); |
| 133 |
const settings = context.settings; |
| 134 |
const options = context.options; |
| 135 |
return getRuleCreateFunc(rule)( |
| 136 |
Object.freeze( |
| 137 |
Object.create( |
| 138 |
context, |
| 139 |
{ |
| 140 |
report: { |
| 141 |
enumerable: true, |
| 142 |
value() { |
| 143 |
const reportDescriptor = getReportNormalizer(rule).apply(null, arguments); |
| 144 |
if (predicate(reportDescriptor, { |
| 145 |
sourceCode, settings, options, filename, |
| 146 |
})) { |
| 147 |
context.report(removeMessageIfMessageIdPresent(reportDescriptor)); |
| 148 |
} |
| 149 |
}, |
| 150 |
}, |
| 151 |
} |
| 152 |
) |
| 153 |
) |
| 154 |
); |
| 155 |
}, |
| 156 |
schema: rule.schema, |
| 157 |
meta: getRuleMeta(rule), |
| 158 |
}); |
| 159 |
}, |
| 160 |
mapReports(rule, iteratee) { |
| 161 |
return Object.freeze({ |
| 162 |
create(context) { |
| 163 |
const filename = context.getFilename(); |
| 164 |
const sourceCode = context.getSourceCode(); |
| 165 |
const settings = context.settings; |
| 166 |
const options = context.options; |
| 167 |
return getRuleCreateFunc(rule)( |
| 168 |
Object.freeze( |
| 169 |
Object.create( |
| 170 |
context, |
| 171 |
{ |
| 172 |
report: { |
| 173 |
enumerable: true, |
| 174 |
value() { |
| 175 |
context.report( |
| 176 |
removeMessageIfMessageIdPresent( |
| 177 |
iteratee( |
| 178 |
getReportNormalizer(rule).apply(null, arguments), |
| 179 |
{ |
| 180 |
sourceCode, settings, options, filename, |
| 181 |
} |
| 182 |
) |
| 183 |
) |
| 184 |
); |
| 185 |
}, |
| 186 |
}, |
| 187 |
} |
| 188 |
) |
| 189 |
) |
| 190 |
); |
| 191 |
}, |
| 192 |
schema: rule.schema, |
| 193 |
meta: getRuleMeta(rule), |
| 194 |
}); |
| 195 |
}, |
| 196 |
joinReports(rules) { |
| 197 |
return Object.freeze({ |
| 198 |
create(context) { |
| 199 |
return rules |
| 200 |
.map(rule => getRuleCreateFunc(rule)(context)) |
| 201 |
.reduce( |
| 202 |
(allListeners, ruleListeners) => |
| 203 |
Object.keys(ruleListeners).reduce( |
| 204 |
(combinedListeners, key) => { |
| 205 |
const currentListener = combinedListeners[key]; |
| 206 |
const ruleListener = ruleListeners[key]; |
| 207 |
if (currentListener) { |
| 208 |
return Object.assign({}, combinedListeners, { |
| 209 |
[key]() { |
| 210 |
currentListener.apply(null, arguments); |
| 211 |
ruleListener.apply(null, arguments); |
| 212 |
}, |
| 213 |
}); |
| 214 |
} |
| 215 |
return Object.assign({}, combinedListeners, { [key]: ruleListener }); |
| 216 |
}, |
| 217 |
allListeners |
| 218 |
), |
| 219 |
Object.create(null) |
| 220 |
); |
| 221 |
}, |
| 222 |
meta: Object.freeze({ |
| 223 |
messages: Object.assign.apply( |
| 224 |
null, |
| 225 |
[Object.create(null)].concat(rules.map(getMessageIds)) |
| 226 |
), |
| 227 |
fixable: 'code', |
| 228 |
}), |
| 229 |
}); |
| 230 |
}, |
| 231 |
}); |