【问题标题】:Rubocop: Exclude files from a list given on command lineRubocop:从命令行给出的列表中排除文件
【发布时间】:2020-08-27 11:20:24
【问题描述】:
我从我的预提交挂钩脚本运行 rubocop:
bin/bundle exec rubocop ${FILES}
${FILES} 是暂存待提交的文件列表。但是,这并不尊重rubocop.yml 中AllCops/Exclude 部分的内容。
由于有些文件总是会产生攻击(例如 schema.rb),有没有办法让配置 Exclude 部分位于命令行参数之前?
【问题讨论】:
标签:
git
rubocop
pre-commit
【解决方案1】:
我不知道,但我同意应该有(或者应该更容易)。
already opened 对此有一些问题。也许需要将该设置一分为二(例如使用AllCops/AlwaysExclude 或AllCops/ExcludeByDefault)。
有一个 inherit_mode 选项,但我不能 100% 确定它是如何工作的,或者是否 it could help
【解决方案2】:
我编辑了我的预提交脚本以生成一个临时的 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