def leave_SimpleString( self, original_node: cst.SimpleString, updated_node: cst.SimpleString ) -> cst.SimpleString: # For prettiness, convert all single-quoted forward refs to double-quoted. if updated_node.value.startswith("'") and updated_node.value.endswith("'"): return updated_node.with_changes(value=f'"{updated_node.value[1:-1]}"') return updated_node
def update_version(self, original_node: cst.SimpleString, updated_node: cst.SimpleString) -> cst.SimpleString: if self.new_version: raise Exception("Multiple versions found.") old_version = Version(updated_node.evaluated_value) self.new_version = self.version_mod(old_version) return updated_node.with_changes(value=f'"{self.new_version}"')
def visit_SimpleString(self, node: cst.SimpleString) -> None: matched = re.match(r"^(\'|\")(?P<igcode>IG\d+ )\S", node.value) if matched is not None: replacement_string = node.value.replace(matched.group("igcode"), "", 1) self.report( node, self.MESSAGE, replacement=node.with_changes(value=replacement_string), )
def leave_SimpleString(self, original_node: cst.SimpleString, updated_node: cst.SimpleString) -> cst.SimpleString: # For prettiness, convert all single-quoted forward refs to double-quoted. if "'" in updated_node.quote: new_value = f'"{updated_node.value[1:-1]}"' try: if updated_node.evaluated_value == ast.literal_eval(new_value): return updated_node.with_changes(value=new_value) except SyntaxError: pass return updated_node
def leave_SimpleString(self, original_node: cst.SimpleString, updated_node: cst.SimpleString) -> cst.SimpleString: if self.avoid_quote in updated_node.quote: # Attempt to swap the value out, verify that the string is still identical # before and after transformation. new_quote = updated_node.quote.replace(self.avoid_quote, self.replace_quote) new_value = ( f"{updated_node.prefix}{new_quote}{updated_node.raw_value}{new_quote}" ) try: new_str = ast.literal_eval(new_value) if updated_node.evaluated_value != new_str: # This isn't the same! return updated_node return updated_node.with_changes(value=new_value) except Exception: # Failed to parse string, changing the quoting screwed us up. pass # Either failed to parse the new string, or don't need to make changes. return updated_node
def leave_SimpleString( self, original_node: cst.SimpleString, updated_node: cst.SimpleString) -> cst.SimpleString: return updated_node.with_changes( value= f'"{"".join(reversed(literal_eval(updated_node.value)))}"')
def leave_string2( self, original_node: cst.SimpleString, updated_node: cst.SimpleString) -> cst.SimpleString: return updated_node.with_changes( value=f'"{literal_eval(updated_node.value)}suffix"')
def leave_SimpleString(self, original_node: cst.SimpleString, updated_node: cst.SimpleString): return updated_node.with_changes( value="\"[string]\"" ) if updated_node.value != '"""[docstring]"""' else updated_node