Skip to content

Commit

Permalink
feat: prettier is a dev dependency
Browse files Browse the repository at this point in the history
..and upgrade it while we're here
  • Loading branch information
FauxFaux committed May 26, 2020
1 parent 5a50d0e commit 371ffc4
Show file tree
Hide file tree
Showing 37 changed files with 194 additions and 197 deletions.
1 change: 0 additions & 1 deletion .prettierrc.json
@@ -1,6 +1,5 @@
{
"arrowParens": "always",
"trailingComma": "es5",
"singleQuote": true,
"htmlWhitespaceSensitivity": "ignore"
}
2 changes: 1 addition & 1 deletion lib/add.js
Expand Up @@ -16,7 +16,7 @@ function add(policy, type, options) {

const id = options.id;
const path = options.path;
const data = Object.keys(options).reduce(function(acc, curr) {
const data = Object.keys(options).reduce(function (acc, curr) {
if (curr === 'id' || curr === 'path') {
return acc;
}
Expand Down
2 changes: 1 addition & 1 deletion lib/filter/get-vuln-source.js
Expand Up @@ -9,7 +9,7 @@ const statSync = require('fs').statSync;
let { parsePackageString: moduleToObject } = require('snyk-module');

function getVulnSource(vuln, cwd, live) {
const from = vuln.from.slice(1).map(function(pkg) {
const from = vuln.from.slice(1).map(function (pkg) {
return moduleToObject(pkg).name;
});

Expand Down
6 changes: 3 additions & 3 deletions lib/filter/ignore.js
Expand Up @@ -20,7 +20,7 @@ function filterIgnored(ignore, vuln, filtered) {
const now = new Date().toJSON();

return vuln
.map(function(vuln) {
.map(function (vuln) {
if (!ignore[vuln.id]) {
return vuln;
}
Expand All @@ -33,7 +33,7 @@ function filterIgnored(ignore, vuln, filtered) {
// keep it.

// if rules.find, then ignore vuln
const appliedRules = ignore[vuln.id].filter(function(rule) {
const appliedRules = ignore[vuln.id].filter(function (rule) {
const path = Object.keys(rule)[0]; // this is a string
let expires = rule[path].expires;

Expand Down Expand Up @@ -77,7 +77,7 @@ function filterIgnored(ignore, vuln, filtered) {

if (appliedRules.length) {
vuln.filtered = {
ignored: appliedRules.map(function(rule) {
ignored: appliedRules.map(function (rule) {
const path = Object.keys(rule)[0];
const ruleData = cloneDeep(rule[path]);
ruleData.path = path.split(' > ');
Expand Down
2 changes: 1 addition & 1 deletion lib/filter/index.js
Expand Up @@ -60,7 +60,7 @@ function filter(vulns, policy, root) {
};
const level = levels[policy.failThreshold];
vulns.ok = true;
vulns.vulnerabilities.some(function(vuln) {
vulns.vulnerabilities.some(function (vuln) {
if (levels[vuln.severity] >= level) {
vulns.ok = false;
return true; // breaks
Expand Down
4 changes: 2 additions & 2 deletions lib/filter/notes.js
Expand Up @@ -10,15 +10,15 @@ function attachNotes(notes, vuln) {
debug('attaching notes');
const now = new Date().toJSON();

return vuln.map(function(vuln) {
return vuln.map(function (vuln) {
if (!notes[vuln.id]) {
return vuln;
}

debug('%s has rules', vuln.id);

// if rules.some, then add note to the vuln
notes[vuln.id].forEach(function(rule) {
notes[vuln.id].forEach(function (rule) {
const path = Object.keys(rule)[0]; // this is a string
let expires = rule[path].expires;

Expand Down
8 changes: 4 additions & 4 deletions lib/filter/patch.js
Expand Up @@ -19,7 +19,7 @@ function filterPatched(patched, vulns, cwd, skipVerifyPatch, filteredPatches) {

debug('filtering patched');
return vulns
.map(function(vuln) {
.map(function (vuln) {
if (!patched[vuln.id]) {
return vuln;
}
Expand All @@ -33,7 +33,7 @@ function filterPatched(patched, vulns, cwd, skipVerifyPatch, filteredPatches) {

// if rules.find, then ignore vuln
const vulnRules = patched[vuln.id]
.map(function(rule) {
.map(function (rule) {
// first check if the path is a match on the rule
const pathMatch = matchToRule(vuln, rule);

Expand All @@ -52,7 +52,7 @@ function filterPatched(patched, vulns, cwd, skipVerifyPatch, filteredPatches) {
.filter(Boolean);

// run through the potential rules to check if there's a patch flag in place
const appliedRules = vulnRules.filter(function() {
const appliedRules = vulnRules.filter(function () {
// the target directory where our module name will live
if (skipVerifyPatch) {
return true;
Expand All @@ -79,7 +79,7 @@ function filterPatched(patched, vulns, cwd, skipVerifyPatch, filteredPatches) {

if (appliedRules.length) {
vuln.filtered = {
patches: appliedRules.map(function(rule) {
patches: appliedRules.map(function (rule) {
const path = Object.keys(rule)[0];
const ruleData = cloneDeep(rule[path]) || {};
ruleData.path = path.split(' > ');
Expand Down
40 changes: 20 additions & 20 deletions lib/index.js
Expand Up @@ -29,7 +29,7 @@ function defaultFilename() {
}

function attachMethods(policy) {
policy.filter = function(vulns, root) {
policy.filter = function (vulns, root) {
return filter(vulns, policy, root || path.dirname(policy.__filename));
};
policy.save = save.bind(null, policy);
Expand All @@ -42,7 +42,7 @@ function attachMethods(policy) {
}

function loadFromText(text) {
return new Promise(function(resolve) {
return new Promise(function (resolve) {
const policy = parse.import(text);
const now = Date.now();

Expand Down Expand Up @@ -86,14 +86,14 @@ function load(root, options) {
filename = path.resolve(filename, '.snyk');
}

const promise = new Promise(function(resolve) {
const promise = new Promise(function (resolve) {
if (ignorePolicy) {
return resolve(parse.import());
}

if (!ignorePolicy && Array.isArray(root)) {
return resolve(
mergePolicies(root, options).then(function(res) {
mergePolicies(root, options).then(function (res) {
if (debug.enabled) {
debug('final policy:');
debug(JSON.stringify(res, '', 2));
Expand All @@ -108,21 +108,21 @@ function load(root, options) {

const promises = [
promise,
fs.stat(filename).catch(function() {
fs.stat(filename).catch(function () {
return {};
}),
];

return Promise.all(promises)
.catch(function(error) {
.catch(function (error) {
if (options.loose && error.code === 'ENOENT') {
debug('ENOENT on file, but running loose');
return [parse.import(), {}];
}

throw error;
})
.then(function(res) {
.then(function (res) {
const policy = res[0];

policy.__modified = res[1].mtime;
Expand All @@ -143,30 +143,30 @@ function mergePolicies(policyDirs, options) {
const ignoreTarget = options['trust-policies'] ? 'ignore' : 'suggest';

return Promise.all(
policyDirs.map(function(dir) {
policyDirs.map(function (dir) {
return load(dir, options);
})
).then(function(policies) {
).then(function (policies) {
// firstly extend the paths in the ignore and patch
const rootPolicy = policies[0];
const others = policies.slice(1);

return Promise.all(
others
.filter(function(policy) {
.filter(function (policy) {
return policy.__filename; // filter out non loaded policies
})
.map(function(policy) {
.map(function (policy) {
const filename = path.dirname(policy.__filename) + '/package.json';

return tryRequire(filename).then(function(pkg) {
return tryRequire(filename).then(function (pkg) {
const full = pkg.name + '@' + pkg.version;

mergePath('ignore', ignoreTarget, full, rootPolicy, policy);
mergePath('patch', 'patch', full, rootPolicy, policy);
});
})
).then(function() {
).then(function () {
return rootPolicy;
});
});
Expand All @@ -178,9 +178,9 @@ function mergePath(type, into, pathRoot, rootPolicy, policy) {
rootPolicy[into] = {};
}

Object.keys(policy[type]).forEach(function(id) {
Object.keys(policy[type]).forEach(function (id) {
// convert the path from `module@version` to `parent > module@version`
policy[type][id] = policy[type][id].map(function(path) {
policy[type][id] = policy[type][id].map(function (path) {
// this is because our policy file format favours "readable" yaml,
// instead of easy to use object structures.
const key = Object.keys(path).pop();
Expand Down Expand Up @@ -208,17 +208,17 @@ function save(object, root, spinner) {
const lbl = 'Saving .snyk policy file...';

if (!spinner) {
spinner = function(res) {
spinner = function (res) {
return Promise.resolve(res);
};
spinner.clear = spinner;
}

return spinner(lbl)
.then(function() {
.then(function () {
return parse.export(object);
})
.then(function(yaml) {
.then(function (yaml) {
return fs.writeFile(filename, yaml);
})
.then(spinner.clear(lbl));
Expand All @@ -227,10 +227,10 @@ function save(object, root, spinner) {
/* istanbul ignore if */
if (!module.parent) {
load(process.argv[2])
.then(function(res) {
.then(function (res) {
console.log(JSON.stringify(res, '', 2));
})
.catch(function(e) {
.catch(function (e) {
console.log(e.stack);
});
}
12 changes: 6 additions & 6 deletions lib/match.js
Expand Up @@ -21,7 +21,7 @@ function matchPath(from, path) {
const parts = path.split(' > ');
debugPolicy('checking path: %s vs. %s', path, from);
let offset = 0;
const res = parts.every(function(pkg, i) {
const res = parts.every(function (pkg, i) {
debugPolicy('for %s...(against %s)', pkg, from[i + offset]);
let fromPkg = from[i + offset] ? moduleToObject(from[i + offset]) : {};

Expand Down Expand Up @@ -106,7 +106,7 @@ function matchPath(from, path) {
}

function matchToRule(vuln, rule) {
return Object.keys(rule).some(function(path) {
return Object.keys(rule).some(function (path) {
return matchToSingleRule(vuln, path);
});
}
Expand All @@ -132,18 +132,18 @@ function getByVuln(policy, vuln) {
return found;
}

['ignore', 'patch'].forEach(function(key) {
Object.keys(policy[key] || []).forEach(function(p) {
['ignore', 'patch'].forEach(function (key) {
Object.keys(policy[key] || []).forEach(function (p) {
if (p === vuln.id) {
policy[key][p].forEach(function(rule) {
policy[key][p].forEach(function (rule) {
if (matchToRule(vuln, rule)) {
found = {
type: key,
id: vuln.id,
rule: vuln.from,
};
const rootRule = Object.keys(rule).pop();
Object.keys(rule[rootRule]).forEach(function(key) {
Object.keys(rule[rootRule]).forEach(function (key) {
found[key] = rule[rootRule][key];
});
}
Expand Down
2 changes: 1 addition & 1 deletion lib/parser/add-comments.js
Expand Up @@ -14,7 +14,7 @@ function addComments(policyExport) {
const lines = policyExport.split('\n');
lines.unshift(initialComment);

Object.keys(inlineComments).forEach(function(key) {
Object.keys(inlineComments).forEach(function (key) {
const position = lines.indexOf(key + ':');
if (position !== -1) {
lines.splice(position, 0, inlineComments[key]);
Expand Down
6 changes: 3 additions & 3 deletions lib/parser/demunge.js
Expand Up @@ -5,10 +5,10 @@ function demunge(policy, apiRoot) {
apiRoot = '';
}

const res = ['ignore', 'patch'].reduce(function(acc, type) {
const res = ['ignore', 'patch'].reduce(function (acc, type) {
acc[type] = policy[type]
? Object.keys(policy[type]).map(function(id) {
const paths = policy[type][id].map(function(pathObj) {
? Object.keys(policy[type]).map(function (id) {
const paths = policy[type][id].map(function (pathObj) {
const path = Object.keys(pathObj).pop();
const res = {
path: path,
Expand Down
2 changes: 1 addition & 1 deletion lib/parser/index.js
Expand Up @@ -43,7 +43,7 @@ function exports(policy) {
const data = cloneDeep(policy);

// remove any private information on the policy
Object.keys(data).map(function(key) {
Object.keys(data).map(function (key) {
if (key.indexOf('__') === 0) {
delete data[key];
}
Expand Down
14 changes: 7 additions & 7 deletions lib/parser/v1.js
Expand Up @@ -9,7 +9,7 @@ module.exports = function imports(policy) {
policy.patch = {};
}

Object.keys(policy.patch).forEach(function(id) {
Object.keys(policy.patch).forEach(function (id) {
if (!Array.isArray(policy.patch[id])) {
delete policy.patch[id];
}
Expand All @@ -34,7 +34,7 @@ function checkForOldFormat(ignore) {
// this is a cursory test to ensure that we're working with a snyk format
// that we recognise. if the property is an object, then it's the early
// alpha format, and we'll throw
Object.keys(ignore).forEach(function(id) {
Object.keys(ignore).forEach(function (id) {
if (!Array.isArray(ignore[id])) {
const error = new Error('old, unsupported .snyk format detected');
error.code = 'OLD_DOTFILE_FORMAT';
Expand All @@ -47,7 +47,7 @@ function validate(policy) {
const fix = needsFixing(policy);

if (fix) {
fix.forEach(function(item) {
fix.forEach(function (item) {
const o = {};
o[item.key] = item.rule;
policy[item.id].push(o);
Expand All @@ -57,8 +57,8 @@ function validate(policy) {

function needsFixing(policy) {
const move = [];
Object.keys(policy).forEach(function(id) {
policy[id].forEach(function(rule) {
Object.keys(policy).forEach(function (id) {
policy[id].forEach(function (rule) {
const keys = Object.keys(rule);
keys.shift(); // drop the first

Expand All @@ -68,7 +68,7 @@ function needsFixing(policy) {

// this means our policy has become corrupted, and we need to move
// the additional keys into their own position in the policy
keys.forEach(function(key) {
keys.forEach(function (key) {
move.push({
id: id,
key: key,
Expand All @@ -92,7 +92,7 @@ function getFailThreshold(policy) {
'failthreshold',
'threshold',
'fail_threshhold',
].some(function(key) {
].some(function (key) {
// if we have the value, set it and return it - which will exit loop
return (threshold = policy[key] || null); // jshint ignore:line
});
Expand Down

0 comments on commit 371ffc4

Please sign in to comment.