Coverage for /pythoncovmergedfiles/medio/medio/usr/local/lib/python3.8/site-packages/libcst/codemod/_context.py: 95%
19 statements
« prev ^ index » next coverage.py v7.3.1, created at 2023-09-25 06:43 +0000
« prev ^ index » next coverage.py v7.3.1, created at 2023-09-25 06:43 +0000
1# Copyright (c) Meta Platforms, Inc. and affiliates.
2#
3# This source code is licensed under the MIT license found in the
4# LICENSE file in the root directory of this source tree.
5#
6from dataclasses import dataclass, field
7from typing import Any, Dict, List, Optional
9import libcst as cst
10import libcst.metadata as meta
13@dataclass(frozen=True)
14class CodemodContext:
15 """
16 A context holding all information that is shared amongst all transforms
17 and visitors in a single codemod invocation. When chaining multiple
18 transforms together, the context holds the state that needs to be passed
19 between transforms. The context is responsible for keeping track of
20 metadata wrappers and the filename of the file that is being modified
21 (if available).
22 """
24 #: List of warnings gathered while running a codemod. Add to this list
25 #: by calling :meth:`~libcst.codemod.Codemod.warn` method from a class
26 #: that subclasses from :class:`~libcst.codemod.Codemod`,
27 #: :class:`~libcst.codemod.ContextAwareTransformer` or
28 #: :class:`~libcst.codemod.ContextAwareVisitor`.
29 warnings: List[str] = field(default_factory=list)
31 #: Scratch dictionary available for codemods which are spread across multiple
32 #: transforms. Codemods are free to add to this at will.
33 scratch: Dict[str, Any] = field(default_factory=dict)
35 #: The current filename if a codemod is being executed against a file that
36 #: lives on disk. Populated by
37 #: :func:`libcst.codemod.parallel_exec_transform_with_prettyprint` when
38 #: running codemods from the command line.
39 filename: Optional[str] = None
41 #: The current module if a codemod is being executed against a file that
42 #: lives on disk, and the repository root is correctly configured. This
43 #: Will take the form of a dotted name such as ``foo.bar.baz`` for a file
44 #: in the repo named ``foo/bar/baz.py``.
45 full_module_name: Optional[str] = None
47 #: The current package if a codemod is being executed against a file that
48 #: lives on disk, and the repository root is correctly configured. This
49 #: Will take the form of a dotted name such as ``foo.bar`` for a file
50 #: in the repo named ``foo/bar/baz.py``
51 full_package_name: Optional[str] = None
53 #: The current top level metadata wrapper for the module being modified.
54 #: To access computed metadata when inside an actively running codemod, use
55 #: the :meth:`~libcst.MetadataDependent.get_metadata` method on
56 #: :class:`~libcst.codemod.Codemod`.
57 wrapper: Optional[cst.MetadataWrapper] = None
59 #: The current repo-level metadata manager for the active codemod.
60 metadata_manager: Optional[meta.FullRepoManager] = None
62 @property
63 def module(self) -> Optional[cst.Module]:
64 """
65 The current top level module being modified. As a convenience, you can
66 use the :attr:`~libcst.codemod.Codemod.module` property on
67 :class:`~libcst.codemod.Codemod` to refer to this when inside an actively
68 running codemod.
69 """
71 wrapper = self.wrapper
72 if wrapper is None:
73 return None
74 return wrapper.module