Tim van der Lippe | 16b8228 | 2021-11-08 13:50:26 +0000 | [diff] [blame] | 1 | 'use strict' |
Mathias Bynens | 79e2cf0 | 2020-05-29 16:46:17 +0200 | [diff] [blame] | 2 | |
Tim van der Lippe | 16b8228 | 2021-11-08 13:50:26 +0000 | [diff] [blame] | 3 | let pico = require('picocolors') |
Mathias Bynens | 79e2cf0 | 2020-05-29 16:46:17 +0200 | [diff] [blame] | 4 | |
Tim van der Lippe | 16b8228 | 2021-11-08 13:50:26 +0000 | [diff] [blame] | 5 | let terminalHighlight = require('./terminal-highlight') |
Mathias Bynens | 79e2cf0 | 2020-05-29 16:46:17 +0200 | [diff] [blame] | 6 | |
Tim van der Lippe | 16b8228 | 2021-11-08 13:50:26 +0000 | [diff] [blame] | 7 | class CssSyntaxError extends Error { |
| 8 | constructor(message, line, column, source, file, plugin) { |
| 9 | super(message) |
| 10 | this.name = 'CssSyntaxError' |
| 11 | this.reason = message |
Mathias Bynens | 79e2cf0 | 2020-05-29 16:46:17 +0200 | [diff] [blame] | 12 | |
| 13 | if (file) { |
Tim van der Lippe | 16b8228 | 2021-11-08 13:50:26 +0000 | [diff] [blame] | 14 | this.file = file |
Alex Rudenko | 6c0f161 | 2021-11-05 06:28:44 +0000 | [diff] [blame] | 15 | } |
Tim van der Lippe | 2b4a9df | 2021-11-08 12:58:12 +0000 | [diff] [blame] | 16 | if (source) { |
Tim van der Lippe | 16b8228 | 2021-11-08 13:50:26 +0000 | [diff] [blame] | 17 | this.source = source |
Tim van der Lippe | 2b4a9df | 2021-11-08 12:58:12 +0000 | [diff] [blame] | 18 | } |
Tim van der Lippe | 2b4a9df | 2021-11-08 12:58:12 +0000 | [diff] [blame] | 19 | if (plugin) { |
Tim van der Lippe | 16b8228 | 2021-11-08 13:50:26 +0000 | [diff] [blame] | 20 | this.plugin = plugin |
Tim van der Lippe | 2b4a9df | 2021-11-08 12:58:12 +0000 | [diff] [blame] | 21 | } |
Tim van der Lippe | 2b4a9df | 2021-11-08 12:58:12 +0000 | [diff] [blame] | 22 | if (typeof line !== 'undefined' && typeof column !== 'undefined') { |
Tim van der Lippe | 4cb0974 | 2022-01-07 14:25:03 +0100 | [diff] [blame^] | 23 | if (typeof line === 'number') { |
| 24 | this.line = line |
| 25 | this.column = column |
| 26 | } else { |
| 27 | this.line = line.line |
| 28 | this.column = line.column |
| 29 | this.endLine = column.line |
| 30 | this.endColumn = column.column |
| 31 | } |
Tim van der Lippe | 2b4a9df | 2021-11-08 12:58:12 +0000 | [diff] [blame] | 32 | } |
| 33 | |
Tim van der Lippe | 16b8228 | 2021-11-08 13:50:26 +0000 | [diff] [blame] | 34 | this.setMessage() |
Mathias Bynens | 79e2cf0 | 2020-05-29 16:46:17 +0200 | [diff] [blame] | 35 | |
| 36 | if (Error.captureStackTrace) { |
Tim van der Lippe | 16b8228 | 2021-11-08 13:50:26 +0000 | [diff] [blame] | 37 | Error.captureStackTrace(this, CssSyntaxError) |
Mathias Bynens | 79e2cf0 | 2020-05-29 16:46:17 +0200 | [diff] [blame] | 38 | } |
Mathias Bynens | 79e2cf0 | 2020-05-29 16:46:17 +0200 | [diff] [blame] | 39 | } |
| 40 | |
Tim van der Lippe | 16b8228 | 2021-11-08 13:50:26 +0000 | [diff] [blame] | 41 | setMessage() { |
| 42 | this.message = this.plugin ? this.plugin + ': ' : '' |
| 43 | this.message += this.file ? this.file : '<css input>' |
Mathias Bynens | 79e2cf0 | 2020-05-29 16:46:17 +0200 | [diff] [blame] | 44 | if (typeof this.line !== 'undefined') { |
Tim van der Lippe | 16b8228 | 2021-11-08 13:50:26 +0000 | [diff] [blame] | 45 | this.message += ':' + this.line + ':' + this.column |
Mathias Bynens | 79e2cf0 | 2020-05-29 16:46:17 +0200 | [diff] [blame] | 46 | } |
Tim van der Lippe | 16b8228 | 2021-11-08 13:50:26 +0000 | [diff] [blame] | 47 | this.message += ': ' + this.reason |
Mathias Bynens | 79e2cf0 | 2020-05-29 16:46:17 +0200 | [diff] [blame] | 48 | } |
Mathias Bynens | 79e2cf0 | 2020-05-29 16:46:17 +0200 | [diff] [blame] | 49 | |
Tim van der Lippe | 16b8228 | 2021-11-08 13:50:26 +0000 | [diff] [blame] | 50 | showSourceCode(color) { |
| 51 | if (!this.source) return '' |
Mathias Bynens | 79e2cf0 | 2020-05-29 16:46:17 +0200 | [diff] [blame] | 52 | |
Tim van der Lippe | 16b8228 | 2021-11-08 13:50:26 +0000 | [diff] [blame] | 53 | let css = this.source |
| 54 | if (color == null) color = pico.isColorSupported |
| 55 | if (terminalHighlight) { |
| 56 | if (color) css = terminalHighlight(css) |
Mathias Bynens | 79e2cf0 | 2020-05-29 16:46:17 +0200 | [diff] [blame] | 57 | } |
| 58 | |
Tim van der Lippe | 16b8228 | 2021-11-08 13:50:26 +0000 | [diff] [blame] | 59 | let lines = css.split(/\r?\n/) |
| 60 | let start = Math.max(this.line - 3, 0) |
| 61 | let end = Math.min(this.line + 2, lines.length) |
Mathias Bynens | 79e2cf0 | 2020-05-29 16:46:17 +0200 | [diff] [blame] | 62 | |
Tim van der Lippe | 16b8228 | 2021-11-08 13:50:26 +0000 | [diff] [blame] | 63 | let maxWidth = String(end).length |
Mathias Bynens | 79e2cf0 | 2020-05-29 16:46:17 +0200 | [diff] [blame] | 64 | |
Tim van der Lippe | 16b8228 | 2021-11-08 13:50:26 +0000 | [diff] [blame] | 65 | let mark, aside |
| 66 | if (color) { |
| 67 | let { bold, red, gray } = pico.createColors(true) |
| 68 | mark = text => bold(red(text)) |
| 69 | aside = text => gray(text) |
| 70 | } else { |
| 71 | mark = aside = str => str |
Mathias Bynens | 79e2cf0 | 2020-05-29 16:46:17 +0200 | [diff] [blame] | 72 | } |
| 73 | |
Tim van der Lippe | 16b8228 | 2021-11-08 13:50:26 +0000 | [diff] [blame] | 74 | return lines |
| 75 | .slice(start, end) |
| 76 | .map((line, index) => { |
| 77 | let number = start + 1 + index |
| 78 | let gutter = ' ' + (' ' + number).slice(-maxWidth) + ' | ' |
| 79 | if (number === this.line) { |
| 80 | let spacing = |
| 81 | aside(gutter.replace(/\d/g, ' ')) + |
| 82 | line.slice(0, this.column - 1).replace(/[^\t]/g, ' ') |
| 83 | return mark('>') + aside(gutter) + line + '\n ' + spacing + mark('^') |
| 84 | } |
| 85 | return ' ' + aside(gutter) + line |
| 86 | }) |
| 87 | .join('\n') |
Mathias Bynens | 79e2cf0 | 2020-05-29 16:46:17 +0200 | [diff] [blame] | 88 | } |
Mathias Bynens | 79e2cf0 | 2020-05-29 16:46:17 +0200 | [diff] [blame] | 89 | |
Tim van der Lippe | 16b8228 | 2021-11-08 13:50:26 +0000 | [diff] [blame] | 90 | toString() { |
| 91 | let code = this.showSourceCode() |
Mathias Bynens | 79e2cf0 | 2020-05-29 16:46:17 +0200 | [diff] [blame] | 92 | if (code) { |
Tim van der Lippe | 16b8228 | 2021-11-08 13:50:26 +0000 | [diff] [blame] | 93 | code = '\n\n' + code + '\n' |
Mathias Bynens | 79e2cf0 | 2020-05-29 16:46:17 +0200 | [diff] [blame] | 94 | } |
Tim van der Lippe | 16b8228 | 2021-11-08 13:50:26 +0000 | [diff] [blame] | 95 | return this.name + ': ' + this.message + code |
Tim van der Lippe | 2b4a9df | 2021-11-08 12:58:12 +0000 | [diff] [blame] | 96 | } |
Tim van der Lippe | 16b8228 | 2021-11-08 13:50:26 +0000 | [diff] [blame] | 97 | } |
Tim van der Lippe | 2b4a9df | 2021-11-08 12:58:12 +0000 | [diff] [blame] | 98 | |
Tim van der Lippe | 16b8228 | 2021-11-08 13:50:26 +0000 | [diff] [blame] | 99 | module.exports = CssSyntaxError |
| 100 | CssSyntaxError.default = CssSyntaxError |