Skip to content

Commit f35d9c5

Browse files
committed
Fix vulnerable deps
1 parent b903bd8 commit f35d9c5

14 files changed

+5594
-223
lines changed

Gruntfile.js

Lines changed: 151 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,151 @@
1+
const requirejs = require('requirejs');
2+
const amdclean = require('./build/amdclean');
3+
const fs = require("fs");
4+
const Jasmine = require('jasmine');
5+
6+
module.exports = function (grunt) {
7+
function getHeaderText() {
8+
let packageJson = JSON.parse(fs.readFileSync('./package.json', 'utf8')),
9+
licenseText = '\n\n/*' + fs.readFileSync('./LICENSE.txt', 'utf8') + '\n*/\n\n',
10+
currentDate = (function () {
11+
var today = new Date(),
12+
dd = today.getDate(),
13+
mm = today.getMonth() + 1,
14+
yyyy = today.getFullYear();
15+
16+
if (dd < 10) {
17+
dd = '0' + dd
18+
}
19+
20+
if (mm < 10) {
21+
mm = '0' + mm
22+
}
23+
24+
today = yyyy + '-' + mm + '-' + dd;
25+
return today;
26+
}()),
27+
currentYear = (function () {
28+
var today = new Date(),
29+
yyyy = today.getFullYear();
30+
31+
return yyyy;
32+
}());
33+
return '/*! amdclean - v' + packageJson.version + ' - ' + currentDate +
34+
'\n* https://github.com/gfranko/amdclean' +
35+
'\n* Copyright (c) ' + currentYear + ' Greg Franko */\n' + licenseText;
36+
}
37+
const header = getHeaderText();
38+
// Project configuration.
39+
grunt.initConfig({
40+
pkg: grunt.file.readJSON('package.json'),
41+
uglify: {
42+
options: {
43+
banner: header,
44+
},
45+
build: {
46+
src: 'src/amdclean.js',
47+
dest: 'build/amdclean.min.js'
48+
}
49+
},
50+
jshint: {
51+
amdclean: {
52+
options: {
53+
"loopfunc": true,
54+
"evil": true,
55+
},
56+
src: ['src/amdclean.js'],
57+
},
58+
},
59+
requirejs: {
60+
"./build/amdclean.optimized.js": {
61+
'findNestedDependencies': false,
62+
'baseUrl': './src/modules/',
63+
'optimize': 'none',
64+
'paths': {
65+
'amdclean': 'index'
66+
},
67+
'include': ['amdclean'],
68+
}
69+
},
70+
amdclean: {
71+
"./build/amdclean.optimized.cleaned.js": {
72+
'filePath': "./build/amdclean.optimized.js",
73+
'transformAMDChecks': false,
74+
'aggressiveOptimizations': true,
75+
'ignoreModules': ['esprima', 'estraverse', 'escodegen', 'lodash', 'fs', 'sourcemap_to_ast'], // wtf? parsed name here?
76+
'removeUseStricts': false,
77+
'wrap': {
78+
// All of the third party dependencies are hoisted here
79+
// It's a hack, but it's not too painful
80+
'start': ';(function(esprima, estraverse, escodegen, _, sourcemapToAst) {\n',
81+
'end': '}(typeof esprima !== "undefined" ? esprima: null, typeof estraverse !== "undefined" ? estraverse: null, typeof escodegen !== "undefined" ? escodegen: null, typeof _ !== "undefined" ? _ : null, typeof sourcemapToAst !== "undefined" ? sourcemapToAst : null));'
82+
},
83+
'createAnonymousAMDModule': true
84+
}
85+
},
86+
prepend: {
87+
"./src/amdclean.js": {
88+
header: getHeaderText,
89+
src: "./build/amdclean.optimized.cleaned.js",
90+
}
91+
}
92+
});
93+
94+
// Load the plugin that provides the "uglify" task.
95+
grunt.loadNpmTasks('grunt-contrib-uglify');
96+
grunt.loadNpmTasks('grunt-contrib-jshint');
97+
// Default task(s).
98+
grunt.registerTask('build', ['requirejs', 'amdclean', 'prepend:./src/amdclean.js']);
99+
grunt.registerTask('default', ['build', 'jshint:amdclean', 'test']);
100+
101+
grunt.registerTask('lint', ['build', 'jshint:amdclean']);
102+
grunt.registerTask('minify', ['build', 'jshint:amdclean', 'test', 'uglify']);
103+
104+
105+
grunt.registerTask('test', 'Runs Jasmine on the Spec File', function() {
106+
const cb = this.async();
107+
const jasmine = new Jasmine();
108+
jasmine.loadConfig({
109+
spec_dir: 'test/specs',
110+
spec_files: [
111+
'convert.js'
112+
]
113+
});
114+
jasmine.exitOnCompletion = false;
115+
jasmine.execute().then((data)=>{
116+
cb(data.overallStatus === "passed");
117+
});
118+
});
119+
120+
grunt.registerMultiTask('requirejs', 'Uses RequireJS to optimize a file', function () {
121+
const target = this.target;
122+
const data = this.data;
123+
const cb = this.async();
124+
requirejs.optimize({
125+
...data,
126+
out: target,
127+
'onModuleBundleComplete': function (data) {
128+
cb();
129+
}
130+
});
131+
});
132+
133+
grunt.registerMultiTask('amdclean', "Uses AMDClean on a file", function () {
134+
const target = this.target;
135+
const data = this.data;
136+
const code = amdclean.clean({
137+
...data,
138+
});
139+
fs.writeFileSync(target, code);
140+
});
141+
grunt.registerMultiTask('prepend', 'Prepends some text to a file', function() {
142+
const target = this.target;
143+
const data = this.data;
144+
let header = data.header;
145+
if(typeof header === "function") {
146+
header = header();
147+
}
148+
const file = header + fs.readFileSync(data.src);
149+
fs.writeFileSync(target, file);
150+
})
151+
};

