.gitignore added
[dotfiles/.git] / .config / coc / extensions / node_modules / coc-prettier / node_modules / vfile / core.js
1 'use strict';
2
3 var path = require('path');
4 var replace = require('replace-ext');
5 var buffer = require('is-buffer');
6
7 module.exports = VFile;
8
9 var own = {}.hasOwnProperty;
10 var proto = VFile.prototype;
11
12 proto.toString = toString;
13
14 /* Order of setting (least specific to most), we need this because
15  * otherwise `{stem: 'a', path: '~/b.js'}` would throw, as a path
16  * is needed before a stem can be set. */
17 var order = [
18   'history',
19   'path',
20   'basename',
21   'stem',
22   'extname',
23   'dirname'
24 ];
25
26 /* Construct a new file. */
27 function VFile(options) {
28   var prop;
29   var index;
30   var length;
31
32   if (!options) {
33     options = {};
34   } else if (typeof options === 'string' || buffer(options)) {
35     options = {contents: options};
36   } else if ('message' in options && 'messages' in options) {
37     return options;
38   }
39
40   if (!(this instanceof VFile)) {
41     return new VFile(options);
42   }
43
44   this.data = {};
45   this.messages = [];
46   this.history = [];
47   this.cwd = process.cwd();
48
49   /* Set path related properties in the correct order. */
50   index = -1;
51   length = order.length;
52
53   while (++index < length) {
54     prop = order[index];
55
56     if (own.call(options, prop)) {
57       this[prop] = options[prop];
58     }
59   }
60
61   /* Set non-path related properties. */
62   for (prop in options) {
63     if (order.indexOf(prop) === -1) {
64       this[prop] = options[prop];
65     }
66   }
67 }
68
69 /* Access full path (`~/index.min.js`). */
70 Object.defineProperty(proto, 'path', {
71   get: function () {
72     return this.history[this.history.length - 1];
73   },
74   set: function (path) {
75     assertNonEmpty(path, 'path');
76
77     if (path !== this.path) {
78       this.history.push(path);
79     }
80   }
81 });
82
83 /* Access parent path (`~`). */
84 Object.defineProperty(proto, 'dirname', {
85   get: function () {
86     return typeof this.path === 'string' ? path.dirname(this.path) : undefined;
87   },
88   set: function (dirname) {
89     assertPath(this.path, 'dirname');
90     this.path = path.join(dirname || '', this.basename);
91   }
92 });
93
94 /* Access basename (`index.min.js`). */
95 Object.defineProperty(proto, 'basename', {
96   get: function () {
97     return typeof this.path === 'string' ? path.basename(this.path) : undefined;
98   },
99   set: function (basename) {
100     assertNonEmpty(basename, 'basename');
101     assertPart(basename, 'basename');
102     this.path = path.join(this.dirname || '', basename);
103   }
104 });
105
106 /* Access extname (`.js`). */
107 Object.defineProperty(proto, 'extname', {
108   get: function () {
109     return typeof this.path === 'string' ? path.extname(this.path) : undefined;
110   },
111   set: function (extname) {
112     var ext = extname || '';
113
114     assertPart(ext, 'extname');
115     assertPath(this.path, 'extname');
116
117     if (ext) {
118       if (ext.charAt(0) !== '.') {
119         throw new Error('`extname` must start with `.`');
120       }
121
122       if (ext.indexOf('.', 1) !== -1) {
123         throw new Error('`extname` cannot contain multiple dots');
124       }
125     }
126
127     this.path = replace(this.path, ext);
128   }
129 });
130
131 /* Access stem (`index.min`). */
132 Object.defineProperty(proto, 'stem', {
133   get: function () {
134     return typeof this.path === 'string' ? path.basename(this.path, this.extname) : undefined;
135   },
136   set: function (stem) {
137     assertNonEmpty(stem, 'stem');
138     assertPart(stem, 'stem');
139     this.path = path.join(this.dirname || '', stem + (this.extname || ''));
140   }
141 });
142
143 /* Get the value of the file. */
144 function toString(encoding) {
145   var value = this.contents || '';
146   return buffer(value) ? value.toString(encoding) : String(value);
147 }
148
149 /* Assert that `part` is not a path (i.e., does
150  * not contain `path.sep`). */
151 function assertPart(part, name) {
152   if (part.indexOf(path.sep) !== -1) {
153     throw new Error('`' + name + '` cannot be a path: did not expect `' + path.sep + '`');
154   }
155 }
156
157 /* Assert that `part` is not empty. */
158 function assertNonEmpty(part, name) {
159   if (!part) {
160     throw new Error('`' + name + '` cannot be empty');
161   }
162 }
163
164 /* Assert `path` exists. */
165 function assertPath(path, name) {
166   if (!path) {
167     throw new Error('Setting `' + name + '` requires `path` to be set too');
168   }
169 }