-
Notifications
You must be signed in to change notification settings - Fork 284
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
Add codemod for 'React.DOM.div' -> 'React.createElement("div"' #133
Merged
Merged
Changes from 1 commit
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,190 @@ | ||
/** | ||
* 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'; | ||
|
||
module.exports = function(file, api, options) { | ||
const j = api.jscodeshift; | ||
const root = j(file.source); | ||
|
||
let hasModifications; | ||
|
||
const DOMModuleName = 'DOM'; | ||
|
||
const isDOMSpecifier = specifier => ( | ||
specifier.imported && | ||
specifier.imported.name === DOMModuleName | ||
); | ||
|
||
/** | ||
* Replaces 'DOM' with 'createElement' in places where we grab 'DOM' out of | ||
* 'React' with destructuring. | ||
*/ | ||
const replaceDestructuredDOMStatement = (j, root) => { | ||
let hasModifications = false; | ||
//--------- | ||
// First update import statments. eg: | ||
// import { | ||
// DOM, | ||
// foo, | ||
// } from 'react'; | ||
root | ||
.find(j.ImportDeclaration) | ||
.filter(path => ( | ||
path.node.specifiers.filter(isDOMSpecifier).length > 0 && | ||
path.node.source.value === 'react' | ||
)) | ||
.forEach(path => { | ||
hasModifications = true; | ||
|
||
// Replace the DOM key with 'createElement' | ||
path.node.specifiers = path.node.specifiers.map( | ||
specifier => { | ||
if (specifier.imported && specifier.imported.name === DOMModuleName) { | ||
return j.importSpecifier(j.identifier('createElement')); | ||
} else { | ||
return specifier; | ||
} | ||
} | ||
); | ||
}); | ||
|
||
//--------- | ||
// Next update require statments. | ||
// This matches both | ||
// const { | ||
// Component, | ||
// DOM, | ||
// } = React; | ||
// and | ||
// const { | ||
// Component, | ||
// DOM, | ||
// } = require('react'); | ||
root | ||
.find(j.ObjectPattern) | ||
.filter(path => ( | ||
path.parent.node.init && | ||
( | ||
// matches '} = React;' | ||
path.parent.node.init.name === 'React' | ||
|| | ||
( | ||
// matches "} = require('react');" | ||
path.parent.node.init.type === 'CallExpression' && | ||
path.parent.node.init.callee.name === 'require' && | ||
path.parent.node.init.arguments[0].value === 'react' | ||
) | ||
) && | ||
path.node.properties.some(property => { | ||
return property.key.name === DOMModuleName; | ||
}) | ||
)) | ||
.forEach(path => { | ||
hasModifications = true; | ||
|
||
// Replace the DOM key with 'createElement' | ||
path.node.properties = path.node.properties.map((property) => { | ||
if (property.key.name === DOMModuleName) { | ||
return j.identifier('createElement'); | ||
} else { | ||
return property; | ||
} | ||
}); | ||
}); | ||
return hasModifications; | ||
}; | ||
|
||
hasModifications = | ||
replaceDestructuredDOMStatement(j, root) || hasModifications; | ||
|
||
const isDOMIdentifier = path => ( | ||
path.node.name === DOMModuleName && | ||
path.parent.parent.node.type === 'CallExpression' | ||
); | ||
|
||
/** | ||
* Update cases where DOM.div is being called | ||
* eg 'foo = DOM.div('a'...' | ||
* replace with 'foo = createElement('div', 'a'...' | ||
*/ | ||
function replaceDOMReferences(j, root) { | ||
let hasModifications = false; | ||
|
||
root | ||
.find(j.Identifier) | ||
.filter(isDOMIdentifier) | ||
.forEach(path => { | ||
hasModifications = true; | ||
|
||
const DOMargs = path.parent.parent.node.arguments; | ||
const DOMFactoryPath = path.parent.node.property; | ||
const DOMFactoryType = DOMFactoryPath.name; | ||
|
||
// DOM.div(... -> createElement(... | ||
j(path.parent).replaceWith( | ||
j.identifier('createElement') | ||
); | ||
// createElement(... -> createElement('div', ... | ||
DOMargs.unshift(j.literal(DOMFactoryType)); | ||
}); | ||
|
||
return hasModifications; | ||
} | ||
|
||
// We only need to update 'DOM.div' syntax if there was a deconstructed | ||
// reference to React.DOM | ||
if (hasModifications) { | ||
hasModifications = replaceDOMReferences(j, root) || hasModifications; | ||
} | ||
|
||
// matches 'React.DOM' | ||
const isReactDOMIdentifier = path => ( | ||
path.node.name === DOMModuleName && | ||
( | ||
path.parent.node.type === 'MemberExpression' && | ||
path.parent.node.object.name === 'React' | ||
) | ||
); | ||
|
||
/** | ||
* Update React.DOM references | ||
* eg 'foo = React.DOM.div('a'...' | ||
* replace with 'foo = React.createElement('div', 'a'...' | ||
*/ | ||
function replaceReactDOMReferences(j, root) { | ||
let hasModifications = false; | ||
|
||
root | ||
.find(j.Identifier) | ||
.filter(isReactDOMIdentifier) | ||
.forEach(path => { | ||
hasModifications = true; | ||
const DOMargs = path.parent.parent.parent.node.arguments; | ||
const DOMFactoryPath = path.parent.parent.node.property; | ||
const DOMFactoryType = DOMFactoryPath.name; | ||
|
||
// React.DOM.div(... -> React.DOM.createElement(... | ||
path.parent.parent.node.property = j.identifier('createElement'); | ||
// React.DOM.createElement(... -> React.createElement(... | ||
j(path.parent).replaceWith(j.identifier('React')); | ||
// React.createElement(... -> React.createElement('div'... | ||
DOMargs.unshift(j.literal(DOMFactoryType)); | ||
}); | ||
|
||
return hasModifications; | ||
} | ||
|
||
hasModifications = replaceReactDOMReferences(j, root) || hasModifications; | ||
|
||
return hasModifications | ||
? root.toSource({ quote: 'single' }) | ||
: null; | ||
}; |
7 changes: 7 additions & 0 deletions
7
transforms/__testfixtures__/React-DOM-to-react-dom-factories/react-dom-basic-case.input.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
const React = require('react'); | ||
|
||
class Hello extends React.Component { | ||
render() { | ||
return React.DOM.div(null, `Hello ${this.props.toWhat}`); | ||
} | ||
} |
7 changes: 7 additions & 0 deletions
7
transforms/__testfixtures__/React-DOM-to-react-dom-factories/react-dom-basic-case.output.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
const React = require('react'); | ||
|
||
class Hello extends React.Component { | ||
render() { | ||
return React.createElement('div', null, `Hello ${this.props.toWhat}`); | ||
} | ||
} |
11 changes: 11 additions & 0 deletions
11
...__testfixtures__/React-DOM-to-react-dom-factories/react-dom-deconstructed-import.input.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
import ReactDOM from 'ReactDOM'; | ||
import { | ||
Component, | ||
DOM | ||
} from 'react'; | ||
|
||
class Hello extends Component { | ||
render() { | ||
return DOM.div(null, `Hello ${this.props.toWhat}`); | ||
} | ||
} |
11 changes: 11 additions & 0 deletions
11
..._testfixtures__/React-DOM-to-react-dom-factories/react-dom-deconstructed-import.output.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
import ReactDOM from 'ReactDOM'; | ||
import { | ||
Component, | ||
createElement | ||
} from 'react'; | ||
|
||
class Hello extends Component { | ||
render() { | ||
return createElement('div', null, `Hello ${this.props.toWhat}`); | ||
} | ||
} |
11 changes: 11 additions & 0 deletions
11
...ures__/React-DOM-to-react-dom-factories/react-dom-deconstructed-require-part-two.input.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
const ReactDOM = require('ReactDOM'); | ||
const { | ||
Component, | ||
DOM | ||
} = require('react'); | ||
|
||
class Hello extends Component { | ||
render() { | ||
return DOM.div(null, `Hello ${this.props.toWhat}`); | ||
} | ||
} |
11 changes: 11 additions & 0 deletions
11
...res__/React-DOM-to-react-dom-factories/react-dom-deconstructed-require-part-two.output.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
const ReactDOM = require('ReactDOM'); | ||
const { | ||
Component, | ||
createElement | ||
} = require('react'); | ||
|
||
class Hello extends Component { | ||
render() { | ||
return createElement('div', null, `Hello ${this.props.toWhat}`); | ||
} | ||
} |
12 changes: 12 additions & 0 deletions
12
..._testfixtures__/React-DOM-to-react-dom-factories/react-dom-deconstructed-require.input.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
const React = require('react'); | ||
const ReactDOM = require('ReactDOM'); | ||
const { | ||
Component, | ||
DOM | ||
} = React; | ||
|
||
class Hello extends Component { | ||
render() { | ||
return DOM.div(null, `Hello ${this.props.toWhat}`); | ||
} | ||
} |
12 changes: 12 additions & 0 deletions
12
...testfixtures__/React-DOM-to-react-dom-factories/react-dom-deconstructed-require.output.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
const React = require('react'); | ||
const ReactDOM = require('ReactDOM'); | ||
const { | ||
Component, | ||
createElement | ||
} = React; | ||
|
||
class Hello extends Component { | ||
render() { | ||
return createElement('div', null, `Hello ${this.props.toWhat}`); | ||
} | ||
} |
13 changes: 13 additions & 0 deletions
13
...__/React-DOM-to-react-dom-factories/react-dom-no-change-dom-from-other-libraries.input.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
let {DOM} = require('Free'); | ||
import {DOM} from 'Free'; | ||
|
||
const foo = DOM.div('a', 'b', 'c'); | ||
const bar = Free.DOM.div('a', 'b', 'c'); | ||
|
||
DOM = 'this is a test!'; | ||
|
||
foo.DOM = {}; | ||
|
||
foo.DOM.div = () => null; | ||
|
||
const bar = foo.DOM.div('a', 'b', 'c'); |
Empty file.
7 changes: 7 additions & 0 deletions
7
...rms/__testfixtures__/React-DOM-to-react-dom-factories/react-dom-no-change-import.input.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
import React from 'react'; | ||
|
||
class Hello extends React.Component { | ||
render() { | ||
return React.createElement('div', null, `Hello ${this.props.toWhat}`); | ||
} | ||
} |
Empty file.
7 changes: 7 additions & 0 deletions
7
...ms/__testfixtures__/React-DOM-to-react-dom-factories/react-dom-no-change-require.input.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
const React = require('react'); | ||
|
||
class Hello extends React.Component { | ||
render() { | ||
return React.createElement('div', null, `Hello ${this.props.toWhat}`); | ||
} | ||
} |
Empty file.
34 changes: 34 additions & 0 deletions
34
transforms/__tests__/React-DOM-to-react-dom-factories-test.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
/** | ||
* 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 = [ | ||
'react-dom-basic-case', | ||
'react-dom-deconstructed-import', | ||
'react-dom-deconstructed-require', | ||
'react-dom-deconstructed-require-part-two', | ||
'react-dom-no-change-import', | ||
'react-dom-no-change-require', | ||
'react-dom-no-change-dom-from-other-libraries', | ||
]; | ||
|
||
const defineTest = require('jscodeshift/dist/testUtils').defineTest; | ||
|
||
describe('React-DOM-to-react-dom-factories', () => { | ||
tests.forEach(test => | ||
defineTest( | ||
__dirname, | ||
'React-DOM-to-react-dom-factories', | ||
null, | ||
`React-DOM-to-react-dom-factories/${ test }` | ||
) | ||
); | ||
}); |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This will match any function called
DOM
. Could we restrict this so it only matches if there's a corresponding import/require statement?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Glad you are thinking of that!
The way I structured the codemod, it only gets used after we have already detected the corresponding import/require statement. I can restructure things to make that more clear.
That's also why I added this test.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oh great, didn't see that!