@@ -100,81 +100,61 @@ def iter_field_matches(node: Any) -> Iterator[tuple[str, Any, Any]]:
100100 yield key , match .value , parent
101101
102102
103- def parse_rules_file (content : str , data : Any ) -> list [RuleInfo ]:
104- """Parse a standard rules file with name/tag entries"""
103+ def _extract_item_fields (item : Any , is_unicode : bool ) -> tuple [str , str | None , str | None , Any ] | None :
104+ if is_unicode :
105+ if isinstance (item , dict ) and len (item ) == 1 :
106+ char_key = str (next (iter (item .keys ())))
107+ return char_key , None , None , item [char_key ]
108+ else :
109+ if isinstance (item , dict ) and "name" in item :
110+ rule_name = str (item .get ("name" ))
111+ tag = format_tag (item .get ("tag" ))
112+ return f"{ rule_name } |{ tag or 'unknown' } " , rule_name , tag , item
113+ return None
114+
115+
116+ def _build_rule_items (content : str , data : Any , is_unicode_file : bool ) -> list [RuleInfo ]:
105117 if not isinstance (data , list ):
106118 return []
107119
108- rules : list [RuleInfo ] = []
109120 lines = content .splitlines ()
110-
111121 start_lines : list [int ] = []
112- rule_items : list [Any ] = []
122+ extracted : list [tuple [str , str | None , str | None , Any ]] = []
123+
113124 for idx , item in enumerate (data ):
114- if isinstance (item , dict ) and "name" in item :
125+ fields = _extract_item_fields (item , is_unicode_file )
126+ if fields is not None :
115127 line = data .lc .item (idx )[0 ] if hasattr (data , "lc" ) else 0
116128 start_lines .append (line )
117- rule_items .append (item )
118-
129+ extracted .append (fields )
119130 raw_blocks = build_raw_blocks (lines , start_lines )
120131
121- for item , raw_content , line_idx in zip (rule_items , raw_blocks , start_lines ):
122- rule_name = str (item .get ("name" ))
123- tag = format_tag (item .get ("tag" ))
124- rule_key = f"{ rule_name } |{ tag or 'unknown' } "
132+ rules : list [RuleInfo ] = []
133+ for (key , name , tag , item_data ), raw_content , line_idx in zip (extracted , raw_blocks , start_lines ):
125134 rules .append (
126135 RuleInfo (
127- name = rule_name ,
136+ name = name ,
128137 tag = tag ,
129- key = rule_key ,
138+ key = key ,
130139 line_number = line_idx + 1 ,
131140 raw_content = raw_content ,
132- data = item ,
133- untranslated_entries = find_untranslated_text_entries (item ),
134- line_map = build_line_map (item ),
141+ data = item_data ,
142+ untranslated_entries = find_untranslated_text_entries (item_data ),
143+ line_map = build_line_map (item_data ),
135144 audit_ignore = has_audit_ignore (raw_content ),
136145 )
137146 )
138-
139147 return rules
140148
141149
142- def parse_unicode_file (content : str , data : Any ) -> list [RuleInfo ]:
143- """Parse a unicode file with character/range keys"""
144- if not isinstance (data , list ):
145- return []
146-
147- rules : list [RuleInfo ] = []
148- lines = content .splitlines ()
149-
150- start_lines : list [int ] = []
151- entries : list [tuple [str , Any ]] = []
152- for idx , item in enumerate (data ):
153- if isinstance (item , dict ) and len (item ) == 1 :
154- key = next (iter (item .keys ()))
155- value = item [key ]
156- line = data .lc .item (idx )[0 ] if hasattr (data , "lc" ) else 0
157- start_lines .append (line )
158- entries .append ((str (key ), value ))
150+ def parse_rules_file (content : str , data : Any ) -> list [RuleInfo ]:
151+ """Parse a standard rules file with name/tag entries."""
152+ return _build_rule_items (content , data , is_unicode_file = False )
159153
160- raw_blocks = build_raw_blocks (lines , start_lines )
161154
162- for (char_key , value ), raw_content , line_idx in zip (entries , raw_blocks , start_lines ):
163- rules .append (
164- RuleInfo (
165- name = None ,
166- tag = None ,
167- key = char_key ,
168- line_number = line_idx + 1 ,
169- raw_content = raw_content ,
170- data = value ,
171- untranslated_entries = find_untranslated_text_entries (value ),
172- line_map = build_line_map (value ),
173- audit_ignore = has_audit_ignore (raw_content ),
174- )
175- )
176-
177- return rules
155+ def parse_unicode_file (content : str , data : Any ) -> list [RuleInfo ]:
156+ """Parse a unicode file with character/range keys."""
157+ return _build_rule_items (content , data , is_unicode_file = True )
178158
179159
180160def has_audit_ignore (content : str ) -> bool :
0 commit comments