@@ -202,16 +202,29 @@ module.exports = function(file, api, options) {
202202 return property && property . typeAnnotation . typeAnnotation ;
203203 } ;
204204
205- const build = useArrows => ( name , body , typeAnnotation , destructure ) => {
205+ const isDefaultExport = path =>
206+ path . parentPath && path . parentPath . value . type === 'ExportDefaultDeclaration' ;
207+
208+ const safelyDefaultExportDeclaration = ( path ) => {
209+ const localName = path . value . declarations [ 0 ] . id . name ;
210+ j ( path . parent )
211+ . replaceWith ( _ => path . value )
212+ . insertAfter (
213+ j . exportDeclaration ( true , { type : 'Identifier' , name : localName } )
214+ ) ;
215+ } ;
216+
217+ const build = useArrows => ( name , body , typeAnnotation , destructure , hasThisDotProps ) => {
206218 const identifier = j . identifier ( name ) ;
207219 const propsIdentifier = buildIdentifierWithTypeAnnotation (
208220 'props' ,
209221 typeAnnotation
210222 ) ;
211- const propsArg = [
223+
224+ const propsArg = hasThisDotProps ? [
212225 ( destructure && destructureProps ( j ( body ) , typeAnnotation ) ) ||
213226 propsIdentifier
214- ] ;
227+ ] : [ ] ;
215228 if ( useArrows ) {
216229 return j . variableDeclaration ( 'const' , [
217230 j . variableDeclarator (
@@ -261,6 +274,7 @@ module.exports = function(file, api, options) {
261274 if ( ! isPure && ! silenceWarnings ) {
262275 reportSkipped ( path ) ;
263276 }
277+
264278 return isPure ;
265279 }
266280 ) ;
@@ -269,6 +283,10 @@ module.exports = function(file, api, options) {
269283 return null ;
270284 }
271285
286+ // Save the names of the deleted pure classes super class
287+ // We need this to prune unused variables at the end.
288+ const parentClassNames = pureClasses . nodes ( ) . map ( node => node . superClass . name ) ;
289+
272290 pureClasses . replaceWith ( p => {
273291 const name = p . node . id . name ;
274292 const renderMethod = p . value . body . body . filter ( isRenderMethod ) [ 0 ] ;
@@ -281,6 +299,7 @@ module.exports = function(file, api, options) {
281299 console . warn ( `Unable to destructure ${ name } props.` ) ;
282300 }
283301
302+ const hasThisDotProps = j ( renderBody ) . find ( j . MemberExpression , THIS_PROPS ) . length > 0 ;
284303 replaceThisProps ( renderBody ) ;
285304
286305 if ( useArrows ) {
@@ -289,7 +308,8 @@ module.exports = function(file, api, options) {
289308 name ,
290309 renderBody ,
291310 propsTypeAnnotation ,
292- destructure
311+ destructure ,
312+ hasThisDotProps
293313 ) ,
294314 ...buildStatics ( name , statics )
295315 ] ;
@@ -299,11 +319,20 @@ module.exports = function(file, api, options) {
299319 name ,
300320 renderBody ,
301321 propsTypeAnnotation ,
302- destructure
322+ destructure ,
323+ hasThisDotProps
303324 ) ,
304325 ...buildStatics ( name , statics )
305326 ] ;
306327 }
328+ } ) . forEach ( p => {
329+ // Check for combining default keyword with const declaration
330+ if ( useArrows && isDefaultExport ( p ) ) {
331+ safelyDefaultExportDeclaration ( p ) ;
332+ }
333+ } ) . forEach ( ( p , i ) => {
334+ const parentClassName = parentClassNames [ i ] ;
335+ ReactUtils . removeUnusedSuperClassImport ( j ( p ) , f , parentClassName ) ;
307336 } ) ;
308337
309338 return f . toSource ( printOptions ) ;
0 commit comments