crystal-ameba/ameba

GitHub: crystal-ameba/ameba

Crystal 语言的静态代码分析工具,用于强制代码风格一致性并捕捉代码坏味道。

Stars: 560 | Forks: 42

Ameba

Code style linter for Crystal

(a single-celled animal that catches food and moves about by extending fingerlike projections of protoplasm)

- [关于](#about) - [用法](#usage) - [观看教程](#watch-a-tutorial) - [自动修正](#autocorrection) - [解释问题](#explain-issues) - [描述规则](#describe-rules) - [并行运行](#run-in-parallel) - [安装](#installation) - [作为项目依赖](#as-a-project-dependency) - [OS X](#os-x) - [Docker](#docker) - [从源码构建](#from-sources) - [配置](#configuration) - [源文件](#sources) - [规则](#rules) - [行内禁用](#inline-disabling) - [编辑器与集成](#editors--integrations) - [致谢与灵感来源](#credits--inspirations) - [贡献者](#contributors) ## 关于 Ameba 是一个用于 Crystal 语言的静态代码分析工具。 它强制执行一致的 [Crystal 代码风格](https://crystal-lang.org/reference/conventions/coding_style.html),同时还能捕捉代码坏味道和错误的代码结构。 另请参见 [路线图](https://github.com/crystal-ameba/ameba/wiki#roadmap)。 ## 用法 在你的项目目录中运行 `ameba` 二进制文件以捕捉代码问题: ``` $ ameba Inspecting 107 files ...............F.....................FF.................................................................... src/ameba/formatter/flycheck_formatter.cr:6:37 [W] Lint/UnusedArgument: Unused argument `location`. If it's necessary, use `_` as an argument name to indicate that it won't be used. > source.issues.each do |issue, location| ^------^ src/ameba/formatter/base_formatter.cr:16:14 [W] Lint/UselessAssign: Useless assignment to variable `size` > return size += issues.size ^--^ src/ameba/formatter/base_formatter.cr:16:7 [Correctable] [C] Style/RedundantReturn: Redundant `return` detected > return size += issues.size ^------------------------^ Finished in 389.45 milliseconds 107 inspected, 3 failures ``` ### 观看教程 [🎬 观看展示如何使用 Ameba 的 LuckyCast](https://luckycasts.com/videos/ameba) ### 自动修正 在输出中被标记为 `[Correctable]` 的规则可以使用 `--fix` 标志自动修正: ``` $ ameba --fix ``` ### 解释问题 Ameba 允许你深入了解某个问题,展示关于该问题的详细信息 以及其被报告的原因。 为了方便起见,你可以直接从报告 中复制 `PATH:line:column` 字符串,并粘贴到 `ameba` 命令后面进行查看。 ``` $ ameba crystal/command/format.cr:26:83 # show explanation for the issue $ ameba --explain crystal/command/format.cr:26:83 # same thing ``` ### 描述规则 你可以使用 `--describe` 标志来获取规则的详细描述: ``` $ ameba --describe Lint/UselessAssign ``` ### 并行运行 在 Crystal 仓库上运行 Ameba 时测得的一些快速基准测试结果: ``` $ CRYSTAL_WORKERS=1 ameba #=> 29.11 seconds $ CRYSTAL_WORKERS=2 ameba #=> 19.49 seconds $ CRYSTAL_WORKERS=4 ameba #=> 13.48 seconds $ CRYSTAL_WORKERS=8 ameba #=> 10.14 seconds ``` ## 安装 ### 作为项目依赖 将以下内容添加到你的应用程序的 `shard.yml` 中: ``` development_dependencies: ameba: github: crystal-ameba/ameba ``` 为了优先考虑运行时性能而非编译时间,你可以将 `ameba` 目标添加到 `shard.yml` 文件中: ``` targets: ameba: main: lib/ameba/bin/ameba.cr ``` 然后运行: ``` $ shards build ameba -Dpreview_mt ``` 或者,跳过添加 `ameba` 目标,直接使用 `crystal build` 命令: ``` $ crystal build -Dpreview_mt -o bin/ameba lib/ameba/bin/ameba.cr ``` 这两种方式都会生成一个编译好的二进制文件,并放在 `bin/ameba` 路径下。 你也可以直接运行 `lib/ameba/bin/ameba.cr` 文件,即时编译,这是最慢的选项: ``` $ lib/ameba/bin/ameba.cr ``` ### OS X ``` $ brew tap crystal-ameba/ameba $ brew install ameba ``` ### Docker 构建镜像: ``` $ docker build -t ghcr.io/crystal-ameba/ameba . ``` 要在本地源代码文件夹中使用生成的镜像,请将当前(或目标)目录挂载到 `/src`: ``` $ docker run -v $(pwd):/src ghcr.io/crystal-ameba/ameba ``` 也可在 GitHub 上获取:https://github.com/crystal-ameba/ameba/pkgs/container/ameba ### 从源码构建 ``` $ git clone https://github.com/crystal-ameba/ameba && cd ameba $ make install ``` ## 配置 默认配置文件是 `.ameba.yml`。 它允许你配置规则属性、禁用特定规则以及从规则中排除源文件。 通过运行 `ameba --gen-config` 生成新文件。 ### 源文件 **运行 Ameba 的源文件列表可以通过以下方式全局配置:** - `Globs` 部分 - 一个通配符(或路径)数组,用于将文件包含到 检查中。默认为 `%w[**/*.cr **/*.ecr]`,这意味着它包含项目中所有 扩展名为 `*.cr` 和 `*.ecr` 的文件。 - `Excluded` 部分 - 一个通配符(或路径)数组,用于从 `Globs` 定义的 源列表中排除。默认为 `%w[lib]`,这意味着它排除了 `lib` 文件夹。 在这个例子中,我们定义了默认的 glob 并排除了 `lib` 和 `src/compiler` 文件夹: ``` Globs: - "**/*.cr" - "**/*.ecr" Excluded: - lib - src/compiler ``` **可以在规则级别排除特定的源文件**: ``` Style/RedundantBegin: Excluded: - src/server/processor.cr - src/server/api.cr ``` ### 规则 可以通过命令行参数包含或排除一个或多个规则,或一个或多个规则组: ``` $ ameba --only Lint/Syntax # runs only Lint/Syntax rule $ ameba --only Style,Lint # runs only rules from Style and Lint groups $ ameba --except Lint/Syntax # runs all rules except Lint/Syntax $ ameba --except Style,Lint # runs all rules except rules in Style and Lint groups ``` 或者通过配置文件: ``` Style/RedundantBegin: Enabled: false ``` ### 行内禁用 可以使用行内指令禁用一个或多个规则,或一个或多个规则组: ``` # ameba:disable Style/LargeNumbers time = Time.epoch(1483859302) time = Time.epoch(1483859302) # ameba:disable Style/LargeNumbers, Lint/UselessAssign time = Time.epoch(1483859302) # ameba:disable Style, Lint ``` ## 编辑器与集成 - Vim: [vim-crystal](https://github.com/rhysd/vim-crystal)、[Ale](https://github.com/w0rp/ale) - Emacs: [ameba.el](https://github.com/crystal-ameba/ameba.el) - Sublime Text: [Sublime Linter Ameba](https://github.com/epergo/SublimeLinter-contrib-ameba) - VSCode: [vscode-crystal-ameba](https://github.com/crystal-ameba/vscode-crystal-ameba) - Codacy: [codacy-ameba](https://github.com/codacy/codacy-ameba) - GitHub Actions: [github-action](https://github.com/crystal-ameba/github-action) ## 致谢与灵感来源 - [Crystal 语言](https://crystal-lang.org) - [Rubocop](https://rubocop.org) - [Credo](http://credo-ci.org) - [Dogma](https://github.com/lpil/dogma) ## 贡献者 - [veelenga](https://github.com/veelenga) Vitalii Elenhaupt - 创始人、维护者 - [Sija](https://github.com/Sija) Sijawusz Pur Rahnama - 贡献者、维护者
标签:Apache Flink, Crystal, Linter, pptx, 代码规范检查, 请求拦截, 错误基检测, 静态代码分析