Skip to content

Commit

Permalink
test: fix tests
Browse files Browse the repository at this point in the history
  • Loading branch information
bbmoz committed Apr 23, 2017
1 parent 8a0c7ca commit 87cf28b
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 20 deletions.
13 changes: 8 additions & 5 deletions lib/src/AstParser.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,27 +6,30 @@ const { stripCwd } = require('./common')
class AstParser {
parser: any
sourceType: string
strip: Function

constructor (parser: any = esprima,
sourceType: string = 'module') {
sourceType: string = 'module',
strip: Function = stripCwd) {
this.parser = parser
this.sourceType = 'module'
this.strip = strip
}

parse (filePath: string, text: string) {
const ast: Array<any> = this.parser.parse(text, { sourceType: this.sourceType }).body
return {
imports: extractImports(filePath, ast),
imports: extractImports(filePath, ast, this.strip),
exports: extractExports(ast)
}
}
}

function extractImports (filePath: string, ast: Array<any>) {
function extractImports (filePath: string, ast: Array<any>, strip: Function) {
const imports = {}
ast.filter(isImport)
.forEach(importAst => {
const sourcePath = resolvePath(filePath, importAst.source.value)
const sourcePath = strip(resolvePath(filePath, importAst.source.value))
imports[sourcePath] = importAst.specifiers[0].local.name
})
return imports
Expand Down Expand Up @@ -56,7 +59,7 @@ function extractExports (ast: Array<any>) {

function resolvePath (filePath: string, source: string) {
const stripFilePath = filePath.substr(0, filePath.lastIndexOf('/'))
return stripCwd(`${resolve(stripFilePath, source)}.js`)
return `${resolve(stripFilePath, source)}.js`
}

function isImport (statement: any) {
Expand Down
37 changes: 22 additions & 15 deletions lib/test/AstParser.js
Original file line number Diff line number Diff line change
@@ -1,53 +1,60 @@
import test from 'ava'
import { stub } from 'sinon'
import AstParser from './../src/AstParser'

test('.parse(text): ast of import', t => { t.plan(2)
test('.parse(path, text): ast of import', t => { t.plan(2)
const text = 'import foo from "bar"; const a = 2;'
const ast = astParser.parse(text)
const ast = astParser.parse(path, text, stripCwdStub)
t.deepEqual(ast.imports, { bar: 'foo' })
t.deepEqual(ast.exports, {})
})

test('.parse(text): ast of export default', t => { t.plan(2)
test('.parse(path, text): ast of export default', t => { t.plan(2)
const text = 'const a = 2; export default a;'
const ast = astParser.parse(text)
const ast = astParser.parse(path, text)
t.deepEqual(ast.imports, {})
t.deepEqual(ast.exports, { default: 'a' })
})

test('.parse(text): ast of import and export *', t => { t.plan(2)
test('.parse(path, text): ast of import and export *', t => { t.plan(2)
const text = 'import foo from "bar"; export * from "bar";'
const ast = astParser.parse(text)
const ast = astParser.parse(path, text)
t.deepEqual(ast.imports, { bar: 'foo' })
t.deepEqual(ast.exports, { all: 'bar' })
})

test('.parse(text): ast of import and export default', t => { t.plan(2)
test('.parse(path, text): ast of import and export default', t => { t.plan(2)
const text = 'import foo from "bar"; const a = 2; export default a;'
const ast = astParser.parse(text)
const ast = astParser.parse(path, text)
t.deepEqual(ast.imports, { bar: 'foo' })
t.deepEqual(ast.exports, { default: 'a' })
})

test('.parse(text): ast of import and export', t => { t.plan(2)
test('.parse(path, text): ast of import and export', t => { t.plan(2)
const text = 'import foo from "bar"; const a = 2; export { a };'
const ast = astParser.parse(text)
const ast = astParser.parse(path, text)
t.deepEqual(ast.imports, { bar: 'foo' })
t.deepEqual(ast.exports, { named: ['a'] })
})

test('.parse(text): ast of import, export, and export default', t => { t.plan(2)
test('.parse(path, text): ast of import, export, and export default', t => { t.plan(2)
const text = 'import foo from "bar"; import dog from "cat"; const a = 2; const b = 3; export { a }; export default b;'
const ast = astParser.parse(text)
const ast = astParser.parse(path, text)
t.deepEqual(ast.imports, { bar: 'foo', cat: 'dog' })
t.deepEqual(ast.exports, { named: ['a'], default: 'b' })
})

test.todo('.parse(text): ast of require')
test.todo('.parse(path, text): ast of require')

test.todo('.parse(text): ast of module.exports')
test.todo('.parse(path, text): ast of module.exports')

let astParser
let path
let stripCwdStub
test.beforeEach('setup', () => {
astParser = new AstParser()
path = '/hello/app/bar.js'
stripCwdStub = stub()
stripCwdStub.withArgs('/hello/app/bar.js').returns('bar')
stripCwdStub.withArgs('/hello/app/cat.js').returns('cat')
astParser = new AstParser(undefined, undefined, stripCwdStub)
})

0 comments on commit 87cf28b

Please sign in to comment.