All files / create-gas-project/lib/classes ESLintrc.js

9.09% Statements 3/33
0% Branches 0/20
0% Functions 0/7
10.34% Lines 3/29

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 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 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 1331x       1x                                                                                                                                                                                                                                                             1x  
const Configfile = require( './Config-file' );
 
 
 
const defaultConfig = {
  'env': {
    'amd'                               : true,
    'es6'                               : true,
    'googleappsscript/googleappsscript' : true,
    'node'                              : true
  },
  'extends' : '',
  'globals' : {
    'Calendar'          : true,
    'CalendarApp'       : true,
    'CardService'       : true,
    'Charts'            : true,
    'ContactsApp'       : true,
    'DataStudioApp'     : true,
    'DocumentApp'       : true,
    'Drive'             : true,
    'DriveApp'          : true,
    'FirebaseApp'       : true,
    'FormApp'           : true,
    'Gmail'             : true,
    'GmailApp'          : true,
    'GroupsApp'         : true,
    'HtmlService'       : true,
    'LanguageApp'       : true,
    'MailApp'           : true,
    'Maps'              : true,
    'OAuth1'            : true,
    'OAuth2'            : true,
    'PropertiesService' : true,
    'SitesApp'          : true,
    'Slides'            : true,
    'SlidesApp'         : true,
    'SpreadsheetApp'    : true
  },
  'parser'  : 'babel-eslint',
  'plugins' : [ 'googleappsscript' ],
  'root'    : true
};
 
/**
 * User-specified options from prompt interface
 * @param {*} options
 * @returns
 */
function eslintConfigObj( options ) {
  const eslintObj = options.filePath ?
    useExistingEslintObj( options ) :
    createNewEslintObj( options );
 
  console.log( chalk.grey( `Printed from ${__filename}` ) );
  print( eslintObj );
 
  return eslintObj;
};
 
/** @class ESLintrc */
class ESLintrc extends Configfile {
  /**
   * Creates an instance of ESLintrc.
   * @param {Object} object An eslint ruleset to use for generating the eslintrc instance.
   * @memberof ESLintrc
   */
  constructor( object ) {
    super();
    this.default = defaultConfig;
 
    if ( object && this.validate() ) this.importedObject = object;
    else this.importedObject = defaultConfig;
 
  }
 
  /**
   * @param {string} value The value for the "extends" rule
   * @memberof ESLintrc
   */
  setExtendsRule( value ) {
    const acceptValues = [ 'standard', 'eslint:recommended', 'airbnb-base' ];
    if ( acceptValues.includes( value ) ) {
      if ( typeof value === 'string' ) this.content.extends = value;
      else throw Error( 'Input must be of type string. Received: ' + typeof value );
    } else {
      throw Error( `Unrecognized value for the "extends" rule: ${value} Available values: ` +
        acceptValues.reduce( ( prev, cur ) => prev + '\n' + cur )
      );
    }
 
  }
 
  /**
   * Adds the 'googleappsscript' environment rule
   * @memberof ESLintrc
   */
  addEnvProp() {
    const env = this.importedObject.env;
    const propKey = 'googleappsscript/googleappsscript';
    if ( !env[ propKey ] ) env[ propKey ] = true;
  }
 
  /**
   * Modifies the rule set to be ideal for local Apps Script dev tooling
   * @memberof ESLintrc
   */
  modify() {
    Object.keys( this.default ).forEach( ( key ) => {
      if ( key === 'extends' ) {
        /* Don't overwrite the user's existing base config */
        if ( this.importedObject.extends ) return;
      }
      if ( key === 'plugins' ) {
        if ( this.importedObject[ key ] instanceof Array ) {
          /* Only add to the existing plugin array - don't overwrite it */
          this.importedObject[ key ] = this.importedObject[ key ].concat( this.default[ key ] );
        } else this.importedObject[ key ] = this.default[ key ]; /* Unless there isn't one */
 
        return;
      }
 
      /* Overwrite */
      this.importedObject[ key ] = this.default[ key ];
    } );
  }
 
 
}
 
// ANCHOR module.exports
module.exports = ESLintrc;