Socket
Socket
Sign inDemoInstall

mr-dep-walk

Package Overview
Dependencies
3
Maintainers
2
Versions
9
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

Comparing version 1.0.1 to 1.1.0

.travis.yml

64

lib/deps-from-source.js

@@ -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']);
});
});
});
SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc