Rubocop:从命令行给出的列表中排除文件

Rubocop: Exclude files from a list given on command line

我 运行 来自我的预提交挂钩脚本的 rubocop:

bin/bundle exec rubocop ${FILES}

其中 ${FILES} 是准备提交的文件列表。但是,这并不符合 rubocop.yml.

AllCops/Exclude 部分的内容

由于有些文件总是会产生违规行为(例如schema.rb),有没有办法让配置Exclude部分位于命令行参数之前?

我不知道,但我同意应该有(或者应该更容易)。

有几个问题 already opened 与此有关。也许需要将该设置一分为二(例如 AllCops/AlwaysExcludeAllCops/ExcludeByDefault)。

有一个 inherit_mode 选项,但我不是 100% 确定它是如何工作的,或者如果 it could help

我编辑了我的 pre-commit 脚本以生成一个临时的 Rubocop 配置,它继承自主要配置。临时配置仅包含 AllCopsInclude 部分,这似乎与主配置中的 AllCops/Exclude 一起正常工作。无需篡改 inherit_mode.

TEMP_CONFIG=/tmp/rubocop-`date +%s`.yml
RUBOCOP_CONFIG=`realpath .rubocop.yml`
cat << EOF > $TEMP_CONFIG
inherit_from: $RUBOCOP_CONFIG
AllCops:
  Include:
EOF
for file in "${FILES[@]}"; do
    echo "    - $file" >> $TEMP_CONFIG
done
bin/bundle exec rubocop -a -c $TEMP_CONFIG
rm $TEMP_CONFIG

rubocop 有 command-line 选项 --force-exclusion,它排除了 .rubocop.yml Exclude 部分中指定的文件。这是在 pull request 922.

上添加的