-
Notifications
You must be signed in to change notification settings - Fork 192
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add extended example for metadata and fix some docstrings
- Loading branch information
Ray Zeng
committed
Aug 15, 2019
1 parent
b248b1a
commit 796f864
Showing
6 changed files
with
123 additions
and
135 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
{ | ||
"cells": [ | ||
{ | ||
"cell_type": "raw", | ||
"metadata": { | ||
"raw_mimetype": "text/restructuredtext" | ||
}, | ||
"source": [ | ||
"=====================\n", | ||
"Working with Metadata\n", | ||
"=====================\n", | ||
"LibCST handles node metadata in a somewhat unusal manner in order to maintain the immutability of the tree. See :doc:`Metadata <metadata>` for the complete documentation. Here's an example of a provider that lablels marks nodes as function parameters that is used by a visitor that prints all names that are function parameters." | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"import libcst as cst\n", | ||
"\n", | ||
"\n", | ||
"class IsParamProvider(cst.BatchableMetadataProvider[bool]):\n", | ||
" \"\"\"\n", | ||
" Marks Name nodes found as a parameter to a function.\n", | ||
" \"\"\"\n", | ||
" def __init__(self):\n", | ||
" super().__init__()\n", | ||
" self.is_param = False\n", | ||
" \n", | ||
" def visit_Param(self, node: cst.Param) -> None:\n", | ||
" # Mark the child Name node as a parameter \n", | ||
" self.set_metadata(node.name, True)\n", | ||
" \n", | ||
" def visit_Name(self, node: cst.Name) -> None:\n", | ||
" # Mark all other Name nodes as not parameters\n", | ||
" if not self.get_metadata(type(self), node, False):\n", | ||
" self.set_metadata(node, False)\n", | ||
"\n", | ||
"\n", | ||
"class ParamPrinter(cst.CSTVisitor):\n", | ||
" METADATA_DEPENDENCIES = (IsParamProvider, cst.SyntacticPositionProvider,)\n", | ||
"\n", | ||
" def visit_Name(self, node: cst.Name) -> None:\n", | ||
" # Only print out names that are parameters\n", | ||
" if self.get_metadata(IsParamProvider, node):\n", | ||
" pos = self.get_metadata(cst.SyntacticPositionProvider, node).start\n", | ||
" print(f\"{node.value} found at line {pos.line}, column {pos.column}\")\n", | ||
"\n", | ||
"\n", | ||
"wrapper = cst.MetadataWrapper(cst.parse_module(\"def foo(x):\\n y = 1\\n return x + y\"))\n", | ||
"result = wrapper.visit(ParamPrinter())" | ||
] | ||
} | ||
], | ||
"metadata": { | ||
"celltoolbar": "Raw Cell Format", | ||
"kernelspec": { | ||
"display_name": "Python 3", | ||
"language": "python", | ||
"name": "python3" | ||
}, | ||
"language_info": { | ||
"codemirror_mode": { | ||
"name": "ipython", | ||
"version": 3 | ||
}, | ||
"file_extension": ".py", | ||
"mimetype": "text/x-python", | ||
"name": "python", | ||
"nbconvert_exporter": "python", | ||
"pygments_lexer": "ipython3", | ||
"version": "3.6.2+" | ||
} | ||
}, | ||
"nbformat": 4, | ||
"nbformat_minor": 2 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters