ast-module-types
Advanced tools
Comparing version 2.7.1 to 3.0.0
240
index.js
@@ -0,61 +1,63 @@ | ||
'use strict'; | ||
// Deprecated | ||
// Whether or not the node represents a generic define() call | ||
// Whether or not the node represents a generic define() call | ||
// Note: this should not be used as it will have false positives. | ||
// It is mostly used to guide sniffs for other methods and will be made private eventually. | ||
module.exports.isDefine = function (node) { | ||
module.exports.isDefine = function(node) { | ||
if (!node) return false; | ||
var c = node.callee; | ||
const c = node.callee; | ||
return c && | ||
node.type === 'CallExpression' && | ||
c.type === 'Identifier' && | ||
c.name === 'define'; | ||
node.type === 'CallExpression' && | ||
c.type === 'Identifier' && | ||
c.name === 'define'; | ||
}; | ||
// Whether or not the node represents any of the AMD define() forms | ||
module.exports.isDefineAMD = function (node) { | ||
module.exports.isDefineAMD = function(node) { | ||
if (!node) return false; | ||
var e = module.exports; | ||
const e = module.exports; | ||
return e.isNamedForm(node) || e.isDependencyForm(node) || | ||
e.isFactoryForm(node) || e.isNoDependencyForm(node) || | ||
e.isREMForm(node); | ||
return e.isNamedForm(node) || e.isDependencyForm(node) || | ||
e.isFactoryForm(node) || e.isNoDependencyForm(node) || | ||
e.isREMForm(node); | ||
}; | ||
// Whether or not the node represents a require function call | ||
module.exports.isRequire = function (node) { | ||
return this.isPlainRequire(node) || this.isMainScopedRequire(node); | ||
module.exports.isRequire = function(node) { | ||
return this.isPlainRequire(node) || this.isMainScopedRequire(node); | ||
}; | ||
// Whether or not the node represents a plain require function call [require(...)] | ||
module.exports.isPlainRequire = function (node) { | ||
if (!node) return false; | ||
module.exports.isPlainRequire = function(node) { | ||
if (!node) return false; | ||
var c = node.callee; | ||
const c = node.callee; | ||
return c && | ||
node.type === 'CallExpression' && | ||
c.type === 'Identifier' && | ||
c.name === 'require'; | ||
} | ||
return c && | ||
node.type === 'CallExpression' && | ||
c.type === 'Identifier' && | ||
c.name === 'require'; | ||
}; | ||
// Whether or not the node represents main-scoped require function call [require.main.require(...)] | ||
module.exports.isMainScopedRequire = function (node) { | ||
if (!node) return false; | ||
module.exports.isMainScopedRequire = function(node) { | ||
if (!node) return false; | ||
var c = node.callee; | ||
const c = node.callee; | ||
return c && | ||
node.type === 'CallExpression' && | ||
c.type === 'MemberExpression' && | ||
c.object.type === 'MemberExpression' && | ||
c.object.object.type === 'Identifier' && | ||
c.object.object.name === 'require' && | ||
c.object.property.type === 'Identifier' && | ||
c.object.property.name === 'main' && | ||
c.property.type === 'Identifier' && | ||
c.property.name === 'require'; | ||
} | ||
return c && | ||
node.type === 'CallExpression' && | ||
c.type === 'MemberExpression' && | ||
c.object.type === 'MemberExpression' && | ||
c.object.object.type === 'Identifier' && | ||
c.object.object.name === 'require' && | ||
c.object.property.type === 'Identifier' && | ||
c.object.property.name === 'main' && | ||
c.property.type === 'Identifier' && | ||
c.property.name === 'require'; | ||
}; | ||
@@ -65,5 +67,5 @@ // Whether or not the node represents a require at the top of the module | ||
// just take the root and check its immediate child | ||
module.exports.isTopLevelRequire = function (node) { | ||
module.exports.isTopLevelRequire = function(node) { | ||
if (node.type !== 'Program' || !node.body || | ||
!node.body.length || !node.body[0].expression) { | ||
node.body.length === 0 || !node.body[0].expression) { | ||
return false; | ||
@@ -77,106 +79,101 @@ } | ||
// Example: require(deps, function) | ||
module.exports.isAMDDriverScriptRequire = function (node) { | ||
return this.isRequire(node) && | ||
node.arguments && | ||
node.arguments[0] && node.arguments[0].type && | ||
node.arguments[0].type === 'ArrayExpression'; | ||
module.exports.isAMDDriverScriptRequire = function(node) { | ||
return this.isRequire(node) && | ||
node.arguments && | ||
node.arguments[0] && node.arguments[0].type && | ||
node.arguments[0].type === 'ArrayExpression'; | ||
}; | ||
// Whether or not the node represents the use of | ||
// assigning (and possibly attaching) something to module.exports or exports | ||
module.exports.isExports = function (node) { | ||
if (node.type !== 'AssignmentExpression') return; | ||
function isExportsIdentifier(obj) { | ||
return obj.type && obj.type === 'Identifier' && obj.name === 'exports'; | ||
} | ||
// Only the left side matters | ||
node = node.left; | ||
function isModuleIdentifier(obj) { | ||
return obj.type && obj.type === 'Identifier' && obj.name === 'module'; | ||
} | ||
function isExportsIdentifier(obj) { | ||
return obj.type && obj.type === 'Identifier' && obj.name === 'exports'; | ||
} | ||
// module.exports.foo | ||
function isModuleExportsAttach(node) { | ||
if (!node.object || !node.object.object || !node.object.property) return false; | ||
function isModuleIdentifier(obj) { | ||
return obj.type && obj.type === 'Identifier' && obj.name === 'module'; | ||
} | ||
return node.type === 'MemberExpression' && | ||
isModuleIdentifier(node.object.object) && | ||
isExportsIdentifier(node.object.property); | ||
} | ||
// module.exports.foo | ||
function isModuleExportsAttach() { | ||
if (!node.object || !node.object.object || !node.object.property) return false; | ||
// module.exports | ||
function isModuleExportsAssign(node) { | ||
if (!node.object || !node.property) return false; | ||
var objObj = node.object.object, | ||
objProp = node.object.property; | ||
return node.type === 'MemberExpression' && | ||
isModuleIdentifier(node.object) && | ||
isExportsIdentifier(node.property); | ||
} | ||
return node.type === 'MemberExpression' && | ||
isModuleIdentifier(objObj) && | ||
isExportsIdentifier(objProp); | ||
} | ||
// exports | ||
function isExportsAssign(node) { | ||
return isExportsIdentifier(node); | ||
} | ||
// module.exports | ||
function isModuleExportsAssign() { | ||
if (!node.object || !node.property) return false; | ||
// exports.foo | ||
function isExportsAttach(node) { | ||
return node.type === 'MemberExpression' && isExportsIdentifier(node.object); | ||
} | ||
return node.type === 'MemberExpression' && | ||
isModuleIdentifier(node.object) && | ||
isExportsIdentifier(node.property); | ||
} | ||
// Whether or not the node represents the use of | ||
// assigning (and possibly attaching) something to module.exports or exports | ||
module.exports.isExports = function(node) { | ||
if (node.type !== 'AssignmentExpression') return; | ||
// exports | ||
function isExportsAssign() { | ||
return isExportsIdentifier(node); | ||
} | ||
// Only the left side matters | ||
const leftNode = node.left; | ||
// exports.foo | ||
function isExportsAttach() { | ||
return node.type === 'MemberExpression' && | ||
isExportsIdentifier(node.object); | ||
} | ||
return isModuleExportsAttach() || isModuleExportsAssign() || | ||
isExportsAttach() || isExportsAssign(); | ||
return isModuleExportsAttach(leftNode) || isModuleExportsAssign(leftNode) || | ||
isExportsAttach(leftNode) || isExportsAssign(leftNode); | ||
}; | ||
// define('name', [deps], func) | ||
module.exports.isNamedForm = function (node) { | ||
module.exports.isNamedForm = function(node) { | ||
if (!this.isDefine(node)) return false; | ||
var args = node['arguments']; | ||
const args = node.arguments; | ||
return args && args.length == 3 && | ||
(args[0].type === 'Literal' || args[0].type === 'StringLiteral') && | ||
(args[1].type == 'ArrayExpression') && | ||
(args[2].type == 'FunctionExpression'); | ||
return args && args.length === 3 && | ||
(args[0].type === 'Literal' || args[0].type === 'StringLiteral') && | ||
args[1].type === 'ArrayExpression' && | ||
args[2].type === 'FunctionExpression'; | ||
}; | ||
// define([deps], func) | ||
module.exports.isDependencyForm = function (node) { | ||
module.exports.isDependencyForm = function(node) { | ||
if (!this.isDefine(node)) return false; | ||
var args = node['arguments']; | ||
const args = node.arguments; | ||
return args && args.length == 2 && | ||
args[0].type === 'ArrayExpression' && | ||
args[1].type == 'FunctionExpression'; | ||
return args && args.length === 2 && | ||
args[0].type === 'ArrayExpression' && | ||
args[1].type === 'FunctionExpression'; | ||
}; | ||
// define(func(require)) | ||
module.exports.isFactoryForm = function (node) { | ||
module.exports.isFactoryForm = function(node) { | ||
if (!this.isDefine(node)) return false; | ||
var args = node['arguments'], | ||
firstParamNode = args.length && args[0].params ? args[0].params[0] : null; | ||
const args = node.arguments; | ||
const firstParamNode = args.length > 0 && args[0].params ? args[0].params[0] : null; | ||
// Node should have a function whose first param is 'require' | ||
return args && args.length == 1 && | ||
args[0].type === 'FunctionExpression' && | ||
firstParamNode && firstParamNode.type === 'Identifier' && | ||
firstParamNode.name === 'require'; | ||
return args && args.length === 1 && | ||
args[0].type === 'FunctionExpression' && | ||
firstParamNode && firstParamNode.type === 'Identifier' && | ||
firstParamNode.name === 'require'; | ||
}; | ||
// define({}) | ||
module.exports.isNoDependencyForm = function (node) { | ||
module.exports.isNoDependencyForm = function(node) { | ||
if (!this.isDefine(node)) return false; | ||
var args = node['arguments']; | ||
const args = node.arguments; | ||
return args && args.length == 1 && | ||
args[0].type === 'ObjectExpression'; | ||
return args && args.length === 1 && args[0].type === 'ObjectExpression'; | ||
}; | ||
@@ -188,21 +185,18 @@ | ||
var args = node['arguments'], | ||
params = args.length ? args[0].params : null, | ||
first, second, third; | ||
const args = node.arguments; | ||
const params = args.length > 0 ? args[0].params : null; | ||
if (!args || args.length == 0 || args[0].type !== 'FunctionExpression' || params.length !== 3) { | ||
if (!args || args.length === 0 || args[0].type !== 'FunctionExpression' || params.length !== 3) { | ||
return false; | ||
} | ||
first = params[0]; | ||
second = params[1]; | ||
third = params[2]; | ||
const [first, second, third] = params; | ||
return first.type === 'Identifier' && first.name === 'require' && | ||
second.type === 'Identifier' && second.name === 'exports' && | ||
third.type === 'Identifier' && third.name === 'module'; | ||
second.type === 'Identifier' && second.name === 'exports' && | ||
third.type === 'Identifier' && third.name === 'module'; | ||
}; | ||
module.exports.isES6Import = function(node) { | ||
switch(node.type) { | ||
switch (node.type) { | ||
case 'Import': | ||
@@ -213,9 +207,9 @@ case 'ImportDeclaration': | ||
return true; | ||
}; | ||
return false; | ||
default: | ||
return false; | ||
} | ||
}; | ||
module.exports.isES6Export = function (node) { | ||
switch(node.type) { | ||
module.exports.isES6Export = function(node) { | ||
switch (node.type) { | ||
case 'ExportDeclaration': | ||
@@ -227,9 +221,9 @@ case 'ExportNamedDeclaration': | ||
return true; | ||
}; | ||
return false; | ||
default: | ||
return false; | ||
} | ||
}; | ||
module.exports.isDynamicImport = function(node) { | ||
return node.callee && node.callee.type === 'Import' && node.arguments.length; | ||
return node.callee && node.callee.type === 'Import' && node.arguments.length > 0; | ||
}; |
{ | ||
"name": "ast-module-types", | ||
"version": "2.7.1", | ||
"version": "3.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", | ||
"files": [ | ||
"index.js" | ||
], | ||
"scripts": { | ||
"test": "mocha test/test.js" | ||
"lint": "eslint index.js \"test/*.js\"", | ||
"mocha": "mocha", | ||
"test": "npm run lint && npm run mocha" | ||
}, | ||
"repository": { | ||
"type": "git", | ||
"url": "https://github.com/mrjoelkemp/node-ast-module-types" | ||
"url": "git+https://github.com/dependents/node-ast-module-types.git" | ||
}, | ||
@@ -21,12 +26,16 @@ "keywords": [ | ||
], | ||
"author": "Joel Kemp <joel@mrjoelkemp.com> (http://www.mrjoelkemp.com/)", | ||
"author": "Joel Kemp <joel@mrjoelkemp.com> (https://mrjoelkemp.com/)", | ||
"license": "MIT", | ||
"bugs": { | ||
"url": "https://github.com/mrjoelkemp/node-ast-module-types/issues" | ||
"url": "https://github.com/dependents/node-ast-module-types/issues" | ||
}, | ||
"homepage": "https://github.com/mrjoelkemp/node-ast-module-types", | ||
"homepage": "https://github.com/dependents/node-ast-module-types", | ||
"engines": { | ||
"node": ">=6.0" | ||
}, | ||
"devDependencies": { | ||
"mocha": "^2.0.1", | ||
"node-source-walk": "^3.0.0" | ||
"eslint": "^5.16.0", | ||
"mocha": "^6.2.3", | ||
"node-source-walk": "^4.2.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
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
New author
Supply chain riskA new npm collaborator published a version of the package for the first time. New collaborators are usually benign additions to a project, but do indicate a change to the security surface area of a package.
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
48
10897
3
4
177
2