diff --git a/lib/index.js b/lib/index.js index 640d8a9..ee5d6fb 100644 --- a/lib/index.js +++ b/lib/index.js @@ -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) } } diff --git a/package.json b/package.json index 204bee0..8adf57c 100644 --- a/package.json +++ b/package.json @@ -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", diff --git a/test/index.js b/test/index.js index 032836c..d23b316 100644 --- a/test/index.js +++ b/test/index.js @@ -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 () => {