Skip to content

Commit 74ca2c2

Browse files
committed
Instead of showing location of debug call, print full code frame
1 parent 411dbe4 commit 74ca2c2

File tree

3 files changed

+50
-15
lines changed

3 files changed

+50
-15
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,11 +38,11 @@
3838
"types"
3939
],
4040
"dependencies": {
41+
"@babel/code-frame": "^7.10.4",
4142
"@babel/runtime": "^7.10.3",
4243
"@types/aria-query": "^4.2.0",
4344
"aria-query": "^4.2.2",
4445
"dom-accessibility-api": "^0.5.0",
45-
"chalk": "^4.1.0",
4646
"pretty-format": "^25.5.0"
4747
},
4848
"devDependencies": {

src/__tests__/get-user-trace.js

Lines changed: 30 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,19 @@
11
import {getUserTrace} from '../get-user-trace'
22

3-
jest.mock('chalk', () => ({
4-
gray: msg => msg,
3+
jest.mock('fs', () => ({
4+
readFileSync: () => `
5+
import { screen } from '@testing-library/dom'
6+
7+
it('renders', () => {
8+
document.body.appendChild(
9+
document.createTextNode('Hello world')
10+
)
11+
screen.debug()
12+
13+
14+
expect(screen.getByText('Hello world')).toBeInTheDocument()
15+
})
16+
`,
517
}))
618

719
let globalErrorMock
@@ -18,19 +30,23 @@ afterEach(() => {
1830
test('it returns only client error when frames from node_modules are first', () => {
1931
const stack = `Error: Kaboom
2032
at Object.<anonymous> (/home/john/projects/projects/sample-error/node_modules/@es2050/console/build/index.js:4:10)
21-
at somethingWrong (/home/john/projects/sample-error/error-example.js:2:13)
33+
at somethingWrong (/home/john/projects/sample-error/error-example.js:8:7)
2234
`
2335
globalErrorMock.mockImplementationOnce(() => ({stack}))
2436
const userTrace = getUserTrace(stack)
25-
expect(userTrace).toEqual(
26-
'/home/john/projects/sample-error/error-example.js:2:13\n',
27-
)
37+
expect(userTrace).toMatchInlineSnapshot(`
38+
" 6 | document.createTextNode('Hello world')
39+
7 | )
40+
> 8 | screen.debug()
41+
| ^
42+
"
43+
`)
2844
})
2945

3046
test('it returns only client error when node frames are present afterwards', () => {
3147
const stack = `Error: Kaboom
3248
at Object.<anonymous> (/home/john/projects/projects/sample-error/node_modules/@es2050/console/build/index.js:4:10)
33-
at somethingWrong (/home/john/projects/sample-error/error-example.js:2:13)
49+
at somethingWrong (/home/john/projects/sample-error/error-example.js:8:7)
3450
at Object.<anonymous> (/home/user/Documents/projects/sample-error/error-example.js:14:1)
3551
at Module._compile (internal/modules/cjs/loader.js:1151:30)
3652
at Object.Module._extensions..js (internal/modules/cjs/loader.js:1171:10)
@@ -41,7 +57,11 @@ test('it returns only client error when node frames are present afterwards', ()
4157
`
4258
globalErrorMock.mockImplementationOnce(() => ({stack}))
4359
const userTrace = getUserTrace()
44-
expect(userTrace).toEqual(
45-
'/home/john/projects/sample-error/error-example.js:2:13\n',
46-
)
60+
expect(userTrace).toMatchInlineSnapshot(`
61+
" 6 | document.createTextNode('Hello world')
62+
7 | )
63+
> 8 | screen.debug()
64+
| ^
65+
"
66+
`)
4767
})

src/get-user-trace.js

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,26 @@
1-
import chalk from 'chalk'
1+
import fs from 'fs'
2+
import { codeFrameColumns } from '@babel/code-frame';
23

34
// Frame has the form "at myMethod (location/to/my/file.js:10:2)"
4-
function getFrameLocation(frame) {
5+
function getCodeFrame(frame) {
56
const locationStart = frame.indexOf('(') + 1
67
const locationEnd = frame.indexOf(')')
8+
const frameLocation = frame.slice(locationStart, locationEnd)
79

8-
return frame.slice(locationStart, locationEnd)
10+
const frameLocationElements = frameLocation.split(":")
11+
const [filename, line, column] = [frameLocationElements[0], parseInt(frameLocationElements[1], 10), parseInt(frameLocationElements[2], 10)]
12+
let rawFileContents = ""
13+
try {
14+
rawFileContents = fs.readFileSync(filename, 'utf-8')
15+
} catch {
16+
console.warn(`Couldn't read file ${filename} for displaying the code frame`)
17+
}
18+
return codeFrameColumns(rawFileContents, {
19+
start: { line, column },
20+
}, {
21+
highlightCode: true,
22+
linesBelow: 0,
23+
})
924
}
1025

1126
function getUserTrace() {
@@ -15,7 +30,7 @@ function getUserTrace() {
1530
.slice(1) // Remove first line which has the form "Error: TypeError"
1631
.find(frame => !frame.includes('node_modules/')) // Ignore frames from 3rd party libraries
1732

18-
return chalk.gray(`${getFrameLocation(firstClientCodeFrame)}\n`)
33+
return `${getCodeFrame(firstClientCodeFrame)}\n`
1934
}
2035

2136
export {getUserTrace}

0 commit comments

Comments
 (0)