Skip to content

Commit 98b61ba

Browse files
committed
Replace chalk to nanocolors
1 parent 67e3d7b commit 98b61ba

File tree

6 files changed

+120
-96
lines changed

6 files changed

+120
-96
lines changed

lib/css-syntax-error.es6

Lines changed: 20 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
import supportsColor from 'supports-color'
2-
import chalk from 'chalk'
1+
import nanocolors from 'nanocolors'
32

43
import terminalHighlight from './terminal-highlight'
54

@@ -175,7 +174,7 @@ class CssSyntaxError extends Error {
175174

176175
let css = this.source
177176
if (terminalHighlight) {
178-
if (typeof color === 'undefined') color = supportsColor.stdout
177+
if (typeof color === 'undefined') color = nanocolors.isColorSupported
179178
if (color) css = terminalHighlight(css)
180179
}
181180

@@ -186,28 +185,32 @@ class CssSyntaxError extends Error {
186185
let maxWidth = String(end).length
187186

188187
function mark (text) {
189-
if (color && chalk.red) {
190-
return chalk.red.bold(text)
188+
if (color && nanocolors.red) {
189+
return nanocolors.red(nanocolors.bold(text))
191190
}
192191
return text
193192
}
194193
function aside (text) {
195-
if (color && chalk.gray) {
196-
return chalk.gray(text)
194+
if (color && nanocolors.gray) {
195+
return nanocolors.gray(text)
197196
}
198197
return text
199198
}
200199

201-
return lines.slice(start, end).map((line, index) => {
202-
let number = start + 1 + index
203-
let gutter = ' ' + (' ' + number).slice(-maxWidth) + ' | '
204-
if (number === this.line) {
205-
let spacing = aside(gutter.replace(/\d/g, ' ')) +
206-
line.slice(0, this.column - 1).replace(/[^\t]/g, ' ')
207-
return mark('>') + aside(gutter) + line + '\n ' + spacing + mark('^')
208-
}
209-
return ' ' + aside(gutter) + line
210-
}).join('\n')
200+
return lines
201+
.slice(start, end)
202+
.map((line, index) => {
203+
let number = start + 1 + index
204+
let gutter = ' ' + (' ' + number).slice(-maxWidth) + ' | '
205+
if (number === this.line) {
206+
let spacing =
207+
aside(gutter.replace(/\d/g, ' ')) +
208+
line.slice(0, this.column - 1).replace(/[^\t]/g, ' ')
209+
return mark('>') + aside(gutter) + line + '\n ' + spacing + mark('^')
210+
}
211+
return ' ' + aside(gutter) + line
212+
})
213+
.join('\n')
211214
}
212215

213216
/**

lib/terminal-highlight.es6

Lines changed: 18 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,24 @@
1-
import chalk from 'chalk'
1+
import nanocolors from 'nanocolors'
22

33
import tokenizer from './tokenize'
44
import Input from './input'
55

66
const HIGHLIGHT_THEME = {
7-
'brackets': chalk.cyan,
8-
'at-word': chalk.cyan,
9-
'comment': chalk.gray,
10-
'string': chalk.green,
11-
'class': chalk.yellow,
12-
'call': chalk.cyan,
13-
'hash': chalk.magenta,
14-
'(': chalk.cyan,
15-
')': chalk.cyan,
16-
'{': chalk.yellow,
17-
'}': chalk.yellow,
18-
'[': chalk.yellow,
19-
']': chalk.yellow,
20-
':': chalk.yellow,
21-
';': chalk.yellow
7+
brackets: nanocolors.cyan,
8+
'at-word': nanocolors.cyan,
9+
comment: nanocolors.gray,
10+
string: nanocolors.green,
11+
class: nanocolors.yellow,
12+
call: nanocolors.cyan,
13+
hash: nanocolors.magenta,
14+
'(': nanocolors.cyan,
15+
')': nanocolors.cyan,
16+
'{': nanocolors.yellow,
17+
'}': nanocolors.yellow,
18+
'[': nanocolors.yellow,
19+
']': nanocolors.yellow,
20+
':': nanocolors.yellow,
21+
';': nanocolors.yellow
2222
}
2323

2424
function getTokenType ([type, value], processor) {
@@ -47,7 +47,8 @@ function terminalHighlight (css) {
4747
let token = processor.nextToken()
4848
let color = HIGHLIGHT_THEME[getTokenType(token, processor)]
4949
if (color) {
50-
result += token[1].split(/\r?\n/)
50+
result += token[1]
51+
.split(/\r?\n/)
5152
.map(i => color(i))
5253
.join('\n')
5354
} else {

package.json

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,8 @@
2929
"homepage": "https://postcss.org/",
3030
"repository": "postcss/postcss",
3131
"dependencies": {
32-
"chalk": "^2.4.2",
33-
"source-map": "^0.6.1",
34-
"supports-color": "^6.1.0"
32+
"nanocolors": "^0.2.2",
33+
"source-map": "^0.6.1"
3534
},
3635
"devDependencies": {
3736
"@babel/core": "^7.11.6",
@@ -82,8 +81,6 @@
8281
},
8382
"browser": {
8483
"./lib/terminal-highlight": false,
85-
"supports-color": false,
86-
"chalk": false,
8784
"fs": false
8885
},
8986
"browserslist": [

test/browser.test.js

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
1-
jest.doMock('fs', () => ({ }))
2-
jest.doMock('chalk', () => ({ }))
3-
jest.doMock('supports-color', () => ({ }))
1+
jest.doMock('fs', () => ({}))
2+
jest.doMock('nanocolors', () => require('nanocolors/index.browser.cjs'))
43

54
let postcss = require('..')
65

7-
it('shows code without chalk', () => {
6+
it('shows code', () => {
87
let error
98
try {
109
postcss.parse('a{')
@@ -15,14 +14,16 @@ it('shows code without chalk', () => {
1514
throw e
1615
}
1716
}
18-
expect(error.showSourceCode(true)).toEqual('> 1 | a{\n' +
19-
' | ^')
17+
expect(error.showSourceCode(true)).toEqual('> 1 | a{\n' + ' | ^')
2018
})
2119

2220
it('generates source map without fs', () => {
23-
expect(postcss([() => true]).process('a{}', { from: 'a.css', map: true }).css)
24-
.toEqual('a{}\n/*# sourceMappingURL=data:application/json;base64,' +
25-
'eyJ2ZXJzaW9uIjozLCJzb3VyY2VzIjpbImEuY3NzIl0sIm5hbWVzIjpbXSw' +
26-
'ibWFwcGluZ3MiOiJBQUFBLEVBQUUiLCJmaWxlIjoiYS5jc3MiLCJzb3VyY2' +
27-
'VzQ29udGVudCI6WyJhe30iXX0= */')
21+
expect(
22+
postcss([() => true]).process('a{}', { from: 'a.css', map: true }).css
23+
).toEqual(
24+
'a{}\n/*# sourceMappingURL=data:application/json;base64,' +
25+
'eyJ2ZXJzaW9uIjozLCJzb3VyY2VzIjpbImEuY3NzIl0sIm5hbWVzIjpbXSw' +
26+
'ibWFwcGluZ3MiOiJBQUFBLEVBQUUiLCJmaWxlIjoiYS5jc3MiLCJzb3VyY2' +
27+
'VzQ29udGVudCI6WyJhe30iXX0= */'
28+
)
2829
})

test/css-syntax-error.test.js

Lines changed: 63 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1+
let nanocolors = require('nanocolors')
12
let stripAnsi = require('strip-ansi')
23
let Concat = require('concat-with-sourcemaps')
3-
let chalk = require('chalk')
44
let path = require('path')
55

66
let CssSyntaxError = require('../lib/css-syntax-error')
@@ -39,65 +39,77 @@ it('saves source', () => {
3939
})
4040

4141
it('has stack trace', () => {
42-
expect(parseError('a {\n content: "\n}').stack)
43-
.toMatch(/css-syntax-error\.test\.js/)
42+
expect(parseError('a {\n content: "\n}').stack).toMatch(
43+
/css-syntax-error\.test\.js/
44+
)
4445
})
4546

4647
it('highlights broken line with colors', () => {
47-
let c = chalk
48+
let c = nanocolors
4849
expect(parseError('#a .b {').showSourceCode(true)).toEqual(
49-
c.red.bold('>') + c.gray(' 1 | ') +
50-
c.magenta('#a') + ' ' + c.yellow('.b') + ' ' +
51-
c.yellow('{') + '\n ' +
52-
c.gray(' | ') + c.red.bold('^'))
50+
c.red(c.bold('>')) +
51+
c.gray(' 1 | ') +
52+
c.magenta('#a') +
53+
' ' +
54+
c.yellow('.b') +
55+
' ' +
56+
c.yellow('{') +
57+
'\n ' +
58+
c.gray(' | ') +
59+
c.red(c.bold('^'))
60+
)
5361
})
5462

