From 9b7d10fd6fcd0e519227c3f97af602d91a4947e2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jakub=20Ro=C5=BCek?= Date: Sat, 3 Aug 2019 01:18:53 +0700 Subject: [PATCH] fix: make ast.errors empty (#21) * fix: make ast.errors empty * perf: improve memory efficiency of computeLineMap --- src/parseWithPointers.ts | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-) diff --git a/src/parseWithPointers.ts b/src/parseWithPointers.ts index 55468f3..2a1356b 100644 --- a/src/parseWithPointers.ts +++ b/src/parseWithPointers.ts @@ -50,6 +50,10 @@ export const parseWithPointers = (value: string, options?: IParseOptions): Ya parsed.diagnostics.sort((itemA, itemB) => itemA.range.start.line - itemB.range.start.line); } + if (Array.isArray(parsed.ast.errors)) { + parsed.ast.errors.length = 0; + } + return parsed; }; @@ -148,15 +152,17 @@ const dereferenceAnchor = (node: YAMLNode, anchorId: string): YAMLNode | YAMLNod // builds up the line map, for use by linesForPosition const computeLineMap = (input: string) => { - const lines = input.split(/\n/); const lineMap: number[] = []; - let sum = 0; - for (const line of lines) { - sum += line.length + 1; - lineMap.push(sum); + let i = 0; + for (; i < input.length; i++) { + if (input[i] === '\n') { + lineMap.push(i + 1); + } } + lineMap.push(i + 1); + return lineMap; };