phase3: nullfix field-formal field-nullable handler. 907 -> 872
This commit is contained in:
@@ -101,6 +101,17 @@ def insert_bang(line, col0, name, kind):
|
||||
return None
|
||||
return None
|
||||
|
||||
def make_field_nullable(lines, name):
|
||||
"""找字段声明 'Type name;' 并在类型后加 '?'。返回 (行索引, 新行) 或 (None,None)。"""
|
||||
pat = re.compile(r'^(\s+(?:final\s+|const\s+)?(?:static\s+)?)(.+)\s+'+re.escape(name)+r';\s*$')
|
||||
for i,L in enumerate(lines):
|
||||
m=pat.match(L)
|
||||
if not m: continue
|
||||
mod, typ = m.group(1), m.group(2).rstrip()
|
||||
if '?' in typ or '=' in L: continue
|
||||
return i, f"{mod}{typ}? {name};"
|
||||
return None, None
|
||||
|
||||
def insert_late(line):
|
||||
stripped=line.strip()
|
||||
if not stripped or stripped.startswith('late '): return None
|
||||
@@ -147,7 +158,16 @@ def apply_round(errs):
|
||||
if cls: new=insert_bang(old, col-1, cls[0], cls[1])
|
||||
elif rule=='missing_default_value_for_parameter':
|
||||
nm=NAME.search(msg)
|
||||
if nm: new=insert_nullable_before_name(old, nm.group(1), col-1)
|
||||
if nm:
|
||||
pname=nm.group(1)
|
||||
# field-formal this.x ? 则把字段改可空(跨行)
|
||||
if ('this.'+pname) in old:
|
||||
fl=get(path)
|
||||
fi, fnew = make_field_nullable(fl, pname)
|
||||
if fi is not None and fl[fi]!=fnew:
|
||||
fl[fi]=fnew; changed+=1
|
||||
else:
|
||||
new=insert_nullable_before_name(old, pname, col-1)
|
||||
elif rule in ('not_initialized_non_nullable_instance_field','not_initialized_non_nullable_variable'):
|
||||
new=insert_late(old)
|
||||
if new and new!=old:
|
||||
|
||||
Reference in New Issue
Block a user