File: add_pyre_directive.py

package info (click to toggle)
python-libcst 1.8.6-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 6,240 kB
  • sloc: python: 78,096; makefile: 15; sh: 2
file content (58 lines) | stat: -rw-r--r-- 1,681 bytes parent folder | download | duplicates (2)
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
# Copyright (c) Meta Platforms, Inc. and affiliates.
#
# This source code is licensed under the MIT license found in the
# LICENSE file in the root directory of this source tree.
#
import re
from abc import ABC
from typing import Pattern

import libcst
from libcst.codemod import CodemodContext, VisitorBasedCodemodCommand
from libcst.helpers import insert_header_comments


class AddPyreDirectiveCommand(VisitorBasedCodemodCommand, ABC):
    PYRE_TAG: str

    def __init__(self, context: CodemodContext) -> None:
        super().__init__(context)
        self._regex_pattern: Pattern[str] = re.compile(
            rf"^#\s+pyre-{self.PYRE_TAG}\s*$"
        )
        self.needs_add = True

    def visit_Comment(self, node: libcst.Comment) -> None:
        if self._regex_pattern.search(node.value):
            self.needs_add = False

    def leave_Module(
        self, original_node: libcst.Module, updated_node: libcst.Module
    ) -> libcst.Module:
        # If the tag already exists, don't modify the file.
        if not self.needs_add:
            return updated_node

        return insert_header_comments(updated_node, [f"# pyre-{self.PYRE_TAG}"])


class AddPyreStrictCommand(AddPyreDirectiveCommand):
    """
    Given a source file, we'll add the strict tag if the file doesn't already
    contain it.
    """

    PYRE_TAG: str = "strict"

    DESCRIPTION: str = "Add the 'pyre-strict' tag to a module."


class AddPyreUnsafeCommand(AddPyreDirectiveCommand):
    """
    Given a source file, we'll add the unsafe tag if the file doesn't already
    contain it.
    """

    PYRE_TAG: str = "unsafe"

    DESCRIPTION: str = "Add the 'pyre-unsafe' tag to a module."