aboutsummaryrefslogtreecommitdiffhomepage
path: root/scripts/linters/src/Linters/NoFormatTrailingComma.php
blob: dc364846a0f1b12ba9cd93787ad537db68b1641c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
<?php

declare(strict_types=1);

namespace Shirabe\Lint\Linters;

use Shirabe\Lint\Linter;
use Shirabe\Lint\Support\FileFinder;
use Shirabe\Lint\Support\Paths;

final class NoFormatTrailingComma implements Linter
{
    public function name(): string
    {
        return 'no_format_trailing_comma';
    }

    public function failureIntro(): string
    {
        return 'Found `,)` introduced by formatting. Remove it.';
    }

    public function check(string $rootDir, array $excludes): array
    {
        $errors = [];

        foreach (FileFinder::rustFiles($rootDir) as $path) {
            $relative = Paths::relativeTo($rootDir, $path);
            if (in_array($relative, $excludes, true)) {
                continue;
            }

            foreach (file($path) as $idx => $raw) {
                if (!str_contains($raw, ',)')) {
                    continue;
                }
                // `(,)` is a macro repetition fragment (e.g. `$(,)?`).
                if (str_contains($raw, '(,)')) {
                    continue;
                }

                $errors[] = "{$relative}:" . ($idx + 1) . ': trailing `,)` before a closing paren';
            }
        }

        return $errors;
    }
}