Skip to content

feat: support --rule CLI flag for runtime rule overrides#603

Merged
chenjiahan merged 1 commit intomainfrom
feat/cli-rule-flag-20260407
Apr 7, 2026
Merged

feat: support --rule CLI flag for runtime rule overrides#603
chenjiahan merged 1 commit intomainfrom
feat/cli-rule-flag-20260407

Conversation

@fansenze
Copy link
Copy Markdown
Contributor

@fansenze fansenze commented Apr 7, 2026

Summary

Add ESLint-compatible --rule CLI flag that allows overriding rule severity and options from the command line without modifying config files.

Syntax:

rslint --rule 'no-console: error'
rslint --rule 'no-console: [warn, {"allow": ["warn", "error"]}]'
rslint --rule 'no-console: off' --rule 'no-debugger: error'
rslint src/ --rule '@typescript-eslint/no-explicit-any: off'

Implementation:

  • internal/config/cli_rules.goParseCLIRuleFlag parses --rule values (severity or JSON array with options), BuildCLIRuleEntry builds a synthetic ConfigEntry with no files field (matches all files)
  • cmd/rslint/cmd.gorepeatedFlag type for multiple --rule, synthetic entry appended to config array end (both single and multi-config/monorepo)
  • packages/rslint/src/utils/args.ts — registers rule as known string flag (multiple: true), reorders flags before positional args (Go's flag.Parse stops at first positional), preserves -- option-terminator position
  • website/docs/en/guide/cli.md — CLI reference updated with --rule flag and "Rule Overrides" section

Zero changes to core config merging (GetConfigForFile) or linter logic (GetEnabledRules).

Tests:

  • internal/config/cli_rules_test.go — 30 Go unit tests (parsing, whitespace, nested JSON options, construction, integration with config merge)
  • packages/rslint/tests/args.test.ts — 17 JS unit tests (rest ordering, --rule reordering, -- terminator handling)
  • packages/rslint-test-tools/tests/cli/rule-flag.test.ts — 36 e2e tests using JS config (basic usage, severity/options override, ordering matrix, multi-rule × flag cross, whitespace variations, error handling, -- terminator, plugin rules, per-file override)

Related Links

Design aligned with ESLint v10's --rule flag behavior.

Checklist

  • Tests updated (or not required).
  • Documentation updated (or not required).

Copy link
Copy Markdown
Contributor

@gemini-code-assist gemini-code-assist Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request introduces the --rule CLI flag, allowing users to override or add linting rules directly from the command line using an ESLint-compatible format. The implementation includes a new repeatedFlag type in the Go command-line interface to handle multiple occurrences of the flag, a parsing utility for rule configurations (supporting both simple severity strings and complex JSON arrays), and logic to merge these overrides into the existing configuration. Additionally, the JavaScript argument parser was updated to ensure flags are reordered before positional arguments, preventing them from being ignored by Go's flag parser. Comprehensive unit, integration, and CLI tests have been added to verify the new functionality. I have no feedback to provide as the implementation is robust and well-tested.

@cloudflare-workers-and-pages
Copy link
Copy Markdown

cloudflare-workers-and-pages Bot commented Apr 7, 2026

Deploying rslint with  Cloudflare Pages  Cloudflare Pages

Latest commit: 57f7e4c
Status: ✅  Deploy successful!
Preview URL: https://f648e106.rslint.pages.dev
Branch Preview URL: https://feat-cli-rule-flag-20260407.rslint.pages.dev

View logs

@fansenze fansenze force-pushed the feat/cli-rule-flag-20260407 branch from a618314 to 1c0e0ee Compare April 7, 2026 04:35
Add ESLint-compatible --rule flag that allows overriding rule severity
and options from the command line without modifying config files.

Syntax: --rule 'ruleName: severity' or --rule 'ruleName: [severity, options]'
Multiple --rule flags supported, later overrides earlier for same rule.

Implementation injects a synthetic ConfigEntry (no files = matches all)
at the end of the config array, leveraging existing merge semantics
for highest precedence with zero changes to core config/linter logic.

JS side registers rule as a known string flag and reorders flags before
positional args to work around Go's flag.Parse stopping at the first
positional argument. Option-terminator (--) position is preserved.
@fansenze fansenze force-pushed the feat/cli-rule-flag-20260407 branch from d5ec8f2 to 57f7e4c Compare April 7, 2026 05:00
@chenjiahan chenjiahan merged commit 71d53b8 into main Apr 7, 2026
20 of 22 checks passed
@chenjiahan chenjiahan deleted the feat/cli-rule-flag-20260407 branch April 7, 2026 05:54
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants