summaryrefslogtreecommitdiff
path: root/tools/eslint/node_modules/vfile/readme.md
blob: 48f5269c5488aa72b78be7ce9df91a53c7561b5a (plain)
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
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
# ![vfile](https://cdn.rawgit.com/wooorm/vfile/master/logo.svg)

[![Build Status](https://img.shields.io/travis/wooorm/vfile.svg)](https://travis-ci.org/wooorm/vfile) [![Coverage Status](https://img.shields.io/codecov/c/github/wooorm/vfile.svg)](https://codecov.io/github/wooorm/vfile)

**VFile** is a virtual file format used by [**retext**](https://github.com/wooorm/retext)
(natural language) and [**remark**](https://github.com/wooorm/remark)
(markdown). Two processors which parse, transform, and compile text. Both need
a virtual representation of files and a place to store metadata and messages.
And, they work in the browser. **VFile** provides these requirements.

Also, **VFile** exposes a warning mechanism compatible with [**ESLint**](https://github.com/eslint/eslint)s
formatters, making it easy to expose [stylish](https://github.com/eslint/eslint/blob/master/lib/formatters/stylish.js)
warnings, or export [tap](https://github.com/eslint/eslint/blob/master/lib/formatters/tap.js)
compliant messages.

> **VFile** is different from (the excellent :+1:) [**vinyl**](https://github.com/wearefractal/vinyl)
> in that it does not include file-system or node-only functionality. No
> buffers, streams, or stats. In addition, the focus on
> [metadata](#vfilenamespacekey) and [messages](#vfilemessagereason-position-ruleid)
> are useful when processing a file through a
> [middleware](https://github.com/segmentio/ware) pipeline.

## Installation

[npm](https://docs.npmjs.com/cli/install):

```bash
npm install vfile
```

**VFile** is also available for [duo](http://duojs.org/#getting-started),
and as an AMD, CommonJS, and globals module, [uncompressed and
compressed](https://github.com/wooorm/vfile/releases).

## Table of Contents

*   [Usage](#usage)

*   [Related Tools](#related-tools)

*   [API](#api)

    *   [VFile()](#vfile-1)
    *   [VFile#contents](#vfilecontents)
    *   [VFile#directory](#vfiledirectory)
    *   [VFile#filename](#vfilefilename)
    *   [VFile#extension](#vfileextension)
    *   [VFile#basename()](#vfilebasename)
    *   [VFile#quiet](#vfilequiet)
    *   [VFile#messages](#vfilemessages)
    *   [VFile#history](#vfilehistory)
    *   [VFile#toString()](#vfiletostring)
    *   [VFile#filePath()](#vfilefilepath)
    *   [VFile#move(options)](#vfilemoveoptions)
    *   [VFile#namespace(key)](#vfilenamespacekey)
    *   [VFile#message(reason\[, position\[, ruleId\]\])](#vfilemessagereason-position-ruleid)
    *   [VFile#warn(reason\[, position\[, ruleId\]\])](#vfilewarnreason-position-ruleid)
    *   [VFile#fail(reason\[, position\[, ruleId\]\])](#vfilefailreason-position-ruleid)
    *   [VFile#hasFailed()](#vfilehasfailed)
    *   [VFileMessage](#vfilemessage)

*   [License](#license)

## Usage

```js
var VFile = require('vfile');

var file = new VFile({
  'directory': '~',
  'filename': 'example',
  'extension': 'txt',
  'contents': 'Foo *bar* baz'
});

file.toString(); // 'Foo *bar* baz'
file.filePath(); // '~/example.txt'

file.move({'extension': 'md'});
file.filePath(); // '~/example.md'

file.warn('Something went wrong', {'line': 1, 'column': 3});
// { [~/example.md:1:3: Something went wrong]
//   name: '~/example.md:1:3',
//   file: '~/example.md',
//   reason: 'Something went wrong',
//   line: 1,
//   column: 3,
//   fatal: false }
```

## Related Tools

[**VFile**](#api)s are used by both [**retext**](https://github.com/wooorm/retext)
and [**remark**](https://github.com/wooorm/remark).

In addition, here’s a list of useful tools:

*   [`dustinspecker/convert-vinyl-to-vfile`](https://github.com/dustinspecker/convert-vinyl-to-vfile)
    — Convert a [Vinyl](https://github.com/wearefractal/vinyl) file to a VFile;

*   [`shinnn/is-vfile-message`](https://github.com/shinnn/is-vfile-message)
    — Check if a value is a `VFileMessage` object;

*   [`wooorm/to-vfile`](https://github.com/wooorm/to-vfile)
    — Create a virtual file from a file-path;

*   [`wooorm/vfile-find-down`](https://github.com/wooorm/vfile-find-down)
    — Find one or more files by searching the file system downwards;

*   [`wooorm/vfile-find-up`](https://github.com/wooorm/vfile-find-up)
    — Find one or more files by searching the file system upwards;

*   [`wooorm/vfile-location`](https://github.com/wooorm/vfile-location)
    — Convert between positions (line and column-based) and offsets
    (range-based) locations;

*   [`shinnn/vfile-messages-to-vscode-diagnostics`](https://github.com/shinnn/vfile-messages-to-vscode-diagnostics)
    — Convert `VFileMessage`s into an array of VS Code diagnostics;

*   [`wooorm/vfile-reporter`](https://github.com/wooorm/vfile-reporter)
    — Stylish reporter for virtual files.

*   [`wooorm/vfile-sort`](https://github.com/wooorm/vfile-sort)
    — Sort virtual file messages by line/column;

## API

### `VFile()`

**VFile** objects make it easy to move files, to trigger warnings and
errors, and to store supplementary metadata relating to files, all without
accessing the file-system.

**Example**:

```js
var file = new VFile({
  'directory': '~',
  'filename': 'example',
  'extension': 'txt',
  'contents': 'Foo *bar* baz'
});

file === VFile(file); // true
file === new VFile(file); // true

VFile('foo') instanceof VFile; // true
```

**Signatures**:

*   `file = VFile(contents|options|vFile?)`.

**Parameters**:

*   `contents` (`string`) — Contents of the file;

*   `vFile` (`VFile`) — Existing representation, returned without modification;

*   `options` (`Object`):

    *   `directory` (`string?`, default: `''`)
        — Parent directory;

    *   `filename` (`string?`, default: `''`)
        — Name, without extension;

    *   `extension` (`string?`, default: `''`)
        — Extension(s), without initial dot;

    *   `contents` (`string?`, default: `''`)
        — Raw value.

**Returns**:

`vFile` — Instance.

**Notes**:

`VFile` exposes an interface compatible with ESLint’s formatters.  For example,
to expose warnings using ESLint’s `compact` formatter, execute the following:

```javascript
var compact = require('eslint/lib/formatters/compact');
var VFile = require('vfile');

var vFile = new VFile({
    'directory': '~',
    'filename': 'hello',
    'extension': 'txt'
});

vFile.warn('Whoops, something happened!');

console.log(compact([vFile]));
```

Which would yield the following:

```text
~/hello.txt: line 0, col 0, Warning - Whoops, something happened!

1 problem
```

### `VFile#contents`

`string` — Content of file.

### `VFile#directory`

`string` — Path to parent directory.

### `VFile#filename`

`string` — Filename. A file-path can still be generated when no filename exists.

### `VFile#extension`

`string` — Extension. A file-path can still be generated when no extension
exists.

### `VFile#basename()`

Get the filename, with extension, if applicable.

**Example**:

```js
var file = new VFile({
  'directory': '~',
  'filename': 'example',
  'extension': 'txt'
});

file.basename() // example.txt
```

**Signatures**:

*   `string = vFile.basename()`.

**Returns**:

`string`— Returns the file path without a directory, if applicable.
Otherwise,an empty string is returned.

### `VFile#quiet`

`boolean?` — Whether an error created by [`VFile#fail()`](#vfilemessagereason-position-ruleid)
is returned (when truthy) or thrown (when falsey).

Ensure all `messages` associated with a file are handled properly when setting
this to `true`.

### `VFile#messages`

`Array.<VFileMessage>` — List of associated messages.

**Notes**:

`VFile#message()`, and in turn `VFile#warn()` and `VFile#fail()`, return
`Error` objects that adhere to the [`VFileMessage`](#vfilemessage) schema.
Its results can populate `messages`.

### `VFile#history`

`Array.<String>` — List of file-paths the file [`move`](#vfilemoveoptions)d
between.

### `VFile#toString()`

Get the value of the file.

**Example**:

```js
var vFile = new VFile('Foo');
String(vFile); // 'Foo'
```

**Signatures**:

*   `string = vFile.toString()`.

**Returns**:

`string` — Contents.

### `VFile#filePath()`

Get the filename, with extension and directory, if applicable.

**Example**:

```js
var file = new VFile({
  'directory': '~',
  'filename': 'example',
  'extension': 'txt'
});

String(file.filePath); // ~/example.txt
file.filePath() // ~/example.txt
```

**Signatures**:

*   `string = vFile.filePath()`.

**Returns**:

`string` — If the `vFile` has a `filename`, it will be prefixed with the
directory (slashed), if applicable, and suffixed with the (dotted) extension
(if applicable).  Otherwise, an empty string is returned.

### `VFile#move(options)`

Move a file by passing a new directory, filename, and extension.  When these
are not given, the default values are kept.

**Example**:

```js
var file = new VFile({
  'directory': '~',
  'filename': 'example',
  'extension': 'txt',
  'contents': 'Foo *bar* baz'
});

file.move({'directory': '/var/www'});
file.filePath(); // '/var/www/example.txt'

file.move({'extension': 'md'});
file.filePath(); // '/var/www/example.md'
```

**Signatures**:

*   `vFile = vFile.move(options?)`.

**Parameters**:

*   `options` (`Object`):

    *   `directory` (`string`, default: `''`)
        — Parent directory;

    *   `filename` (`string?`, default: `''`)
        — Name, without extension;

    *   `extension` (`string`, default: `''`)
        — Extension(s), without initial dot.

**Returns**:

`vFile` — Context object (chainable).

### `VFile#namespace(key)`

Access metadata.

**Example**:

```js
var file = new VFile('Foo');

file.namespace('foo').bar = 'baz';

console.log(file.namespace('foo').bar) // 'baz';
```

**Parameters**:

*   `key` (`string`) — Namespace key.

**Returns**:

`Object` — Private namespace for metadata.

### `VFile#message(reason[, position[, ruleId]])`

Create a message with `reason` at `position`. When an error is passed in as
`reason`, copies the stack. This does not add a message to `messages`.

**Example**:

```js
var file = new VFile();

file.message('Something went wrong');
// { [1:1: Something went wrong]
//   name: '1:1',
//   file: '',
//   reason: 'Something went wrong',
//   line: null,
//   column: null }
```

**Signatures**:

*   `VFileMessage = vFile.message(err|reason, node|location|position?,
    ruleId?)`.

**Parameters**:

*   `err` (`Error`) — Original error, whose stack and message are used;

*   `reason` (`string`) — Reason for message;

*   `node` (`Node`) — Syntax tree object;

*   `location` (`Object`) — Syntax tree location (found at `node.position`);

*   `position` (`Object`) — Syntax tree position (found at
    `node.position.start` or `node.position.end`).

*   `ruleId` (`string`) — Category of warning.

**Returns**:

[`VFileMessage`](#vfilemessage) — File-related message with location
information.

### `VFile#warn(reason[, position[, ruleId]])`

Warn. Creates a non-fatal message (see [`VFile#message()`](#vfilemessagereason-position-ruleid)),
and adds it to the file's [`messages`](#vfilemessages) list.

**Example**:

```js
var file = new VFile();

file.warn('Something went wrong');
// { [1:1: Something went wrong]
//   name: '1:1',
//   file: '',
//   reason: 'Something went wrong',
//   line: null,
//   column: null,
//   fatal: false }
```

**See**:

*   [`VFile#message`](#vfilemessagereason-position-ruleid)

### `VFile#fail(reason[, position[, ruleId]])`

Fail. Creates a fatal message (see `VFile#message()`), sets `fatal: true`,
adds it to the file's `messages` list.

If `quiet` is not `true`, throws the error.

**Example**:

```js
var file = new VFile();

file.fail('Something went wrong');
// 1:1: Something went wrong
//     at VFile.exception (vfile/index.js:296:11)
//     at VFile.fail (vfile/index.js:360:20)
//     at repl:1:6

file.quiet = true;
file.fail('Something went wrong');
// { [1:1: Something went wrong]
//   name: '1:1',
//   file: '',
//   reason: 'Something went wrong',
//   line: null,
//   column: null,
//   fatal: true }
```

**See**:

*   [`VFile#message`](#vfilemessagereason-position-ruleid)

### `VFile#hasFailed()`

Check if a fatal message occurred making the file no longer processable.

**Example**:

```js
var file = new VFile();
file.quiet = true;

file.hasFailed(); // false

file.fail('Something went wrong');
file.hasFailed(); // true
```

**Signatures**:

*   `boolean = vFile.hasFailed()`.

**Returns**:

`boolean` — `true` if at least one of file’s `messages` has a `fatal`
property set to `true`.

### `VFileMessage`

`Error` — File-related message with location information.

**Properties**:

*   `name` (`string`)
    — (Starting) location of the message, preceded by its file-path when
    available, and joined by `':'`. Used by the native
    [`Error#toString()`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error/name);

*   `file` (`string`) — File-path;

*   `reason` (`string`) — Reason for message;

*   `line` (`number?`) — Line of error, when available;

*   `column` (`number?`) — Column of error, when available;

*   `stack` (`string?`) — Stack of message, when available;

*   `fatal` (`boolean?`) — Whether the associated file is still processable.

*   `location` (`object`) — Full range information, when available. Has
    `start` and `end` properties, both set to an object with `line` and
    `column`, set to `number?`.

## License

[MIT](LICENSE) © [Titus Wormer](http://wooorm.com)