5563
it('highlights broken line', () => {
56-
expect(parseError('a {\n content: "\n}').showSourceCode(false))
57-
.toEqual(' 1 | a {\n' +
58-
'> 2 | content: "\n' +
59-
' | ^\n' +
60-
' 3 | }')
64+
expect(parseError('a {\n content: "\n}').showSourceCode(false)).toEqual(
65+
' 1 | a {\n' + '> 2 | content: "\n' + ' | ^\n' + ' 3 | }'
66+
)
6167
})
6268

6369
it('highlights broken line, when indented with tabs', () => {
64-
expect(parseError('a {\n\t \t content:\t"\n}').showSourceCode(false))
65-
.toEqual(' 1 | a {\n' +
66-
'> 2 | \t \t content:\t"\n' +
67-
' | \t \t \t^\n' +
68-
' 3 | }')
70+
expect(
71+
parseError('a {\n\t \t content:\t"\n}').showSourceCode(false)
72+
).toEqual(
73+
' 1 | a {\n' +
74+
'> 2 | \t \t content:\t"\n' +
75+
' | \t \t \t^\n' +
76+
' 3 | }'
77+
)
6978
})
7079

7180
it('highlights small code example', () => {
72-
expect(parseError('a {').showSourceCode(false))
73-
.toEqual('> 1 | a {\n' +
74-
' | ^')
81+
expect(parseError('a {').showSourceCode(false)).toEqual(
82+
'> 1 | a {\n' + ' | ^'
83+
)
7584
})
7685

