Completed
Push — master ( 9a96b1...2c19bd )
by Vitaly
22s
created

stacktracey.js (3 issues)

1
"use strict";
2
3
/*  ------------------------------------------------------------------------ */
4
5
const O            = Object,
0 ignored issues
show
Comprehensibility Best Practice introduced by
You seem to be aliasing the built-in name Object as O. This makes your code very difficult to follow, consider using the built-in name directly.
Loading history...
6
      isBrowser    = (typeof window !== 'undefined') && (window.window === window) && window.navigator,
7
      lastOf       = x => x[x.length - 1],
8
      getSource    = require ('get-source'),
9
      partition    = require ('./impl/partition'),
10
      asTable      = require ('as-table'),
11
      nixSlashes   = x => x.replace (/\\/g, '/'),
12
      pathRoot     = isBrowser ? window.location.href : (nixSlashes (process.cwd ()) + '/')
13
14
/*  ------------------------------------------------------------------------ */
15
16
class StackTracey extends Array {
17
18
    constructor (input, offset) {
19
        
20
        const originalInput          = input
21
            , isParseableSyntaxError = input && (input instanceof SyntaxError && !isBrowser)
22
        
23
        super ()
24
25
    /*  Fixes for Safari    */
26
27
        this.constructor = StackTracey
28
        this.__proto__   = StackTracey.prototype
29
30
    /*  new StackTracey ()            */
31
32
        if (!input) {
33
             input = new Error ()
34
             offset = (offset === undefined) ? 1 : offset
35
        }
36
37
    /*  new StackTracey (Error)      */
38
39
        if (input instanceof Error) {
40
            input = input[StackTracey.stack] || input.stack || ''
41
        }
42
43
    /*  new StackTracey (string)     */
44
45
        if (typeof input === 'string') {
46
            input = StackTracey.rawParse (input).slice (offset).map (StackTracey.extractEntryMetadata)
47
        }
48
49
    /*  new StackTracey (array)      */
50
51
        if (Array.isArray (input)) {
52
53
            if (isParseableSyntaxError) {
54
                
55
                const rawLines   = module.require ('util').inspect (originalInput).split ('\n')
56
                    , fileLine = rawLines[0].split (':')
57
                    , line = fileLine.pop ()
58
                    , file = fileLine.join (':')
59
60
                if (file) {
61
                    input.unshift ({
62
                        file: nixSlashes (file),
63
                        line: line,
64
                        column: (rawLines[2] || '').indexOf ('^') + 1,
65
                        sourceLine: rawLines[1],
66
                        callee: '(syntax error)',
67
                        syntaxError: true
68
                    })
69
                }
70
            }
71
72
            this.length = input.length
73
            input.forEach ((x, i) => this[i] = x)
74
        }
75
    }
76
77
    static extractEntryMetadata (e) {
78
        
79
        const fileRelative = StackTracey.relativePath (e.file || '')
80
81
        return O.assign (e, {
82
83
            calleeShort:  e.calleeShort || lastOf ((e.callee || '').split ('.')),
84
            fileRelative: fileRelative,
85
            fileShort:    StackTracey.shortenPath (fileRelative),
86
            fileName:     lastOf ((e.file || '').split ('/')),
87
            thirdParty:   StackTracey.isThirdParty (fileRelative) && !e.index
88
        })
89
    }
90
91
    static shortenPath (relativePath) {
92
        return relativePath.replace (/^node_modules\//, '')
93
                           .replace (/^webpack\/bootstrap\//, '')
94
    }
95
96
    static relativePath (fullPath) {
97
        return fullPath.replace (pathRoot, '')
98
                       .replace (/^.*\:\/\/?\/?/, '')
99
    }
100
101
    static isThirdParty (relativePath) {
102
        return (relativePath[0] === '~')                          || // webpack-specific heuristic
103
               (relativePath[0] === '/')                          || // external source
104
               (relativePath.indexOf ('node_modules')      === 0) ||
105
               (relativePath.indexOf ('webpack/bootstrap') === 0)
106
    }
107
108
    static rawParse (str) {
109
110
        const lines = (str || '').split ('\n')
111
112
        const entries = lines.map (line => { line = line.trim ()
113
114
            var callee, fileLineColumn = [], native, planA, planB
0 ignored issues
show
The assignment to variable fileLineColumn seems to be never used. Consider removing it.
Loading history...
115
116
            if ((planA = line.match (/at (.+) \((.+)\)/)) ||
117
                (planA = line.match (/(.*)@(.*)/))) {
118
119
                callee         =  planA[1]
120
                native         = (planA[2] === 'native')
121
                fileLineColumn = (planA[2].match (/(.*):(.+):(.+)/) || []).slice (1) }
122
123
            else if ((planB = line.match (/^(at\s+)*(.+):([0-9]+):([0-9]+)/) )) {
124
                fileLineColumn = (planB).slice (2) }
125
126
            else {
127
                return undefined }
128
129
        /*  Detect things like Array.reduce
130
            TODO: detect more built-in types            */
131
            
132
            if (callee && !fileLineColumn[0]) {
133
                const type = callee.split ('.')[0]
134
                if (type === 'Array') {
135
                    native = true
136
                }
137
            }
138
139
            return {
140
                beforeParse: line,
141
                callee:      callee || '',
142
                index:       isBrowser && (fileLineColumn[0] === window.location.href),
143
                native:      native || false,
144
                file:        nixSlashes (fileLineColumn[0] || ''),
145
                line:        parseInt (fileLineColumn[1] || '', 10) || undefined,
146
                column:      parseInt (fileLineColumn[2] || '', 10) || undefined } })
147
148
        return entries.filter (x => (x !== undefined))
149
    }
150
151
    withSource (i) {
152
        return this[i] && StackTracey.withSource (this[i])
153
    }
154
155
    static withSource (loc) {
156
157
        if (loc.sourceFile || (loc.file && loc.file.indexOf ('<') >= 0)) { // skip things like <anonymous> and stuff that was already fetched
158
            return loc
159
            
160
        } else {
161
162
            let resolved = getSource (loc.file || '').resolve (loc)
163
            
164
            if (!resolved.sourceFile.error) {
165
                resolved.file = nixSlashes (resolved.sourceFile.path)
166
                resolved = StackTracey.extractEntryMetadata (resolved)
167
            }
168
169
            if (!resolved.sourceLine.error && resolved.sourceLine.includes ('// @hide')) {
170
                resolved.sourceLine         = resolved.sourceLine.replace  ('// @hide', '')
171
                resolved.hide               = true
172
            }
173
174
            return O.assign ({ sourceLine: '' }, loc, resolved)
175
        }
176
    }
177
178
    get withSources () {
179
        return new StackTracey (this.map (StackTracey.withSource))
180
    }
181
182
    get mergeRepeatedLines () {
183
        return new StackTracey (
184
            partition (this, e => e.file + e.line).map (
185
                group => {
186
                    return group.items.slice (1).reduce ((memo, entry) => {
187
                        memo.callee      = (memo.callee      || '<anonymous>') + ' → ' + (entry.callee      || '<anonymous>')
188
                        memo.calleeShort = (memo.calleeShort || '<anonymous>') + ' → ' + (entry.calleeShort || '<anonymous>')
189
                        return memo }, O.assign ({}, group.items[0])) }))
190
    }
191
192
    get clean () {
193
        return this.withSources.mergeRepeatedLines.filter ((e, i) => (i === 0) || !(e.thirdParty || e.hide || e.native))
194
    }
195
196
    at (i) {
197
        return O.assign ({
198
199
            beforeParse: '',
200
            callee:      '<???>',
201
            index:       false,
202
            native:      false,
203
            file:        '<???>',
204
            line:        0,
205
            column:      0
206
207
        }, this[i])
208
    }
209
210
    static locationsEqual (a, b) {
211
        return (a.file   === b.file) &&
212
               (a.line   === b.line) &&
213
               (a.column === b.column)
214
    }
215
216
    get pretty () {
217
218
        const trimEnd   = (s, n) => s && ((s.length > n) ? (s.slice (0, n-1) + '…') : s)   
219
        const trimStart = (s, n) => s && ((s.length > n) ? ('…' + s.slice (-(n-1))) : s)
220
221
        return asTable (this.withSources.map (
222
                            e => [
223
                                ('at ' + trimEnd (e.calleeShort, 30)),
224
                                trimStart ((e.fileShort && (e.fileShort + ':' + e.line)) || '', 40),
225
                                trimEnd (((e.sourceLine || '').trim () || ''), 80)
226
                            ]))
227
    }
228
229
    static resetCache () {
230
231
        getSource.resetCache ()
232
    }
233
}
234
235
/*  Chaining helper for .isThirdParty
236
    ------------------------------------------------------------------------ */
237
238
(() => {
239
240
    const methods = {
241
242
        include (pred) {
243
244
            const f = StackTracey.isThirdParty
245
            O.assign (StackTracey.isThirdParty = (path => f (path) ||  pred (path)), methods)
246
        },
247
248
        except (pred) {
249
250
            const f = StackTracey.isThirdParty
251
            O.assign (StackTracey.isThirdParty = (path => f (path) && !pred (path)), methods)
252
        },
253
    }
254
255
    O.assign (StackTracey.isThirdParty, methods)
256
257
}) ()
258
259
/*  Array methods
260
    ------------------------------------------------------------------------ */
261
262
;['map', 'filter', 'slice', 'concat', 'reverse'].forEach (name => {
263
264
    StackTracey.prototype[name] = function (/*...args */) { // no support for ...args in Node v4 :(
265
        
266
        const arr = Array.from (this)
267
        return new StackTracey (arr[name].apply (arr, arguments))
268
    }
269
})
270
271
/*  A private field that an Error instance can expose
272
    ------------------------------------------------------------------------ */
273
274
StackTracey.stack = /* istanbul ignore next */ (typeof Symbol !== 'undefined') ? Symbol.for ('StackTracey') : '__StackTracey'
0 ignored issues
show
The variable Symbol seems to be never declared. If this is a global, consider adding a /** global: Symbol */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
275
276
/*  ------------------------------------------------------------------------ */
277
278
module.exports = StackTracey
279
280
/*  ------------------------------------------------------------------------ */
281
282