module-definition
Advanced tools
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", |
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
6049
159