ast-module-types
Advanced tools
Comparing version 1.2.0 to 2.0.0
98
index.js
// Whether or not the node represents an AMD define() call | ||
module.exports.isDefine = function (node) { | ||
if (!node) return false; | ||
var c = node.callee; | ||
@@ -13,2 +15,4 @@ | ||
module.exports.isRequire = function (node) { | ||
if (!node) return false; | ||
var c = node.callee; | ||
@@ -23,6 +27,11 @@ | ||
// Whether or not the node represents a require at the top of the module | ||
// Instead of trying to find the require then backtrack to the top, | ||
// just take the root and check its immediate child | ||
module.exports.isTopLevelRequire = function (node) { | ||
// If there's a require, it's either the top-level or nested | ||
// at which it still has at least 3 parents | ||
return this.isRequire(node) && node.parent.parent.parent.type === 'Program'; | ||
if (!node.type === 'Program' || !node.body || | ||
!node.body.length || !node.body[0].expression) { | ||
return false; | ||
} | ||
return this.isRequire(node.body[0].expression); | ||
}; | ||
@@ -41,9 +50,51 @@ | ||
// Whether or not the node represents the use of | ||
// assigning something to module.exports or exports | ||
// assigning (and possibly attaching) something to module.exports or exports | ||
module.exports.isExports = function (node) { | ||
var c = node.object; | ||
return c && | ||
node.type === 'MemberExpression' && | ||
c.type === 'Identifier' && | ||
(c.name === 'module' || c.name === 'exports'); | ||
if (node.type !== 'AssignmentExpression') return; | ||
// Only the left side matters | ||
node = node.left; | ||
function isExportsIdentifier(obj) { | ||
return obj.type && obj.type === 'Identifier' && obj.name === 'exports'; | ||
} | ||
function isModuleIdentifier(obj) { | ||
return obj.type && obj.type === 'Identifier' && obj.name === 'module'; | ||
} | ||
// module.exports.foo | ||
function isModuleExportsAttach() { | ||
if (!node.object || !node.object.object || !node.object.property) return false; | ||
var objObj = node.object.object, | ||
objProp = node.object.property; | ||
return node.type === 'MemberExpression' && | ||
isModuleIdentifier(objObj) && | ||
isExportsIdentifier(objProp); | ||
} | ||
// module.exports | ||
function isModuleExportsAssign() { | ||
if (!node.object || !node.property) return false; | ||
return node.type === 'MemberExpression' && | ||
isModuleIdentifier(node.object) && | ||
isExportsIdentifier(node.property); | ||
} | ||
// exports | ||
function isExportsAssign() { | ||
return isExportsIdentifier(node); | ||
} | ||
// exports.foo | ||
function isExportsAttach() { | ||
return node.type === 'MemberExpression' && | ||
isExportsIdentifier(node.object); | ||
} | ||
return isModuleExportsAttach() || isModuleExportsAssign() || | ||
isExportsAttach() || isExportsAssign(); | ||
}; | ||
@@ -53,2 +104,4 @@ | ||
module.exports.isNamedForm = function (node) { | ||
if (!this.isDefine(node)) return false; | ||
var args = node['arguments']; | ||
@@ -62,2 +115,4 @@ | ||
module.exports.isDependencyForm = function (node) { | ||
if (!this.isDefine(node)) return false; | ||
var args = node['arguments']; | ||
@@ -70,2 +125,4 @@ | ||
module.exports.isFactoryForm = function (node) { | ||
if (!this.isDefine(node)) return false; | ||
var args = node['arguments'], | ||
@@ -81,2 +138,4 @@ firstParamNode = args.length && args[0].params ? args[0].params[0] : null; | ||
module.exports.isNoDependencyForm = function (node) { | ||
if (!this.isDefine(node)) return false; | ||
var args = node['arguments']; | ||
@@ -86,1 +145,22 @@ | ||
}; | ||
// define(function(require, exports, module) | ||
module.exports.isREMForm = function(node) { | ||
if (!this.isDefine(node)) return false; | ||
var args = node['arguments'], | ||
params = args.length ? args[0].params : null, | ||
first, second, third; | ||
if (!args || args[0].type !== 'FunctionExpression' || params.length !== 3) { | ||
return false; | ||
} | ||
first = params[0]; | ||
second = params[1]; | ||
third = params[2]; | ||
return first.type === 'Identifier' && first.name === 'require' && | ||
second.type === 'Identifier' && second.name === 'exports' && | ||
third.type === 'Identifier' && third.name === 'module'; | ||
}; |
{ | ||
"name": "ast-module-types", | ||
"version": "1.2.0", | ||
"version": "2.0.0", | ||
"description": "Collection of useful helper functions when trying to determine module type (CommonJS or AMD) properties of an AST node.", | ||
"main": "index.js", | ||
"scripts": { | ||
"test": "echo \"Error: no test specified\" && exit 1" | ||
"test": "mocha test/test.js" | ||
}, | ||
@@ -26,3 +26,7 @@ "repository": { | ||
}, | ||
"homepage": "https://github.com/mrjoelkemp/node-ast-module-types" | ||
"homepage": "https://github.com/mrjoelkemp/node-ast-module-types", | ||
"devDependencies": { | ||
"esprima": "~1.2.2", | ||
"node-source-walk": "~1.1.2" | ||
} | ||
} |
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 tests
QualityPackage does not have any tests. This is a strong signal of a poorly maintained or low quality package.
Found 1 instance in 1 package
9541
4
192
0
2