Skip to content
This repository has been archived by the owner on Mar 25, 2021. It is now read-only.

Commit

Permalink
Clean up combineCodeTextAndErrorLines (#2745)
Browse files Browse the repository at this point in the history
  • Loading branch information
andy-hanson authored and adidahiya committed May 24, 2017
1 parent a4dd395 commit b34a4e0
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 21 deletions.
4 changes: 2 additions & 2 deletions src/test/lines.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ export function parseLine(text: string): Line {
* @param code - If line represents error markup, this is the line of code preceding the markup.
* Otherwise, this parameter is not required.
*/
export function printLine(line: Line, code?: string): string | null {
export function printLine(line: Line, code?: string): string | undefined {
if (line instanceof ErrorLine) {
if (code == null) {
throw new Error("Must supply argument for code parameter when line is an ErrorLine");
Expand Down Expand Up @@ -108,5 +108,5 @@ export function printLine(line: Line, code?: string): string | null {
} else if (line instanceof CodeLine) {
return line.contents;
}
return null;
return undefined;
}
19 changes: 3 additions & 16 deletions src/test/parse.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import * as ts from "typescript";
import {format} from "util";

import {flatMap, mapDefined} from "../utils";
import {
CodeLine,
EndErrorLine,
Expand Down Expand Up @@ -200,11 +201,7 @@ export function createMarkupFromErrors(code: string, lintErrors: LintError[]) {

if (startPos.line === endPos.line) {
// single line error
errorLinesForCodeText[startPos.line].push(new EndErrorLine(
startPos.col,
endPos.col,
message,
));
errorLinesForCodeText[startPos.line].push(new EndErrorLine(startPos.col, endPos.col, message));
} else {
// multiline error
errorLinesForCodeText[startPos.line].push(new MultilineErrorLine(startPos.col));
Expand All @@ -215,20 +212,10 @@ export function createMarkupFromErrors(code: string, lintErrors: LintError[]) {
}
}

const finalText = combineCodeTextAndErrorLines(codeText, errorLinesForCodeText);
return finalText.join("\n");
return flatMap(codeText, (line, i) => [line, ...mapDefined(errorLinesForCodeText[i], (err) => printLine(err, line))]).join("\n");
}
/* tslint:enable:object-literal-sort-keys */

function combineCodeTextAndErrorLines(codeText: string[], errorLinesForCodeText: ErrorLine[][]) {
return codeText.reduce<string[]>((resultText, code, i) => {
resultText.push(code);
const errorPrintLines = errorLinesForCodeText[i].map((line) => printLine(line, code)).filter((line) => line !== null) as string[];
resultText.push(...errorPrintLines);
return resultText;
}, []);
}

function createCodeLineNoToErrorsMap(lines: Line[]) {
const errorLinesForCodeLine: ErrorLine[][] = [];
for (const line of lines) {
Expand Down
6 changes: 3 additions & 3 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -140,10 +140,10 @@ export function find<T, U>(inputs: T[], getResult: (t: T) => U | undefined): U |
}

/** Returns an array that is the concatenation of all output arrays. */
export function flatMap<T, U>(inputs: T[], getOutputs: (input: T) => U[]): U[] {
export function flatMap<T, U>(inputs: T[], getOutputs: (input: T, index: number) => U[]): U[] {
const out = [];
for (const input of inputs) {
out.push(...getOutputs(input));
for (let i = 0; i < inputs.length; i++) {
out.push(...getOutputs(inputs[i], i));
}
return out;
}
Expand Down

0 comments on commit b34a4e0

Please sign in to comment.