MCPcopy Create free account
hub / github.com/astral-sh/ruff / write_node

Function write_node

crates/ruff_python_ast/generate.py:1070–1114  ·  view source on GitHub ↗
(out: list[str], ast: Ast)

Source from the content-addressed store, hash-verified

1068
1069
1070def write_node(out: list[str], ast: Ast) -> None:
1071 group_names = [group.name for group in ast.groups]
1072 for group in ast.groups:
1073 for node in group.nodes:
1074 if node.fields is None:
1075 continue
1076 if node.doc is not None:
1077 write_rustdoc(out, node.doc)
1078 out.append(
1079 "#[derive(Clone"
1080 + ("" if node.custom_debug else ", Debug")
1081 + ", PartialEq"
1082 + "".join(f", {derive}" for derive in node.derives)
1083 + ")]"
1084 )
1085 out.append('#[cfg_attr(feature = "get-size", derive(get_size2::GetSize))]')
1086 name = node.name
1087 out.append(f"pub struct {name} {{")
1088 out.append("pub node_index: crate::AtomicNodeIndex,")
1089 if not node.custom_range:
1090 out.append("pub range: ruff_text_size::TextRange,")
1091 for field in node.fields:
1092 field_str = f"pub {field.name}: "
1093 ty = field.parsed_ty
1094
1095 rust_ty = f"{field.parsed_ty.name}"
1096 if ty.name in types_requiring_crate_prefix:
1097 rust_ty = f"crate::{rust_ty}"
1098
1099 if ty.sequence_kind is SequenceKind.VEC:
1100 rust_ty = f"Vec<{rust_ty}>"
1101 elif ty.sequence_kind is SequenceKind.BOXED_SLICE:
1102 rust_ty = f"Box<[{rust_ty}]>"
1103 elif ty.sequence_kind is SequenceKind.THIN_VEC:
1104 rust_ty = f"thin_vec::ThinVec<{rust_ty}>"
1105 else:
1106 if ty.name in group_names:
1107 rust_ty = f"Box<{rust_ty}>"
1108 if ty.optional:
1109 rust_ty = f"Option<{rust_ty}>"
1110
1111 field_str += rust_ty + ","
1112 out.append(field_str)
1113 out.append("}")
1114 out.append("")
1115
1116
1117# ------------------------------------------------------------------------------

Callers 1

generateFunction · 0.85

Calls 3

write_rustdocFunction · 0.85
appendMethod · 0.45
joinMethod · 0.45

Tested by

no test coverage detected