module-definition
Advanced tools
Comparing version 1.1.0 to 1.2.0
93
index.js
@@ -5,16 +5,7 @@ var Walker = require('node-source-walk'), | ||
function isCommonJS(node) { | ||
return types.isExports(node) || | ||
// there's a require with no define | ||
(hasRequire(node) && ! hasDefine(node)); | ||
} | ||
// Whether or not the node has a require call | ||
// somewhere in its ast | ||
function hasRequire(node) { | ||
function hasRequire(ast) { | ||
var sawRequire = false; | ||
var walker = new Walker(); | ||
walker.traverse(node, function (node) { | ||
walker.traverse(ast, function (node) { | ||
if (types.isRequire(node)) { | ||
@@ -29,9 +20,7 @@ sawRequire = true; | ||
// Whether or not the node has a define call | ||
// somewhere in its ast | ||
function hasDefine(node) { | ||
function hasDefine(ast) { | ||
var sawDefine = false; | ||
var walker = new Walker(); | ||
walker.traverse(node, function (node) { | ||
walker.traverse(ast, function (node) { | ||
if (types.isDefine(node)) { | ||
@@ -46,17 +35,67 @@ sawDefine = true; | ||
function isAMD(node) { | ||
return types.isDefine(node); | ||
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(); | ||
} | ||
}); | ||
return result; | ||
} | ||
function hasExports(ast) { | ||
var result = false, | ||
walker = new Walker(); | ||
walker.traverse(ast, function(node) { | ||
if (types.isExports(node)) { | ||
result = true; | ||
walker.stopWalking(); | ||
} | ||
}); | ||
return result; | ||
} | ||
function isAMD(ast) { | ||
return hasDefine(ast) || | ||
hasAMDTopLevelRequire(ast); | ||
} | ||
function isCommonJS(ast) { | ||
return hasExports(ast) || | ||
// there's a require with no define | ||
hasRequire(ast) && ! hasDefine(ast); | ||
} | ||
function fromSource(source) { | ||
if (! source) throw new Error('source not supplied'); | ||
var type = 'none'; | ||
var walker = new Walker(); | ||
walker.walk(source, function (node) { | ||
if (isCommonJS(node)) { | ||
type = 'commonjs'; | ||
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 (isAMD(node)) { | ||
type = 'amd'; | ||
} else if (isCommonJS(ast)) { | ||
type = 'commonjs'; | ||
walker.stopWalking(); | ||
@@ -78,6 +117,6 @@ } | ||
if (! file) throw new Error('filename missing'); | ||
if (! cb) throw new Error('callback missing'); | ||
var walker = new Walker(); | ||
// Read file | ||
fs.readFile(file, function (err, data) { | ||
@@ -89,6 +128,6 @@ if (err) { | ||
var src = data.toString(); | ||
var type = fromSource(data.toString()); | ||
var src = data.toString(), | ||
type = fromSource(data.toString()); | ||
cb && cb(type); | ||
if (cb) cb(type); | ||
}); | ||
@@ -99,2 +138,2 @@ }; | ||
module.exports.sync = sync; | ||
module.exports.fromSource = fromSource; | ||
module.exports.fromSource = fromSource; |
{ | ||
"name": "module-definition", | ||
"version": "1.1.0", | ||
"version": "1.2.0", | ||
"description": "Determines if a file is using a CommonJS or AMD module definition", | ||
@@ -17,4 +17,4 @@ "main": "index.js", | ||
"dependencies": { | ||
"node-source-walk": "^1.1.0", | ||
"ast-module-types": "^1.0.0" | ||
"ast-module-types": "^1.1.1", | ||
"node-source-walk": "^1.1.0" | ||
}, | ||
@@ -21,0 +21,0 @@ "devDependencies": { |
@@ -0,3 +1,4 @@ | ||
// none | ||
;(function () { | ||
console.log('old school brah brah'); | ||
})(); | ||
})(); |
@@ -0,3 +1,4 @@ | ||
// amd | ||
define(function(require) { | ||
var a = require('bulllllshit'); | ||
}); | ||
}); |
@@ -41,3 +41,13 @@ var getModuleType = require('../'); | ||
asyncTest("./e.js", "amd"); | ||
asyncTest("./f.js", "amd"); | ||
asyncTest("./g.js", "commonjs"); | ||
it('throws if the filename is not supplied', function() { | ||
expect(function () { getModuleType(); } ).toThrow(); | ||
}); | ||
it('throws if a callback is not supplied', function() { | ||
expect(function () { getModuleType('./a.js'); } ).toThrow(); | ||
}); | ||
afterEach(function() { | ||
@@ -54,2 +64,8 @@ jasmine.DEFAULT_TIMEOUT_INTERVAL = this.originalTimeout; | ||
syncTest("./e.js", "amd"); | ||
syncTest("./f.js", "amd"); | ||
syncTest("./g.js", "commonjs"); | ||
it('throws if the filename is not supplied', function() { | ||
expect(function () { getModuleType.sync(); } ).toThrow(); | ||
}); | ||
}); | ||
@@ -63,2 +79,8 @@ | ||
sourceTest("./e.js", "amd"); | ||
}); | ||
sourceTest("./f.js", "amd"); | ||
sourceTest("./g.js", "commonjs"); | ||
it('throws if source code is not supplied', function() { | ||
expect(function () { getModuleType.fromSource(); } ).toThrow(); | ||
}); | ||
}); |
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
Dynamic require
Supply chain riskDynamic require can indicate the package is performing dangerous or unsafe dynamic code execution.
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
7154
12
198
3
Updatedast-module-types@^1.1.1