Skip to content

Commit e396010

Browse files
authored
Change to return undefined for invalid points, positions
Closes GH-12. Reviewed-by: Christian Murphy <christian.murphy.42@gmail.com> Reviewed-by: Remco Haszing <remcohaszing@gmail.com>
1 parent 516399b commit e396010

File tree

3 files changed

+115
-40
lines changed

3 files changed

+115
-40
lines changed

lib/index.js

Lines changed: 22 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -44,11 +44,16 @@ export const pointEnd = point('end')
4444
*
4545
* @param {NodeLike | Node | null | undefined} [node]
4646
* Node.
47-
* @returns {Position}
47+
* @returns {Position | undefined}
4848
* Position.
4949
*/
5050
export function position(node) {
51-
return {start: pointStart(node), end: pointEnd(node)}
51+
const start = pointStart(node)
52+
const end = pointEnd(node)
53+
54+
if (start && end) {
55+
return {start, end}
56+
}
5257
}
5358

5459
/**
@@ -66,19 +71,25 @@ function point(type) {
6671
* Get the point info of `node` at a bound side.
6772
*
6873
* @param {NodeLike | Node | null | undefined} [node]
69-
* @returns {Point}
74+
* @returns {Point | undefined}
7075
*/
7176
function point(node) {
7277
const point = (node && node.position && node.position[type]) || {}
7378

74-
// To do: next major: don’t return points when invalid.
75-
return {
76-
// @ts-expect-error: in practice, null is allowed.
77-
line: point.line || null,
78-
// @ts-expect-error: in practice, null is allowed.
79-
column: point.column || null,
80-
// @ts-expect-error: in practice, null is allowed.
81-
offset: point.offset > -1 ? point.offset : null
79+
if (
80+
typeof point.line === 'number' &&
81+
point.line > 0 &&
82+
typeof point.column === 'number' &&
83+
point.column > 0
84+
) {
85+
return {
86+
line: point.line,
87+
column: point.column,
88+
offset:
89+
typeof point.offset === 'number' && point.offset > -1
90+
? point.offset
91+
: undefined
92+
}
8293
}
8394
}
8495
}

readme.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ Get the positional info of `node`.
106106

107107
###### Returns
108108

109-
Position ([`Position`][unist-position]).
109+
Position, if valid ([`Position`][unist-position] or `undefined`).
110110

111111
### `pointEnd(node)`
112112

@@ -119,7 +119,7 @@ Get the ending point of `node`.
119119

120120
###### Returns
121121

122-
Point ([`Point`][unist-point]).
122+
Point, if valid ([`Point`][unist-point] or `undefined`).
123123

124124
### `pointStart(node)`
125125

@@ -132,7 +132,7 @@ Get the starting point of `node`.
132132

133133
###### Returns
134134

135-
Point ([`Point`][unist-point]).
135+
Point, if valid ([`Point`][unist-point] or `undefined`).
136136

137137
## Types
138138

test.js

Lines changed: 90 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,6 @@ const noPoints = {type: 'c', position: {}}
1717

1818
const noPosition = {type: 'd'}
1919

