aboutsummaryrefslogtreecommitdiffhomepage
path: root/scripts/linters/src/Linters/NoStdCollectionsMaps.php
blob: c2f9c32e2551d9355985866a85dfc361929303d3 (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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
<?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 NoStdCollectionsMaps implements Linter
{
    private const BANNED_MAP_NAMES = ['HashMap', 'HashSet', 'BTreeMap', 'BTreeSet'];

    public function name(): string
    {
        return 'no_std_collections_maps';
    }

    public function failureIntro(): string
    {
        return "Found uses of `std::collections::{HashMap, HashSet, BTreeMap, BTreeSet}`.\n"
            . 'Use `indexmap::IndexMap` / `indexmap::IndexSet` instead:';
    }

    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;
            }

            array_push($errors, ...$this->findStdMapUsages($path, $relative));
        }

        return $errors;
    }

    /** @return list<string> */
    private function findStdMapUsages(string $path, string $relative): array
    {
        $errors = [];

        foreach (file($path) as $idx => $raw) {
            $code = explode('//', $raw, 2)[0];

            if (preg_match_all('/\bstd::collections::(HashMap|HashSet|BTreeMap|BTreeSet)\b/', $code, $m)) {
                foreach ($m[1] as $name) {
                    $errors[] = "{$relative}:" . ($idx + 1) . ": use of `std::collections::{$name}` (use `indexmap::" . self::indexmapReplacement($name) . '` instead)';
                }
            }

            if (preg_match_all('/\bstd::collections::\{([^}]*)\}/', $code, $m)) {
                foreach ($m[1] as $group) {
                    foreach (explode(',', $group) as $entry) {
                        $name = preg_split('/\s+as\s+/', trim($entry))[0];
                        if (!in_array($name, self::BANNED_MAP_NAMES, true)) {
                            continue;
                        }
                        $errors[] = "{$relative}:" . ($idx + 1) . ": import of `std::collections::{$name}` (use `indexmap::" . self::indexmapReplacement($name) . '` instead)';
                    }
                }
            }
        }

        return array_values(array_unique($errors));
    }

    private static function indexmapReplacement(string $name): string
    {
        return match ($name) {
            'HashMap', 'BTreeMap' => 'IndexMap',
            'HashSet', 'BTreeSet' => 'IndexSet',
            default => throw new \LogicException("unexpected map name: {$name}"),
        };
    }
}