-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathapi.py
More file actions
58 lines (47 loc) · 1.63 KB
/
api.py
File metadata and controls
58 lines (47 loc) · 1.63 KB
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
from abc import ABCMeta
from typing import Callable
import libcst as cst
from codemodder.codemods.base_codemod import ( # noqa: F401
BaseCodemod,
FindAndFixCodemod,
Metadata,
Reference,
RemediationCodemod,
ReviewGuidance,
ToolMetadata,
ToolRule,
)
from codemodder.codemods.libcst_transformer import (
LibcstResultTransformer,
LibcstTransformerPipeline,
)
from codemodder.codemods.semgrep import SemgrepRuleDetector
from codemodder.file_context import FileContext # noqa: F401
class SimpleCodemod(LibcstResultTransformer, metaclass=ABCMeta):
"""
Base class for codemods with a single detector and transformer
Child classes must implement the following attributes:
- metadata: Metadata
- codemod_base: type[BaseCodemod]
"""
metadata: Metadata
detector_pattern: str
on_result_found: Callable[[cst.CSTNode, cst.CSTNode], cst.CSTNode]
codemod_base: type[BaseCodemod]
def __init__(self, *args, **kwargs):
"""Obfuscates the type of the constructor to make the type checker happy"""
super().__init__(*args, **kwargs)
def __new__(cls, *args, **kwargs):
del args
if kwargs.get("_transformer", False):
return super().__new__(cls)
return cls.codemod_base(
metadata=cls.metadata,
detector=(
SemgrepRuleDetector(cls.detector_pattern)
if getattr(cls, "detector_pattern", None)
else None
),
# This allows the transformer to inherit all the methods of the class itself
transformer=LibcstTransformerPipeline(cls),
)