|
1 | 1 | import {location} from 'vfile-location' |
2 | 2 |
|
3 | | -var search = /\r?\n|\r/g |
| 3 | +const search = /\r?\n|\r/g |
4 | 4 |
|
5 | 5 | /** |
6 | 6 | * @param {import('unist').Node|import('unist').Position} value Value to get |
7 | 7 | * @param {import('vfile').VFile|import('vfile').VFileValue} file File in which `value` exists |
8 | 8 | * @returns {string|null} |
9 | 9 | */ |
10 | 10 | export function source(value, file) { |
11 | | - var doc = String(file) |
12 | | - var loc = location(file) |
| 11 | + const doc = String(file) |
| 12 | + const loc = location(file) |
13 | 13 | /** @type {import('unist').Position} */ |
14 | | - // @ts-ignore Looks like a node. |
15 | | - var position = (value && value.position) || value || {} |
16 | | - var startOffset = loc.toOffset(position.start) |
17 | | - var endOffset = loc.toOffset(position.end) |
18 | | - /** @type {Array.<string>} */ |
19 | | - var results = [] |
20 | | - /** @type {RegExpMatchArray} */ |
21 | | - var match |
22 | | - /** @type {number} */ |
23 | | - var end |
| 14 | + // @ts-expect-error Looks like a node. |
| 15 | + const position = (value && value.position) || value || {} |
| 16 | + const endOffset = loc.toOffset(position.end) |
| 17 | + let startOffset = loc.toOffset(position.start) |
24 | 18 |
|
25 | | - if (startOffset === -1 || endOffset === -1) { |
| 19 | + if (endOffset === -1 || startOffset === -1) { |
26 | 20 | return null |
27 | 21 | } |
28 | 22 |
|
| 23 | + /** @type {Array.<string>} */ |
| 24 | + const results = [] |
| 25 | + |
29 | 26 | while (startOffset < endOffset) { |
30 | 27 | search.lastIndex = startOffset |
31 | | - match = search.exec(doc) |
32 | | - end = match && match.index < endOffset ? match.index : endOffset |
| 28 | + const match = search.exec(doc) |
| 29 | + const end = match && match.index < endOffset ? match.index : endOffset |
33 | 30 | results.push(doc.slice(startOffset, end)) |
34 | 31 | startOffset = end |
35 | 32 |
|
|
0 commit comments