อยู่กับ SwiftLint บน Legacy

เล่าก่อน lint หรือ linter คือโปรแรกมที่ใช้วิเคราะห์ code ในภาษา C แต่ปัจจุบันนี้หมายถึง static code analyzer ในภาษาโปรแกรมต่าง ๆ

เช่น ถ้าเรา เขียน

if var studentName = student.name {
print(studentName)
}

มันจะเตือนเราว่า ประกาศ var ทำไม ไม่มีการแก้ไขค่า ทำไมไม่ประกาศ let

หรือ ถ้า code แบบเมื่อกี้

if(let studentName=student.name){
print(student.name)
}
let a = student.age

รอบนี้มันก็จะฟ้องว่า

  • ไม่ต้องใส่วงเล็บก็ได้สวยกว่า
  • ทำไมไม่เว้นวรรค ก่อนและหลัง expression ใน if
  • ทำไมข้างหน้า print ไม่ย่อหน้า
  • studentName ประกาศมาแล้วไม่ได้ใช้
  • ชื่อตัวแปล a สั้นเกินไป ต้องการอย่างน้อย 3 ตัวอักษรนะ
  • มี whitespace ว่าง ๆ 4 ตัวในบรรทัดสุดท้ายเอาออกเสีย

ซึ่งบางอย่างเช่น whitespace ใน SwiftLint สามารถใช้ autocorrect ช่วยได้

ทีนี้ linting เลยกลายเป็น verb ว่าคำการวิเคราะห์ code ด้วย lint

ทั้งนี้ lint สามารถตั้งกฎขึ้นมาเองได้ แต่ตัว SwiftLint จะยึดตาม GitHub’s Swift Style Guide. https://github.com/github/swift-style-guide

ถ้าใครยังไม่เคยติดตั้งหรือใช้งาน SwiftLint ให้ทำตามคู่มือ ใน https://github.com/realm/SwiftLint ได้เลย

กลับ มาที่ SwiftLint ถ้าอยู่ดี ๆ เอา SwiftLint ไปใส่เลย ก็จะบึ้มได้ error, หรือ warning ขึ้น 999+ เพราะปกติแล้วเราไม่ได้เขียน code มาตาม lint หรอก แต่เราก็สามารถเพิกเฉย file, directories ได้ (เข้าไปดูวิธีการ ignore ตาม uri ตะกี้ได้) ปกติก็จะ ignore ใน directory ของ Pods หรือ dependencies อื่น ๆ อยู่แล้ว

และเราไม่สามารถแก้ไขให้ตาม SwiftLint ได้ทั้งโปรเจคในคราวเดียว เพราะเราจะแก้ Error ทั้งหมด คงไม่ไหว ยิ่งถ้าโปรเจคใหญ่มากผมเลยแนะนำ script ข้างล่างนี้

# Run SwiftLint
START_DATE=$(date +"%s")
SWIFT_LINT=/usr/local/bin/swiftlint# Run SwiftLint for given filename
run_swiftlint() {
local filename="${1}"
if [[ "${filename##*.}" == "swift" ]]; then
${SWIFT_LINT} autocorrect --path "${filename}"
${SWIFT_LINT} lint --path "${filename}"
fi
}
if [[ -e "${SWIFT_LINT}" ]]; then
echo "SwiftLint version: $(${SWIFT_LINT} version)"
# Run for both staged and unstaged files
git diff --name-only | while read filename; do run_swiftlint "${filename}"; done
git diff --cached --name-only | while read filename; do run_swiftlint "${filename}"; done
else
echo "${SWIFT_LINT} is not installed."
exit 0
fi
END_DATE=$(date +"%s")
DIFF=$(($END_DATE - $START_DATE))
echo "SwiftLint took $(($DIFF / 60)) minutes and $(($DIFF % 60)) seconds to complete."

ไปใส่ไว้ในไฟล์

Scripts/swiftlint.sh

เสร็จก็ไปใส่ไว้ใน Build Phases ของ Project ว่า

“${SRCROOT}/Scripts/swiftlint.sh”

โดยการทำงานของ script จะตรวจเฉพาะไฟล์ที่ยังไม่ได้ commit เท่านั้น เพราะฉนั้นมันจะตรวจบนไฟล์ใหม่ ๆ หรือ เมื่อมีการแก้ไฟล์เดิมเท่านั้น

ทั้งนี้สามารถลักไก่ได้ด้วยการ commit ไฟล์เข้าไปเลย แต่ก็เป็นสิ่งที่ไม่แนะนำ เราควรจะแก้ตาม lint ที่ได้ตกลงกันไว้แทน

ปล. script ข้างบนผมได้มาจาก stackoverflow.com เมื่อนานมาแล้วผมพยายามหาใหม่แล้วหาไม่เจอ แต่ถ้าใครหา link เจอมาบอกผมด้วยนะครับ ผมจะได้ใส่อ้างอิงให้เค้า

odds.team

Odds Team

Wasith T. (Bai-Phai)

Written by

ตบมือเป็นกำลังใจให้ผมด้วยนะครับ 😘

odds.team

odds.team

Odds Team

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade