import estraverse from 'estraverse';
import ExportDefaultDeclaration from '../../syntax/export-default-declaration';

export default
  function (ast) {
    estraverse.replace(ast, {
      enter: traverse
    });
  }

function traverse(node, parent) {
  if (isModuleExportsAssignment(node) && parent.type === 'Program') {
    return new ExportDefaultDeclaration(node.expression.right);
  }
}

function isModuleExportsAssignment(node) {
  return node.type === 'ExpressionStatement' &&
    node.expression.type === 'AssignmentExpression' &&
    node.expression.operator === '=' &&
    isModuleExportsMemberExpression(node.expression.left);
}

function isModuleExportsMemberExpression(node) {
  return node.type === 'MemberExpression' &&
    node.computed === false &&
    isIdentifier(node.object, 'module') &&
    isIdentifier(node.property, 'exports');
}

function isIdentifier(node, name) {
  return node.type === 'Identifier' && node.name === name;
}

@@ -30,16 +30,66 @@
 verse';%0A
+import matchesAst from '../../utils/matches-ast';%0A
 import E
@@ -160,16 +160,16 @@
 ation';%0A
-
 %0Aexport 
@@ -439,127 +439,142 @@
 %0A%7D%0A%0A
-function isModuleExportsAssignment(node) %7B%0A  return node.type === 'ExpressionStatement' &&%0A    node.expression.type ===
+// Matches: module.exports = ...%0Avar isModuleExportsAssignment = matchesAst(%7B%0A  type: 'ExpressionStatement',%0A  expression: %7B%0A    type:
  'As
@@ -596,40 +596,22 @@
 ion'
- &&
+,
 %0A    
-node.expression.
 operator
  ===
@@ -610,153 +610,38 @@
 ator
- ===
+:
  '='
- &&
+,
 %0A    
-isModuleExportsMemberExpression(node.expression.left);%0A%7D%0A%0Afunction isModuleExportsMemberExpression(node) %7B%0A  return node.type ===
+left: %7B%0A      type:
  'Me
@@ -659,29 +659,24 @@
 ion'
- &&
+,
 %0A    
-node.
+  
 computed
  ===
@@ -675,205 +675,181 @@
 uted
- ===
+:
  false
- &&%0A    isIdentifier(node.object, 'module') &&%0A    isIdentifier(node.property, 'exports');%0A%7D%0A%0Afunction isIdentifier(node, name) %7B%0A  return node.type === 'Identifier' && node.name === name;%0A%7D
+,%0A      object: %7B%0A        type: 'Identifier',%0A        name: 'module'%0A      %7D,%0A      property: %7B%0A        type: 'Identifier',%0A        name: 'exports'%0A      %7D%0A    %7D%0A  %7D%0A%7D);
 %0A

Use matchesAst() to rewrite export-commonjs