mr-dep-walk
Advanced tools
Comparing version 1.0.1 to 1.1.0
@@ -5,9 +5,13 @@ 'use strict'; | ||
// lifted from: https://github.com/ef4/ember-browserify/blob/master/lib/stubs.js (EF4 deserves credit); | ||
// | ||
const STOP = { }; | ||
function forEachNode(node, visit) { | ||
if (node && typeof node === 'object' && !node._eb_visited) { | ||
node._eb_visited = true; | ||
visit(node); | ||
let shouldStop = visit(node); | ||
if (STOP === shouldStop) { return STOP; } | ||
var keys = Object.keys(node); | ||
for (var i=0; i < keys.length; i++) { | ||
forEachNode(node[keys[i]], visit); | ||
let shouldStop = forEachNode(node[keys[i]], visit); | ||
if (STOP === shouldStop) { return STOP; } | ||
} | ||
@@ -19,20 +23,23 @@ } | ||
// TODO: add a persistent cache | ||
try { | ||
return es5(source); | ||
} catch (e) { | ||
if (e.name === 'SyntaxError') { | ||
// assume it is ES6 syntax, and try again | ||
return es6(source); | ||
} | ||
// no idea what went wrong, rethrow | ||
throw e; | ||
} | ||
}; | ||
function es5(src) { | ||
var imports = []; | ||
var ast = acorn.parse(src); | ||
var ast = acorn.parse(source, { | ||
ecmaVersion: 6, | ||
sourceType: 'module' | ||
}); | ||
var hasImportDeclaration = false; | ||
forEachNode(ast, function(entry) { | ||
if (entry.type === 'ImportDeclaration') { | ||
hasImportDeclaration = true; | ||
let value = entry.source.value; | ||
if (value === 'exports' || value === 'require') { | ||
return; | ||
} | ||
imports.push(value); | ||
} | ||
if (hasImportDeclaration) { return; } | ||
if (entry.type === 'CallExpression' && entry.callee.name === 'define') { | ||
@@ -49,29 +56,10 @@ for (let i = 0; i < entry.arguments.length; i++) { | ||
} | ||
break; | ||
return STOP; | ||
} | ||
} | ||
return STOP; | ||
} | ||
}); | ||
return imports; | ||
} | ||
function es6(src) { | ||
let imports = [] | ||
let ast = acorn.parse(src, { | ||
ecmaVersion: 6, | ||
sourceType: 'module' | ||
}); | ||
forEachNode(ast, function(entry) { | ||
if (entry.type === 'ImportDeclaration') { | ||
let value = entry.source.value; | ||
if (value === 'exports' || value === 'require') { | ||
return; | ||
} | ||
imports.push(value); | ||
} | ||
}); | ||
return imports; | ||
} | ||
}; |
@@ -16,3 +16,3 @@ { | ||
"name": "mr-dep-walk", | ||
"version": "1.0.1", | ||
"version": "1.1.0", | ||
"main": "index.js", | ||
@@ -19,0 +19,0 @@ "directories": { |
@@ -40,2 +40,21 @@ 'use strict'; | ||
}); | ||
}) | ||
describe('ES mixed', function() { | ||
it('define then es6', function() { | ||
expect(depsFromSrouce(` | ||
define('foo', ['bar'], function() { }); | ||
import x from 'a'; | ||
import y from 'b/c'; | ||
`)).to.eql(['bar']); | ||
}); | ||
it('es6 then define', function() { | ||
expect(depsFromSrouce(` | ||
import x from 'a'; | ||
import y from 'b/c'; | ||
define('foo', ['bar'], function() { }); | ||
`)).to.eql(['a', 'b/c']); | ||
}); | ||
}); | ||
}); |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
No README
QualityPackage does not have a README. This may indicate a failed publish or a low quality package.
Found 1 instance in 1 package
19731
13
282
0
7