LibCST/libcst/metadata/base_provider.py
Ray Zeng dbbe3d1927 Add metadata dependencies to CSTVisitor and add metadata runner
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.
2019-07-22 19:53:49 -07:00

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