Skip to content
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,14 @@ Converts calls to `React.createElement` into JSX elements.
jscodeshift -t react-codemod/transforms/create-element-to-jsx.js <path>
```

#### `error-boundaries`

Renames the experimental `unstable_handleError` lifecycle hook to `componentDidCatch`.

```sh
jscodeshift -t react-codemod/transforms/error-boundaries.js <path>
```

#### `findDOMNode`

Updates `this.getDOMNode()` or `this.refs.foo.getDOMNode()` calls inside of
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import React from "react";

export class ComponentOne extends React.Component {
unstable_handleError(error) {}
render() {
return <div />;
}
}

export class ComponentTwo extends React.Component {
unstable_handleError = error => {};
render() {
return <div />;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import React from "react";

export class ComponentOne extends React.Component {
componentDidCatch(error) {}
render() {
return <div />;
}
}

export class ComponentTwo extends React.Component {
componentDidCatch = error => {};
render() {
return <div />;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
var React = require("react");
var createClass = require("create-react-class");

var ComponentOne = createClass({
render: function() {
return <div />;
},
unstable_handleError: function(error) {}
});

var ComponentTwo = createClass({
render() {
return <div />;
},
unstable_handleError(error) {}
});

module.exports = { ComponentOne, ComponentTwo };
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
var React = require("react");
var createClass = require("create-react-class");

var ComponentOne = createClass({
render: function() {
return <div />;
},
componentDidCatch: function(error) {}
});

var ComponentTwo = createClass({
render() {
return <div />;
},
componentDidCatch(error) {}
});

module.exports = { ComponentOne, ComponentTwo };
19 changes: 19 additions & 0 deletions transforms/__tests__/error-boundaries.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
/**
* Copyright 2013-2015, Facebook, Inc.
* All rights reserved.
*
* This source code is licensed under the BSD-style license found in the
* LICENSE file in the root directory of this source tree. An additional grant
* of patent rights can be found in the PATENTS file in the same directory.
*
*/

"use strict";

const tests = ["class-component", "create-class-component"];

const defineTest = require("jscodeshift/dist/testUtils").defineTest;

tests.forEach(test => {
defineTest(__dirname, "error-boundaries", null, `error-boundaries/${test}`);
});
12 changes: 12 additions & 0 deletions transforms/error-boundaries.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
module.exports = function(file, api, options) {
const j = api.jscodeshift;

return j(file.source)
.find(j.Identifier)
.forEach(path => {
if (path.node.name === "unstable_handleError") {
j(path).replaceWith(j.identifier("componentDidCatch"));
}
})
.toSource();
};