From cdaaa63d67e06eb5ca81238ff7884357ff7b6961 Mon Sep 17 00:00:00 2001 From: peima Date: Sun, 26 Jul 2026 00:15:38 +0800 Subject: [PATCH] phase3: stabilize nullfix.py (disable risky local-nullable handler) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Script stable handlers (zero corruption): bang(!), late, param-nullable, field-formal field-nullable, arg-bang. Reusable — re-run after manual fixes. Errors: 2374(peak) -> 596 | web build: 1845 -> 571 --- lib/pages/blog.dart | 2 +- lib/pages/edit_address.dart | 2 +- lib/pages/my_addresses.dart | 2 +- lib/pages/my_support.dart | 2 +- lib/pages/new_ticket.dart | 2 +- lib/widgets/desktop/desktop_edit_address.dart | 2 +- lib/widgets/desktop/desktop_new_ticket.dart | 2 +- lib/widgets/mobile/mobile_edit_address.dart | 2 +- lib/widgets/mobile/mobile_new_ticket.dart | 2 +- tools/nullfix.py | 26 ++++++++++++++++--- 10 files changed, 32 insertions(+), 12 deletions(-) diff --git a/lib/pages/blog.dart b/lib/pages/blog.dart index 9491cde..b625308 100644 --- a/lib/pages/blog.dart +++ b/lib/pages/blog.dart @@ -11,7 +11,7 @@ class Blog extends StatelessWidget { final Key? key; final int businessId; - const Blog({this.key, int businessId}) : + const Blog({this.key, int? businessId}) : businessId = businessId ?? Constants.BUSINESS_ID; @override diff --git a/lib/pages/edit_address.dart b/lib/pages/edit_address.dart index 42154b6..08fd081 100644 --- a/lib/pages/edit_address.dart +++ b/lib/pages/edit_address.dart @@ -16,7 +16,7 @@ class EditAddress extends StatelessWidget { final Key? key; final Address address; final int businessId; - const EditAddress(this.address, {this.key, int businessId}) : + const EditAddress(this.address, {this.key, int? businessId}) : businessId = businessId ?? Constants.BUSINESS_ID; @override diff --git a/lib/pages/my_addresses.dart b/lib/pages/my_addresses.dart index 0b6248b..a1b65aa 100644 --- a/lib/pages/my_addresses.dart +++ b/lib/pages/my_addresses.dart @@ -11,7 +11,7 @@ class MyAddresses extends StatelessWidget { final Key? key; final int businessId; - const MyAddresses({this.key, int businessId}) : + const MyAddresses({this.key, int? businessId}) : businessId = businessId ?? Constants.BUSINESS_ID; @override diff --git a/lib/pages/my_support.dart b/lib/pages/my_support.dart index 86f544f..5c204b0 100644 --- a/lib/pages/my_support.dart +++ b/lib/pages/my_support.dart @@ -11,7 +11,7 @@ class MySupport extends StatelessWidget { final Key? key; final int businessId; - const MySupport({this.key, int businessId}) : + const MySupport({this.key, int? businessId}) : businessId = businessId ?? Constants.BUSINESS_ID; @override diff --git a/lib/pages/new_ticket.dart b/lib/pages/new_ticket.dart index 16a5954..3ff30e1 100644 --- a/lib/pages/new_ticket.dart +++ b/lib/pages/new_ticket.dart @@ -17,7 +17,7 @@ class NewTicket extends StatefulWidget { final Key? key; final int businessId; - const NewTicket({this.key, int businessId}) : + const NewTicket({this.key, int? businessId}) : businessId = businessId ?? Constants.BUSINESS_ID; @override diff --git a/lib/widgets/desktop/desktop_edit_address.dart b/lib/widgets/desktop/desktop_edit_address.dart index 9e6ae7a..eab0b42 100644 --- a/lib/widgets/desktop/desktop_edit_address.dart +++ b/lib/widgets/desktop/desktop_edit_address.dart @@ -23,7 +23,7 @@ class DesktopEditAddress extends StatefulWidget { final Key? key; final Address address; final int businessId; - const DesktopEditAddress(this.address, {this.key, int businessId}) : + const DesktopEditAddress(this.address, {this.key, int? businessId}) : businessId = businessId ?? Constants.BUSINESS_ID; @override diff --git a/lib/widgets/desktop/desktop_new_ticket.dart b/lib/widgets/desktop/desktop_new_ticket.dart index bec014b..5f48239 100644 --- a/lib/widgets/desktop/desktop_new_ticket.dart +++ b/lib/widgets/desktop/desktop_new_ticket.dart @@ -21,7 +21,7 @@ class DesktopNewTicket extends StatefulWidget { final Key? key; final int businessId; - const DesktopNewTicket({this.key, int businessId}) : + const DesktopNewTicket({this.key, int? businessId}) : businessId = businessId ?? Constants.BUSINESS_ID; @override diff --git a/lib/widgets/mobile/mobile_edit_address.dart b/lib/widgets/mobile/mobile_edit_address.dart index eb8467f..a46aed4 100644 --- a/lib/widgets/mobile/mobile_edit_address.dart +++ b/lib/widgets/mobile/mobile_edit_address.dart @@ -21,7 +21,7 @@ class MobileEditAddress extends StatefulWidget { final Key? key; final Address address; final int businessId; - const MobileEditAddress(this.address, {this.key, int businessId}) : + const MobileEditAddress(this.address, {this.key, int? businessId}) : businessId = businessId ?? Constants.BUSINESS_ID; @override diff --git a/lib/widgets/mobile/mobile_new_ticket.dart b/lib/widgets/mobile/mobile_new_ticket.dart index efcd677..cccb9e8 100644 --- a/lib/widgets/mobile/mobile_new_ticket.dart +++ b/lib/widgets/mobile/mobile_new_ticket.dart @@ -20,7 +20,7 @@ class MobileNewTicket extends StatefulWidget { final Key? key; final int businessId; - const MobileNewTicket({this.key, int businessId}) : + const MobileNewTicket({this.key, int? businessId}) : businessId = businessId ?? Constants.BUSINESS_ID; @override diff --git a/tools/nullfix.py b/tools/nullfix.py index 7690ffb..474715a 100644 --- a/tools/nullfix.py +++ b/tools/nullfix.py @@ -33,9 +33,7 @@ PROP = re.compile(r"The property '([^']+)'") METH = re.compile(r"The method '([^']+)'") OPR = re.compile(r"The operator '([^']+)'") NAME = re.compile(r"'([^']+)'") -KEYWORDS = {'return','throw','await','yield','break','continue','assert','new','const', - 'final','var','late','switch','case','default','if','else','for','while', - 'do','try','catch','finally','in','is','as','super','this'} +KEYWORDS = {'return','throw','yield','await','break','continue'} def analyze(): out = subprocess.run(['dart','analyze','lib'], capture_output=True, text=True).stdout @@ -101,6 +99,19 @@ def insert_bang(line, col0, name, kind): return None return None +def make_local_nullable(lines, name, upto_line): + """从 upto_line 向上找局部变量声明 'Type name;' 并改可空。""" + pat = re.compile(r'^(\s+)(final\s+|const\s+)?(static\s+)?([\w<>?,\s\.]+?)\s+'+re.escape(name)+r'\s*;\s*$') + for i in range(upto_line-1, -1, -1): + L=lines[i] + m=pat.match(L) + if not m: continue + indent, fmod, smod, typ = m.groups() + if typ in ('var',) or '?' in typ or '=' in L: continue + return i, f"{indent}{fmod or ''}{smod or ''}{typ}? {name};" + break + return None, None + def insert_arg_bang(line, col0): """在参数值末尾插 '!'(仅限简单值:标识符/成员/调用/下标)。""" depth=0; end=None @@ -195,6 +206,15 @@ def apply_round(errs): new=insert_late(old) elif rule in ('argument_type_not_assignable','invalid_assignment'): new=insert_arg_bang(old, col-1) + # not_assigned_potentially_non_nullable_local_variable 不自动处理 + # (make_local_nullable 易误改 return/throw 语句,风险高,留 IDE) + elif False and rule=='not_assigned_potentially_non_nullable_local_variable': + nm=NAME.search(msg) + if nm: + fl=get(path) + fi, fnew = make_local_nullable(fl, nm.group(1), line) + if fi is not None and fl[fi]!=fnew: + fl[fi]=fnew; changed+=1 if new and new!=old: L[line-1]=new; changed+=1 flush()