Skip to content

Commit cc69959

Browse files
committed
Refactor code-style
* Add more docs to JSDoc * Add support for `null` in input of API types
1 parent 1a59cc6 commit cc69959

File tree

2 files changed

+47
-8
lines changed

2 files changed

+47
-8
lines changed

lib/index.js

+31-6
Original file line numberDiff line numberDiff line change
@@ -5,27 +5,52 @@
55
* @typedef {import('vfile').VFileValue} VFileValue
66
*/
77

8+
/**
9+
* @typedef NodeLike
10+
* @property {string} type
11+
* @property {PositionLike | null | undefined} [position]
12+
*
13+
* @typedef PositionLike
14+
* @property {PointLike | null | undefined} [start]
15+
* @property {PointLike | null | undefined} [end]
16+
*
17+
* @typedef PointLike
18+
* @property {number | null | undefined} [line]
19+
* @property {number | null | undefined} [column]
20+
* @property {number | null | undefined} [offset]
21+
*/
22+
823
import {location} from 'vfile-location'
924

1025
const search = /\r?\n|\r/g
1126

1227
/**
1328
* Get the source of a node or at a position.
1429
*
15-
* @param {Node|Position} value
30+
* @param {Node | NodeLike | Position | PositionLike | null | undefined} value
1631
* Value to get.
17-
* @param {VFile|VFileValue} file
32+
* @param {VFile | VFileValue} file
1833
* File in which `value` exists.
19-
* @returns {string|null}
34+
* @returns {string | null}
2035
* Source of `value` in `doc`, if available.
2136
*/
2237
export function source(value, file) {
2338
const doc = String(file)
2439
const loc = location(file)
25-
/** @type {import('unist').Position} */
26-
// @ts-expect-error Looks like a node.
27-
const position = (value && value.position) || value || {}
40+
const position =
41+
value && typeof value === 'object'
42+
? 'type' in value
43+
? value.position
44+
: value
45+
: undefined
46+
47+
if (!position || !position.start || !position.end) {
48+
return null
49+
}
50+
51+
// @ts-expect-error: To do: add `PointLike` to `vfile-location`
2852
const endOffset = loc.toOffset(position.end)
53+
// @ts-expect-error: To do: add `PointLike` to `vfile-location`
2954
let startOffset = loc.toOffset(position.start)
3055

3156
if (endOffset === -1 || startOffset === -1) {

test.js

+16-2
Original file line numberDiff line numberDiff line change
@@ -50,13 +50,27 @@ test('unist-util-source', (t) => {
5050
assert(node.type === 'text')
5151
t.equal(source(node, file), 'Hello', 'text')
5252

53+
t.equal(source(node.position, file), 'Hello', 'position')
54+
55+
t.equal(
56+
source(
57+
/** @type {Text} */ ({
58+
type: 'text',
59+
value: 'qwe',
60+
position: {start: {line: 0, column: 0}, end: {line: 0, column: 0}}
61+
}),
62+
file
63+
),
64+
null,
65+
'out of bounds data'
66+
)
67+
5368
t.equal(
54-
source(/** @type {Text} */ ({type: node.type, value: node.value}), file),
69+
source(/** @type {Text} */ ({type: 'text', value: 'qwe'}), file),
5570
null,
5671
'generated'
5772
)
5873

59-
// @ts-expect-error: runtime.
6074
t.equal(source(null, file), null, 'missing')
6175

6276
file = new VFile('a\r\nb')

0 commit comments

Comments
 (0)