All files / lib/report score.js

97.18% Statements 69/71
88.88% Branches 16/18
100% Functions 2/2
97.18% Lines 69/71

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 7227x 27x 27x 27x 27x 27x 27x 27x 27x 27x 27x 27x 27x 27x 27x 27x 27x 27x 27x 27x 27x 27x 27x 27x 27x 5700x 5700x 5700x 5166x 5700x 340x 5700x 162x 5700x 32x 5700x   5700x   5700x 5700x 27x 598x 598x 565x 565x 565x 9638x 9638x 50x 50x 50x 9638x 5085x 5085x 9638x 565x 565x 9638x 9638x 9638x 9638x 9638x 534x 534x 565x 565x 565x 598x 27x 27x  
'use strict'
 
const scores = {
  'readme-exists': -20,
  'readme-size': -10,
  'disk-usage-expanded-size': -15,
  'disk-usage-file-count': -10,
  'disk-usage-dir-count': -10,
  'has-scm-info': -10,
  'scm-tagged-versions': -10,
  'has-install-scripts': -25,
  'has-gyp-file': -20,
  'uses-eval': -20,
  'uses-dynamic-require': -20,
  'missing-strict-mode': -20,
  'has-unsafe-regexps': -50,
  'uses-deprecated-node-apis': -40,
  'has-lost-callback-errs': -30,
  'has-abandoned-promises': -10,
  'has-callback-in-async-function': -25,
  'has-async-without-await': -25,
  license: -100,
  deprecated: -100
}
 
function calcSev (sev) {
  switch (sev) {
    case 'NONE':
      return 0
    case 'LOW':
      return -10
    case 'MEDIUM':
      return -25
    case 'HIGH':
      return -50
    case 'CRITICAL':
      return -85
    default:
      return 0
  }
}
 
function score (pkgScores = [], maxSeverity) {
  if (maxSeverity === 4) return 0
  let totalScore = 100
  let sevScore = 0
  pkgScores.forEach(({ pass, group, severity, name }) => {
    switch (group) {
      case 'security':
        sevScore = calcSev(severity)
        if (sevScore !== 0) sevScore += -100
        break
      case 'risk':
        sevScore += calcSev(severity) * 2
        break
      case 'compliance':
        sevScore += calcSev(severity)
        break
    }
 
    // Quality scores do not affect a package’s risk level
    // https://docs.nodesource.com/ncmv2/docs#quality-severity
    if (!pass && group !== 'quality') {
      sevScore += scores[name] || 0
    }
  })
  totalScore += sevScore
  return Math.max(totalScore, 0)
}
 
module.exports = score