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