Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix removing whitespacing in dev overlay #28277

Merged
merged 10 commits into from Aug 8, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Expand Up @@ -14,7 +14,11 @@ export const CodeFrame: React.FC<CodeFrameProps> = function CodeFrame({
const formattedFrame = React.useMemo<string>(() => {
const lines = codeFrame.split(/\r?\n/g)
const prefixLength = lines
.map((line) => /^>? +\d+ +\| ( *)/.exec(stripAnsi(line)))
.map((line) =>
/^>? +\d+ +\| [ ]+/.exec(stripAnsi(line)) === null
? null
: /^>? +\d+ +\| ( *)/.exec(stripAnsi(line))
)
.filter(Boolean)
.map((v) => v!.pop()!)
.reduce((c, n) => (isNaN(c) ? n.length : Math.min(c, n.length)), NaN)
Expand Down
30 changes: 30 additions & 0 deletions test/development/acceptance/ReactRefreshLogBox.test.ts
Expand Up @@ -15,6 +15,36 @@ describe('ReactRefreshLogBox', () => {
})
afterAll(() => next.destroy())

test('should strip whitespace correctly with newline', async () => {
const { session, cleanup } = await sandbox(next)

await session.patch(
'index.js',
`
export default function Page() {
return (
<>

<p>index page</p>

<a onClick={() => {
throw new Error('idk')
}}>
click me
</a>
</>
)
}
`
)
await session.evaluate(() => document.querySelector('a').click())

expect(await session.hasRedbox(true)).toBe(true)
expect(await session.getRedboxSource()).toMatchSnapshot()

await cleanup()
})

test('logbox: can recover from a syntax error without losing state', async () => {
const { session, cleanup } = await sandbox(next)

Expand Down
Expand Up @@ -104,6 +104,18 @@ exports[`ReactRefreshLogBox render error not shown right after syntax error 1`]
9 | }"
`;

exports[`ReactRefreshLogBox should strip whitespace correctly with newline 1`] = `
"index.js (9:34) @ onClick

7 |
8 | <a onClick={() => {
> 9 | throw new Error('idk')
| ^
10 | }}>
11 | click me
12 | </a>"
`;

exports[`ReactRefreshLogBox stuck error 1`] = `
"Foo.js (4:10) @ Foo

Expand Down
20 changes: 10 additions & 10 deletions test/integration/config-devtool-dev/test/index.test.js
Expand Up @@ -37,16 +37,16 @@ describe('devtool set in development mode in next config', () => {
// TODO: add win32 snapshot
} else {
expect(await getRedboxSource(browser)).toMatchInlineSnapshot(`
"pages/index.js (5:10) @ eval

3 | export default function Index(props) {
4 | useEffect(() => {
> 5 | throw new Error('this should render')
| ^
6 | }, [])
7 | return <div>Index Page</div>
8 | }"
`)
"pages/index.js (5:10) @ eval

3 | export default function Index(props) {
4 | useEffect(() => {
> 5 | throw new Error('this should render')
| ^
6 | }, [])
7 | return <div>Index Page</div>
8 | }"
`)
}
await browser.close()

Expand Down