LibCST/libcst/metadata/parent_node_provider.py
2019-09-26 17:39:16 -07:00

27 lines
891 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 Optional
import libcst as cst
from libcst.metadata.base_provider import BatchableMetadataProvider
class ParentNodeVisitor(cst.CSTVisitor):
def __init__(self, provider: "ParentNodeProvider") -> None:
self.provider: ParentNodeProvider = provider
super().__init__()
def on_leave(self, original_node: cst.CSTNode) -> None:
for child in original_node.children:
self.provider.set_metadata(child, original_node)
super().on_leave(original_node)
class ParentNodeProvider(BatchableMetadataProvider[Optional[cst.CSTNode]]):
def visit_Module(self, node: cst.Module) -> Optional[bool]:
node.visit(ParentNodeVisitor(self))