diff --git a/cli/src/commands/lint.ts b/cli/src/commands/lint.ts index 66a9a480f..69a6a0183 100644 --- a/cli/src/commands/lint.ts +++ b/cli/src/commands/lint.ts @@ -2,6 +2,7 @@ import { Command, flags } from "@oclif/command"; import { lint } from "../../../lib/src/linting/linter"; import { parse } from "../../../lib/src/parser"; import { findLintViolations } from "../../../lib/src/linting/find-lint-violations"; +import { availableRules } from "../../../lib/src/linting/rules"; const ARG_API = "spot_contract"; @@ -9,7 +10,6 @@ export interface LintConfig { rules: Record; } -// TODO: Make it possible to specify by reading a config file const lintConfig: LintConfig = { rules: { "no-omittable-fields-within-response-bodies": "warn" @@ -22,7 +22,11 @@ const lintConfig: LintConfig = { export default class Lint extends Command { static description = "Lint a Spot contract"; - static examples = ["$ spot lint api.ts"]; + static examples = [ + "$ spot lint api.ts", + "$ spot lint --has-descriminator=error", + "$ spot lint --no-nullable-arrays=off" + ]; static args = [ { @@ -33,16 +37,37 @@ export default class Lint extends Command { } ]; - static flags = { - help: flags.help({ char: "h" }) - }; + static flags = this.buildFlags(); + + static buildFlags() { + // Arguments depend on the list of available rules, it cannot be typed ahead of time. + // eslint-disable-next-line @typescript-eslint/no-explicit-any + const finalFlags: flags.Input = { + help: flags.help({ char: "h" }) + }; + + Object.keys(availableRules).forEach((rule: string) => { + finalFlags[rule] = flags.enum({ + description: `Setting for ${rule}`, + options: ["error", "warn", "off"] + }); + }); + + return finalFlags; + } async run(): Promise { - const { args } = this.parse(Lint); + const { args, flags } = this.parse(Lint); const contractPath = args[ARG_API]; const contract = parse(contractPath); const groupedLintErrors = lint(contract); + Object.keys(availableRules).forEach((rule: string) => { + if (flags[rule] !== undefined) { + lintConfig.rules[rule] = flags[rule]; + } + }); + const { errorCount, warningCount } = findLintViolations( groupedLintErrors, lintConfig,