File: _decorators.py

package info (click to toggle)
python-libcst 1.4.0-1.2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 5,928 kB
  • sloc: python: 76,235; makefile: 10; sh: 2
file content (120 lines) | stat: -rw-r--r-- 5,130 bytes parent folder | download
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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
# 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.

from typing import Callable, TypeVar

from libcst.matchers._matcher_base import BaseMatcherNode

_CSTVisitFuncT = TypeVar("_CSTVisitFuncT")


VISIT_POSITIVE_MATCHER_ATTR: str = "_call_if_inside_matcher"
VISIT_NEGATIVE_MATCHER_ATTR: str = "_call_if_not_inside_matcher"
CONSTRUCTED_VISIT_MATCHER_ATTR: str = "_visit_matcher"
CONSTRUCTED_LEAVE_MATCHER_ATTR: str = "_leave_matcher"


def call_if_inside(
    matcher: BaseMatcherNode,
    # pyre-fixme[34]: `Variable[_CSTVisitFuncT]` isn't present in the function's parameters.
) -> Callable[[_CSTVisitFuncT], _CSTVisitFuncT]:
    """
    A decorator for visit and leave methods inside a :class:`MatcherDecoratableTransformer`
    or a :class:`MatcherDecoratableVisitor`. A method that is decorated with this decorator
    will only be called if it or one of its parents matches the supplied matcher.
    Use this to selectively gate visit and leave methods to be called only when
    inside of another relevant node. Note that this works for both node and attribute
    methods, so you can decorate a ``visit_<Node>`` or a ``visit_<Node>_<Attr>`` method.
    """

    def inner(original: _CSTVisitFuncT) -> _CSTVisitFuncT:
        setattr(
            original,
            VISIT_POSITIVE_MATCHER_ATTR,
            [*getattr(original, VISIT_POSITIVE_MATCHER_ATTR, []), matcher],
        )
        return original

    return inner


def call_if_not_inside(
    matcher: BaseMatcherNode,
    # pyre-fixme[34]: `Variable[_CSTVisitFuncT]` isn't present in the function's parameters.
) -> Callable[[_CSTVisitFuncT], _CSTVisitFuncT]:
    """
    A decorator for visit and leave methods inside a :class:`MatcherDecoratableTransformer`
    or a :class:`MatcherDecoratableVisitor`. A method that is decorated with this decorator
    will only be called if it or one of its parents does not match the supplied
    matcher. Use this to selectively gate visit and leave methods to be called only
    when outside of another relevant node. Note that this works for both node and
    attribute methods, so you can decorate a ``visit_<Node>`` or a ``visit_<Node>_<Attr>``
    method.
    """

    def inner(original: _CSTVisitFuncT) -> _CSTVisitFuncT:
        setattr(
            original,
            VISIT_NEGATIVE_MATCHER_ATTR,
            [*getattr(original, VISIT_NEGATIVE_MATCHER_ATTR, []), matcher],
        )
        return original

    return inner


# pyre-fixme[34]: `Variable[_CSTVisitFuncT]` isn't present in the function's parameters.
def visit(matcher: BaseMatcherNode) -> Callable[[_CSTVisitFuncT], _CSTVisitFuncT]:
    """
    A decorator that allows a method inside a :class:`MatcherDecoratableTransformer`
    or a :class:`MatcherDecoratableVisitor` visitor to be called when visiting a node
    that matches the provided matcher. Note that you can use this in combination with
    :func:`call_if_inside` and :func:`call_if_not_inside` decorators. Unlike explicit
    ``visit_<Node>`` and ``leave_<Node>`` methods, functions decorated with this
    decorator cannot stop child traversal by returning ``False``. Decorated visit
    functions should always have a return annotation of ``None``.

    There is no restriction on the number of visit decorators allowed on a method.
    There is also no restriction on the number of methods that may be decorated
    with the same matcher. When multiple visit decorators are found on the same
    method, they act as a simple or, and the method will be called when any one
    of the contained matches is ``True``.
    """

    def inner(original: _CSTVisitFuncT) -> _CSTVisitFuncT:
        setattr(
            original,
            CONSTRUCTED_VISIT_MATCHER_ATTR,
            [*getattr(original, CONSTRUCTED_VISIT_MATCHER_ATTR, []), matcher],
        )
        return original

    return inner


# pyre-fixme[34]: `Variable[_CSTVisitFuncT]` isn't present in the function's parameters.
def leave(matcher: BaseMatcherNode) -> Callable[[_CSTVisitFuncT], _CSTVisitFuncT]:
    """
    A decorator that allows a method inside a :class:`MatcherDecoratableTransformer`
    or a :class:`MatcherDecoratableVisitor` visitor to be called when leaving a node
    that matches the provided matcher. Note that you can use this in combination
    with :func:`call_if_inside` and :func:`call_if_not_inside` decorators.

    There is no restriction on the number of leave decorators allowed on a method.
    There is also no restriction on the number of methods that may be decorated
    with the same matcher. When multiple leave decorators are found on the same
    method, they act as a simple or, and the method will be called when any one
    of the contained matches is ``True``.
    """

    def inner(original: _CSTVisitFuncT) -> _CSTVisitFuncT:
        setattr(
            original,
            CONSTRUCTED_LEAVE_MATCHER_ATTR,
            [*getattr(original, CONSTRUCTED_LEAVE_MATCHER_ATTR, []), matcher],
        )
        return original

    return inner