Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

module-definition

Package Overview
Dependencies
Maintainers
1
Versions
30
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

module-definition - npm Package Compare versions

Comparing version 1.2.0 to 1.2.1

117

index.js

@@ -5,100 +5,53 @@ var Walker = require('node-source-walk'),

function hasRequire(ast) {
var sawRequire = false;
// Allows us to differentiate from the funky commonjs case
function hasAMDArguments(requireNode) {
var args = requireNode.arguments;
var walker = new Walker();
walker.traverse(ast, function (node) {
if (types.isRequire(node)) {
sawRequire = true;
walker.stopWalking();
}
});
return sawRequire;
return args &&
args[0].type !== 'Literal' &&
// For some dynamic node requires
args[0].type !== 'Identifier';
}
function hasDefine(ast) {
var sawDefine = false;
function fromSource(source) {
if (! source) throw new Error('source not supplied');
var walker = new Walker();
walker.traverse(ast, function (node) {
var type = 'none',
walker = new Walker(),
hasDefine = false,
hasAMDTopLevelRequire = false,
hasRequire = false,
hasExports = false,
isAMD, isCommonJS;
walker.walk(source, function (node) {
if (types.isDefine(node)) {
sawDefine = true;
walker.stopWalking();
hasDefine = true;
}
});
return sawDefine;
}
function hasAMDTopLevelRequire(ast) {
var result = false,
walker = new Walker(),
// Allows us to differentiate from the funky commonjs case
hasValidArguments = function(requireNode) {
var args = requireNode.arguments;
return args &&
args[0].type !== 'Literal' &&
// For some dynamic node requires
args[0].type !== 'Identifier';
};
walker.traverse(ast, function(node) {
if (types.isTopLevelRequire(node) && hasValidArguments(node)) {
result = true;
walker.stopWalking();
if (types.isRequire(node)) {
hasRequire = true;
}
});
return result;
}
function hasExports(ast) {
var result = false,
walker = new Walker();
walker.traverse(ast, function(node) {
if (types.isExports(node)) {
result = true;
walker.stopWalking();
hasExports = true;
}
if (types.isTopLevelRequire(node) && hasAMDArguments(node)) {
hasAMDTopLevelRequire = true;
}
});
return result;
}
isAMD = hasDefine || hasAMDTopLevelRequire;
isCommonJS = hasExports || (hasRequire && ! hasDefine);
function isAMD(ast) {
return hasDefine(ast) ||
hasAMDTopLevelRequire(ast);
}
if (isAMD) {
return 'amd';
}
function isCommonJS(ast) {
return hasExports(ast) ||
// there's a require with no define
hasRequire(ast) && ! hasDefine(ast);
}
if(isCommonJS) {
return'commonjs';
}
function fromSource(source) {
if (! source) throw new Error('source not supplied');
var type = 'none';
var walker = new Walker();
walker.walk(source, function (ast) {
// Check for amd first because it's a simpler traversal
// Also, there's a funky case when a top-level require could be an amd driver script
// or a commonjs module that starts with require('something'). It's easier to check
// for the AMD driver script case
if (isAMD(ast)) {
type = 'amd';
walker.stopWalking();
} else if (isCommonJS(ast)) {
type = 'commonjs';
walker.stopWalking();
}
});
return type;
return 'none';
}

@@ -105,0 +58,0 @@

{
"name": "module-definition",
"version": "1.2.0",
"version": "1.2.1",
"description": "Determines if a file is using a CommonJS or AMD module definition",

@@ -5,0 +5,0 @@ "main": "index.js",

SocketSocket SOC 2 Logo

Product

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

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc