-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
acf09db
commit 2bcb7a7
Showing
3 changed files
with
89 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
}; | ||
}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} |