7786
it('add leading space for line numbers', () => {
7887
let css = '\n\n\n\n\n\n\na {\n content: "\n}\n\n\n'
79-
expect(parseError(css).showSourceCode(false))
80-
.toEqual(' 7 | \n' +
81-
' 8 | a {\n' +
82-
'> 9 | content: "\n' +
83-
' | ^\n' +
84-
' 10 | }\n' +
85-
' 11 | ')
88+
expect(parseError(css).showSourceCode(false)).toEqual(
89+
' 7 | \n' +
90+
' 8 | a {\n' +
91+
'> 9 | content: "\n' +
92+
' | ^\n' +
93+
' 10 | }\n' +
94+
' 11 | '
95+
)
8696
})
8797

8898
it('prints with highlight', () => {
89-
expect(stripAnsi(parseError('a {').toString()))
90-
.toEqual('CssSyntaxError: <css input>:1:1: Unclosed block\n' +
91-
'\n' +
92-
'> 1 | a {\n' +
93-
' | ^\n')
99+
expect(stripAnsi(parseError('a {').toString())).toEqual(
100+
'CssSyntaxError: <css input>:1:1: Unclosed block\n' +
101+
'\n' +
102+
'> 1 | a {\n' +
103+
' | ^\n'
104+
)
94105
})
95106

96107
it('misses highlights without source content', () => {
97108
let error = parseError('a {')
98109
error.source = null
99-
expect(error.toString())
100-
.toEqual('CssSyntaxError: <css input>:1:1: Unclosed block')
110+
expect(error.toString()).toEqual(
111+
'CssSyntaxError: <css input>:1:1: Unclosed block'
112+
)
101113
})
102114

103115
it('misses position without source', () => {
@@ -160,8 +172,9 @@ it('does not uses wrong source map', () => {
160172
it('set source plugin', () => {
161173
let error = postcss.parse('a{}').first.error('Error', { plugin: 'PL' })
162174
expect(error.plugin).toEqual('PL')
163-
expect(error.toString())
164-
.toMatch(/^CssSyntaxError: PL: <css input>:1:1: Error/)
175+
expect(error.toString()).toMatch(
176+
/^CssSyntaxError: PL: <css input>:1:1: Error/
177+
)
165178
})
166179

167180
it('set source plugin automatically', () => {
@@ -171,11 +184,13 @@ it('set source plugin automatically', () => {
171184
}
172185
})
173186

174-
return postcss([plugin]).process('a{}').catch(error => {
175-
if (error.name !== 'CssSyntaxError') throw error
176-
expect(error.plugin).toEqual('test-plugin')
177-
expect(error.toString()).toMatch(/test-plugin/)
178-
})
187+
return postcss([plugin])
188+
.process('a{}')
189+
.catch(error => {
190+
if (error.name !== 'CssSyntaxError') throw error
191+
expect(error.plugin).toEqual('test-plugin')
192+
expect(error.toString()).toMatch(/test-plugin/)
193+
})
179194
})
180195

181196
it('set plugin automatically in async', () => {
@@ -187,8 +202,10 @@ it('set plugin automatically in async', () => {
187202
}
188203
})
189204

190-
return postcss([plugin]).process('a{}').catch(error => {
191-
if (error.name !== 'CssSyntaxError') throw error
192-
expect(error.plugin).toEqual('async-plugin')
193-
})
205+
return postcss([plugin])
206+
.process('a{}')
207+
.catch(error => {
208+
if (error.name !== 'CssSyntaxError') throw error
209+
expect(error.plugin).toEqual('async-plugin')
210+
})
194211
})

yarn.lock

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5129,6 +5129,11 @@ nan@^2.9.2:
51295129
resolved "https://registry.yarnpkg.com/nan/-/nan-2.13.2.tgz#f51dc7ae66ba7d5d55e1e6d4d8092e802c9aefe7"
51305130
integrity sha512-TghvYc72wlMGMVMluVo9WRJc0mB8KxxF/gZ4YYFy7V2ZQX9l7rgbPg7vjS9mt6U5HXODVFVI2bOduCzwOMv/lw==
51315131

5132+
nanocolors@^0.2.2:
5133+
version "0.2.2"
5134+
resolved "https://registry.yarnpkg.com/nanocolors/-/nanocolors-0.2.2.tgz#94a4b7e45ecdebdbea7cb0a8349f9447a9677675"
5135+
integrity sha512-t4mwdKizEGLFeZzDbr1r6SjwKYZQEB0Z/trGsdAIsw5EadaR010/zvJ0arkw6vznyTevcMi9BtppprJ5aqLXRg==
5136+
51325137
nanomatch@^1.2.9:
51335138
version "1.2.13"
51345139
resolved "https://registry.yarnpkg.com/nanomatch/-/nanomatch-1.2.13.tgz#b87a8aa4fc0de8fe6be88895b38983ff265bd119"

0 commit comments

Comments
 (0)