-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathassertion.ts
35 lines (27 loc) · 924 Bytes
/
assertion.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
// Copyright 2021-Present the Unitest authors. All rights reserved. MIT license.
import { brightBlack, red } from "../deps.ts";
type CodeInfo = {
code: string;
lineNumber: number;
column: number;
};
// /** insert something to beginning of line */
// function BOL(text: string, symbol: string): string {
// return `${symbol}${text.replaceAll("\n", `\n${symbol}`)}`;
// }
function prettyCode({ code, lineNumber }: CodeInfo): string {
const codes = code.split("\n");
const sliced = codes.slice(
lineNumber - 2,
lineNumber + 2,
);
const numberedCodes = sliced.map((fragment, i) => {
const number = i + lineNumber - 1;
const isWrongLine = number === lineNumber;
return `${isWrongLine ? red(">") : " "} ${brightBlack(String(number))} ${
brightBlack("|")
} ${isWrongLine ? fragment : brightBlack(fragment)}`;
});
return numberedCodes.join("\n");
}
export { prettyCode };