Skip to content

Commit

Permalink
fix: make ast.errors empty (#21)
Browse files Browse the repository at this point in the history
* fix: make ast.errors empty

* perf: improve memory efficiency of computeLineMap
  • Loading branch information
P0lip authored and marbemac committed Aug 2, 2019
1 parent cc00861 commit 9b7d10f
Showing 1 changed file with 11 additions and 5 deletions.
16 changes: 11 additions & 5 deletions src/parseWithPointers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,10 @@ export const parseWithPointers = <T>(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;
};

Expand Down Expand Up @@ -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;
};

Expand Down

0 comments on commit 9b7d10f

Please sign in to comment.