build/amdclean.js

Lines changed: 21 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -586,14 +586,14 @@ convertToFunctionExpression = function convertToFunctionExpression(obj) {
586586
});
587587
callbackFunc.body.body = body;
588588
// Returns an array of all return statements
589-
returnStatements = _.where(body, { 'type': 'ReturnStatement' });
590-
exportsExpressions = _.where(body, {
589+
returnStatements = _.filter(body, { 'type': 'ReturnStatement' });
590+
exportsExpressions = _.filter(body, {
591591
'left': {
592592
'type': 'Identifier',
593593
'name': 'exports'
594594
}
595595
});
596-
moduleExportsExpressions = _.where(body, {
596+
moduleExportsExpressions = _.filter(body, {
597597
'left': {
598598
'type': 'MemberExpression',
599599
'object': {
@@ -647,18 +647,18 @@ convertToFunctionExpression = function convertToFunctionExpression(obj) {
647647
}(), hasReturnStatement = function () {
648648
var returns = [];
649649
if (callbackFunc && callbackFunc.body && _.isArray(callbackFunc.body.body)) {
650-
returns = _.where(callbackFunc.body.body, { 'type': 'ReturnStatement' });
650+
returns = _.filter(callbackFunc.body.body, { 'type': 'ReturnStatement' });
651651
if (returns.length) {
652652
return true;
653653
}
654654
}
655655
return false;
656656
}(), originalCallbackFuncParams, hasExportsParam = function () {
657657
var cbParams = callbackFunc.params || [];
658-
return _.where(cbParams, { 'name': 'exports' }).length;
658+
return _.filter(cbParams, { 'name': 'exports' }).length;
659659
}(), hasModuleParam = function () {
660660
var cbParams = callbackFunc.params || [];
661-
return _.where(cbParams, { 'name': 'module' }).length;
661+
return _.filter(cbParams, { 'name': 'module' }).length;
662662
}(), normalizeDependencyNames = {}, dependencyNames = function () {
663663
var deps = [], currentName;
664664
_.each(dependencies, function (currentDependency) {
@@ -709,7 +709,7 @@ convertToFunctionExpression = function convertToFunctionExpression(obj) {
709709
if (node.id && node.id.name && node.init && node.init['arguments'] && node.init['arguments'][0] && node.init['arguments'][0].value) {
710710
variableName = node.id.name;
711711
expressionName = normalizeModuleName.call(amdclean, utils.normalizeDependencyName(moduleId, node.init['arguments'][0].value, moduleId));
712-
if (!_.contains(ignoreModules, expressionName) && variableName === expressionName) {
712+
if (!_.includes(ignoreModules, expressionName) && variableName === expressionName) {
713713
matchingNames.push({
714714
'originalName': expressionName,
715715
'newName': findNewParamName(expressionName),
@@ -792,7 +792,7 @@ convertToFunctionExpression = function convertToFunctionExpression(obj) {
792792
'count': 1
793793
}];
794794
} else {
795-
mappedParameter = _.where(amdclean.callbackParameterMap[dependencyNames[iterator].name], { 'name': currentName });
795+
mappedParameter = _.filter(amdclean.callbackParameterMap[dependencyNames[iterator].name], { 'name': currentName });
796796
if (mappedParameter.length) {
797797
mappedParameter = mappedParameter[0];
798798
mappedParameter.count += 1;
@@ -852,11 +852,11 @@ convertToFunctionExpression = function convertToFunctionExpression(obj) {
852852
if (utils.isRequireExpression(node)) {
853853
if (node['arguments'] && node['arguments'][0] && node['arguments'][0].value) {
854854
normalizedModuleName = normalizeModuleName.call(amdclean, utils.normalizeDependencyName(moduleId, node['arguments'][0].value, moduleId));
855-
if (_.contains(ignoreModules, normalizedModuleName)) {
855+
if (_.includes(ignoreModules, normalizedModuleName)) {
856856
return node;
857857
}
858-
if (_.where(matchingRequireExpressionNames, { 'originalName': normalizedModuleName }).length) {
859-
newName = _.where(matchingRequireExpressionNames, { 'originalName': normalizedModuleName })[0].newName;
858+
if (_.filter(matchingRequireExpressionNames, { 'originalName': normalizedModuleName }).length) {
859+
newName = _.filter(matchingRequireExpressionNames, { 'originalName': normalizedModuleName })[0].newName;
860860
}
861861
return {
862862
'type': 'Identifier',
@@ -898,13 +898,13 @@ convertToObjectDeclaration = function (obj, type) {
898898
modReturnValue = obj.moduleReturnValue;
899899
callee = modReturnValue.callee;
900900
params = callee.params;
901-
if (params && params.length && _.isArray(params) && _.where(params, { 'name': 'global' })) {
901+
if (params && params.length && _.isArray(params) && _.filter(params, { 'name': 'global' })) {
902902
if (_.isObject(callee.body) && _.isArray(callee.body.body)) {
903-
returnStatement = _.where(callee.body.body, { 'type': 'ReturnStatement' })[0];
903+
returnStatement = _.filter(callee.body.body, { 'type': 'ReturnStatement' })[0];
904904
if (_.isObject(returnStatement) && _.isObject(returnStatement.argument) && returnStatement.argument.type === 'FunctionExpression') {
905905
internalFunctionExpression = returnStatement.argument;
906906
if (_.isObject(internalFunctionExpression.body) && _.isArray(internalFunctionExpression.body.body)) {
907-
nestedReturnStatement = _.where(internalFunctionExpression.body.body, { 'type': 'ReturnStatement' })[0];
907+
nestedReturnStatement = _.filter(internalFunctionExpression.body.body, { 'type': 'ReturnStatement' })[0];
908908
if (_.isObject(nestedReturnStatement.argument) && _.isObject(nestedReturnStatement.argument.right) && _.isObject(nestedReturnStatement.argument.right.property)) {
909909
if (nestedReturnStatement.argument.right.property.name) {
910910
modReturnValue = {
@@ -1032,7 +1032,7 @@ convertDefinesAndRequires = function convertDefinesAndRequires(node, parent) {
10321032
} else {
10331033
deps = [];
10341034
}
1035-
hasExportsParam = _.where(deps, { 'value': 'exports' }).length;
1035+
hasExportsParam = _.filter(deps, { 'value': 'exports' }).length;
10361036
if (_.isArray(deps) && deps.length) {
10371037
_.each(deps, function (currentDependency) {
10381038
if (dependencyBlacklist[currentDependency.value] && !shouldOptimize) {
@@ -1072,7 +1072,7 @@ convertDefinesAndRequires = function convertDefinesAndRequires(node, parent) {
10721072
amdclean.options.ignoreModules.push(moduleName);
10731073
return node;
10741074
}
1075-
if (_.contains(options.removeModules, moduleName)) {
1075+
if (_.includes(options.removeModules, moduleName)) {
10761076
// Remove the current module from the source
10771077
return { 'type': 'EmptyStatement' };
10781078
}
@@ -1089,7 +1089,7 @@ convertDefinesAndRequires = function convertDefinesAndRequires(node, parent) {
10891089
} else if (params.moduleReturnValue && params.moduleReturnValue.type === 'Identifier') {
10901090
type = 'functionExpression';
10911091
}
1092-
if (_.contains(options.ignoreModules, moduleName)) {
1092+
if (_.includes(options.ignoreModules, moduleName)) {
10931093
return node;
10941094
} else if (utils.isFunctionExpression(moduleReturnValue) || type === 'functionExpression') {
10951095
return convertToFunctionExpression.call(amdclean, params);
@@ -1116,16 +1116,16 @@ convertDefinesAndRequires = function convertDefinesAndRequires(node, parent) {
11161116
}
11171117
} else {
11181118
// If the node is a function expression that has an exports parameter and does not return anything, return exports
1119-
if (node.type === 'FunctionExpression' && _.isArray(node.params) && _.where(node.params, {
1119+
if (node.type === 'FunctionExpression' && _.isArray(node.params) && _.filter(node.params, {
11201120
'type': 'Identifier',
11211121
'name': 'exports'
1122-
}).length && _.isObject(node.body) && _.isArray(node.body.body) && !_.where(node.body.body, { 'type': 'ReturnStatement' }).length) {
1122+
}).length && _.isObject(node.body) && _.isArray(node.body.body) && !_.filter(node.body.body, { 'type': 'ReturnStatement' }).length) {
11231123
parentHasFunctionExpressionArgument = function () {
11241124
if (!parent || !parent.arguments) {
11251125
return false;
11261126
}
11271127
if (parent && parent.arguments && parent.arguments.length) {
1128-
return _.where(parent.arguments, { 'type': 'FunctionExpression' }).length;
1128+
return _.filter(parent.arguments, { 'type': 'FunctionExpression' }).length;
11291129
}
11301130
return false;
11311131
}();
@@ -1432,7 +1432,7 @@ clean = function clean() {
14321432
}, {})), hoistedCallbackParameters);
14331433
// Creates variable declarations for each AMD module/callback parameter that needs to be hoisted
14341434
_.each(hoistedVariables, function (moduleValue, moduleName) {
1435-
if (!_.contains(options.ignoreModules, moduleName)) {
1435+
if (!_.includes(options.ignoreModules, moduleName)) {
14361436
var _initValue = amdclean.exportsModules[moduleName] !== true ? null : {
14371437
type: 'ObjectExpression',
14381438
properties: []

build/amdclean.min.js

Lines changed: 5 additions & 5 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)