name: Validate JS

on:
  push:
    branches:
      - main
    paths:
      - ".github/workflows/validate-js.yml"
      - "package/src/**"
      - "package/*.json"
      - "package/*.js"
      - "package/*.lock"
      - "example/src/**"
      - "example/*.json"
      - "example/*.js"
      - "example/*.lock"
      - "example/*.tsx"
  pull_request:
    paths:
      - ".github/workflows/validate-js.yml"
      - "package/src/**"
      - "package/*.json"
      - "package/*.js"
      - "package/*.lock"
      - "example/src/**"
      - "example/*.json"
      - "example/*.js"
      - "example/*.lock"
      - "example/*.tsx"

jobs:
  compile:
    name: Compile JS (tsc)
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4

      - name: Install reviewdog
        uses: reviewdog/action-setup@v1

      - uses: oven-sh/setup-bun@v2

      - name: Install npm dependencies (bun)
        run: bun install

      - name: Run TypeScript # Reviewdog tsc errorformat: %f:%l:%c - error TS%n: %m
        run: |
          cd package && bun typescript | reviewdog -name="tsc" -efm="%f(%l,%c): error TS%n: %m" -reporter="github-pr-review" -filter-mode="nofilter" -fail-on-error -tee
        env:
          REVIEWDOG_GITHUB_API_TOKEN: ${{ secrets.GITHUB_TOKEN }}

      - name: Run TypeScript in example/ # Reviewdog tsc errorformat: %f:%l:%c - error TS%n: %m
        run: |
          cd example && bun typescript | reviewdog -name="tsc" -efm="%f(%l,%c): error TS%n: %m" -reporter="github-pr-review" -filter-mode="nofilter" -fail-on-error -tee && cd ..
        env:
          REVIEWDOG_GITHUB_API_TOKEN: ${{ secrets.GITHUB_TOKEN }}

  lint:
    name: Lint JS (eslint, prettier)
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4

      - uses: oven-sh/setup-bun@v2

      - name: Install npm dependencies (bun)
        run: bun install

      - name: Run ESLint
        run: cd ./package && bun lint-ci

      - name: Run ESLint with auto-fix
        run: cd ./package && bun run lint

      - name: Run ESLint in example/
        run: cd example && bun lint-ci

      - name: Run ESLint in example/ with auto-fix
        run: cd example && bun run lint

      - name: Verify no files have changed after auto-fix
        run: git diff --exit-code HEAD
