Skip to content

Add support for non-conforming nullish values #3

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
May 23, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,16 @@ export function toPoint(lspPosition) {
* The LSP range.
*/
export function fromPosition(unistPosition) {
let end = unistPosition.end

// eslint-disable-next-line eqeqeq, no-eq-null
if (!end || end.line == null || end.column == null) {
end = unistPosition.start
}

return {
start: fromPoint(unistPosition.start),
end: fromPoint(unistPosition.end)
end: fromPoint(end)
}
}

Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@
"remark-preset-wooorm": "^9.0.0",
"type-coverage": "^2.0.0",
"typescript": "^4.0.0",
"xo": "^0.53.0"
"xo": "^0.54.0"
},
"scripts": {
"prepack": "npm run build && npm run format",
Expand Down
39 changes: 39 additions & 0 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,45 @@ test('fromPosition', async () => {
},
'should convert unist positions to LSP ranges'
)

assert.deepEqual(
fromPosition({
start: {line: 1, column: 2},
// @ts-expect-error We test invalid user input here.
end: {line: null, column: 4}
}),
{
start: {line: 0, character: 1},
end: {line: 0, character: 1}
},
'should convert unist positions to LSP ranges if end line is null'
)

assert.deepEqual(
fromPosition({
start: {line: 1, column: 2},
// @ts-expect-error We test invalid user input here.
end: {line: 3, column: null}
}),
{
start: {line: 0, character: 1},
end: {line: 0, character: 1}
},
'should convert unist positions to LSP ranges if end line is null'
)

assert.deepEqual(
fromPosition({
start: {line: 1, column: 2},
// @ts-expect-error We test invalid user input here.
end: null
}),
{
start: {line: 0, character: 1},
end: {line: 0, character: 1}
},
'should convert unist positions to LSP ranges if end line is null'
)
})

test('toPosition', async () => {
Expand Down