Skip to content

Commit

Permalink
✨ Create generate todo
Browse files Browse the repository at this point in the history
  • Loading branch information
markbrouch committed Dec 19, 2019
1 parent acf09db commit 2bcb7a7
Show file tree
Hide file tree
Showing 3 changed files with 89 additions and 0 deletions.
33 changes: 33 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import fs from "fs";
import util from "util";
import { CLIEngine, Linter } from "eslint";

import { renderAsYAML, renderAsJSON } from "./render";
import { mapReportToOverrides } from "./overrides";

const writeFile = util.promisify(fs.writeFile);

export async function execute(
files: Array<string>,
{
level,
format = "yaml",
path
}: { level: Linter.RuleLevel; format: "yaml" | "json"; path?: string }
): Promise<string> {
const cli = new CLIEngine({});

const report = cli.executeOnFiles(files) as CLIEngine.LintReport;
const config = { overrides: mapReportToOverrides(report, level) };

const outputPath =
path || `.eslintrc-todo.${format === "yaml" ? "yml" : "json"}`;

await writeFile(
outputPath,
format === "yaml" ? renderAsYAML(config) : renderAsJSON(config),
{ encoding: "utf8" }
);

return outputPath;
}
33 changes: 33 additions & 0 deletions src/overrides.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import path from "path";
import { CLIEngine, Linter } from "eslint";

function stripEmptyFiles(
results: CLIEngine.LintResult[]
): CLIEngine.LintResult[] {
return results.filter(result => !!result.messages.length);
}

function stripNullRuleIds(ruleIds: (string | null)[]): string[] {
return ruleIds.filter((ruleId): ruleId is string => ruleId !== null);
}

export function mapReportToOverrides(
report: CLIEngine.LintReport,
level: Linter.RuleLevel
): Linter.RuleOverride[] {
return stripEmptyFiles(report.results).map((result: CLIEngine.LintResult) => {
const uniqueRuleIds = Array.from(
new Set(result.messages.map(message => message.ruleId))
);

const rules: Linter.RulesRecord = {};
stripNullRuleIds(uniqueRuleIds).forEach(ruleId => {
rules[ruleId] = level;
});

return {
files: [path.relative(process.cwd(), result.filePath)],
rules
};
});
}
23 changes: 23 additions & 0 deletions src/render.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import { Linter } from "eslint";
import yaml from "js-yaml";

function generateYAMLCommentBlock(): string {
return `# This configuration was generated by
# \`eslint-generate-todo\` (http://github.com/doximity/eslint-generate-todo)
#
# on ${new Date().toISOString()}.
#
# It contains all the known ESLint errors/warnings, and prevents them from
# showing up as errors until devs can remove them from the code base.
# Regenerate this file as errors are fixed.
`;
}

export function renderAsYAML(config: Linter.Config): string {
return `${generateYAMLCommentBlock()}${yaml.dump(config)}`;
}

export function renderAsJSON(config: Linter.Config): string {
return JSON.stringify(config, null, 2);
}

0 comments on commit 2bcb7a7

Please sign in to comment.