Commit 81bef6e7 authored by David Sveningsson's avatar David Sveningsson Committed by David Sveningsson

feat(formatters): use factory to load formatters to make it more webpack-friendly

parent 217bf0cc
const chalk = require("chalk");
const HtmlValidate = require("../../../../build/htmlvalidate").default;
const codeframe = require("../../../../build/formatters/codeframe");
const codeframe = require("../../../../build/formatters/codeframe").default;
module.exports = function generateValidationResultsProcessor(log, validateMap) {
return {
......
......@@ -90,4 +90,11 @@ describe("cli/formatters", () => {
expect(fs.mkdirSync).toHaveBeenCalledWith("mydir", { recursive: true });
expect(fs.writeFileSync).toHaveBeenCalledWith("mydir/foo.txt", "", "utf-8");
});
it("should throw error when formatter is missing", () => {
expect.assertions(1);
expect(() => cli.getFormatter("missing")).toThrow(
'No formatter named "missing"'
);
});
});
import fs from "fs";
import path from "path";
import { Formatter } from "../formatters";
import { getFormatter as formatterFactory, Formatter } from "../formatters";
import { Report, Result } from "../reporter";
import { UserError } from "../error";
type WrappedFormatter = (results: Result[]) => string;
......@@ -24,13 +25,19 @@ function wrap(
};
}
function loadFormatter(name: string): Formatter {
const fn = formatterFactory(name);
if (!fn) {
throw new UserError(`No formatter named "${name}"`);
}
return fn;
}
export function getFormatter(formatters: string): (report: Report) => string {
const fn: WrappedFormatter[] = formatters.split(",").map((cur) => {
const [name, dst] = cur.split("=", 2);
const moduleName = name.replace(/[^a-z]+/g, "");
/* eslint-disable-next-line import/no-dynamic-require */
const formatter = require(`../formatters/${moduleName}`);
return wrap(formatter, dst);
const fn = loadFormatter(name);
return wrap(fn, dst);
});
return (report: Report) => {
return fn
......
import { Message, Result } from "../reporter";
import { FormatterModule } from ".";
import { Formatter } from "./formatter";
const entities: { [key: string]: string } = {
">": ">",
......@@ -27,7 +27,7 @@ function getMessageType(message: Message): "error" | "warning" {
}
}
export default function checkstyleFormatter(results: Result[]): string {
function checkstyleFormatter(results: Result[]): string {
let output = "";
output += `<?xml version="1.0" encoding="utf-8"?>`;
......@@ -61,5 +61,5 @@ export default function checkstyleFormatter(results: Result[]): string {
return output;
}
declare const module: FormatterModule;
module.exports = checkstyleFormatter;
const formatter: Formatter = checkstyleFormatter;
export default formatter;
import path from "path";
import { codeFrameColumns } from "@babel/code-frame";
import { Message, Result } from "../reporter";
import { FormatterModule } from ".";
import { Formatter } from "./formatter";
import chalk = require("chalk");
......@@ -14,8 +14,6 @@ interface SourcePoint {
* Codeframe formatter based on ESLint codeframe.
*/
declare const module: FormatterModule;
/**
* Given a word and a count, append an s if count is not one.
* @param {string} word A word in its singular form.
......@@ -137,7 +135,7 @@ function formatSummary(errors: number, warnings: number): string {
return chalk[summaryColor].bold(`${summary.join(" and ")} found.`);
}
export default function codeframe(results: Result[]): string {
function codeframe(results: Result[]): string {
let errors = 0;
let warnings = 0;
......@@ -165,4 +163,5 @@ export default function codeframe(results: Result[]): string {
return errors + warnings > 0 ? output : "";
}
module.exports = codeframe;
const formatter: Formatter = codeframe;
export default formatter;
import { Result } from "../reporter";
export type Formatter = (results: Result[]) => string;
import { Result } from "../reporter";
import checkstyle from "./checkstyle";
import codeframe from "./codeframe";
import json from "./json";
import stylish from "./stylish";
import text from "./text";
import { Formatter } from "./formatter";
export type Formatter = (results: Result[]) => string;
export { Formatter } from "./formatter";
export interface FormatterModule {
exports: Formatter;
const availableFormatters: Record<string, Formatter> = {
checkstyle,
codeframe,
json,
stylish,
text,
};
/**
* Get formatter function by name.
*
* @param name - Name of formatter.
* @returns Formatter function or null if it doesn't exist.
*/
export function getFormatter(name: string): Formatter | null {
return availableFormatters[name] ?? null;
}
import { Result } from "../reporter";
import { FormatterModule } from ".";
import { Formatter } from "./formatter";
export default function jsonFormatter(results: Result[]): string {
function jsonFormatter(results: Result[]): string {
return JSON.stringify(results);
}
declare const module: FormatterModule;
module.exports = jsonFormatter;
const formatter: Formatter = jsonFormatter;
export default formatter;
import { FormatterModule } from ".";
import { Formatter } from "./formatter";
const stylish = require("eslint/lib/cli-engine/formatters/stylish");
export default stylish;
declare const module: FormatterModule;
module.exports = stylish;
const formatter: Formatter = stylish;
export default formatter;
import { Result } from "../reporter";
import { FormatterModule } from ".";
import { Formatter } from "./formatter";
export default function textFormatter(results: Result[]): string {
function textFormatter(results: Result[]): string {
let output = "";
let total = 0;
......@@ -33,5 +33,5 @@ export default function textFormatter(results: Result[]): string {
return total > 0 ? output : "";
}
declare const module: FormatterModule;
module.exports = textFormatter;
const formatter: Formatter = textFormatter;
export default formatter;
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment