Skip to content

Introduction

  • eslint คือ linter ที่อย่กับ javascript มานาน คนใช้กันมากที่สุด
  • ดู eslint.org faviconrules ที่หลากหลาย
  • มี eslint.org faviconconfig inspector ด้วย

Usage

ถ้าจะใช้ ESLint ให้ใช้ผ่าน ESLint plugins ที่กำหนดไว้ให้แล้ว จะได้เป็น standard เดียวกัน

  1. ติดตั้ง dependencies
bash
npm install --save-dev eslint @typescript-eslint/parser @typescript-eslint/eslint-plugin
  1. config (eslint.config.js)
javascript
// eslint.config.js
import typescriptPlugin from "@typescript-eslint/eslint-plugin";

export default [
  {
    parser: "@typescript-eslint/parser", // กำหนดให้ใช้ parser ของ TypeScript
    plugins: {
      "@typescript-eslint": typescriptPlugin,
    },
    extends: [
      "eslint:recommended",
      "plugin:@typescript-eslint/recommended", // ใช้ rule ที่แนะนำจาก @typescript-eslint
    ],
    rules: {
      "@typescript-eslint/explicit-module-boundary-types": "warn", // แนะนำให้กำหนด type ให้ชัดเจน
      "@typescript-eslint/no-explicit-any": "off", // ถ้าต้องการใช้ any สามารถปิดได้
      "@typescript-eslint/indent": ["error", 2], // ตั้งค่าการเยื้องโค้ด 2 ช่อง
    },
  },
];
  1. รันคำสั่งใน terminal
npm
bash
npx eslint .
pnpm
bash
pnpm dlx eslint .
yarn
bash
yarn dlx eslint .
bun
bash
bunx eslint .

:::