diff --git a/lib/src/AstParser.js b/lib/src/AstParser.js index 77c857d..55a99d1 100644 --- a/lib/src/AstParser.js +++ b/lib/src/AstParser.js @@ -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 = 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) { +function extractImports (filePath: string, ast: Array, 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 @@ -56,7 +59,7 @@ function extractExports (ast: Array) { 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) { diff --git a/lib/test/AstParser.js b/lib/test/AstParser.js index 58fe621..c5a4797 100644 --- a/lib/test/AstParser.js +++ b/lib/test/AstParser.js @@ -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) })