-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstringify-position.ts
More file actions
130 lines (120 loc) · 3.56 KB
/
Copy pathstringify-position.ts
File metadata and controls
130 lines (120 loc) · 3.56 KB
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
/**
* @file stringifyPosition
* @module unist-util-stringify-position/stringifyPosition
*/
import type {
Info,
NodeLike,
Options,
PointLike,
PositionLike,
Range
} from './types'
import type State from './types/state'
import { point, position, range } from './util'
/**
* Serialize the positional info of a node, point, position, or range.
*
* The serialized info is returned in one the following formats:
*
* - `ls:cs-le:ce, os-oe` (node, position, range)
* - `ls:cs-le:ce` (node, position, range)
* - `l:c` (point)
*
* where `l` stands for line, `c` for column, `o` for offset, `s` for `start`,
* and `e` for end.
*
* An empty string (`''`) is returned if the given info is neither a node,
* point, position, or range.
*
* @see {@linkcode Info}
* @see {@linkcode Options}
*
* @param {(Info | null)?} [info] - Node, point, position, or range
* @param {(Options | null)?} [options] - Configuration options
* @param {(boolean | null)?} [options.offsets=false] - Serialize offsets if
* `info` is a node, position, or range
* @return {string} Pretty printed positional info
*/
function stringifyPosition(
info?: Info | null,
options?: Options | null
): string
/**
* Serialize the positional info of a node, point, position, or range.
*
* The serialized info is returned in one the following formats:
*
* - `ls:cs-le:ce, os-oe` (node, position, range)
* - `ls:cs-le:ce` (node, position, range)
* - `l:c` (point)
*
* where `l` stands for line, `c` for column, `o` for offset, `s` for `start`,
* and `e` for end.
*
* An empty string (`''`) is returned if the given info is neither a node,
* point, position, or range.
*
* @see {@linkcode Options}
*
* @param {unknown?} [info] - Node, point, position, or range
* @param {(Options | null)?} [options] - Configuration options
* @param {(boolean | null)?} [options.offsets=false] - Serialize offsets if
* `info` is a node, position, or range
* @return {string} Pretty printed positional info
*/
function stringifyPosition(
info?: unknown,
options?: Options | null
): string
/**
* Serialize the positional info of a node, point, position, or range.
*
* The serialized info is returned in one the following formats:
*
* - `ls:cs-le:ce, os-oe` (node, position, range)
* - `ls:cs-le:ce` (node, position, range)
* - `l:c` (point)
*
* where `l` stands for line, `c` for column, `o` for offset, `s` for `start`,
* and `e` for end.
*
* An empty string (`''`) is returned if the given info is neither node, point,
* position, nor range.
*
* @see {@linkcode Info}
* @see {@linkcode Options}
*
* @param {unknown?} [info] - Node, point, position, or range
* @param {(Options | null)?} [options] - Configuration options
* @param {(boolean | null)?} [options.offsets=false] - Serialize offsets if
* `info` is a node, position, or range
* @return {string} Pretty printed positional info
*/
function stringifyPosition(
info?: unknown,
options?: Options | null
): string {
if (typeof info === 'object' && info) {
/**
* Info passed around.
*
* @const {State} state
*/
const state: State = { offsets: options?.offsets ?? false }
// range
if (Array.isArray(info)) return range(<Range>info, state)
// node
if ('position' in info || 'type' in info) {
return position((<NodeLike>info).position, state)
}
// position
if ('end' in info || 'start' in info) {
return position(<PositionLike>info, state)
}
// point
if ('column' in info || 'line' in info) return point(<PointLike>info)
}
return ''
}
export default stringifyPosition