Skip to content

Commit 66570a0

Browse files
committed
react error boundaries
1 parent 109f8a6 commit 66570a0

File tree

3 files changed

+110
-0
lines changed

3 files changed

+110
-0
lines changed
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
# react#error-boundaries
2+
3+
Renames the experimental `unstable_handleError` lifecycle hook to `componentDidCatch`.
4+
5+
```js
6+
/* INPUT */
7+
import React from "react";
8+
9+
export class ComponentOne extends React.Component {
10+
unstable_handleError(error) {}
11+
render() {
12+
return <div />;
13+
}
14+
}
15+
16+
/* OUTPUT */
17+
import React from "react";
18+
19+
export class ComponentOne extends React.Component {
20+
componentDidCatch(error) {}
21+
render() {
22+
return <div />;
23+
}
24+
}
25+
```
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
import { applyTransform } from '@codeshift/test-utils';
2+
import * as transformer from './transform';
3+
4+
describe('react#error-boundaries transform', () => {
5+
it('should transform class to error boundary', async () => {
6+
const result = await applyTransform(
7+
transformer,
8+
`
9+
import React from "react";
10+
11+
export class ComponentOne extends React.Component {
12+
unstable_handleError(error) {}
13+
render() {
14+
return <div />;
15+
}
16+
}
17+
18+
export class ComponentTwo extends React.Component {
19+
unstable_handleError = error => {};
20+
render() {
21+
return <div />;
22+
}
23+
}
24+
`,
25+
{ parser: 'tsx' },
26+
);
27+
28+
expect(result).toMatchInlineSnapshot(`
29+
import React from "react"; export class ComponentOne extends React.Component
30+
{ componentDidCatch(error) {} render() { return
31+
<div />; } } export class ComponentTwo extends React.Component { componentDidCatch
32+
= error => {}; render() { return
33+
<div />; } }
34+
`);
35+
});
36+
37+
it('should transform createClass to error boundary', async () => {
38+
const result = await applyTransform(
39+
transformer,
40+
`
41+
var React = require("react");
42+
var createClass = require("create-react-class");
43+
44+
var ComponentOne = createClass({
45+
render: function() {
46+
return <div />;
47+
},
48+
unstable_handleError: function(error) {}
49+
});
50+
51+
var ComponentTwo = createClass({
52+
render() {
53+
return <div />;
54+
},
55+
unstable_handleError(error) {}
56+
});
57+
`,
58+
{ parser: 'tsx' },
59+
);
60+
61+
expect(result).toMatchInlineSnapshot(`
62+
var React = require("react"); var createClass = require("create-react-class");
63+
var ComponentOne = createClass({ render: function() { return
64+
<div />; }, componentDidCatch: function(error) {} }); var ComponentTwo = createClass({
65+
render() { return
66+
<div />; }, componentDidCatch(error) {} });
67+
`);
68+
});
69+
});
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import { API, FileInfo, Options } from 'jscodeshift';
2+
3+
export default function transformer(
4+
file: FileInfo,
5+
{ jscodeshift: j }: API,
6+
options: Options,
7+
) {
8+
return j(file.source)
9+
.find(j.Identifier)
10+
.forEach(path => {
11+
if (path.node.name === 'unstable_handleError') {
12+
j(path).replaceWith(j.identifier('componentDidCatch'));
13+
}
14+
})
15+
.toSource(options.printOptions);
16+
}

0 commit comments

Comments
 (0)