Skip to content

Commit

Permalink
fix: handle as named export, Object.defineProperty of exports oth…
Browse files Browse the repository at this point in the history
…er than `__esModule`

closes #59
  • Loading branch information
59naga committed Jul 16, 2018
1 parent c78dccb commit a1b82d1
Showing 1 changed file with 23 additions and 3 deletions.
26 changes: 23 additions & 3 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,18 +56,20 @@ class ExportsFinder {
.get('body')
.forEach(path => {
if (path.isVariableDeclaration()) {
this.findExport(path.get('declarations.0'), 'init')
this.findExports(path.get('declarations.0'), 'init')
} else if (
path.isExpressionStatement() &&
path.get('expression').isAssignmentExpression()
) {
this.findExport(path)
this.findExports(path)
} else {
this.findExportsInCallExpression(path)
}
})
return this.hasExportsDefault && !this.hasExportsNamed && !this.hasModuleExports
}

findExport(path, property = 'expression') {
findExports(path, property = 'expression') {
// Not `exports.anything`, skip
if (!path.get(`${property}.left`).node || !path.get(`${property}.left.object`).node) {
return
Expand All @@ -87,6 +89,24 @@ class ExportsFinder {
}
}

findExportsInCallExpression(path) {
const self = this
path.traverse({
CallExpression(path) {
if (!path.get('callee').matchesPattern('Object.defineProperty')) {
return
}

const [identifier, prop] = path.get('arguments')
const objectName = identifier.get('name').node
const propertyName = prop.get('value').node
if (objectName === 'exports' && propertyName !== '__esModule') {
self.hasExportsNamed = true
}
}
})
}

isAmd() {
const rootPath = this.getRootPath()
const hasntAmdRoot = !(rootPath.parentPath && rootPath.parentPath.parentPath)
Expand Down

0 comments on commit a1b82d1

Please sign in to comment.