|
4 | 4 | * @typedef {[number, number]} RangeLike |
5 | 5 | * |
6 | 6 | * @typedef PointLike |
7 | | - * @property {number|null|undefined} [line] |
8 | | - * @property {number|null|undefined} [column] |
| 7 | + * @property {number | null | undefined} [line] |
| 8 | + * @property {number | null | undefined} [column] |
9 | 9 | * |
10 | 10 | * @typedef LocLike |
11 | | - * @property {PointLike|null|undefined} [start] |
12 | | - * @property {PointLike|null|undefined} [end] |
| 11 | + * @property {PointLike | null | undefined} [start] |
| 12 | + * @property {PointLike | null | undefined} [end] |
13 | 13 | * |
14 | 14 | * @typedef NodeLike |
15 | | - * @property {LocLike|null|undefined} [loc] |
16 | | - * @property {RangeLike|null|undefined} [range] |
17 | | - * @property {number|null|undefined} [start] |
18 | | - * @property {number|null|undefined} [end] |
| 15 | + * @property {LocLike | null | undefined} [loc] |
| 16 | + * @property {RangeLike | null | undefined} [range] |
| 17 | + * @property {number | null | undefined} [start] |
| 18 | + * @property {number | null | undefined} [end] |
19 | 19 | */ |
20 | 20 |
|
21 | 21 | /** |
22 | | - * Given an estree `node`, returns a unist `position`. |
23 | | - * @param {NodeLike|null} [value] |
| 22 | + * Turn an estree `node` into a unist `position`. |
| 23 | + * |
| 24 | + * @param {NodeLike | null | undefined} [value] |
| 25 | + * estree node. |
24 | 26 | * @returns {Position} |
| 27 | + * unist position. |
25 | 28 | */ |
26 | 29 | export function positionFromEstree(value) { |
27 | | - /** @type {NodeLike} */ |
28 | 30 | const node = value || {} |
29 | | - /** @type {LocLike} */ |
30 | 31 | const loc = node.loc || {} |
31 | | - /** @type {RangeLike} */ |
32 | 32 | const range = node.range || [0, 0] |
33 | | - const startOffset = range[0] || node.start |
34 | | - const endOffset = range[1] || node.end |
| 33 | + const startColumn = loc.start |
| 34 | + ? numberOrUndefined(loc.start.column) |
| 35 | + : undefined |
| 36 | + const endColumn = loc.end ? numberOrUndefined(loc.end.column) : undefined |
35 | 37 |
|
36 | 38 | return { |
37 | 39 | start: { |
38 | 40 | // @ts-expect-error: return no point / no position next major. |
39 | | - line: |
40 | | - loc.start && typeof loc.start.line === 'number' && loc.start.line > -1 |
41 | | - ? loc.start.line |
42 | | - : undefined, |
| 41 | + line: loc.start ? numberOrUndefined(loc.start.line) : undefined, |
43 | 42 | // @ts-expect-error: return no point / no position next major. |
44 | | - column: |
45 | | - loc.start && |
46 | | - typeof loc.start.column === 'number' && |
47 | | - loc.start.column > -1 |
48 | | - ? loc.start.column + 1 |
49 | | - : undefined, |
50 | | - offset: |
51 | | - typeof startOffset === 'number' && startOffset > -1 |
52 | | - ? startOffset |
53 | | - : undefined |
| 43 | + column: startColumn === undefined ? undefined : startColumn + 1, |
| 44 | + offset: numberOrUndefined(range[0] || node.start) |
54 | 45 | }, |
55 | 46 | end: { |
56 | 47 | // @ts-expect-error: return no point / no position next major. |
57 | | - line: |
58 | | - loc.end && typeof loc.end.line === 'number' && loc.end.line > -1 |
59 | | - ? loc.end.line |
60 | | - : undefined, |
| 48 | + line: loc.end ? numberOrUndefined(loc.end.line) : undefined, |
61 | 49 | // @ts-expect-error: return no point / no position next major. |
62 | | - column: |
63 | | - loc.end && typeof loc.end.column === 'number' && loc.end.column > -1 |
64 | | - ? loc.end.column + 1 |
65 | | - : undefined, |
66 | | - offset: |
67 | | - typeof endOffset === 'number' && endOffset > -1 ? endOffset : undefined |
| 50 | + column: endColumn === undefined ? undefined : endColumn + 1, |
| 51 | + offset: numberOrUndefined(range[1] || node.end) |
68 | 52 | } |
69 | 53 | } |
70 | 54 | } |
| 55 | + |
| 56 | +/** |
| 57 | + * |
| 58 | + * @param {number | null | undefined} value |
| 59 | + * @returns {number | undefined} |
| 60 | + */ |
| 61 | +function numberOrUndefined(value) { |
| 62 | + return typeof value === 'number' && value > -1 ? value : undefined |
| 63 | +} |
0 commit comments