Skip to content

auto-cr: a lightning-fast SWC-powered code review toolkit—CLI + extensible rule SDK—to surface risky JS/TS changes before merge.

License

Notifications You must be signed in to change notification settings

wangweiwei/auto-cr

auto-cr logo

Automated Code Review CLI ⚡️

NPM Version NPM Downloads MIT License GitHub Stars

🎯 auto-cr-cmd is a high-speed automated code review CLI powered by SWC static analysis, built for JavaScript / TypeScript teams to surface risky code before it merges.

🔧 auto-cr-rules provides an extensible static analysis rule set and SDK so you can tailor enterprise-grade review policies with minimal effort.

📘 Prefer Chinese? Read the Chinese README.

Feature Highlights (Automated Code Review & Static Analysis)

  • Built-in Rule Library: Ships with SWC AST static analysis rules out of the box, such as no-deep-relative-imports.
  • Extensible SDK: auto-cr-rules exposes helpers like defineRule and helpers.imports, reducing the friction of authoring custom TypeScript / JavaScript rules.
  • Workspace Friendly: Manage both the CLI and rule package via pnpm workspaces and validate the full pipeline with a single build.
  • Publishing Toolkit: Version bump scripts and npm publish commands keep both packages in sync.

Package Overview (auto-cr-cmd & auto-cr-rules)

  • auto-cr-cmd: A lightning-fast SWC-based CLI focused on automated reviews, CI integration, and static code scanning.
  • auto-cr-rules: A developer-facing rule SDK with tag-based grouping, internationalized messaging, and support for publishing team-specific rules.

Quick Start

npx auto-cr-cmd --language en [path-to-your-code]

Common flags:

  • --language <zh|en>: Switch CLI output language (defaults to auto-detection).
  • --rule-dir <directory>: Load additional custom rules from a directory or package.
  • --output <text|json>: Choose between human-friendly text logs or structured JSON results (defaults to text).
  • --help: Display the full command reference.

Sample output:

 WARN  [12:56:18] ⚠️ [Base Rules]: no-deep-relative-imports

    File: /Volumes/Wei/Codes/github/auto-cr/examples/src/app/features/admin/pages/dashboard.ts:2 
    Description: Import path "../../../../shared/deep/utils" must not exceed max depth 2 
    Code: ../../../../shared/deep/utils
    Suggestion: Use a path alias (for example: @shared/deep/utils). | Create an index file at a higher level to re-export the module and shorten the import.

 WARN  [12:56:18] ⚠️ [untagged]: no-index-import

    File: /Volumes/Wei/Codes/github/auto-cr/examples/src/app/features/admin/pages/dashboard.ts:3
    Description: Import ../../consts/index is not allowed. Import the concrete file instead.

✔  Code scan complete, scanned 3 files: 0 with errors, 1 with warnings, 0 with optimizing hints! 

JSON output sample:

npx auto-cr-cmd --output json -- ./src | jq
{
  "summary": {
    "scannedFiles": 2,
    "filesWithErrors": 1,
    "filesWithWarnings": 0,
    "filesWithOptimizing": 1,
    "violationTotals": {
      "total": 3,
      "error": 2,
      "warning": 0,
      "optimizing": 1
    }
  },
  "files": [
    {
      "filePath": "/workspace/src/example.ts",
      "severityCounts": {
        "error": 2,
        "warning": 0,
        "optimizing": 1
      },
      "totalViolations": 3,
      "errorViolations": 2,
      "violations": [
        {
          "tag": "imports",
          "ruleName": "no-deep-relative-imports",
          "severity": "error",
          "message": "Avoid deep relative imports from src/components/button",
          "line": 13
        }
      ]
    }
  ],
  "notifications": []
}

Writing Custom Rules

The CLI consumes rules from the auto-cr-rules package by default, and you can extend it with your own logic.

1. Prepare a Directory

mkdir custom-rules

Place Node.js-compatible .js / .cjs / .mjs files inside the directory.

2. Install the SDK

pnpm add auto-cr-rules

3. Implement a Rule

// custom-rules/no-index-import.js
const { defineRule } = require('auto-cr-rules')

module.exports = defineRule('no-index-import', ({ helpers, language }) => {
  for (const ref of helpers.imports) {
    if (ref.value.endsWith('/index')) {
      const message =
        language === 'zh'
          ? `禁止直接导入 ${ref.value},请改用具体文件`
          : `Import ${ref.value} is not allowed. Import the concrete file instead.`

      helpers.reportViolation(message, ref.span)
    }
  }
})

RuleContext offers:

  • helpers.imports: Normalized import / require / dynamic import references.
  • helpers.isRelativePath, helpers.relativeDepth: Common path utilities.
  • helpers.reportViolation(message, span?): Unified reporting API.
  • language and reporter: Access the active language and low-level reporter APIs.

You can export multiple rules at once:

const { defineRule } = require('auto-cr-rules')

const ruleA = defineRule('rule-a', (context) => { /* ... */ })
const ruleB = defineRule('rule-b', (context) => { /* ... */ })

module.exports = { rules: [ruleA, ruleB] }

4. Run It

cd examples
npx auto-cr-cmd -l zh -r ./custom-rules/rules -- ./custom-rules/demo

Project Layout

packages/
  auto-cr-rules/   # Rule SDK and built-in rules (createRuleContext, defineRule, etc.)
  auto-cr-cmd/     # CLI entry point, reporter, i18n, and command handling
scripts/
  bump-version.mjs # Keep both package versions aligned
examples/
  custom-rules     # Custom rule samples
  src              # Example that triggers the base rule

Essential scripts:

  • pnpm run version [major|minor|patch]: Bump both packages together (defaults to patch).
  • pnpm run publish: Run version bump, build, and publish for both packages sequentially.

Contributing

We welcome contributions through Issues or Pull Requests. Please read:

Community & Support


Auto CR © [2025] [dengfengwang]. Licensed under the MIT License

About

auto-cr: a lightning-fast SWC-powered code review toolkit—CLI + extensible rule SDK—to surface risky JS/TS changes before merge.

Resources

License

Code of conduct

Contributing

Security policy

Stars

Watchers

Forks

Packages

No packages published

Contributors 2

  •  
  •