Skip to content

Commit

Permalink
Fixed default unnamed exports, enhanced imports accordingly.
Browse files Browse the repository at this point in the history
  • Loading branch information
yamafaktory committed Jan 9, 2017
1 parent cc35707 commit fa05f06
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 4 deletions.
35 changes: 31 additions & 4 deletions index.js
Expand Up @@ -69,6 +69,13 @@ class Shrimpit {
return [...new Set(array)]
}

deExtensionize (path) {
return [
...this.getDir(path),
this.getBase(path, true)
].join('/')
}

error (e) {
log(chalk.red(`! ${e} `))

Expand All @@ -94,8 +101,8 @@ class Shrimpit {
}
}

getBase (extPath) {
return path.parse(extPath).base
getBase (extPath, dropExt) {
return dropExt ? path.parse(extPath).name : path.parse(extPath).base
}

getDir (extPath) {
Expand All @@ -122,6 +129,10 @@ class Shrimpit {
}
}

joinPaths (...paths) {
return path.join(...paths)
}

read (rootPath, target) {
const extPath = path.normalize(
`${rootPath !== null ? rootPath + '/' : ''}${target}`
Expand Down Expand Up @@ -173,15 +184,31 @@ class Shrimpit {
}

walkAST (extPath) {
const self = this
let exports = []
let imports = []

const defaultExportVisitor = {
Expression (path) {
// Use path as default.
exports.push(self.deExtensionize(extPath))
},

Function (path) {
path.traverse(exportVisitor, true)
}
}

const exportVisitor = {
Identifier (path) {
exports.push(path.node.name)

// Stop traversal to avoid collecting unwanted identifiers.
path.stop()
},

Statement (path, expectNamedFunction) {
if (expectNamedFunction) exports.push(self.deExtensionize(extPath))
}
}

Expand All @@ -191,7 +218,7 @@ class Shrimpit {
},

ExportDefaultDeclaration (path) {
path.traverse(exportVisitor)
path.traverse(defaultExportVisitor)
},

ExportDefaultSpecifier (path) {
Expand All @@ -211,7 +238,7 @@ class Shrimpit {
},

ImportDefaultSpecifier (path) {
imports.push(path.node.local.name)
imports.push(self.joinPaths(extPath, '../', path.parent.source.value))
},

ImportNamespaceSpecifier (path) {
Expand Down
2 changes: 2 additions & 0 deletions test/a/a1.js
@@ -1,3 +1,5 @@
import a4 from './a4'

export function a11 () {}

export default function a12 () {}
6 changes: 6 additions & 0 deletions test/a/a4.js
@@ -1,3 +1,9 @@
import { a11, a12 } from './a1'

export const a4 = '1337'

export default {
propA: '8080',
propB: () => {},
propC: false
}
1 change: 1 addition & 0 deletions test/b/b2.js
@@ -0,0 +1 @@
export default function () {}

0 comments on commit fa05f06

Please sign in to comment.