20-
const generated = {line: null, column: null, offset: null}
21-
2220
test('core', () => {
2321
assert.deepEqual(
2422
Object.keys(mod).sort(),
@@ -34,28 +32,52 @@ test('position', () => {
3432
'should get the whole position'
3533
)
3634

35+
assert.deepEqual(
36+
position({
37+
type: 'x',
38+
position: {
39+
start: {line: 0, column: 0, offset: -1},
40+
end: {line: 0, column: 0, offset: -1}
41+
}
42+
}),
43+
undefined,
44+
'should not get too low values'
45+
)
46+
47+
assert.deepEqual(
48+
position({
49+
type: 'x',
50+
position: {start: {line: 1, column: 1}, end: {line: 1, column: 2}}
51+
}),
52+
{
53+
start: {line: 1, column: 1, offset: undefined},
54+
end: {line: 1, column: 2, offset: undefined}
55+
},
56+
'should support points w/o `offset`'
57+
)
58+
3759
assert.deepEqual(
3860
position(noFields),
39-
{start: generated, end: generated},
40-
'should return an empty position without fields'
61+
undefined,
62+
'should return nothing when without fields'
4163
)
4264

4365
assert.deepEqual(
4466
position(noPoints),
45-
{start: generated, end: generated},
46-
'should return an empty position without points'
67+
undefined,
68+
'should return nothing when without points'
4769
)
4870

4971
assert.deepEqual(
5072
position(noPosition),
51-
{start: generated, end: generated},
52-
'should return an empty position without position'
73+
undefined,
74+
'should return nothing when without position'
5375
)
5476

5577
assert.deepEqual(
5678
position(),
57-
{start: generated, end: generated},
58-
'should return an empty position without node'
79+
undefined,
80+
'should return nothing when without node'
5981
)
6082
})
6183

@@ -66,28 +88,49 @@ test('pointStart', () => {
6688
'should get a side'
6789
)
6890

91+
assert.deepEqual(
92+
pointStart({
93+
type: 'x',
94+
position: {
95+
start: {line: 0, column: 0, offset: -1},
96+
end: {line: 0, column: 0, offset: -1}
97+
}
98+
}),
99+
undefined,
100+
'should not get too low values'
101+
)
102+
103+
assert.deepEqual(
104+
pointStart({
105+
type: 'x',
106+
position: {start: {line: 1, column: 1}, end: {line: 1, column: 2}}
107+
}),
108+
{line: 1, column: 1, offset: undefined},
109+
'should support points w/o `offset`'
110+
)
111+
69112
assert.deepEqual(
70113
pointStart(noFields),
71-
generated,
72-
'should return an empty point without fields'
114+
undefined,
115+
'should return nothing when without fields'
73116
)
74117

75118
assert.deepEqual(
76119
pointStart(noPoints),
77-
generated,
78-
'should return an empty point without points'
120+
undefined,
121+
'should return nothing when without points'
79122
)
80123

81124
assert.deepEqual(
82125
pointStart(noPosition),
83-
generated,
84-
'should return an empty point without position'
126+
undefined,
127+
'should return nothing when without position'
85128
)
86129

87130
assert.deepEqual(
88131
pointStart(),
89-
generated,
90-
'should return an empty point without node'
132+
undefined,
133+
'should return nothing when without node'
91134
)
92135
})
93136

@@ -98,27 +141,48 @@ test('pointEnd', () => {
98141
'should get a side'
99142
)
100143

144+
assert.deepEqual(
145+
pointEnd({
146+
type: 'x',
147+
position: {
148+
start: {line: 0, column: 0, offset: -1},
149+
end: {line: 0, column: 0, offset: -1}
150+
}
151+
}),
152+
undefined,
153+
'should not get too low values'
154+
)
155+
156+
assert.deepEqual(
157+
pointEnd({
158+
type: 'x',
159+
position: {start: {line: 1, column: 1}, end: {line: 1, column: 2}}
160+
}),
161+
{line: 1, column: 2, offset: undefined},
162+
'should support points w/o `offset`'
163+
)
164+
101165
assert.deepEqual(
102166
pointEnd(noFields),
103-
generated,
104-
'should return an empty point without fields'
167+
undefined,
168+
'should return nothing when without fields'
105169
)
106170

107171
assert.deepEqual(
108172
pointEnd(noPoints),
109-
generated,
110-
'should return an empty point without points'
173+
undefined,
174+
'should return nothing when without points'
111175
)
112176

113177
assert.deepEqual(
114178
pointEnd(noPosition),
115-
generated,
116-
'should return an empty point without position'
179+
undefined,
180+
'should return nothing when without position'
117181
)
118182

119183
assert.deepEqual(
120184
pointEnd(),
121-
generated,
122-
'should return an empty point without node'
185+
undefined,
186+
'should return nothing when without node'
123187
)
124188
})

0 commit comments

Comments
 (0)