all files / src/ NpmPackageJsonLint.js

100% Statements 36/36
93.75% Branches 15/16
100% Functions 5/5
100% Lines 36/36
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 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105                                          11×   11×                                                                                              
'use strict';
 
const chalk = require('chalk');
const Config = require('./Config');
const inArray = require('in-array');
const isPlainObj = require('is-plain-obj');
const Rules = require('./Rules');
 
class NpmPackageJsonLint {
 
  /**
   * constructor
   * @param  {Object}           packageJsonData   Valid package.json data
   * @param  {Object|String}    config            Object or string with desired configuration
   * @param  {Object}           options           Object containing run options
   */
  constructor(packageJsonData, config, options) {
    this.packageJsonData = packageJsonData;
    this.ignoreWarnings = options.ignoreWarnings;
    this.arrayRuleTypes = ['valid-values', 'no-restricted-dependencies', 'no-restricted-pre-release-dependencies'];
    this.errors = [];
    this.warnings = [];
 
    this.rules = new Rules();
    this.rules.load();
    this.config = config;
  }
 
  /**
   * Main execution method for package json lint.
   * @return {Object} Results object
   */
  lint() {
    const configObj = this._getConfig(this.config);
 
    for (const rule in configObj) {
      const ruleModule = this.rules.get(rule);
 
      if (inArray(this.arrayRuleTypes, ruleModule.ruleType)) {
        const errorWarningOffSetting = typeof configObj[rule] === 'string' && configObj[rule] === 'off' ? configObj[rule] : configObj[rule][0];
        const ruleConfigArray = configObj[rule][1];
 
        if (errorWarningOffSetting !== 'off') {
          this._processResult(ruleModule.lint(this.packageJsonData, errorWarningOffSetting, ruleConfigArray), errorWarningOffSetting);
        }
 
      } else {
        const errorWarningOffSetting = configObj[rule];
 
        if (errorWarningOffSetting !== 'off') {
          this._processResult(ruleModule.lint(this.packageJsonData, errorWarningOffSetting), errorWarningOffSetting);
        }
      }
    }
 
    return this._getResultsObject();
  }
 
  /**
   * Private method for processing the result
   * @param   {Object|Boolean} lintResult  Either true if no errors or a LintIssue object is invalid
   * @param   {String}         lintType    Error or warning for issue type
   * @return  {undefined}                  No return
   */
  _processResult(lintResult, lintType) {
    Eif (typeof lintResult !== 'boolean') {
      if (lintType === 'error') {
        this.errors.push(lintResult);
      } else {
        this.warnings.push(lintResult);
      }
    }
  }
 
  /**
   * Private method for getting the results object
   * @return {Object} Results based on the run
   */
  _getResultsObject() {
    const result = {
      errors: this.errors
    };
 
    if (!this.ignoreWarnings) {
      result.warnings = this.warnings;
    }
 
    return result;
  }
 
  /**
   * Private method for loading config
   * @param  {Object|String} config   Object or string with desired configuration
   * @return {Object}                 Configuration object for the run
   */
  _getConfig(config) {
    const configObj = new Config(config);
 
    return configObj.get();
  }
 
}
 
module.exports = NpmPackageJsonLint;