iko-error-chalk
Version:
iko AssertionError chalk renderer
123 lines • 4.27 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
const rtext_render_1 = require("rtext-render");
const chalk = require("chalk");
const DEFAULT_COLOR_SCHEMA = {
"matcherHint": "dim",
"hint": "dim",
"info": null,
"received": "red",
"expected": "green",
"highlight": "bgRed",
"diff": null,
"diffPatchMark": "dim",
"diffAdded": "green",
"diffRemoved": "red",
"diffAddedHighlight": "bgGreen",
"diffRemovedHighlight": "bgRed",
};
const PRIORITIES = {
"highlight": 0,
"received": 1,
"expected": 2,
"+": 3,
"-": 4,
"info": 100,
"hint": 101,
"patchMark": 102,
"matcherHint": 1000,
"diff": 1001,
};
function createNewChalk(c, modifier) {
if (typeof modifier === "string") {
return c[modifier];
}
if (modifier !== null) {
for (let i = 0; i < modifier.length; i++) {
c = c[modifier[i]];
}
}
return c;
}
function createAssertionErrorRenderer(colors) {
const colorSchema = colors === undefined ?
DEFAULT_COLOR_SCHEMA : Object.assign({}, DEFAULT_COLOR_SCHEMA, colors);
return rtext_render_1.createRichTextRenderer(PRIORITIES, {
onInit: () => ({
flags: 0,
chalk: chalk,
result: "",
}),
onEnter: (a, p) => {
const type = a.type;
let newChalk = p.chalk;
let flags = p.flags;
switch (type) {
case "matcherHint":
newChalk = createNewChalk(newChalk, colorSchema.matcherHint);
flags = 1 /* MatcherHint */;
break;
case "info":
newChalk = createNewChalk(newChalk, colorSchema.info);
flags = 2 /* Info */;
break;
case "hint":
newChalk = createNewChalk(newChalk, colorSchema.hint);
flags = 4 /* Hint */;
break;
case "received":
newChalk = createNewChalk(newChalk, colorSchema.received);
flags = 8 /* Received */;
break;
case "expected":
newChalk = createNewChalk(newChalk, colorSchema.expected);
flags = 16 /* Expected */;
break;
case "diff":
newChalk = createNewChalk(newChalk, colorSchema.diff);
flags = 64 /* Diff */;
break;
case "patchMark":
newChalk = createNewChalk(newChalk, colorSchema.diffPatchMark);
flags = 128 /* DiffPatchMark */;
break;
case "+":
newChalk = createNewChalk(newChalk, colorSchema.diffAdded);
flags = 256 /* DiffAdded */;
break;
case "-":
newChalk = createNewChalk(newChalk, colorSchema.diffRemoved);
flags = 512 /* DiffRemoved */;
break;
case "highlight":
if ((flags & (256 /* DiffAdded */ | 512 /* DiffRemoved */)) !== 0) {
if ((flags & 256 /* DiffAdded */) !== 0) {
newChalk = createNewChalk(newChalk, colorSchema.diffAddedHighlight);
}
else {
newChalk = createNewChalk(newChalk, colorSchema.diffRemovedHighlight);
}
}
else {
newChalk = createNewChalk(newChalk, colorSchema.highlight);
}
flags = 32 /* Highlight */;
break;
}
return {
flags: flags,
chalk: newChalk,
result: "",
};
},
onExit: (a, c, p) => {
p.result += c.result;
},
onText: (s, c) => {
c.result += c.chalk(s);
},
onResult: (c) => c.result,
});
}
exports.createAssertionErrorRenderer = createAssertionErrorRenderer;
//# sourceMappingURL=index.js.map