Skip to content

Commit

Permalink
refactor: extract remapMessages
Browse files Browse the repository at this point in the history
  • Loading branch information
BenoitZugmeyer committed Feb 6, 2024
1 parent bbdfb88 commit e6fd1e3
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 62 deletions.
63 changes: 63 additions & 0 deletions src/remapMessages.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
module.exports = { remapMessages }

function remapMessages(messages, hasBOM, codePart) {
const newMessages = []

for (const message of messages) {
if (remapMessage(message, hasBOM, codePart)) {
newMessages.push(message)
}
}

return newMessages
}

function remapMessage(message, hasBOM, codePart) {
if (!message.line || !message.column) {
// Some messages apply to the whole file instead of a particular code location. In particular:
// * @typescript-eslint/parser may send messages with no line/column
// * eslint-plugin-eslint-comments send messages with column=0 to bypass ESLint ignore comments.
// See https://github.com/BenoitZugmeyer/eslint-plugin-html/issues/70
// For now, just include them in the output. In the future, we should make sure those messages
// are not print twice.
return true
}

const location = codePart.originalLocation({
line: message.line,
column: message.column,
})

// Ignore messages if they were in transformed code
if (!location) {
return false
}

Object.assign(message, location)
message.source = codePart.getOriginalLine(location.line)

// Map fix range
if (message.fix && message.fix.range) {
const bomOffset = hasBOM ? -1 : 0
message.fix.range = [
codePart.originalIndex(message.fix.range[0]) + bomOffset,
// The range end is exclusive, meaning it should replace all characters with indexes from
// start to end - 1. We have to get the original index of the last targeted character.
codePart.originalIndex(message.fix.range[1] - 1) + 1 + bomOffset,
]
}

// Map end location
if (message.endLine && message.endColumn) {
const endLocation = codePart.originalLocation({
line: message.endLine,
column: message.endColumn,
})
if (endLocation) {
message.endLine = endLocation.line
message.endColumn = endLocation.column
}
}

return true
}
63 changes: 1 addition & 62 deletions src/verifyPatch.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ const getSettings = require("./settings").getSettings
const getFileMode = require("./getFileMode")
const extract = require("./extract")
const { splatSet } = require("./utils")
const { remapMessages } = require("./remapMessages")

const PREPARE_RULE_NAME = "__eslint-plugin-html-prepare"

Expand Down Expand Up @@ -219,65 +220,3 @@ function verifyWithSharedScopes(codeParts, verifyCodePart, parserOptions) {
})
}
}

function remapMessages(messages, hasBOM, codePart) {
const newMessages = []

for (const message of messages) {
if (remapMessage(message, hasBOM, codePart)) {
newMessages.push(message)
}
}

return newMessages
}

function remapMessage(message, hasBOM, codePart) {
if (!message.line || !message.column) {
// Some messages apply to the whole file instead of a particular code location. In particular:
// * @typescript-eslint/parser may send messages with no line/column
// * eslint-plugin-eslint-comments send messages with column=0 to bypass ESLint ignore comments.
// See https://github.com/BenoitZugmeyer/eslint-plugin-html/issues/70
// For now, just include them in the output. In the future, we should make sure those messages
// are not print twice.
return true
}

const location = codePart.originalLocation({
line: message.line,
column: message.column,
})

// Ignore messages if they were in transformed code
if (!location) {
return false
}

Object.assign(message, location)
message.source = codePart.getOriginalLine(location.line)

// Map fix range
if (message.fix && message.fix.range) {
const bomOffset = hasBOM ? -1 : 0
message.fix.range = [
codePart.originalIndex(message.fix.range[0]) + bomOffset,
// The range end is exclusive, meaning it should replace all characters with indexes from
// start to end - 1. We have to get the original index of the last targeted character.
codePart.originalIndex(message.fix.range[1] - 1) + 1 + bomOffset,
]
}

// Map end location
if (message.endLine && message.endColumn) {
const endLocation = codePart.originalLocation({
line: message.endLine,
column: message.endColumn,
})
if (endLocation) {
message.endLine = endLocation.line
message.endColumn = endLocation.column
}
}

return true
}

0 comments on commit e6fd1e3

Please sign in to comment.