22 * Copyright 2015-present, Facebook, Inc.
33 *
44 * This source code is licensed under the MIT license found in the
5- * LICENSE file in the root directory of this source tree.
5+ * LICENSE file in the root directory of this source tree.
66 *
77 */
88
@@ -33,26 +33,24 @@ module.exports = function(file, api, options) {
3333 // } from 'react';
3434 root
3535 . find ( j . ImportDeclaration )
36- . filter ( path => (
37- path . node . specifiers . filter ( specifier => (
38- specifier . imported &&
39- specifier . imported . name === DOMModuleName
40- ) ) . length > 0 &&
41- path . node . source . value === 'react'
42- ) )
36+ . filter (
37+ path =>
38+ path . node . specifiers . filter (
39+ specifier =>
40+ specifier . imported && specifier . imported . name === DOMModuleName
41+ ) . length > 0 && path . node . source . value === 'react'
42+ )
4343 . forEach ( path => {
4444 hasModifications = true ;
4545
4646 // Replace the DOM key with 'createElement'
47- path . node . specifiers = path . node . specifiers . map (
48- specifier => {
49- if ( specifier . imported && specifier . imported . name === DOMModuleName ) {
50- return j . importSpecifier ( j . identifier ( 'createElement' ) ) ;
51- } else {
52- return specifier ;
53- }
47+ path . node . specifiers = path . node . specifiers . map ( specifier => {
48+ if ( specifier . imported && specifier . imported . name === DOMModuleName ) {
49+ return j . importSpecifier ( j . identifier ( 'createElement' ) ) ;
50+ } else {
51+ return specifier ;
5452 }
55- ) ;
53+ } ) ;
5654 } ) ;
5755
5856 //---------
@@ -69,28 +67,24 @@ module.exports = function(file, api, options) {
6967 // } = require('react');
7068 root
7169 . find ( j . ObjectPattern )
72- . filter ( path => (
73- path . parent . node . init &&
74- (
70+ . filter (
71+ path =>
72+ path . parent . node . init &&
7573 // matches ' } = React;'
76- path . parent . node . init . name === 'React'
77- ||
78- (
74+ ( path . parent . node . init . name === 'React' ||
7975 // matches " } = require('react');"
80- path . parent . node . init . type === 'CallExpression' &&
81- path . parent . node . init . callee . name === 'require' &&
82- path . parent . node . init . arguments [ 0 ] . value === 'react'
83- )
84- ) &&
85- path . node . properties . some ( property => {
86- return property . key . name === DOMModuleName ;
87- } )
88- ) )
76+ ( path . parent . node . init . type === 'CallExpression' &&
77+ path . parent . node . init . callee . name === 'require' &&
78+ path . parent . node . init . arguments [ 0 ] . value === 'react' ) ) &&
79+ path . node . properties . some ( property => {
80+ return property . key . name === DOMModuleName ;
81+ } )
82+ )
8983 . forEach ( path => {
9084 hasModifications = true ;
9185
9286 // Replace the DOM key with 'createElement'
93- path . node . properties = path . node . properties . map ( ( property ) => {
87+ path . node . properties = path . node . properties . map ( property => {
9488 if ( property . key . name === DOMModuleName ) {
9589 return j . identifier ( 'createElement' ) ;
9690 } else {
@@ -117,10 +111,9 @@ module.exports = function(file, api, options) {
117111 function replaceDOMReferences ( j , root ) {
118112 let hasModifications = false ;
119113
120- const isDOMIdentifier = path => (
114+ const isDOMIdentifier = path =>
121115 path . node . name === DOMModuleName &&
122- path . parent . parent . node . type === 'CallExpression'
123- ) ;
116+ path . parent . parent . node . type === 'CallExpression' ;
124117
125118 root
126119 . find ( j . Identifier )
@@ -133,9 +126,7 @@ module.exports = function(file, api, options) {
133126 const DOMFactoryType = DOMFactoryPath . name ;
134127
135128 // DOM.div(... -> createElement(...
136- j ( path . parent ) . replaceWith (
137- j . identifier ( 'createElement' )
138- ) ;
129+ j ( path . parent ) . replaceWith ( j . identifier ( 'createElement' ) ) ;
139130 // createElement(... -> createElement('div', ...
140131 DOMargs . unshift ( j . literal ( DOMFactoryType ) ) ;
141132 } ) ;
@@ -155,13 +146,10 @@ module.exports = function(file, api, options) {
155146 let hasModifications = false ;
156147
157148 // matches 'React.DOM'
158- const isReactDOMIdentifier = path => (
149+ const isReactDOMIdentifier = path =>
159150 path . node . name === DOMModuleName &&
160- (
161- path . parent . node . type === 'MemberExpression' &&
162- path . parent . node . object . name === 'React'
163- )
164- ) ;
151+ ( path . parent . node . type === 'MemberExpression' &&
152+ path . parent . node . object . name === 'React' ) ;
165153
166154 root
167155 . find ( j . Identifier )
@@ -185,7 +173,5 @@ module.exports = function(file, api, options) {
185173
186174 hasModifications = replaceReactDOMReferences ( j , root ) || hasModifications ;
187175
188- return hasModifications
189- ? root . toSource ( printOptions )
190- : null ;
176+ return hasModifications ? root . toSource ( printOptions ) : null ;
191177} ;
0 commit comments