Skip to content

Commit

Permalink
fix: closes #57
Browse files Browse the repository at this point in the history
  • Loading branch information
59naga committed Jul 2, 2018
1 parent 2d56b74 commit e6cd0f5
Showing 1 changed file with 25 additions and 6 deletions.
31 changes: 25 additions & 6 deletions src/index.js
Expand Up @@ -9,8 +9,11 @@ module.exports = ({template, types}) => {
AssignmentExpression (path) {
const name = `${path.get('left.object.name').node}.${path.get(`left.property.name`).node}`
if (name === 'exports.default') {
const finder = new ExportFinder(path)
if (!finder.isOnlyDefaultExport()) {
const finder = new ExportsFinder(path)
if (!finder.isOnlyExportsDefault()) {
return
}
if (finder.isAmd()) {
return
}
const rootPath = finder.getRootPath()
Expand All @@ -37,17 +40,17 @@ module.exports = ({template, types}) => {
}
}

class ExportFinder {
constructor (path) {
this.path = path
class ExportsFinder {
constructor (exportsDefaultPath) {
this.path = exportsDefaultPath
this.hasExportsDefault = false
this.hasExportsNamed = false
this.hasModuleExports = false
}
getRootPath () {
return this.path.parentPath.parentPath
}
isOnlyDefaultExport () {
isOnlyExportsDefault () {
this.getRootPath().get('body').forEach((path) => {
if (path.isVariableDeclaration()) {
this.findExport(path.get('declarations.0'), 'init')
Expand Down Expand Up @@ -79,4 +82,20 @@ class ExportFinder {
}
return null
}
isAmd () {
const rootPath = this.getRootPath()
const hasntAmdRoot = !(rootPath.parentPath && rootPath.parentPath.parentPath)
if (hasntAmdRoot) {
return false
}

const amdRoot = rootPath.parentPath.parentPath
if (!amdRoot.isCallExpression()) {
return false
}
if (amdRoot.get('callee.name').node === 'define') {
return true
}
return false
}
}

0 comments on commit e6cd0f5

Please sign in to comment.