2023-08-17 09:47:40 +00:00
|
|
|
'use strict'
|
2022-01-21 08:28:41 +00:00
|
|
|
|
2023-08-17 09:47:40 +00:00
|
|
|
let pico = require('picocolors')
|
2022-01-21 08:28:41 +00:00
|
|
|
|
2023-08-17 09:47:40 +00:00
|
|
|
let terminalHighlight = require('./terminal-highlight')
|
2022-01-21 08:28:41 +00:00
|
|
|
|
2023-08-17 09:47:40 +00:00
|
|
|
class CssSyntaxError extends Error {
|
|
|
|
constructor(message, line, column, source, file, plugin) {
|
|
|
|
super(message)
|
|
|
|
this.name = 'CssSyntaxError'
|
|
|
|
this.reason = message
|
2022-01-21 08:28:41 +00:00
|
|
|
|
|
|
|
if (file) {
|
2023-08-17 09:47:40 +00:00
|
|
|
this.file = file
|
2022-01-21 08:28:41 +00:00
|
|
|
}
|
|
|
|
if (source) {
|
2023-08-17 09:47:40 +00:00
|
|
|
this.source = source
|
2022-01-21 08:28:41 +00:00
|
|
|
}
|
|
|
|
if (plugin) {
|
2023-08-17 09:47:40 +00:00
|
|
|
this.plugin = plugin
|
2022-01-21 08:28:41 +00:00
|
|
|
}
|
|
|
|
if (typeof line !== 'undefined' && typeof column !== 'undefined') {
|
2023-08-17 09:47:40 +00:00
|
|
|
if (typeof line === 'number') {
|
|
|
|
this.line = line
|
|
|
|
this.column = column
|
|
|
|
} else {
|
|
|
|
this.line = line.line
|
|
|
|
this.column = line.column
|
|
|
|
this.endLine = column.line
|
|
|
|
this.endColumn = column.column
|
|
|
|
}
|
2022-01-21 08:28:41 +00:00
|
|
|
}
|
|
|
|
|
2023-08-17 09:47:40 +00:00
|
|
|
this.setMessage()
|
2022-01-21 08:28:41 +00:00
|
|
|
|
|
|
|
if (Error.captureStackTrace) {
|
2023-08-17 09:47:40 +00:00
|
|
|
Error.captureStackTrace(this, CssSyntaxError)
|
2022-01-21 08:28:41 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-08-17 09:47:40 +00:00
|
|
|
setMessage() {
|
|
|
|
this.message = this.plugin ? this.plugin + ': ' : ''
|
|
|
|
this.message += this.file ? this.file : '<css input>'
|
2022-01-21 08:28:41 +00:00
|
|
|
if (typeof this.line !== 'undefined') {
|
2023-08-17 09:47:40 +00:00
|
|
|
this.message += ':' + this.line + ':' + this.column
|
2022-01-21 08:28:41 +00:00
|
|
|
}
|
2023-08-17 09:47:40 +00:00
|
|
|
this.message += ': ' + this.reason
|
2022-01-21 08:28:41 +00:00
|
|
|
}
|
|
|
|
|
2023-08-17 09:47:40 +00:00
|
|
|
showSourceCode(color) {
|
|
|
|
if (!this.source) return ''
|
2022-01-21 08:28:41 +00:00
|
|
|
|
2023-08-17 09:47:40 +00:00
|
|
|
let css = this.source
|
|
|
|
if (color == null) color = pico.isColorSupported
|
|
|
|
if (terminalHighlight) {
|
|
|
|
if (color) css = terminalHighlight(css)
|
2022-01-21 08:28:41 +00:00
|
|
|
}
|
|
|
|
|
2023-08-17 09:47:40 +00:00
|
|
|
let lines = css.split(/\r?\n/)
|
|
|
|
let start = Math.max(this.line - 3, 0)
|
|
|
|
let end = Math.min(this.line + 2, lines.length)
|
2022-01-21 08:28:41 +00:00
|
|
|
|
2023-08-17 09:47:40 +00:00
|
|
|
let maxWidth = String(end).length
|
2022-01-21 08:28:41 +00:00
|
|
|
|
2023-08-17 09:47:40 +00:00
|
|
|
let mark, aside
|
|
|
|
if (color) {
|
|
|
|
let { bold, gray, red } = pico.createColors(true)
|
|
|
|
mark = text => bold(red(text))
|
|
|
|
aside = text => gray(text)
|
|
|
|
} else {
|
|
|
|
mark = aside = str => str
|
2022-01-21 08:28:41 +00:00
|
|
|
}
|
|
|
|
|
2023-08-17 09:47:40 +00:00
|
|
|
return lines
|
|
|
|
.slice(start, end)
|
|
|
|
.map((line, index) => {
|
|
|
|
let number = start + 1 + index
|
|
|
|
let gutter = ' ' + (' ' + number).slice(-maxWidth) + ' | '
|
|
|
|
if (number === this.line) {
|
|
|
|
let spacing =
|
|
|
|
aside(gutter.replace(/\d/g, ' ')) +
|
|
|
|
line.slice(0, this.column - 1).replace(/[^\t]/g, ' ')
|
|
|
|
return mark('>') + aside(gutter) + line + '\n ' + spacing + mark('^')
|
|
|
|
}
|
|
|
|
return ' ' + aside(gutter) + line
|
|
|
|
})
|
|
|
|
.join('\n')
|
2022-01-21 08:28:41 +00:00
|
|
|
}
|
|
|
|
|
2023-08-17 09:47:40 +00:00
|
|
|
toString() {
|
|
|
|
let code = this.showSourceCode()
|
2022-01-21 08:28:41 +00:00
|
|
|
if (code) {
|
2023-08-17 09:47:40 +00:00
|
|
|
code = '\n\n' + code + '\n'
|
2022-01-21 08:28:41 +00:00
|
|
|
}
|
2023-08-17 09:47:40 +00:00
|
|
|
return this.name + ': ' + this.message + code
|
2022-01-21 08:28:41 +00:00
|
|
|
}
|
2023-08-17 09:47:40 +00:00
|
|
|
}
|
2022-01-21 08:28:41 +00:00
|
|
|
|
2023-08-17 09:47:40 +00:00
|
|
|
module.exports = CssSyntaxError
|
|
|
|
CssSyntaxError.default = CssSyntaxError
|