Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[PSC-2029] Allow configuring the lint command via CLI arguments #2153

Merged
merged 1 commit into from
Nov 17, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 31 additions & 6 deletions cli/src/commands/lint.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@ 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";

export interface LintConfig {
rules: Record<string, string>;
}

// TODO: Make it possible to specify by reading a config file
const lintConfig: LintConfig = {
rules: {
"no-omittable-fields-within-response-bodies": "warn"
Expand All @@ -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 = [
{
Expand All @@ -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<any> = {
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<void> {
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,
Expand Down