# Code coverage report for ./../tap/lib/stack.js

## Statements: 39.64% (44 / 111) Branches: 28.79% (19 / 66) Functions: 33.33% (3 / 9) Lines: 39.64% (44 / 111) Ignored: none

All files » ./../tap/lib/ » stack.js

    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
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
    148
    149
    150
    151
    152
    153
    154
    155
    156
    157
    158
    159
    160
    161
    162
    163
    164
    165
    166
    167
    168
    169
    170
    171
    172
    173
    174
    175
    176
    177
    178
    179
    180
    181
    182
    183
    184
    185
    186
    187
    188
    189
    190
    191
    192
    193
    194
    195
    196
    197
    198
    199
    200
    201
    202
    203
    204
    205
    206
    207
    208
    209
    210
    211
    212
    213
    214
    215
    216
    217
    218
    219
    220
    221
    222
    2239
    9
    9
    9
    9

    9

    9

    9

    9
    51

    51

    51
    51

    51
    51

    51
    51

    51
    51
    51
    51
    51

    51

    9
    51

    51

    51
    9

    42

    42

    25

    42

    42

    42

    42
    42

    42

    42
    42

    42
    42

    42

    9

    9

     exports.capture = capture
    exports.captureString = captureString
    exports.at = at
    exports.parseLine = parseLine
    exports.clean = clean

    var cwd = process.cwd()
    // var methods =[
    //   'getThis',
    //   'getFunction',
    //   'getTypeName',
    //   'getFunctionName',
    //   'getMethodName',
    //   'getFileName',
    //   'getLineNumber',
    //   'getColumnNumber',
    //   'getEvalOrigin',
    //   'isToplevel',
    //   'isEval',
    //   'isNative',
    //   'isConstructor'
    // ]

    function clean (stack) {
      if (!Array.isArray(stack))
        stack = stack.split('\n')

      var internals = [
        /\(domain.js:[0-9]+:[0-9]+\)$/,
        /\(events.js:[0-9]+:[0-9]+\)$/,
        /\(node.js:[0-9]+:[0-9]+\)$/,
        /\(timers.js:[0-9]+:[0-9]+\)$/,
        /\(module.js:[0-9]+:[0-9]+\)$/,
        /GeneratorFunctionPrototype.next \(native\)/,
        /node_modules[\\\/]tap[\\\/](.*?)\.js:[0-9]:[0-9]\)?$/
      ]

      if (!(/^\s*at /.test(stack[0])) &&
           (/^\s*at /.test(stack[1])))
        stack = stack.slice(1)

      stack = stack.map(function (st) {
        if (internals.some(function (internal) {
          return internal.test(st)
        })) {
          return null
        }
        return st.trim()
          .replace(/^\s*at /, '')
          .replace(cwd + '/', '')
          .replace(cwd + '\\', '')
      }).filter(function (st) {
        return st
      }).join('\n').trim()

      if (stack)
        return stack + '\n'
      else
        return null
    }

    function captureString (limit, fn) {
      if (typeof limit === 'function') {
        fn = limit
        limit = Infinity
      }
      if (!fn)
        fn = captureString

      var limitBefore = Error.stackTraceLimit
      if (limit)
        Error.stackTraceLimit = limit

      var obj = {}

      Error.captureStackTrace(obj, fn)
      var stack = obj.stack
      Error.stackTraceLimit = limitBefore

      return clean(stack)
    }

    function capture (limit, fn) {
      Iif (typeof limit === 'function') {
        fn = limit
        limit = Infinity
      }
      Iif (!fn)
        fn = capture
      var prepBefore = Error.prepareStackTrace
      var limitBefore = Error.stackTraceLimit

      Error.prepareStackTrace = function (obj, site) {
        return site
      }

      Eif (limit)
        Error.stackTraceLimit = limit

      var obj = {}
      Error.captureStackTrace(obj, fn)
      var stack = obj.stack
      Error.prepareStackTrace = prepBefore
      Error.stackTraceLimit = limitBefore

      return stack
    }

    function at (fn) {
      Iif (!fn)
        fn = at
      var site = capture(1, fn)[0]

      if (!site)
        return {}

      var res = {
        file: site.getFileName(),
        line: site.getLineNumber(),
        column: site.getColumnNumber()
      }

      if (res.file.indexOf(cwd + '/') === 0 ||
          res.file.indexOf(cwd + '\\') === 0)
        res.file = res.file.substr(cwd.length + 1)

      Iif (site.isConstructor())
        res.constructor = true

      Iif (site.isEval())
        res.evalOrigin = site.getEvalOrigin()

      Iif (site.isNative())
        res.native = true

      try {
        var typename = site.getTypeName()
      } catch (er) {}
      Iif (typename && typename !== 'Object' && typename !== '[object Object]')
        res.type = typename

      var fname = site.getFunctionName()
      Iif (fname)
        res.function = fname

      var meth = site.getMethodName()
      Iif (meth && fname !== meth)
        res.method = meth

      return res
    }

    var re = new RegExp(
      '^' +
      // Sometimes we strip out the '    at' because it's noisy
      '(?:\\s*at )?' +
      // $1 = ctor if 'new'
      '(?:(new) )?' +
      // Object.method [as foo] (, maybe
      // $2 = function name
      // $3 = method name
      '(?:([^\\(\\[]*)(?: \\[as ([^\\]]+)\\])? \\()?' +
      // (eval at <anonymous> (file.js:1:1),
      // $4 = eval origin
      // $5:$6:$7 are eval file/line/col, but not normally reported
      '(?:eval at ([^ ]+) \\(([^\\)]+):([0-9]+):([0-9]+)\\), )?' +
      // file:line:col
      // $8:$9:$10
      // $11 = 'native' if native
      '(?:([^\\)]+):([0-9]+):([0-9]+)|(native))' +
      // maybe close the paren, then end
      '\\)?$'
    )

    function parseLine (line) {
      var match = line.match(re)
      if (!match)
        return null

      var ctor = match[1] === 'new'
      var fname = match[2]
      var meth = match[3]
      var evalOrigin = match[4]
      var evalFile = +match[5]
      var evalLine = +match[6]
      var evalCol = +match[7]
      var file = match[8]
      var lnum = match[9]
      var col = match[10]
      var native = match[11] === 'native'

      var res = {
        file: file,
        line: +lnum,
        column: +col
      }

      if (res.file &&
          (res.file.indexOf(cwd + '/') === 0 ||
          res.file.indexOf(cwd + '\\') === 0))
        res.file = res.file.substr(cwd.length + 1)

      if (ctor)
        res.constructor = true

      if (evalOrigin) {
        res.evalOrigin = evalOrigin
        res.evalLine = evalLine
        res.evalColumn = evalCol
      }

      if (native)
        res.native = true

      if (fname)
        res.function = fname

      if (meth && fname !== meth)
        res.method = meth

      return res
    }