All files / lib/agents jsshell.js

100% Statements 40/40
100% Branches 19/19
100% Functions 2/2
100% Lines 40/40

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    1x 1x 1x   1x 1x   1x 1x       151x 3x     151x 2x     151x       150x 150x 150x 30x   30x 30x 40x 40x 30x     10x                   150x 150x   150x   119x   119x 117x     2x 2x 2x 2x     31x 31x   31x 23x             31x   31x     1x   1x  
'use strict';
 
const fs = require('fs');
const runtimePath = require('../runtime-path');
const ConsoleAgent = require('../ConsoleAgent');
 
const errorRe = /^(.*?)?:(\d+):(\d+)? ([\w\d]+): (.+)?$/m;
const customErrorRe = /uncaught exception:\s([\w\d]+): (.+)?$/m;
 
const stackRe = /^([\s\S]*?)\r?\nStack:\r?\n([\s\S]*)$/;
const stackFrameRe = /^(.*?)?@(.*?):(\d+):(\d+)?$/;
 
class JSShell extends ConsoleAgent {
  async evalScript(code, options = {}) {
    if (options.module && this.args[0] !== '--module') {
      this.args.unshift('--module');
    }
 
    if (!options.module && this.args[0] === '--module') {
      this.args.shift();
    }
 
    return super.evalScript(code, options);
  }
 
  parseError(str) {
    const stack = [];
    const stackMatch = str.match(stackRe);
    if (stackMatch) {
      str = stackMatch[1];
 
      const stackStr = stackMatch[2];
      for (const line of stackStr.split(/\r?\n/g)) {
        const match = line.trim().match(stackFrameRe);
        if (!match) {
          continue;
        }
 
        stack.push({
          source: match[0],
          functionName: match[1],
          fileName: match[2],
          lineNumber: match[3],
          columnNumber: match[4],
        });
      }
    }
 
    const error = {};
    let match = str.match(errorRe);
 
    if (!match) {
      // try custom error
      let match = str.match(customErrorRe);
 
      if (!match) {
        return null;
      }
 
      error.name = match[1];
      error.message = match[2];
      error.stack = stack;
      return error;
    }
 
    error.name = match[4];
    error.message = match[5];
 
    if (stack.length === 0) {
      stack.push({
        source: match[0],
        fileName: match[1],
        lineNumber: match[2],
        columnNumber: match[3]
      });
    }
    error.stack = stack;
 
    return error;
  }
}
JSShell.runtime = fs.readFileSync(runtimePath.for('jsshell'), 'utf8');
 
module.exports = JSShell;