UNPKG

1.94 kBJavaScriptView Raw
1module.exports = { remapMessages }
2
3function remapMessages(messages, hasBOM, codePart) {
4 const newMessages = []
5
6 for (const message of messages) {
7 if (remapMessage(message, hasBOM, codePart)) {
8 newMessages.push(message)
9 }
10 }
11
12 return newMessages
13}
14
15function remapMessage(message, hasBOM, codePart) {
16 if (!message.line || !message.column) {
17 // Some messages apply to the whole file instead of a particular code location. In particular:
18 // * @typescript-eslint/parser may send messages with no line/column
19 // * eslint-plugin-eslint-comments send messages with column=0 to bypass ESLint ignore comments.
20 // See https://github.com/BenoitZugmeyer/eslint-plugin-html/issues/70
21 // For now, just include them in the output. In the future, we should make sure those messages
22 // are not print twice.
23 return true
24 }
25
26 const location = codePart.originalLocation({
27 line: message.line,
28 column: message.column,
29 })
30
31 // Ignore messages if they were in transformed code
32 if (!location) {
33 return false
34 }
35
36 Object.assign(message, location)
37 message.source = codePart.getOriginalLine(location.line)
38
39 // Map fix range
40 if (message.fix && message.fix.range) {
41 const bomOffset = hasBOM ? -1 : 0
42 message.fix.range = [
43 codePart.originalIndex(message.fix.range[0]) + bomOffset,
44 // The range end is exclusive, meaning it should replace all characters with indexes from
45 // start to end - 1. We have to get the original index of the last targeted character.
46 codePart.originalIndex(message.fix.range[1] - 1) + 1 + bomOffset,
47 ]
48 }
49
50 // Map end location
51 if (message.endLine && message.endColumn) {
52 const endLocation = codePart.originalLocation({
53 line: message.endLine,
54 column: message.endColumn,
55 })
56 if (endLocation) {
57 message.endLine = endLocation.line
58 message.endColumn = endLocation.column
59 }
60 }
61
62 return true
63}