ให้ Rubocop และ ESLint ignore gem ที่ bundle ไว้ใน GitHub Actions
โปรเจกต์ Rails มักติดตั้ง gem ลงใน vendor directory ใน GitHub actions เพื่อให้ cache ไว้ใช้ในรอบถัด ๆ ไปได้
ถ้าคุณใช้ code linter ใน action คุณต้อง exclude vendor/bundle/\* ออกเพื่อป้องกัน false positive
สำหรับ ESLint ตั้งค่าได้ผ่าน ignorePatterns
// ./.eslintrc.json
{
"ignorePatterns": [
"vendor/bundle/*"
]
}
สำหรับ Rubocop ให้ exclude ผ่าน config AllCops
# ./.rubocop.yml
AllCops:
Exclude:
- 'vendor/**/*'
ทำไมต้อง Ignore Gem ที่ Bundle ไว้ใน Code Linter
ในการพัฒนาซอฟต์แวร์ การรักษาคุณภาพโค้ดให้อยู่ในระดับสูงเป็นสิ่งจำเป็น code linter คือเครื่องมือที่ช่วยเรื่องนี้ด้วยการทำ static code analysis ซึ่งตรวจจับข้อผิดพลาดที่อาจเกิดขึ้นและบังคับใช้ coding style ให้สม่ำเสมอได้ อย่างไรก็ตาม เวลาใช้ linter ในสภาพแวดล้อม continuous integration อย่าง GitHub Actions การตั้งค่าให้ถูกต้องเพื่อเลี่ยง false positive เป็นเรื่องสำคัญมาก
gem ที่ bundle ไว้ ซึ่งมักเก็บอยู่ใน vendor directory คือ dependency ภายนอกที่อาจถูก code linter แจ้งเตือนถ้าไม่ได้ exclude ให้เรียบร้อย การ ignore directory เหล่านี้ช่วยให้ linter โฟกัสที่ source code ของคุณแทน library ภายนอก และทำให้ผลการวิเคราะห์โค้ดยังเชื่อถือได้
ตั้งค่า Linter ให้ Exclude Gem ที่ Bundle ไว้
สำหรับโปรเจกต์ JavaScript ที่ใช้ ESLint คุณตั้งค่าให้ linter ignore directory vendor/bundle/* ได้ผ่านการตั้งค่า ignorePatterns ในไฟล์ .eslintrc.json เพื่อให้ตรวจเฉพาะโค้ดของคุณเท่านั้น คุณภาพโค้ดจะดีขึ้นโดยไม่มีเรื่องอื่นมากวน
// ./.eslintrc.json
{
"ignorePatterns": [
"vendor/bundle/*"
]
}
ในทำนองเดียวกัน สำหรับโปรเจกต์ Ruby ที่ใช้ Rubocop คุณ exclude vendor directory ได้ด้วยการเพิ่มกฎ Exclude ไว้ใต้ AllCops ในไฟล์ config .rubocop.yml เพื่อให้ static code analysis โฟกัสที่ source code จริง ๆ
# ./.rubocop.yml
AllCops:
Exclude:
- 'vendor/**/*'
เมื่อตั้งค่า code linter ให้ exclude gem ที่ bundle ไว้ คุณก็รักษามาตรฐานคุณภาพโค้ดในระดับสูงไว้ได้ พร้อมกับเลี่ยง false positive ใน static code analysis