From a2e362b3d7c53ad7ccfbc317226e89f493968c7b Mon Sep 17 00:00:00 2001 From: Nfsaavedra Date: Wed, 24 Apr 2024 13:44:09 +0100 Subject: [PATCH] Fix block as_dict. Fix pyright problem --- glitch/parsers/puppet.py | 5 ++++- glitch/repr/inter.py | 21 +++++++++++++++++++-- 2 files changed, 23 insertions(+), 3 deletions(-) diff --git a/glitch/parsers/puppet.py b/glitch/parsers/puppet.py index 61ed76c..dd8d18d 100644 --- a/glitch/parsers/puppet.py +++ b/glitch/parsers/puppet.py @@ -309,7 +309,10 @@ def process_hash_value( args = [] for arg in codeelement.parameters: attr = PuppetParser.__process_codeelement(arg, path, code) - args.append(Variable(attr.name, "", True)) + variable = Variable(attr.name, "", True) + variable.line = arg.line + variable.column = arg.col + args.append(Variable(variable)) return ( list( map( diff --git a/glitch/repr/inter.py b/glitch/repr/inter.py index 111ee48..fa487d7 100644 --- a/glitch/repr/inter.py +++ b/glitch/repr/inter.py @@ -1,7 +1,6 @@ from abc import ABC from enum import Enum from typing import List, Union, Dict, Any -from types import NoneType class CodeElement(ABC): @@ -38,11 +37,29 @@ def __init__(self) -> None: def add_statement(self, statement: "ConditionalStatement") -> None: self.statements.append(statement) + @staticmethod + def __as_dict_statement( + stat: Dict[str, Any] | List[Any] | CodeElement | str + ) -> Any: + if isinstance(stat, CodeElement): + return stat.as_dict() + elif isinstance(stat, dict): + for key, value in stat.items(): + stat[key] = Block.__as_dict_statement(value) + return stat + elif isinstance(stat, list): + return [Block.__as_dict_statement(s) for s in stat] + else: + return stat + def as_dict(self) -> Dict[str, Any]: + print(self.statements) + for statement in self.statements: + print(type(statement)) return { **super().as_dict(), "statements": [ - s.as_dict() if not isinstance(s, str) else s for s in self.statements + Block.__as_dict_statement(s) for s in self.statements # type: ignore ], }