.gitignore added
[dotfiles/.git] / .config / coc / extensions / node_modules / coc-prettier / node_modules / vfile-message / index.js
1 'use strict'
2
3 var stringify = require('unist-util-stringify-position')
4
5 module.exports = VMessage
6
7 // Inherit from `Error#`.
8 function VMessagePrototype() {}
9 VMessagePrototype.prototype = Error.prototype
10 VMessage.prototype = new VMessagePrototype()
11
12 // Message properties.
13 var proto = VMessage.prototype
14
15 proto.file = ''
16 proto.name = ''
17 proto.reason = ''
18 proto.message = ''
19 proto.stack = ''
20 proto.fatal = null
21 proto.column = null
22 proto.line = null
23
24 // Construct a new VMessage.
25 //
26 // Note: We cannot invoke `Error` on the created context, as that adds readonly
27 // `line` and `column` attributes on Safari 9, thus throwing and failing the
28 // data.
29 function VMessage(reason, position, origin) {
30   var parts
31   var range
32   var location
33
34   if (typeof position === 'string') {
35     origin = position
36     position = null
37   }
38
39   parts = parseOrigin(origin)
40   range = stringify(position) || '1:1'
41
42   location = {
43     start: {line: null, column: null},
44     end: {line: null, column: null}
45   }
46
47   // Node.
48   if (position && position.position) {
49     position = position.position
50   }
51
52   if (position) {
53     // Position.
54     if (position.start) {
55       location = position
56       position = position.start
57     } else {
58       // Point.
59       location.start = position
60     }
61   }
62
63   if (reason.stack) {
64     this.stack = reason.stack
65     reason = reason.message
66   }
67
68   this.message = reason
69   this.name = range
70   this.reason = reason
71   this.line = position ? position.line : null
72   this.column = position ? position.column : null
73   this.location = location
74   this.source = parts[0]
75   this.ruleId = parts[1]
76 }
77
78 function parseOrigin(origin) {
79   var result = [null, null]
80   var index
81
82   if (typeof origin === 'string') {
83     index = origin.indexOf(':')
84
85     if (index === -1) {
86       result[1] = origin
87     } else {
88       result[0] = origin.slice(0, index)
89       result[1] = origin.slice(index + 1)
90     }
91   }
92
93   return result
94 }