From a8be6bb954781017a46fa0764da8b8deb5e5c9c7 Mon Sep 17 00:00:00 2001 From: Brian Vaughn Date: Mon, 17 Jul 2017 09:48:35 -0700 Subject: [PATCH 1/2] Added codemod for unstable_handleError -> componentDidCatch --- README.md | 8 ++++++ .../error-boundaries/class-component.input.js | 15 +++++++++++ .../class-component.output.js | 15 +++++++++++ .../create-class-component.input.js | 18 +++++++++++++ .../create-class-component.output.js | 18 +++++++++++++ .../function-component.input.js | 7 +++++ .../function-component.output.js | 7 +++++ .../error-boundaries/function.input.js | 1 + .../error-boundaries/function.output.js | 1 + .../error-boundaries/variable.input.js | 1 + .../error-boundaries/variable.output.js | 1 + transforms/__tests__/error-boundaries.js | 25 +++++++++++++++++ transforms/error-boundaries.js | 27 +++++++++++++++++++ 13 files changed, 144 insertions(+) create mode 100644 transforms/__testfixtures__/error-boundaries/class-component.input.js create mode 100644 transforms/__testfixtures__/error-boundaries/class-component.output.js create mode 100644 transforms/__testfixtures__/error-boundaries/create-class-component.input.js create mode 100644 transforms/__testfixtures__/error-boundaries/create-class-component.output.js create mode 100644 transforms/__testfixtures__/error-boundaries/function-component.input.js create mode 100644 transforms/__testfixtures__/error-boundaries/function-component.output.js create mode 100644 transforms/__testfixtures__/error-boundaries/function.input.js create mode 100644 transforms/__testfixtures__/error-boundaries/function.output.js create mode 100644 transforms/__testfixtures__/error-boundaries/variable.input.js create mode 100644 transforms/__testfixtures__/error-boundaries/variable.output.js create mode 100644 transforms/__tests__/error-boundaries.js create mode 100644 transforms/error-boundaries.js diff --git a/README.md b/README.md index 2ce3b184..997f9433 100644 --- a/README.md +++ b/README.md @@ -24,6 +24,14 @@ Converts calls to `React.createElement` into JSX elements. jscodeshift -t react-codemod/transforms/create-element-to-jsx.js ``` +#### `error-boundaries` + +Renames the experimental `unstable_handleError` lifecycle hook to `componentDidCatch`. + +```sh +jscodeshift -t react-codemod/transforms/error-boundaries.js +``` + #### `findDOMNode` Updates `this.getDOMNode()` or `this.refs.foo.getDOMNode()` calls inside of diff --git a/transforms/__testfixtures__/error-boundaries/class-component.input.js b/transforms/__testfixtures__/error-boundaries/class-component.input.js new file mode 100644 index 00000000..15ae076d --- /dev/null +++ b/transforms/__testfixtures__/error-boundaries/class-component.input.js @@ -0,0 +1,15 @@ +import React from "react"; + +export class ComponentOne extends React.Component { + unstable_handleError(error) {} + render() { + return
; + } +} + +export class ComponentTwo extends React.Component { + unstable_handleError = error => {}; + render() { + return
; + } +} diff --git a/transforms/__testfixtures__/error-boundaries/class-component.output.js b/transforms/__testfixtures__/error-boundaries/class-component.output.js new file mode 100644 index 00000000..f097836d --- /dev/null +++ b/transforms/__testfixtures__/error-boundaries/class-component.output.js @@ -0,0 +1,15 @@ +import React from "react"; + +export class ComponentOne extends React.Component { + componentDidCatch(error) {} + render() { + return
; + } +} + +export class ComponentTwo extends React.Component { + componentDidCatch = error => {}; + render() { + return
; + } +} diff --git a/transforms/__testfixtures__/error-boundaries/create-class-component.input.js b/transforms/__testfixtures__/error-boundaries/create-class-component.input.js new file mode 100644 index 00000000..e83da59e --- /dev/null +++ b/transforms/__testfixtures__/error-boundaries/create-class-component.input.js @@ -0,0 +1,18 @@ +var React = require("react"); +var createClass = require("create-react-class"); + +var ComponentOne = createClass({ + render: function() { + return
; + }, + unstable_handleError: function(error) {} +}); + +var ComponentTwo = createClass({ + render() { + return
; + }, + unstable_handleError(error) {} +}); + +module.exports = { ComponentOne, ComponentTwo }; diff --git a/transforms/__testfixtures__/error-boundaries/create-class-component.output.js b/transforms/__testfixtures__/error-boundaries/create-class-component.output.js new file mode 100644 index 00000000..4e0261e9 --- /dev/null +++ b/transforms/__testfixtures__/error-boundaries/create-class-component.output.js @@ -0,0 +1,18 @@ +var React = require("react"); +var createClass = require("create-react-class"); + +var ComponentOne = createClass({ + render: function() { + return
; + }, + componentDidCatch: function(error) {} +}); + +var ComponentTwo = createClass({ + render() { + return
; + }, + componentDidCatch(error) {} +}); + +module.exports = { ComponentOne, ComponentTwo }; diff --git a/transforms/__testfixtures__/error-boundaries/function-component.input.js b/transforms/__testfixtures__/error-boundaries/function-component.input.js new file mode 100644 index 00000000..6583f92e --- /dev/null +++ b/transforms/__testfixtures__/error-boundaries/function-component.input.js @@ -0,0 +1,7 @@ +import React from "react"; + +function ComponentOne() { + return
; +} + +ComponentOne.unstable_handleError = function(error) {}; diff --git a/transforms/__testfixtures__/error-boundaries/function-component.output.js b/transforms/__testfixtures__/error-boundaries/function-component.output.js new file mode 100644 index 00000000..6583f92e --- /dev/null +++ b/transforms/__testfixtures__/error-boundaries/function-component.output.js @@ -0,0 +1,7 @@ +import React from "react"; + +function ComponentOne() { + return
; +} + +ComponentOne.unstable_handleError = function(error) {}; diff --git a/transforms/__testfixtures__/error-boundaries/function.input.js b/transforms/__testfixtures__/error-boundaries/function.input.js new file mode 100644 index 00000000..dbbda033 --- /dev/null +++ b/transforms/__testfixtures__/error-boundaries/function.input.js @@ -0,0 +1 @@ +function unstable_handleError(error) {} diff --git a/transforms/__testfixtures__/error-boundaries/function.output.js b/transforms/__testfixtures__/error-boundaries/function.output.js new file mode 100644 index 00000000..dbbda033 --- /dev/null +++ b/transforms/__testfixtures__/error-boundaries/function.output.js @@ -0,0 +1 @@ +function unstable_handleError(error) {} diff --git a/transforms/__testfixtures__/error-boundaries/variable.input.js b/transforms/__testfixtures__/error-boundaries/variable.input.js new file mode 100644 index 00000000..99ac0f5a --- /dev/null +++ b/transforms/__testfixtures__/error-boundaries/variable.input.js @@ -0,0 +1 @@ +var unstable_handleError = "Who am I?"; diff --git a/transforms/__testfixtures__/error-boundaries/variable.output.js b/transforms/__testfixtures__/error-boundaries/variable.output.js new file mode 100644 index 00000000..99ac0f5a --- /dev/null +++ b/transforms/__testfixtures__/error-boundaries/variable.output.js @@ -0,0 +1 @@ +var unstable_handleError = "Who am I?"; diff --git a/transforms/__tests__/error-boundaries.js b/transforms/__tests__/error-boundaries.js new file mode 100644 index 00000000..7b8da7a3 --- /dev/null +++ b/transforms/__tests__/error-boundaries.js @@ -0,0 +1,25 @@ +/** + * 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", + "function-component", + "function", + "variable" +]; + +const defineTest = require("jscodeshift/dist/testUtils").defineTest; + +tests.forEach(test => { + defineTest(__dirname, "error-boundaries", null, `error-boundaries/${test}`); +}); diff --git a/transforms/error-boundaries.js b/transforms/error-boundaries.js new file mode 100644 index 00000000..8742e571 --- /dev/null +++ b/transforms/error-boundaries.js @@ -0,0 +1,27 @@ +function belongsToClassExpression(path) { + return ( + path.parentPath.value.type === "MethodDefinition" || + path.parentPath.value.type === "ClassProperty" + ); +} + +function belongsToObjectExpression(path) { + return ( + path.parentPath.parentPath.parentPath.value.type === "ObjectExpression" + ); +} + +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") { + if (belongsToClassExpression(path) || belongsToObjectExpression(path)) { + j(path).replaceWith(j.identifier("componentDidCatch")); + } + } + }) + .toSource(); +}; From 411750c820f84116829a880dd0bd1ce600dbbacb Mon Sep 17 00:00:00 2001 From: Brian Vaughn Date: Mon, 17 Jul 2017 09:55:38 -0700 Subject: [PATCH 2/2] Relaxed codemod to rename all identifiers matching 'unstable_handleError' --- .../function-component.input.js | 7 ------- .../function-component.output.js | 7 ------- .../error-boundaries/function.input.js | 1 - .../error-boundaries/function.output.js | 1 - .../error-boundaries/variable.input.js | 1 - .../error-boundaries/variable.output.js | 1 - transforms/__tests__/error-boundaries.js | 8 +------- transforms/error-boundaries.js | 17 +---------------- 8 files changed, 2 insertions(+), 41 deletions(-) delete mode 100644 transforms/__testfixtures__/error-boundaries/function-component.input.js delete mode 100644 transforms/__testfixtures__/error-boundaries/function-component.output.js delete mode 100644 transforms/__testfixtures__/error-boundaries/function.input.js delete mode 100644 transforms/__testfixtures__/error-boundaries/function.output.js delete mode 100644 transforms/__testfixtures__/error-boundaries/variable.input.js delete mode 100644 transforms/__testfixtures__/error-boundaries/variable.output.js diff --git a/transforms/__testfixtures__/error-boundaries/function-component.input.js b/transforms/__testfixtures__/error-boundaries/function-component.input.js deleted file mode 100644 index 6583f92e..00000000 --- a/transforms/__testfixtures__/error-boundaries/function-component.input.js +++ /dev/null @@ -1,7 +0,0 @@ -import React from "react"; - -function ComponentOne() { - return
; -} - -ComponentOne.unstable_handleError = function(error) {}; diff --git a/transforms/__testfixtures__/error-boundaries/function-component.output.js b/transforms/__testfixtures__/error-boundaries/function-component.output.js deleted file mode 100644 index 6583f92e..00000000 --- a/transforms/__testfixtures__/error-boundaries/function-component.output.js +++ /dev/null @@ -1,7 +0,0 @@ -import React from "react"; - -function ComponentOne() { - return
; -} - -ComponentOne.unstable_handleError = function(error) {}; diff --git a/transforms/__testfixtures__/error-boundaries/function.input.js b/transforms/__testfixtures__/error-boundaries/function.input.js deleted file mode 100644 index dbbda033..00000000 --- a/transforms/__testfixtures__/error-boundaries/function.input.js +++ /dev/null @@ -1 +0,0 @@ -function unstable_handleError(error) {} diff --git a/transforms/__testfixtures__/error-boundaries/function.output.js b/transforms/__testfixtures__/error-boundaries/function.output.js deleted file mode 100644 index dbbda033..00000000 --- a/transforms/__testfixtures__/error-boundaries/function.output.js +++ /dev/null @@ -1 +0,0 @@ -function unstable_handleError(error) {} diff --git a/transforms/__testfixtures__/error-boundaries/variable.input.js b/transforms/__testfixtures__/error-boundaries/variable.input.js deleted file mode 100644 index 99ac0f5a..00000000 --- a/transforms/__testfixtures__/error-boundaries/variable.input.js +++ /dev/null @@ -1 +0,0 @@ -var unstable_handleError = "Who am I?"; diff --git a/transforms/__testfixtures__/error-boundaries/variable.output.js b/transforms/__testfixtures__/error-boundaries/variable.output.js deleted file mode 100644 index 99ac0f5a..00000000 --- a/transforms/__testfixtures__/error-boundaries/variable.output.js +++ /dev/null @@ -1 +0,0 @@ -var unstable_handleError = "Who am I?"; diff --git a/transforms/__tests__/error-boundaries.js b/transforms/__tests__/error-boundaries.js index 7b8da7a3..49470560 100644 --- a/transforms/__tests__/error-boundaries.js +++ b/transforms/__tests__/error-boundaries.js @@ -10,13 +10,7 @@ "use strict"; -const tests = [ - "class-component", - "create-class-component", - "function-component", - "function", - "variable" -]; +const tests = ["class-component", "create-class-component"]; const defineTest = require("jscodeshift/dist/testUtils").defineTest; diff --git a/transforms/error-boundaries.js b/transforms/error-boundaries.js index 8742e571..c9e56283 100644 --- a/transforms/error-boundaries.js +++ b/transforms/error-boundaries.js @@ -1,16 +1,3 @@ -function belongsToClassExpression(path) { - return ( - path.parentPath.value.type === "MethodDefinition" || - path.parentPath.value.type === "ClassProperty" - ); -} - -function belongsToObjectExpression(path) { - return ( - path.parentPath.parentPath.parentPath.value.type === "ObjectExpression" - ); -} - module.exports = function(file, api, options) { const j = api.jscodeshift; @@ -18,9 +5,7 @@ module.exports = function(file, api, options) { .find(j.Identifier) .forEach(path => { if (path.node.name === "unstable_handleError") { - if (belongsToClassExpression(path) || belongsToObjectExpression(path)) { - j(path).replaceWith(j.identifier("componentDidCatch")); - } + j(path).replaceWith(j.identifier("componentDidCatch")); } }) .toSource();