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

amd-conversion-analyser

Package Overview
Dependencies
Maintainers
26
Versions
11
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

amd-conversion-analyser - npm Package Compare versions

Comparing version 1.5.0 to 1.6.0

dist/lib/detect-ajs-namespace.js

2

dist/lib/conversiondetector.js

@@ -1,1 +0,1 @@

"use strict";var _typeof="function"==typeof Symbol&&"symbol"==typeof Symbol.iterator?function(a){return typeof a}:function(a){return a&&"function"==typeof Symbol&&a.constructor===Symbol&&a!==Symbol.prototype?"symbol":typeof a},acorn=require("acorn"),glob=require("globby"),detectAMD=require("./detect-amd"),detectGlobals=require("acorn-globals"),globals=require("globals"),arrayUniq=require("array-uniq"),_=require("lodash"),fs=require("fs"),path=require("path"),colors=require("colors/safe"),winston=require("winston");colors.setTheme({ok:"grey",warn:"yellow",error:"red"});function ConversionDetector(a,b,c){function d(a){return new winston.Logger({level:a})}var f=b.symbols,e=b.patterns||["**/*.js"],g=[],h=d(),i=d(),j=d(),k=d(c["file-log-level"]),l=d(),m=!!c["globals-summary"],n=c.globals||c["idea-globals"],o=!1!==c.ok;(function(){n&&h.add(winston.transports.Console,{showLevel:!1}),m&&i.add(winston.transports.Console,{showLevel:!1}),!1!==c.files&&(j.add(winston.transports.Console,{showLevel:!1}),k.add(winston.transports.Console,{showLevel:!1})),!1!==c.summary&&l.add(winston.transports.Console,{showLevel:!1})})(),this._excludedGlobals=[],this._globals=[],this._countNonAMD=0,this._countGlobals=0,this._countAMDDefine=0,this._countAMDRequire=0,this.setupExcludedGlobals=function(a,b){var c="undefined"==typeof b?[]:b;return a&&Object.keys(a).filter(function(b){return a[b]}).forEach(function(a){var b=globals[a];b&&(c=arrayUniq(c.concat(Object.keys(b))))}),c},this.getFilename=function(a){return path.basename(a)},this.markAsNonAMD=function(a){++this._countNonAMD,g.push({filepath:a,amd:!1,is:f.ERR,msg:"Not an AMD module"})},this.markAsAMD=function(a,b){++this._countAMDDefine,this.findGlobals(a,b)},this.markAsAMDRequire=function(a,b){++this._countAMDRequire,this.findGlobals(a,b)},this.trimToEndOfVariableName=function(a){for(var b,c=[],d=a.parents.length-1;0<=d;d--)if(b=a.parents[d],"Identifier"===b.type)c.push(b.name);else if("MemberExpression"===b.type)c.push(b.property.name||b.property.value);else if("ThisExpression"===b.type)c.push("this");else break;return namePath.join(".")},this.isExcludedVariable=function(a){var c=[],d=a.split(".").some(function(a){c.push(a);var d=c.join(".");return-1<b.exclude.variables.indexOf(d)});return d},this.isAMDDefine=function(a){return"object"===("undefined"==typeof a?"undefined":_typeof(a))&&a.hasOwnProperty("type")&&"define"===a.type},this.isAMDRequire=function(a){return"object"===("undefined"==typeof a?"undefined":_typeof(a))&&a.hasOwnProperty("type")&&"require"===a.type},this.isNamedAMDModule=function(a){return this.isAMDDefine(a)&&a.hasOwnProperty("name")&&!!a.name},this.getRelevantGlobals=function(a){var b=this;return detectGlobals(a).filter(function(c){if(-1<b._excludedGlobals.indexOf(c.name))return!1;var d=b.getRelevantOccurrences(a,c.nodes);return 0<d.length})},this.getRelevantOccurrences=function(a,b){var c=this;return b?b.filter(function(a){var b=c.trimToEndOfVariableName(a);return!c.isExcludedVariable(b)&&(++c._countGlobals,!0)}):[]},this.processRelevantOccurrences=function(a,b,c){var d=this;return b?b.map(function(b){var e=acorn.getLineInfo(a,b.start),f=d.trimToEndOfVariableName(b),g=d.isExcludedVariable(f);return{varName:f,lineInfo:e,isExcluded:g,fullFilePath:c}}):[]},this.getIDEALink=function(a,b,c){return b&&c?colors.grey.underline("http://localhost:63342/api/file/"+process.cwd()+"/"+a+":"+b+":"+c):colors.underline("http://localhost:63342/api/file/"+process.cwd()+"/"+a)},this.findGlobals=function(a,b){var c=this;try{var d=this.getRelevantGlobals(b),e=[];if(0===d.length)return void g.push({filepath:a,amd:!0,globals:!1,is:f.OK});n&&d.forEach(function(d){var f=c.processRelevantOccurrences(b,d.nodes,a);e=e.concat(f),c._globals=c._globals.concat(f)}),g.push({filepath:a,amd:!0,is:f.ALERT,globals:!0,globalsList:e,msg:"Globals detected"})}catch(b){g.push({filepath:a,amd:!0,is:f.ERR,err:b,msg:"Error finding globals"})}},this.handleFile=function(a,b,c,d){try{if(a)throw new Error(a.message?a.message:a);var e=detectAMD(b),h=e&&e[0];this.isNamedAMDModule(h)?this.markAsAMD(c,b):this.isAMDDefine(h)?this.markAsAMD(c,b):this.isAMDRequire(h)?this.markAsAMDRequire(c,b):this.markAsNonAMD(c)}catch(a){g.push({filepath:c,is:f.ERR,err:a,msg:"Error parsing file"})}d()},this.printFileSummary=function(){var a=this,b={};b[f.ERR]={color:colors.error,logger:k.error},b[f.ALERT]={color:colors.warn,logger:k.warn},b[f.OK]={color:colors.ok,logger:k.info};var d=_.sortBy(g,"filepath"),e="";d.forEach(function(d){var g=d.is,i=d.filepath,k=d.err,l=d.msg,m=d.globals,n=d.globalsList;if(o||g!==f.OK){var p=b[g],q=p.color,r=p.logger,s=g===f.OK?colors.green(g):q(g),t=a.getFilename(i),u=path.dirname(i),v="";l&&(v+=": "+l),k&&(v+=": \""+k.message+"\""),c.idea&&(v+=" ("+a.getIDEALink(i)+")"),e!==u&&(e=u,j.info(""+u)),r(q(""+(" "+s+" "+t)+v)),m&&n&&n.filter(function(a){return!a.isExcluded}).forEach(function(b){var d=b.varName,e=b.lineInfo,f=b.fullFilePath,g=colors.cyan(e.line+":"+e.column),i=" "+g+" "+d;c["idea-globals"]&&(i+=" ("+a.getIDEALink(f,e.line,e.column)+")"),h.warn(i)})}})},this.saveResults=function(){var a=c.save;if(a){var b=_.groupBy(g,function(a){return path.dirname(a.filepath)}),d={files:b,globals:this._globals,stats:{}};d.stats.percentageOfCovered=this._percentageOfConverted(),d.stats.countTotalModules=this._totalModuleCount(),d.stats.countNonAMD=this._countNonAMD,d.stats.countGlobals=this._countGlobals,d.stats.countAMDDefine=this._countAMDDefine,d.stats.countAMDRequire=this._countAMDRequire,this._saveToFile(d,a)}},this._saveToFile=function(a,b){fs.writeFile(b,JSON.stringify(a),function(a){if(a)return console.error(a)})},this.printGlobalsSummary=function(){if(m){!1!==c.files&&i.info("=====================================");var a=[];_(this._globals).filter(function(a){return!a.isExcluded}).countBy("varName").forEach(function(b,c){return a.push({varName:c,count:b})}),_(a).sortBy(["count","varName"]).forEach(function(a){var b=a.count,c=a.varName;i.info(c+" - "+colors.warn(b))})}},this._totalModuleCount=function(){return this._countAMDDefine+this._countAMDRequire},this._percentageOfConverted=function(){var a=this._totalModuleCount();return 100*(a/(a+this._countNonAMD))},this.printSummary=function(a){if(a)throw a;if(!1!==c.summary){(!1!==c.files||m)&&l.info("=====================================");var b=this._percentageOfConverted();l.info("Files identified as AMD modules: "+this._totalModuleCount()),l.info(" define() calls: "+this._countAMDDefine),l.info(" require() calls: "+this._countAMDRequire),l.warn(colors.error("Files not converted: "+this._countNonAMD)),l.warn(colors.warn("Illegal globals found: "+this._countGlobals)),l.info((isNaN(b)?0:Math.round(b,2))+"% converted")}},this.getGlobs=function(a){var c=[],d=a+"/";return e.forEach(function(a){c.push(d+a)}),b.exclude.patterns&&b.exclude.patterns.forEach(function(a){c.push("!"+d+a)}),c},this.run=function(){var c=this;this._excludedGlobals=this.setupExcludedGlobals(b.env,Object.keys(globals.builtin));var d=glob.sync(this.getGlobs(a));d.forEach(function(a){var b=fs.readFileSync(a,"utf8");c.handleFile(null,b,a,function(){})}),this.printFileSummary(),this.printGlobalsSummary(),this.printSummary(),this.saveResults()}}module.exports=ConversionDetector;
"use strict";var _typeof="function"==typeof Symbol&&"symbol"==typeof Symbol.iterator?function(a){return typeof a}:function(a){return a&&"function"==typeof Symbol&&a.constructor===Symbol&&a!==Symbol.prototype?"symbol":typeof a};function _toConsumableArray(a){if(Array.isArray(a)){for(var b=0,c=Array(a.length);b<a.length;b++)c[b]=a[b];return c}return Array.from(a)}var acorn=require("acorn"),glob=require("globby"),detectAMD=require("./detect-amd"),detectLegacyFallback=require("./detect-ajs-namespace"),detectGlobals=require("acorn-globals"),globals=require("globals"),arrayUniq=require("array-uniq"),_=require("lodash"),fs=require("fs"),path=require("path"),colors=require("colors/safe"),winston=require("winston");colors.setTheme({ok:"grey",warn:"yellow",error:"red"});function ConversionDetector(a,b,c){function d(a){return new winston.Logger({level:a})}var f=b.symbols,e=b.patterns||["**/*.js"],g=[],h=[],i=d(),j=d(),k=d(),l=d(c["file-log-level"]),m=d(),n=!!c["globals-summary"],o=c.globals||c["idea-globals"],p=!1!==c.ok;(function(){o&&i.add(winston.transports.Console,{showLevel:!1}),n&&j.add(winston.transports.Console,{showLevel:!1}),!1!==c.files&&(k.add(winston.transports.Console,{showLevel:!1}),l.add(winston.transports.Console,{showLevel:!1})),!1!==c.summary&&m.add(winston.transports.Console,{showLevel:!1})})(),this._excludedGlobals=[],this._globals=[],this._countNonAMD=0,this._countGlobals=0,this._countAMDDefine=0,this._countAMDRequire=0,this.setupExcludedGlobals=function(a,b){var c="undefined"==typeof b?[]:b;return a&&Object.keys(a).filter(function(b){return a[b]}).forEach(function(a){var b=globals[a];b&&(c=arrayUniq(c.concat(Object.keys(b))))}),c},this.getFilename=function(a){return path.basename(a)},this.markAsNonAMD=function(a){++this._countNonAMD,g.push({filepath:a,amd:!1,is:f.ERR,msg:"Not an AMD module"})},this.markAsAMD=function(a,b){++this._countAMDDefine,this.findGlobals(a,b)},this.markAsAMDRequire=function(a,b){++this._countAMDRequire,this.findGlobals(a,b)},this.trimToEndOfVariableName=function(a){for(var b,c=[],d=a.parents.length-1;0<=d;d--)if(b=a.parents[d],"Identifier"===b.type)c.push(b.name);else if("MemberExpression"===b.type)c.push(b.property.name||b.property.value);else if("ThisExpression"===b.type)c.push("this");else break;return namePath.join(".")},this.isExcludedVariable=function(a){var c=[],d=a.split(".").some(function(a){c.push(a);var d=c.join(".");return-1<b.exclude.variables.indexOf(d)});return d},this.isAMDDefine=function(a){return"object"===("undefined"==typeof a?"undefined":_typeof(a))&&a.hasOwnProperty("type")&&"define"===a.type},this.isAMDRequire=function(a){return"object"===("undefined"==typeof a?"undefined":_typeof(a))&&a.hasOwnProperty("type")&&"require"===a.type},this.isNamedAMDModule=function(a){return this.isAMDDefine(a)&&a.hasOwnProperty("name")&&!!a.name},this.getRelevantGlobals=function(a){var b=this;return detectGlobals(a).filter(function(c){if(-1<b._excludedGlobals.indexOf(c.name))return!1;var d=b.getRelevantOccurrences(a,c.nodes);return 0<d.length})},this.getRelevantOccurrences=function(a,b){var c=this;return b?b.filter(function(a){var b=c.trimToEndOfVariableName(a);return!c.isExcludedVariable(b)&&(++c._countGlobals,!0)}):[]},this.processRelevantOccurrences=function(a,b,c){var d=this;return b?b.map(function(b){var e=acorn.getLineInfo(a,b.start),f=d.trimToEndOfVariableName(b),g=d.isExcludedVariable(f);return{varName:f,lineInfo:e,isExcluded:g,fullFilePath:c}}):[]},this.getIDEALink=function(a,b,c){return b&&c?colors.grey.underline("http://localhost:63342/api/file/"+process.cwd()+"/"+a+":"+b+":"+c):colors.underline("http://localhost:63342/api/file/"+process.cwd()+"/"+a)},this.findGlobals=function(a,b){var c=this;try{var d=this.getRelevantGlobals(b),e=[];if(0===d.length)return void g.push({filepath:a,amd:!0,globals:!1,is:f.OK});o&&d.forEach(function(d){var f=c.processRelevantOccurrences(b,d.nodes,a);e=e.concat(f),c._globals=c._globals.concat(f)}),g.push({filepath:a,amd:!0,is:f.ALERT,globals:!0,globalsList:e,msg:"Globals detected"})}catch(b){g.push({filepath:a,amd:!0,is:f.ERR,err:b,msg:"Error finding globals"})}},this.handleFile=function(a,b,c,d){try{if(a)throw new Error(a.message?a.message:a);var e=detectAMD(b),i=e&&e[0];this.isNamedAMDModule(i)?this.markAsAMD(c,b):this.isAMDDefine(i)?this.markAsAMD(c,b):this.isAMDRequire(i)?this.markAsAMDRequire(c,b):this.markAsNonAMD(c);var j=detectLegacyFallback(b);j.length&&h.push.apply(h,_toConsumableArray(j))}catch(a){g.push({filepath:c,is:f.ERR,err:a,msg:"Error parsing file"})}d()},this.printFileSummary=function(){var a=this,b={};b[f.ERR]={color:colors.error,logger:l.error},b[f.ALERT]={color:colors.warn,logger:l.warn},b[f.OK]={color:colors.ok,logger:l.info};var d=_.sortBy(g,"filepath"),e="";d.forEach(function(d){var g=d.is,h=d.filepath,j=d.err,l=d.msg,m=d.globals,n=d.globalsList;if(p||g!==f.OK){var o=b[g],q=o.color,r=o.logger,s=g===f.OK?colors.green(g):q(g),t=a.getFilename(h),u=path.dirname(h),v="";l&&(v+=": "+l),j&&(v+=": \""+j.message+"\""),c.idea&&(v+=" ("+a.getIDEALink(h)+")"),e!==u&&(e=u,k.info(""+u)),r(q(""+(" "+s+" "+t)+v)),m&&n&&n.filter(function(a){return!a.isExcluded}).forEach(function(b){var d=b.varName,e=b.lineInfo,f=b.fullFilePath,g=colors.cyan(e.line+":"+e.column),h=" "+g+" "+d;c["idea-globals"]&&(h+=" ("+a.getIDEALink(f,e.line,e.column)+")"),i.warn(h)})}})},this.saveResults=function(){var a=c.save;if(a){var b=_.groupBy(g,function(a){return path.dirname(a.filepath)}),d={files:b,legacyFallbacks:h,globals:this._globals,stats:{}};d.stats.percentageOfCovered=this._percentageOfConverted(),d.stats.countTotalModules=this._totalModuleCount(),d.stats.countNonAMD=this._countNonAMD,d.stats.countGlobals=this._countGlobals,d.stats.countAMDDefine=this._countAMDDefine,d.stats.countAMDRequire=this._countAMDRequire,this._saveToFile(d,a)}},this._saveToFile=function(a,b){fs.writeFile(b,JSON.stringify(a),function(a){if(a)return console.error(a)})},this.printGlobalsSummary=function(){if(n){!1!==c.files&&j.info("=====================================");var a=[];_(this._globals).filter(function(a){return!a.isExcluded}).countBy("varName").forEach(function(b,c){return a.push({varName:c,count:b})}),_(a).sortBy(["count","varName"]).forEach(function(a){var b=a.count,c=a.varName;j.info(c+" - "+colors.warn(b))})}},this._totalModuleCount=function(){return this._countAMDDefine+this._countAMDRequire},this._percentageOfConverted=function(){var a=this._totalModuleCount();return 100*(a/(a+this._countNonAMD))},this.printSummary=function(a){if(a)throw a;if(!1!==c.summary){(!1!==c.files||n)&&m.info("=====================================");var b=this._percentageOfConverted();m.info("Files identified as AMD modules: "+this._totalModuleCount()),m.info(" define() calls: "+this._countAMDDefine),m.info(" require() calls: "+this._countAMDRequire),m.warn(colors.error("Files not converted: "+this._countNonAMD)),m.warn(colors.warn("Illegal globals found: "+this._countGlobals)),m.info((isNaN(b)?0:Math.round(b,2))+"% converted")}},this.getGlobs=function(a){var c=[],d=a+"/";return e.forEach(function(a){c.push(d+a)}),b.exclude.patterns&&b.exclude.patterns.forEach(function(a){c.push("!"+d+a)}),c},this.run=function(){var c=this;this._excludedGlobals=this.setupExcludedGlobals(b.env,Object.keys(globals.builtin));var d=glob.sync(this.getGlobs(a));d.forEach(function(a){var b=fs.readFileSync(a,"utf8");c.handleFile(null,b,a,function(){})}),this.printFileSummary(),this.printGlobalsSummary(),this.printSummary(),this.saveResults()}}module.exports=ConversionDetector;
{
"name": "amd-conversion-analyser",
"version": "1.5.0",
"version": "1.6.0",
"license": "Apache-2.0",

@@ -5,0 +5,0 @@ "description": "",

@@ -5,2 +5,3 @@ 'use strict';

let detectAMD = require("./detect-amd");
let detectLegacyFallback = require("./detect-ajs-namespace");
let detectGlobals = require("acorn-globals");

@@ -26,2 +27,3 @@ let globals = require("globals");

const fileMessages = [];
const legacyFallbacks = [];

@@ -230,7 +232,7 @@ const globalLogger = createNewLogger();

try {
if (err) throw new Error(err.message ? err.message : err);
const result = detectAMD(content);
const firstNodeInFile = result && result[0];
if (this.isNamedAMDModule(firstNodeInFile)) {

@@ -242,3 +244,3 @@ this.markAsAMD(fullFilePath, content);

}
else if (this.isAMDRequire(firstNodeInFile)) {
else if (this.isAMDRequire(firstNodeInFile)) {
this.markAsAMDRequire(fullFilePath, content);

@@ -249,2 +251,7 @@ }

}
const outcome = detectLegacyFallback(content);
if(outcome.length) {
legacyFallbacks.push(...outcome);
}
} catch (e) {

@@ -322,2 +329,3 @@ fileMessages.push({ filepath: fullFilePath, is: symbols.ERR, err: e, msg: "Error parsing file" });

files: sortedMessages,
legacyFallbacks,
globals: this._globals,

@@ -324,0 +332,0 @@ stats: {}

@@ -16,3 +16,4 @@ 'use strict';

variables: [
'Excluded.variableRightHere'
'Excluded.variableRightHere',
'AJS.namespace'
],

@@ -131,5 +132,5 @@ patterns: [

expect(stubs.info.withArgs(sinon.match("as AMD modules:")).args[0][0])
.to.have.string("as AMD modules: 4");
.to.have.string("as AMD modules: 5");
expect(stubs.info.withArgs(sinon.match("define() calls:")).args[0][0])
.to.have.string("define() calls: 3");
.to.have.string("define() calls: 4");
expect(stubs.info.withArgs(sinon.match("require() calls:")).args[0][0])

@@ -151,3 +152,3 @@ .to.have.string("require() calls: 1");

expect(stubs.info.withArgs(sinon.match("% converted")).args[0][0])
.to.have.string("67% converted");
.to.have.string("71% converted");
});

@@ -253,3 +254,10 @@ });

"is": "o"
}
},
{
"filepath": "./test/data/amdModuleWithLegacyFallback.js",
"amd": true,
"globals": false,
"is": "o"
},
],

@@ -328,8 +336,13 @@ "./test/data/foo/bar": [

],
"legacyFallbacks": [{
"moduleName": "test/data/amdModuleWithLegacyFallback",
"name": "window.amdModule",
"properName": "amdModule"
}],
"stats": {
"percentageOfCovered": 66.66666666666666,
"countTotalModules": 4,
"percentageOfCovered": 71.42857142857143,
"countTotalModules": 5,
"countNonAMD": 2,
"countGlobals": 2,
"countAMDDefine": 3,
"countAMDDefine": 4,
"countAMDRequire": 1

@@ -336,0 +349,0 @@ }

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