All files / src/reporter index.ts

0% Statements 0/16
0% Branches 0/2
0% Functions 0/4
0% Lines 0/15
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                                                                     
import { AnnotatedComment, Plugin } from "@hygiene/core";
import { Reporter, ReportWrapper } from "./Reporter";
import { text as textReporter } from "./text";
import { json as jsonReporter } from "./json";
 
const wrapReporter = (r: Reporter): ReportWrapper => (
  comments: AnnotatedComment<any>[],
  plugins: Plugin<any, any>[],
  pluginSettings: any[]
) => {
  return r(
    comments.map(comment => {
      const { annotation, ...other } = comment;
      const plugin = plugins.find((plugin, i) => {
        const pluginSetting = pluginSettings[i];
        return plugin.isMine(annotation, pluginSetting);
      });
      if (!plugin) {
        throw new Error("Cannot resolve any plugin");
      }
      const pluginSetting = pluginSettings[plugins.indexOf(plugin)];
      const message = plugin.toMessage(annotation, pluginSetting);
 
      return {
        ruleName: plugin.name,
        message,
        ...other
      };
    })
  );
};
 
export const text = wrapReporter(textReporter);
export const json = wrapReporter(jsonReporter);