All files / src esprima.js

85.37% Statements 35/41
76.47% Branches 13/17
100% Functions 4/4
87.18% Lines 34/39
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 681x 1x 1x 1x 1x 1x         4x 3x 2x 2x 2x 2x 21x 21x 21x   7x 7x         6x 6x   7x 7x   1x 1x               2x 2x 2x 2x         2x 21x 21x 13x 1x   12x               1x        
const debug                            = require('debug')('parser')
const { handleExportNamedDeclaration } = require('./handle-export-named-declaration')
const { handleClassDeclaration }       = require('./class-declaration')
const { handleExpressionStatement }    = require('./handle-expression-statement')
const { handleVariableDeclaration }    = require('./handle-variable-declaration')
const { handleFunctionDeclaration }    = require('./handle-function-declaration')
/*TODO
 * check various file types against func to match all possible cases for exporting
 * */
function generateExports(esprima) {
  return function (fileName, data){
    if(!data) throw new Error('No data was received!')
    let names = {}
    const insertName = hashMapToInject(names)
    const { body } = esprima.parse( data, { sourceType: 'module'} )
    for( let declaration of body ) {
      const { type } = declaration
      debug(type)
      switch ( type ) {
        case 'ExportNamedDeclaration':
          handleExportNamedDeclaration(insertName, debug, declaration)
          break
        case 'ClassDeclaration':
          handleClassDeclaration(insertName, debug, declaration)
          break
        case 'ExpressionStatement':
          handleExpressionStatement(insertName, debug, declaration)
          break
        case 'VariableDeclaration':
          handleVariableDeclaration(insertName, debug, declaration)
          break
        case 'FunctionDeclaration':
          handleFunctionDeclaration(insertName, debug, declaration)
          break
        case 'ExportDefaultDeclaration':
          debug(`Export default received, need to think what to do with it`)
          break;
        default:
          debug(`An unhandled declaration type received: ${type}`)
      }
    }
    debug(`names:${JSON.stringify(names)}`)
    let namesKeys = Object.keys(names)
    const finalExports = `export {\n${namesKeys}\n}`
    return { finalExports, fileName, names:namesKeys }
 
  }
}
function hashMapToInject(hashmap){
  return function insertName(name, isExport){
    Iif(typeof name !== 'string') debug(`name: ${JSON.stringify(name)} is not a string!!!`)
    if( name[0] === name[0].toUpperCase() || isExport ){
      if(hashmap[name]){
        d(`key:${name} already exists!`)
      }else{
        hashmap[name] = name
      }
    }
  }
 
}
 
 
module.exports = {
  generateExports,
  hashMapToInject
}