mirror of
https://github.com/Instagram/LibCST.git
synced 2025-12-23 10:35:53 +00:00
Add metadata access and dependency logic in `CSTVisitor` and `Module.visit` to generate all metadata dependencies before performing a visit pass over a tree and validate access to metadata.
31 lines
894 B
Python
31 lines
894 B
Python
# Copyright (c) Facebook, Inc. and its affiliates.
|
|
#
|
|
# This source code is licensed under the MIT license found in the
|
|
# LICENSE file in the root directory of this source tree.
|
|
|
|
# pyre-strict
|
|
from typing import Generic, TypeVar
|
|
|
|
import libcst.nodes as cst
|
|
from libcst._base_visitor import CSTVisitor
|
|
|
|
|
|
_T_co = TypeVar("_T_co", covariant=True)
|
|
|
|
|
|
class BaseMetadataProvider(CSTVisitor, Generic[_T_co]):
|
|
"""
|
|
Base class for visitor-based metadata providers.
|
|
"""
|
|
|
|
def run(self, module: cst.Module) -> None:
|
|
"""
|
|
Convenience method to run metadata provider over a module.
|
|
"""
|
|
module.visit(self)
|
|
|
|
@classmethod
|
|
# pyre-ignore[35]: Parameter type cannot be covariant. Pyre can't
|
|
# detect that this method is not mutating the Provider class.
|
|
def set_metadata(cls, node: cst.CSTNode, value: _T_co) -> None:
|
|
node._metadata[cls] = value
|