source: imaps-frontend/node_modules/postcss/lib/css-syntax-error.js@ 0c6b92a

main
Last change on this file since 0c6b92a was 0c6b92a, checked in by stefan toskovski <stefantoska84@…>, 5 weeks ago

Pred finalna verzija

  • Property mode set to 100644
File size: 3.3 KB
Line 
1'use strict'
2
3let pico = require('picocolors')
4
5let terminalHighlight = require('./terminal-highlight')
6
7class CssSyntaxError extends Error {
8 constructor(message, line, column, source, file, plugin) {
9 super(message)
10 this.name = 'CssSyntaxError'
11 this.reason = message
12
13 if (file) {
14 this.file = file
15 }
16 if (source) {
17 this.source = source
18 }
19 if (plugin) {
20 this.plugin = plugin
21 }
22 if (typeof line !== 'undefined' && typeof column !== 'undefined') {
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 }
32 }
33
34 this.setMessage()
35
36 if (Error.captureStackTrace) {
37 Error.captureStackTrace(this, CssSyntaxError)
38 }
39 }
40
41 setMessage() {
42 this.message = this.plugin ? this.plugin + ': ' : ''
43 this.message += this.file ? this.file : '<css input>'
44 if (typeof this.line !== 'undefined') {
45 this.message += ':' + this.line + ':' + this.column
46 }
47 this.message += ': ' + this.reason
48 }
49
50 showSourceCode(color) {
51 if (!this.source) return ''
52
53 let css = this.source
54 if (color == null) color = pico.isColorSupported
55
56 let aside = text => text
57 let mark = text => text
58 let highlight = text => text
59 if (color) {
60 let { bold, gray, red } = pico.createColors(true)
61 mark = text => bold(red(text))
62 aside = text => gray(text)
63 if (terminalHighlight) {
64 highlight = text => terminalHighlight(text)
65 }
66 }
67
68 let lines = css.split(/\r?\n/)
69 let start = Math.max(this.line - 3, 0)
70 let end = Math.min(this.line + 2, lines.length)
71 let maxWidth = String(end).length
72
73 return lines
74 .slice(start, end)
75 .map((line, index) => {
76 let number = start + 1 + index
77 let gutter = ' ' + (' ' + number).slice(-maxWidth) + ' | '
78 if (number === this.line) {
79 if (line.length > 160) {
80 let padding = 20
81 let subLineStart = Math.max(0, this.column - padding)
82 let subLineEnd = Math.max(
83 this.column + padding,
84 this.endColumn + padding
85 )
86 let subLine = line.slice(subLineStart, subLineEnd)
87
88 let spacing =
89 aside(gutter.replace(/\d/g, ' ')) +
90 line
91 .slice(0, Math.min(this.column - 1, padding - 1))
92 .replace(/[^\t]/g, ' ')
93
94 return (
95 mark('>') +
96 aside(gutter) +
97 highlight(subLine) +
98 '\n ' +
99 spacing +
100 mark('^')
101 )
102 }
103
104 let spacing =
105 aside(gutter.replace(/\d/g, ' ')) +
106 line.slice(0, this.column - 1).replace(/[^\t]/g, ' ')
107
108 return (
109 mark('>') +
110 aside(gutter) +
111 highlight(line) +
112 '\n ' +
113 spacing +
114 mark('^')
115 )
116 }
117
118 return ' ' + aside(gutter) + highlight(line)
119 })
120 .join('\n')
121 }
122
123 toString() {
124 let code = this.showSourceCode()
125 if (code) {
126 code = '\n\n' + code + '\n'
127 }
128 return this.name + ': ' + this.message + code
129 }
130}
131
132module.exports = CssSyntaxError
133CssSyntaxError.default = CssSyntaxError
Note: See TracBrowser for help on using the repository browser.