From 163901b5b1d92c24550b4bcd0aa0e99c8f24f044 Mon Sep 17 00:00:00 2001 From: Knut Wannheden Date: Mon, 12 Aug 2024 11:02:49 +0200 Subject: [PATCH] Add `source` field to `ParserVisitor` --- .../src/rewrite/python/parser/parser_visitor.py | 8 ++++++++ rewrite/rewrite-python/tests/parser_test.py | 2 +- 2 files changed, 9 insertions(+), 1 deletion(-) diff --git a/rewrite/rewrite-python/src/rewrite/python/parser/parser_visitor.py b/rewrite/rewrite-python/src/rewrite/python/parser/parser_visitor.py index dc39ea34..120ab1c9 100644 --- a/rewrite/rewrite-python/src/rewrite/python/parser/parser_visitor.py +++ b/rewrite/rewrite-python/src/rewrite/python/parser/parser_visitor.py @@ -1,4 +1,5 @@ import ast +from dataclasses import dataclass from pathlib import Path import rewrite.java.tree as J @@ -13,6 +14,13 @@ def map_type(node): class ParserVisitor(ast.NodeVisitor): + _source: str + _cursor: int = 0 + + def __init__(self, source: str): + super().__init__() + self._source = source + def visit_Module(self, node: ast.Module) -> Py.CompilationUnit: return Py.CompilationUnit( random_id(), diff --git a/rewrite/rewrite-python/tests/parser_test.py b/rewrite/rewrite-python/tests/parser_test.py index a749a14d..f4bf120d 100644 --- a/rewrite/rewrite-python/tests/parser_test.py +++ b/rewrite/rewrite-python/tests/parser_test.py @@ -15,5 +15,5 @@ def bar(x): tree = ast.parse(source) # Create the visitor and visit the AST - visitor = ParserVisitor() + visitor = ParserVisitor(source) visitor.visit(tree)