From 5b293318d325b205216e46485366cfdbbd313578 Mon Sep 17 00:00:00 2001 From: Mengwei Ding Date: Mon, 4 Mar 2019 10:56:19 +0800 Subject: [PATCH] [Code] Fix a line number bug for composite content calculation --- .../code/server/utils/composite_source_merger.test.ts | 1 + .../plugins/code/server/utils/composite_source_merger.ts | 8 ++++++-- 2 files changed, 7 insertions(+), 2 deletions(-) diff --git a/x-pack/plugins/code/server/utils/composite_source_merger.test.ts b/x-pack/plugins/code/server/utils/composite_source_merger.test.ts index ea774c1de97bc..91adfd6372e0e 100644 --- a/x-pack/plugins/code/server/utils/composite_source_merger.test.ts +++ b/x-pack/plugins/code/server/utils/composite_source_merger.test.ts @@ -39,4 +39,5 @@ test('extract source by ranges', () => { const expectedLineNumbers = ['1', '2', '3', '...', '8', '9', '10', '11']; expect(lineMappings.toStringArray('...')).toEqual(expectedLineNumbers); expect(lineMappings.lineNumber(7)).toBe(5); + expect(lineMappings.lineNumber(0)).toBe(1); }); diff --git a/x-pack/plugins/code/server/utils/composite_source_merger.ts b/x-pack/plugins/code/server/utils/composite_source_merger.ts index 9e7dfbb05550c..982e77a28cd93 100644 --- a/x-pack/plugins/code/server/utils/composite_source_merger.ts +++ b/x-pack/plugins/code/server/utils/composite_source_merger.ts @@ -119,8 +119,12 @@ export class LineMapping { } public lineNumber(originLineNumber: number, startAtLine: number = 1) { - const n = this.reverseMap.get(originLineNumber) || Number.NaN; - return n + startAtLine; + const n = this.reverseMap.get(originLineNumber); + if (n === undefined) { + return Number.NaN; + } else { + return n + startAtLine; + } } public hasLine(line: number) {