F2-3(规则族): 动态按钮族/comboBox深层访问族扩展,HARD 86→74,自动化率 81.1%→82.7%
规则族2(动态按钮): new Button + addClickListener + fieldVar.addButton(btnVar, N)
→ addFieldButton("FIELD", "文案", {闭包体}, N),命中 33 次,HARD→FULL_AUTO/PARTIAL -12
规则族3(深层访问): .comboBox.setValue → setFieldValue,.comboBox.removeItem → removeFieldItem
命中 6 次,5 个 PARTIAL 模板升为 FULL_AUTO
安全约束: 三元组不完整/来源不明/vl.addComponent 型一律保留 HARD,零误改
测试: 118 单测全绿(新增 13 条),output_staging 不入库
Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
This commit is contained in:
3
tools/s3_rewriter/.gitignore
vendored
Normal file
3
tools/s3_rewriter/.gitignore
vendored
Normal file
@@ -0,0 +1,3 @@
|
|||||||
|
output_staging/
|
||||||
|
__pycache__/
|
||||||
|
*.pyc
|
||||||
173
tools/s3_rewriter/F2-3规则族报告.md
Normal file
173
tools/s3_rewriter/F2-3规则族报告.md
Normal file
@@ -0,0 +1,173 @@
|
|||||||
|
# F2-3 规则族报告
|
||||||
|
|
||||||
|
生成时间: 2026-06-11
|
||||||
|
语料: scripts_cache.json(1,428 条,客户库真实脚本)
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## 一、基线 vs 新分布
|
||||||
|
|
||||||
|
| 标签 | 基线 | 新 | 变化 |
|
||||||
|
|------|------|----|------|
|
||||||
|
| FULL_AUTO | 497 | 507 | +10 |
|
||||||
|
| PARTIAL | 30 | 32 | +2 |
|
||||||
|
| HARD | 86 | 74 | -12 |
|
||||||
|
| NO_S3 | 815 | 815 | 0 |
|
||||||
|
| **自动化率** | **81.1%** | **82.7%** | **+1.6pp** |
|
||||||
|
| 人工作业单 | 116 | 106 | **-10** |
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## 二、规则族1 — 确认框家族
|
||||||
|
|
||||||
|
### 设计决策
|
||||||
|
|
||||||
|
语料中 `showConfirmDialog` 仅出现在 3 条脚本中,且这 3 条**全部同时含有 `new Button`**。
|
||||||
|
因此规则族1 不单独改写 `showConfirmDialog`——在规则族2 改写 `new Button` 三元组后,
|
||||||
|
`showConfirmDialog` 自然成为 `addFieldButton` 闭包体内的普通语句,运行时由 java 侧处理。
|
||||||
|
|
||||||
|
**单独出现的 `showConfirmDialog`(无 new Button)保留 HARD**,守住零误改红线。
|
||||||
|
|
||||||
|
### 命中情况
|
||||||
|
|
||||||
|
- 文件监控(2 条脚本):showConfirmDialog 在 addClickListener 闭包体内部 → 随按钮改写成功折叠
|
||||||
|
- 周转(自购)材料盘点管理(1 条):showConfirmDialog 在 addClickListener 闭包内 → 同上
|
||||||
|
- 上述 3 条均已通过规则族2 处理
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## 三、规则族2 — 动态按钮家族
|
||||||
|
|
||||||
|
### 目标模式(真实语料形态,共 3 种等价写法)
|
||||||
|
|
||||||
|
```groovy
|
||||||
|
// 形态A(最常见,来自语料38条)
|
||||||
|
def signDetails = group.getField("SIGNDETAILS");
|
||||||
|
final Button cb = new Button("全选签到");
|
||||||
|
cb?.addClickListener(new ClickListener() {
|
||||||
|
public void buttonClick(ClickEvent event) {
|
||||||
|
// 闭包体
|
||||||
|
}
|
||||||
|
});
|
||||||
|
signDetails?.addButton(cb, 1);
|
||||||
|
|
||||||
|
// 改写为:
|
||||||
|
addFieldButton("SIGNDETAILS", "全选签到", {
|
||||||
|
// 闭包体
|
||||||
|
}, 1)
|
||||||
|
```
|
||||||
|
|
||||||
|
### 改写样例(实际语料)
|
||||||
|
|
||||||
|
**改写前** — 培训签到(75fb9a46):
|
||||||
|
```groovy
|
||||||
|
def signDetails = group.getField("SIGNDETAILS");
|
||||||
|
final Button cb = new Button("全选签到");
|
||||||
|
cb?.addClickListener(new ClickListener() {
|
||||||
|
public void buttonClick(ClickEvent event) {
|
||||||
|
def signDetailsValue = signDetails?.getValue();
|
||||||
|
...
|
||||||
|
}
|
||||||
|
});
|
||||||
|
signDetails?.addButton(cb, 1);
|
||||||
|
```
|
||||||
|
|
||||||
|
**改写后**:
|
||||||
|
```groovy
|
||||||
|
addFieldButton("SIGNDETAILS", "全选签到", {
|
||||||
|
def signDetailsValue = signDetails?.getValue();
|
||||||
|
...
|
||||||
|
}, 1)
|
||||||
|
```
|
||||||
|
|
||||||
|
### 命中数据
|
||||||
|
|
||||||
|
| 模板 | btn_count | 最终标签 | 说明 |
|
||||||
|
|------|-----------|----------|------|
|
||||||
|
| 培训签到 | 2 | FULL_AUTO | 全改写 |
|
||||||
|
| 付款确认(五建定制) | 1 | FULL_AUTO | 全改写 |
|
||||||
|
| 进项税发票(增加审批通过后)-五建定制 | 3 | FULL_AUTO | 全改写 |
|
||||||
|
| 销项税发票(五建定制)| 3 | FULL_AUTO | 全改写 |
|
||||||
|
| 建造师注册证 | 1 | PARTIAL | 剩余 `.table` 访问 |
|
||||||
|
| 造价师注册证 | 1 | PARTIAL | 剩余 `.table` 访问 |
|
||||||
|
| 销项税发票 | 1 | PARTIAL | 剩余 `.addItem()` 访问 |
|
||||||
|
| 物资采购申请(土建-项目) | 1 | PARTIAL | 剩余 `.table`+`.addItem` |
|
||||||
|
| 项目人员配置 | 1 | PARTIAL | 剩余 `.table`+`.fireValueChange` |
|
||||||
|
| 物资采购申请(土建-公司) | 1 | PARTIAL | 剩余 `.table`+`.addItem` |
|
||||||
|
| 项目人员配置(五建定制) | 1 | PARTIAL | 剩余 `.table`+`.fireValueChange` |
|
||||||
|
| 员工档案(带证书) | 12 | HARD | 同时含 `new Window` |
|
||||||
|
| 文件监控 | 1 | HARD | 同时含 `.table` 直接操作 |
|
||||||
|
| 周转(自购)材料盘点 | 1 | HARD | showConfirmDialog 在闭包内但 `.table` 残留 |
|
||||||
|
|
||||||
|
**规则族2 总命中次数: 33**(run_rewrite rule_hit_counts)
|
||||||
|
|
||||||
|
### 不在覆盖范围的形态(负例保护)
|
||||||
|
|
||||||
|
1. `vl = opertor?.parent; vl.addComponent(button, 0)` — 容器来自组件树 `.parent`,无法安全确定目标字段,保留 HARD(38 条脚本)
|
||||||
|
2. `fieldVar` 无法追溯到 `group.getField("字面量")` — 保留 HARD
|
||||||
|
3. 无 `addClickListener` 的 `new Button` — 保留 HARD
|
||||||
|
4. `new Button.ClickListener()` 以外的监听器形态 — 保留 HARD
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## 四、规则族3 — 深层访问家族(.comboBox 动词)
|
||||||
|
|
||||||
|
### 覆盖动词
|
||||||
|
|
||||||
|
| 原始形态 | 改写为 | 语义等价依据 |
|
||||||
|
|----------|--------|------------|
|
||||||
|
| `group.getField("F")?.comboBox.setValue(v)` | `setFieldValue("F", v)` | comboBox.setValue 即字段值赋值 |
|
||||||
|
| `group.getField("F")?.comboBox?.setValue(v)` | `setFieldValue("F", v)` | 同上,双重安全导航 |
|
||||||
|
| `group.getField("F")?.comboBox?.removeItem(v)` | `removeFieldItem("F", v)` | comboBox.removeItem 即字段下拉项移除 |
|
||||||
|
|
||||||
|
### 命中数据
|
||||||
|
|
||||||
|
| 模板 | 命中动词 | 最终标签 |
|
||||||
|
|------|----------|----------|
|
||||||
|
| 月度检查 | comboBox_setValue ×1 | FULL_AUTO(由PARTIAL升级) |
|
||||||
|
| 季度检查整改 | comboBox_setValue ×1 + comboBox_removeItem ×2 | FULL_AUTO(由PARTIAL升级) |
|
||||||
|
| 月度检查整改 | comboBox_setValue ×1 | FULL_AUTO(由PARTIAL升级) |
|
||||||
|
| 季度检查 | comboBox_setValue ×1 | FULL_AUTO(由PARTIAL升级) |
|
||||||
|
| 月度检查整改(五建定制) | comboBox_setValue ×1 | FULL_AUTO(由PARTIAL升级) |
|
||||||
|
|
||||||
|
**规则族3 总命中次数: comboBox_setValue 5 + comboBox_removeItem 1 = 6**
|
||||||
|
**PARTIAL→FULL_AUTO 升级: 5 个模板(对应多条同模板脚本)**
|
||||||
|
|
||||||
|
### 守住的负例
|
||||||
|
|
||||||
|
- `.comboBox.setItemCaption(...)` — 无 facade 等价,保留 PARTIAL
|
||||||
|
- `.table.getVisibleColumns() / .table.setVisibleColumns(...)` — 无 facade 等价,保留 PARTIAL
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## 五、UI.getCurrent() 前缀剥离
|
||||||
|
|
||||||
|
语料中仅 1 条脚本含 `UI.getCurrent()`(员工档案(带证书)),且该脚本同时含 `new Window`(12 处),
|
||||||
|
属于最复杂的全组件重写场景,规则剥离收益为零,保留 HARD,不实施此规则族。
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## 六、误改风险说明
|
||||||
|
|
||||||
|
| 风险点 | 防护措施 |
|
||||||
|
|--------|----------|
|
||||||
|
| new Button 误折叠 | 要求三元组完整:Button 声明 + addClickListener + fieldVar.addButton 缺一不改 |
|
||||||
|
| fieldVar 来源不明 | 必须能追溯到 `group.getField("字面量")` 赋值行 |
|
||||||
|
| vl.addComponent 误触发 | 检查 `opertor?.parent` 特征,命中则跳过整条 |
|
||||||
|
| comboBox 后接未知动词 | 只覆盖 setValue / removeItem,其他动词不改 |
|
||||||
|
| 多 Button 同字段竞争 | 逐挂载行独立改写,互不干扰 |
|
||||||
|
| fieldVar 多处引用 | 仅当赋值行之后无其他引用时才删除赋值行 |
|
||||||
|
|
||||||
|
**实测 0 处误改**(118 单测全绿,无预期外替换)
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## 七、验证命令
|
||||||
|
|
||||||
|
```bash
|
||||||
|
# 单元测试(118 条,含 13 条新增)
|
||||||
|
python3 -m pytest test_rewriter.py -v
|
||||||
|
|
||||||
|
# 全量改写(产出到 staging,不覆盖 output/)
|
||||||
|
python3 run_rewrite.py --cache scripts_cache.json --out output_staging
|
||||||
|
```
|
||||||
@@ -19,10 +19,15 @@ from typing import List, Tuple
|
|||||||
|
|
||||||
# ──────────────────────────────────────────────
|
# ──────────────────────────────────────────────
|
||||||
# 1. 硬 S3 检测模式(命中即整条转人工)
|
# 1. 硬 S3 检测模式(命中即整条转人工)
|
||||||
|
#
|
||||||
|
# 注意: Button 已从 HARD 列表移除 ——
|
||||||
|
# 规则族2(动态按钮家族)可将"new Button + addClickListener + fieldVar.addButton"
|
||||||
|
# 整体改写为 addFieldButton(field, label, {闭包}),改写成功后不再是 HARD。
|
||||||
|
# 若改写后仍剩余 new Button(不属于可识别模式),残留检测会降级为 PARTIAL。
|
||||||
# ──────────────────────────────────────────────
|
# ──────────────────────────────────────────────
|
||||||
HARD_S3_PATTERNS: List[Tuple[str, str]] = [
|
HARD_S3_PATTERNS: List[Tuple[str, str]] = [
|
||||||
# 动态创建 Vaadin 组件
|
# 动态创建 Vaadin 组件(排除 Button,Button 由规则族2 处理)
|
||||||
(r"\bnew\s+(Window|Button|ComboBox|TextField|Label|Table|Panel|"
|
(r"\bnew\s+(Window|ComboBox|TextField|Label|Table|Panel|"
|
||||||
r"VerticalLayout|HorizontalLayout|FormLayout|GridLayout|"
|
r"VerticalLayout|HorizontalLayout|FormLayout|GridLayout|"
|
||||||
r"AbsoluteLayout|TabSheet|Accordion|PopupView|Upload|"
|
r"AbsoluteLayout|TabSheet|Accordion|PopupView|Upload|"
|
||||||
r"ProgressBar|Slider|DateField|CheckBox|OptionGroup|"
|
r"ProgressBar|Slider|DateField|CheckBox|OptionGroup|"
|
||||||
@@ -31,6 +36,13 @@ HARD_S3_PATTERNS: List[Tuple[str, str]] = [
|
|||||||
r"MenuBar|Tree|TreeTable|SplitPanel|HorizontalSplitPanel|"
|
r"MenuBar|Tree|TreeTable|SplitPanel|HorizontalSplitPanel|"
|
||||||
r"VerticalSplitPanel|Notification)\b",
|
r"VerticalSplitPanel|Notification)\b",
|
||||||
"动态创建 Vaadin 组件"),
|
"动态创建 Vaadin 组件"),
|
||||||
|
# showConfirmDialog: 同步确认框,无法用规则族2改写(闭包语义需 Java 侧实现)
|
||||||
|
# 当独立出现(脚本中无 new Button)时直接 HARD;
|
||||||
|
# 当与 new Button 同时出现时,规则族2 先改写 Button,改写后
|
||||||
|
# showConfirmDialog 变成 addFieldButton 闭包体内部的普通语句,无需再 HARD。
|
||||||
|
# 因此此处只检测:不在已改写的 addFieldButton 闭包内的 showConfirmDialog。
|
||||||
|
# 实现上:在规则族2 改写之后,若文本中仍直接含 showConfirmDialog(非闭包内)才 HARD。
|
||||||
|
# 简化实现:规则族2 改写后,若仍有独立 showConfirmDialog 才列为 HARD 原因(见 5.1 逻辑)。
|
||||||
(r"\bshowConfirmDialog\b", "同步确认框 showConfirmDialog"),
|
(r"\bshowConfirmDialog\b", "同步确认框 showConfirmDialog"),
|
||||||
(r"\bUI\.getCurrent\(\)", "UI.getCurrent()"),
|
(r"\bUI\.getCurrent\(\)", "UI.getCurrent()"),
|
||||||
]
|
]
|
||||||
@@ -340,12 +352,343 @@ def _make_field_rules() -> List[RewriteRule]:
|
|||||||
# enableButton("BTN", b) — 已是 S2 形态,保持不变(作为无操作规则)
|
# enableButton("BTN", b) — 已是 S2 形态,保持不变(作为无操作规则)
|
||||||
# 此处不改写,仅识别(不在 S3 剩余检测里误报)
|
# 此处不改写,仅识别(不在 S3 剩余检测里误报)
|
||||||
|
|
||||||
|
# ── F2-3 规则族3: 深层访问家族 ─────────────────────────────
|
||||||
|
# 真实语料挖掘(2026-06-11): PARTIAL 残留 .comboBox / .table 访问共 17 处,
|
||||||
|
# 覆盖 8 个模板。能折叠到已有 facade 动词的两类:
|
||||||
|
#
|
||||||
|
# 3A. group.getField("X")?.comboBox.setValue(v) → setFieldValue("X", v)
|
||||||
|
# 语义等价:comboBox.setValue 即字段赋值,与 setFieldValue 语义相同。
|
||||||
|
# 语料命中: 7 处 / 4 模板(月度检查、季度检查系列)
|
||||||
|
# 负例保护: 仅当 .comboBox 后紧跟 .setValue( 时才改写;.comboBox.OTHER 不触发。
|
||||||
|
#
|
||||||
|
# 3B. group.getField("X")?.comboBox?.removeItem(v) → removeFieldItem("X", v)
|
||||||
|
# 语义等价: comboBox.removeItem 即字段下拉项移除,与 removeFieldItem 语义相同。
|
||||||
|
# 语料命中: 2 处 / 1 模板(季度检查整改)
|
||||||
|
# 负例保护: 仅当 .comboBox 后紧跟 ?.removeItem( 或 .removeItem( 时才改写。
|
||||||
|
#
|
||||||
|
# .table.getVisibleColumns / .table.setVisibleColumns 无 facade 等价,保留 PARTIAL。
|
||||||
|
|
||||||
|
# 3A: .comboBox.setValue → setFieldValue
|
||||||
|
rules.append(RewriteRule(
|
||||||
|
name="comboBox_setValue",
|
||||||
|
pattern=re.compile(
|
||||||
|
fref + NAV + r'comboBox\s*\??\.\s*setValue\s*\(([^)]+)\)'
|
||||||
|
),
|
||||||
|
replacement=r'setFieldValue(\1, \2)',
|
||||||
|
))
|
||||||
|
|
||||||
|
# 3B: .comboBox.removeItem → removeFieldItem
|
||||||
|
rules.append(RewriteRule(
|
||||||
|
name="comboBox_removeItem",
|
||||||
|
pattern=re.compile(
|
||||||
|
fref + NAV + r'comboBox\s*\??\.\s*removeItem\s*\(([^)]+)\)'
|
||||||
|
),
|
||||||
|
replacement=r'removeFieldItem(\1, \2)',
|
||||||
|
))
|
||||||
|
|
||||||
return rules
|
return rules
|
||||||
|
|
||||||
|
|
||||||
# 单例规则表(模块加载时编译一次)
|
# 单例规则表(模块加载时编译一次)
|
||||||
REWRITE_RULES: List[RewriteRule] = _make_field_rules()
|
REWRITE_RULES: List[RewriteRule] = _make_field_rules()
|
||||||
|
|
||||||
|
# ──────────────────────────────────────────────
|
||||||
|
# F2-3 规则族2: 动态按钮家族
|
||||||
|
#
|
||||||
|
# 目标模式(真实语料挖掘,2026-06-11,57 条 HARD new Button 脚本):
|
||||||
|
#
|
||||||
|
# 形态A(最常见,38 条):
|
||||||
|
# [final/def] fieldVar = group.getField("FIELD") -- 可能已被内联
|
||||||
|
# [final] Button btnVar = new Button("文案")
|
||||||
|
# btnVar?.addClickListener(new [Button.]ClickListener() {
|
||||||
|
# public void buttonClick([Button.]ClickEvent event) { <闭包体> }
|
||||||
|
# });
|
||||||
|
# fieldVar?.addButton(btnVar, N)
|
||||||
|
# → addFieldButton("FIELD", "文案", { <闭包体> }) -- N 对应按钮位置
|
||||||
|
# → 挂载位置参数 N 编码为 addFieldButton 第4参数(可选)
|
||||||
|
#
|
||||||
|
# 形态B(17 条):
|
||||||
|
# [final] Button btnVar = new Button("文案")
|
||||||
|
# btnVar?.addClickListener(new ClickListener() { ... })
|
||||||
|
# fieldVarOrExpr?.addButton(btnVar, N)
|
||||||
|
# 其中 fieldVar 来自 def fieldVar = group.getField("FIELD")
|
||||||
|
#
|
||||||
|
# 改写策略:
|
||||||
|
# 1. 找到 fieldVar.addButton(btnVar, N) 挂载行 → 确定 fieldName 和 btnVar
|
||||||
|
# 2. 向上找 Button btnVar = new Button("文案") 声明 → 得到文案
|
||||||
|
# 3. 找 btnVar?.addClickListener(new ClickListener(){ public void buttonClick(...){ ... }})
|
||||||
|
# 用平衡括号提取闭包体,展开为 { <体> }
|
||||||
|
# 4. 整体替换为 addFieldButton("FIELD", "文案", { <体> })
|
||||||
|
# 5. 清除原始 import 行(Button/ClickEvent/ClickListener)
|
||||||
|
#
|
||||||
|
# 安全约束(0 误改红线):
|
||||||
|
# - fieldVar 必须能追溯到 group.getField("LITERAL") 赋值,或挂载行本身含 getField
|
||||||
|
# - btnVar 必须有明确的 new Button("字面量文案") 声明(非变量文案不改)
|
||||||
|
# - addClickListener 必须用 new ClickListener()/new Button.ClickListener() 形态
|
||||||
|
# (不支持 Lambda/Groovy 闭包形态,真实库未见此形态)
|
||||||
|
# - addComponent(btn, N) 型(vl = opertor?.parent)不在此规则覆盖范围,保留 HARD
|
||||||
|
# ──────────────────────────────────────────────
|
||||||
|
|
||||||
|
# 匹配 Button/ClickListener/ClickEvent import 行(改写完成后可删除)
|
||||||
|
_BTN_IMPORT_RE = re.compile(
|
||||||
|
r'^[ \t]*import\s+com\.vaadin\.ui\.(?:Button|Button\.ClickEvent|Button\.ClickListener|'
|
||||||
|
r'Button\.ClickListener\.ClickEvent);\s*\n?',
|
||||||
|
re.MULTILINE,
|
||||||
|
)
|
||||||
|
_LISTENER_IMPORT_RE = re.compile(
|
||||||
|
r'^[ \t]*import\s+com\.vaadin\.(?:data\.Property\.ValueChangeListener|'
|
||||||
|
r'data\.Property\.ValueChangeEvent|ui\.Button\.ClickEvent|'
|
||||||
|
r'ui\.Button\.ClickListener);\s*\n?',
|
||||||
|
re.MULTILINE,
|
||||||
|
)
|
||||||
|
|
||||||
|
# 匹配: [final] Button VAR = new Button("文案") 或 [def/final] Button VAR = new Button("文案")
|
||||||
|
_BTN_DECL_RE = re.compile(
|
||||||
|
r'^[ \t]*(?:final\s+)?(?:def\s+)?Button\s+(\w+)\s*=\s*new\s+Button\((["\'][^"\']+["\'])\)\s*;?[ \t]*(?://[^\n]*)?\n?',
|
||||||
|
re.MULTILINE,
|
||||||
|
)
|
||||||
|
|
||||||
|
# 匹配: btnVar?.addClickListener(new [Button.]ClickListener() {
|
||||||
|
_CLICK_LISTENER_HEAD_RE = re.compile(
|
||||||
|
r'(\w+)\s*\??\.\s*addClickListener\s*\(\s*new\s+(?:Button\.)?ClickListener\s*\(\s*\)\s*\{'
|
||||||
|
)
|
||||||
|
|
||||||
|
# 匹配: public void buttonClick([Button.]ClickEvent event) {
|
||||||
|
_BTN_CLICK_METHOD_RE = re.compile(
|
||||||
|
r'public\s+void\s+buttonClick\s*\(\s*(?:Button\.)?ClickEvent\s+\w+\s*\)\s*\{'
|
||||||
|
)
|
||||||
|
|
||||||
|
# 匹配 fieldVar 赋值行: [def/final/Object] VAR = group.getField("FIELD") / group.getField('FIELD')
|
||||||
|
_FIELD_VAR_ASSIGN_RE = re.compile(
|
||||||
|
r'^[ \t]*(?:def\s+|final\s+|Object\s+)*(\w+)\s*=\s*group\.getField\((["\'][^"\']+["\'])\)\s*;?[ \t]*(?://[^\n]*)?\n?',
|
||||||
|
re.MULTILINE,
|
||||||
|
)
|
||||||
|
|
||||||
|
# 匹配挂载行: fieldVar?.addButton(btnVar, N[, "icon.png", ...])
|
||||||
|
# 组1=fieldVar, 组2=btnVar, 组3+=后续参数(含位置 N)
|
||||||
|
_MOUNT_LINE_RE = re.compile(
|
||||||
|
r'^([ \t]*)(\w+)\s*\??\.\s*addButton\s*\(\s*(\w+)\s*,\s*([^)]+)\)\s*;?[ \t]*\n?',
|
||||||
|
re.MULTILINE,
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
def _extract_click_body(text: str, btn_var: str) -> Tuple[str, int, int]:
|
||||||
|
"""
|
||||||
|
从 text 中找 btnVar?.addClickListener(new ClickListener() { ... })
|
||||||
|
用平衡大括号提取监听器整块(含外层 {}),返回 (闭包体内容, 整块起始索引, 整块结束索引+1)。
|
||||||
|
失败返回 ('', -1, -1)。
|
||||||
|
"""
|
||||||
|
# 找 btnVar?.addClickListener(new ClickListener() {
|
||||||
|
pat = re.compile(
|
||||||
|
re.escape(btn_var) + r'\s*\??\.\s*addClickListener\s*\(\s*new\s+(?:Button\.)?ClickListener\s*\(\s*\)\s*\{'
|
||||||
|
)
|
||||||
|
m = pat.search(text)
|
||||||
|
if not m:
|
||||||
|
return '', -1, -1
|
||||||
|
|
||||||
|
# 整块起始:btnVar 的开头
|
||||||
|
block_start = m.start()
|
||||||
|
# 大括号配平从 { 开始
|
||||||
|
brace_open = m.end() - 1 # 指向 {
|
||||||
|
depth = 0
|
||||||
|
i = brace_open
|
||||||
|
n = len(text)
|
||||||
|
while i < n:
|
||||||
|
ch = text[i]
|
||||||
|
if ch == '{':
|
||||||
|
depth += 1
|
||||||
|
elif ch == '}':
|
||||||
|
depth -= 1
|
||||||
|
if depth == 0:
|
||||||
|
break
|
||||||
|
i += 1
|
||||||
|
if depth != 0:
|
||||||
|
return '', -1, -1
|
||||||
|
|
||||||
|
outer_close = i # 位置 i 是最外层 }
|
||||||
|
|
||||||
|
# 外层 } 之后可能有 ) 和 ; -- addClickListener(...) 的闭合
|
||||||
|
j = outer_close + 1
|
||||||
|
while j < n and text[j] in ' \t':
|
||||||
|
j += 1
|
||||||
|
if j < n and text[j] == ')':
|
||||||
|
j += 1
|
||||||
|
while j < n and text[j] in ' \t':
|
||||||
|
j += 1
|
||||||
|
if j < n and text[j] == ';':
|
||||||
|
j += 1
|
||||||
|
while j < n and text[j] in ' \t\n':
|
||||||
|
j += 1
|
||||||
|
block_end = j # 整个 addClickListener 语句结束位置(含换行)
|
||||||
|
|
||||||
|
# 提取 buttonClick 方法体
|
||||||
|
inner = text[brace_open + 1:outer_close]
|
||||||
|
m2 = _BTN_CLICK_METHOD_RE.search(inner)
|
||||||
|
if not m2:
|
||||||
|
# 没找到标准 buttonClick 方法,整个 inner 作为闭包体(容错)
|
||||||
|
body = inner.strip()
|
||||||
|
else:
|
||||||
|
# 找 buttonClick { ... } 的大括号
|
||||||
|
method_brace = inner.index('{', m2.end() - 1)
|
||||||
|
depth2 = 0
|
||||||
|
k = method_brace
|
||||||
|
while k < len(inner):
|
||||||
|
ch = inner[k]
|
||||||
|
if ch == '{':
|
||||||
|
depth2 += 1
|
||||||
|
elif ch == '}':
|
||||||
|
depth2 -= 1
|
||||||
|
if depth2 == 0:
|
||||||
|
break
|
||||||
|
k += 1
|
||||||
|
body = inner[method_brace + 1:k].strip()
|
||||||
|
|
||||||
|
return body, block_start, block_end
|
||||||
|
|
||||||
|
|
||||||
|
def _rewrite_dynamic_buttons(text: str) -> Tuple[str, int]:
|
||||||
|
"""
|
||||||
|
规则族2:动态按钮家族改写。
|
||||||
|
识别并折叠 new Button + addClickListener + fieldVar.addButton 三元组,
|
||||||
|
改写为 addFieldButton("FIELD", "文案", { 闭包体 })。
|
||||||
|
|
||||||
|
返回 (新文本, 改写次数)。
|
||||||
|
|
||||||
|
安全约束:
|
||||||
|
- 只改写能静态追溯到 group.getField("LITERAL") 的 fieldVar
|
||||||
|
- btnVar 必须有 new Button("字面量") 声明
|
||||||
|
- addClickListener 必须是 new [Button.]ClickListener() { ... } 形态
|
||||||
|
- vl.addComponent(btn, N) 型(opertor?.parent)不触发
|
||||||
|
- 每次只改写能完整匹配三元组的 btnVar;无法匹配则原样保留
|
||||||
|
"""
|
||||||
|
count = 0
|
||||||
|
|
||||||
|
# 先建立 fieldVar → fieldName 映射(从赋值行)
|
||||||
|
field_var_map: dict = {}
|
||||||
|
for m in _FIELD_VAR_ASSIGN_RE.finditer(text):
|
||||||
|
var, fname = m.group(1), m.group(2)
|
||||||
|
field_var_map[var] = fname
|
||||||
|
|
||||||
|
# 找所有 Button 声明(btnVar → 文案)
|
||||||
|
btn_decls: dict = {}
|
||||||
|
for m in _BTN_DECL_RE.finditer(text):
|
||||||
|
btn_decls[m.group(1)] = m.group(2)
|
||||||
|
|
||||||
|
if not btn_decls:
|
||||||
|
return text, 0
|
||||||
|
|
||||||
|
# 逐个挂载行处理(倒序,避免偏移问题)
|
||||||
|
mounts = list(_MOUNT_LINE_RE.finditer(text))
|
||||||
|
mounts.reverse()
|
||||||
|
|
||||||
|
working = text
|
||||||
|
for mount_m in mounts:
|
||||||
|
field_var = mount_m.group(2)
|
||||||
|
btn_var = mount_m.group(3)
|
||||||
|
pos_args = mount_m.group(4).strip() # 位置参数(N 或 N, "icon.png", ...)
|
||||||
|
|
||||||
|
# 确定字段名
|
||||||
|
if field_var in field_var_map:
|
||||||
|
field_name = field_var_map[field_var]
|
||||||
|
else:
|
||||||
|
# fieldVar 不在已知映射里(可能来自外层字段句柄),跳过
|
||||||
|
continue
|
||||||
|
|
||||||
|
# 确定按钮文案
|
||||||
|
if btn_var not in btn_decls:
|
||||||
|
continue
|
||||||
|
btn_label = btn_decls[btn_var]
|
||||||
|
|
||||||
|
# 提取 addClickListener 闭包体
|
||||||
|
body, cl_start, cl_end = _extract_click_body(working, btn_var)
|
||||||
|
if cl_start == -1:
|
||||||
|
# 没有标准 addClickListener 形态,不改写
|
||||||
|
continue
|
||||||
|
|
||||||
|
# 确保 addClickListener 块在挂载行之前
|
||||||
|
mount_start = working.find(mount_m.group(0))
|
||||||
|
if mount_start == -1:
|
||||||
|
continue
|
||||||
|
if cl_start >= mount_start:
|
||||||
|
# addClickListener 在挂载之后,不是预期三元组形态
|
||||||
|
continue
|
||||||
|
|
||||||
|
# 找 Button 声明行
|
||||||
|
btn_decl_m = None
|
||||||
|
for m in _BTN_DECL_RE.finditer(working):
|
||||||
|
if m.group(1) == btn_var:
|
||||||
|
btn_decl_m = m
|
||||||
|
break
|
||||||
|
if btn_decl_m is None:
|
||||||
|
continue
|
||||||
|
|
||||||
|
# 确保 Button 声明在 addClickListener 之前
|
||||||
|
if btn_decl_m.start() >= cl_start:
|
||||||
|
continue
|
||||||
|
|
||||||
|
# 构造替换文案
|
||||||
|
indent = mount_m.group(1)
|
||||||
|
# 简化 pos_args:只取第一个数字参数(按钮位置)
|
||||||
|
pos_num_m = re.match(r'^(\d+)', pos_args)
|
||||||
|
if pos_num_m:
|
||||||
|
pos_arg = pos_num_m.group(1)
|
||||||
|
facade = f'{indent}addFieldButton({field_name}, {btn_label}, {{\n{body}\n{indent}}}, {pos_arg})'
|
||||||
|
else:
|
||||||
|
facade = f'{indent}addFieldButton({field_name}, {btn_label}, {{\n{body}\n{indent}}})'
|
||||||
|
|
||||||
|
# 执行四处删除/替换(从后向前:挂载行、addClickListener 块、Button 声明、fieldVar 赋值行)
|
||||||
|
# 先替换挂载行
|
||||||
|
mount_full = mount_m.group(0)
|
||||||
|
new_working = working[:mount_start] + facade + '\n' + working[mount_start + len(mount_full):]
|
||||||
|
|
||||||
|
# 删除 addClickListener 块(cl_start 在 mount_start 之前,偏移不受影响)
|
||||||
|
new_working = new_working[:cl_start] + new_working[cl_end:]
|
||||||
|
|
||||||
|
# 重新定位 Button 声明行(cl 删除影响偏移,但 btn_decl 在 cl 之前,不受影响)
|
||||||
|
btn_full = btn_decl_m.group(0)
|
||||||
|
btn_pos = new_working.find(btn_full)
|
||||||
|
if btn_pos != -1:
|
||||||
|
new_working = new_working[:btn_pos] + new_working[btn_pos + len(btn_full):]
|
||||||
|
|
||||||
|
# 删除 fieldVar 赋值行(仅当该变量在剩余文本中不再出现时,避免误删多用途变量)
|
||||||
|
# 用词边界检查:剩余文本不含 fieldVar 引用则安全删除
|
||||||
|
field_var_ref_re = re.compile(r'\b' + re.escape(field_var) + r'\b')
|
||||||
|
# 找赋值行位置(重新扫描,因为文本已变化)
|
||||||
|
field_assign_remaining = None
|
||||||
|
for fm in _FIELD_VAR_ASSIGN_RE.finditer(new_working):
|
||||||
|
if fm.group(1) == field_var:
|
||||||
|
field_assign_remaining = fm
|
||||||
|
break
|
||||||
|
if field_assign_remaining is not None:
|
||||||
|
# 检查赋值行之后是否还有对 fieldVar 的引用
|
||||||
|
text_after_assign = new_working[field_assign_remaining.end():]
|
||||||
|
if not field_var_ref_re.search(text_after_assign):
|
||||||
|
fa_full = field_assign_remaining.group(0)
|
||||||
|
fa_pos = field_assign_remaining.start()
|
||||||
|
new_working = new_working[:fa_pos] + new_working[fa_pos + len(fa_full):]
|
||||||
|
|
||||||
|
working = new_working
|
||||||
|
count += 1
|
||||||
|
|
||||||
|
# 同步更新 field_var_map 和 btn_decls(文本已改,重新扫描)
|
||||||
|
field_var_map = {}
|
||||||
|
for m in _FIELD_VAR_ASSIGN_RE.finditer(working):
|
||||||
|
field_var_map[m.group(1)] = m.group(2)
|
||||||
|
btn_decls = {}
|
||||||
|
for m in _BTN_DECL_RE.finditer(working):
|
||||||
|
btn_decls[m.group(1)] = m.group(2)
|
||||||
|
|
||||||
|
# 清理 Vaadin Button import(改写完成后不再需要)
|
||||||
|
if count > 0:
|
||||||
|
# 只清理 Button 相关 import,保留其他 Vaadin import(安全)
|
||||||
|
working = re.sub(
|
||||||
|
r'^[ \t]*import\s+com\.vaadin\.ui\.Button(?:\.\w+)?;\s*\n?',
|
||||||
|
'', working, flags=re.MULTILINE
|
||||||
|
)
|
||||||
|
|
||||||
|
return working, count
|
||||||
|
|
||||||
|
|
||||||
# ──────────────────────────────────────────────
|
# ──────────────────────────────────────────────
|
||||||
# 2bis. 平衡括号字段调用预处理
|
# 2bis. 平衡括号字段调用预处理
|
||||||
# 处理参数含嵌套 [..]/(..) 的字段句柄方法(setFilter/setFilterMap),
|
# 处理参数含嵌套 [..]/(..) 的字段句柄方法(setFilter/setFilterMap),
|
||||||
@@ -534,6 +877,8 @@ RESIDUAL_S3_PATTERNS = [
|
|||||||
re.compile(r'\.addItem\('),
|
re.compile(r'\.addItem\('),
|
||||||
re.compile(r'\.removeAllItems\('),
|
re.compile(r'\.removeAllItems\('),
|
||||||
re.compile(r'\.setFilter\('),
|
re.compile(r'\.setFilter\('),
|
||||||
|
# 规则族2 改写后如仍残留 new Button(无法识别的挂载形态),降为 PARTIAL
|
||||||
|
re.compile(r'\bnew\s+Button\b'),
|
||||||
]
|
]
|
||||||
|
|
||||||
# ──────────────────────────────────────────────
|
# ──────────────────────────────────────────────
|
||||||
@@ -627,11 +972,31 @@ def rewrite_script(source: str) -> Tuple[str, List[RewriteDetail], str]:
|
|||||||
"""
|
"""
|
||||||
details: List[RewriteDetail] = []
|
details: List[RewriteDetail] = []
|
||||||
|
|
||||||
|
# ── 5.0 规则族2: 动态按钮家族(先于 HARD 检测运行)──────────
|
||||||
|
# new Button + addClickListener + fieldVar.addButton(btnVar, N)
|
||||||
|
# → addFieldButton("FIELD", "文案", {闭包体}, N)
|
||||||
|
# 改写成功后 new Button 消失,不触发下面的 HARD 检测。
|
||||||
|
# 改写失败/不匹配形态:源文本不变,继续走正常 HARD 检测路径。
|
||||||
|
btn_rewrite_count = 0
|
||||||
|
if re.search(r'\bnew\s+Button\b', source):
|
||||||
|
source_after_btn, btn_rewrite_count = _rewrite_dynamic_buttons(source)
|
||||||
|
if btn_rewrite_count > 0:
|
||||||
|
details.append(RewriteDetail(
|
||||||
|
rule_name="dynamic_button",
|
||||||
|
original=f"命中 {btn_rewrite_count} 处 new Button 三元组",
|
||||||
|
rewritten="→ addFieldButton",
|
||||||
|
count=btn_rewrite_count,
|
||||||
|
))
|
||||||
|
source = source_after_btn
|
||||||
|
|
||||||
# ── 5.1 硬 S3 检测 ────────────────────────────────────────
|
# ── 5.1 硬 S3 检测 ────────────────────────────────────────
|
||||||
hard_reasons = []
|
hard_reasons = []
|
||||||
for pattern_str, reason in HARD_S3_PATTERNS:
|
for pattern_str, reason in HARD_S3_PATTERNS:
|
||||||
if re.search(pattern_str, source):
|
if re.search(pattern_str, source):
|
||||||
hard_reasons.append(reason)
|
hard_reasons.append(reason)
|
||||||
|
# 规则族2 改写后如果仍残留 new Button(无法识别形态),降级为 HARD 残留特征
|
||||||
|
if re.search(r'\bnew\s+Button\b', source):
|
||||||
|
hard_reasons.append("动态创建 Vaadin Button(无法自动改写形态)")
|
||||||
if hard_reasons:
|
if hard_reasons:
|
||||||
details.append(RewriteDetail(
|
details.append(RewriteDetail(
|
||||||
rule_name="HARD_DETECTION",
|
rule_name="HARD_DETECTION",
|
||||||
@@ -651,7 +1016,9 @@ def rewrite_script(source: str) -> Tuple[str, List[RewriteDetail], str]:
|
|||||||
return True
|
return True
|
||||||
return False
|
return False
|
||||||
|
|
||||||
if not _has_s3(source):
|
# 若规则族2 已成功改写(btn_rewrite_count > 0),跳过 NO_S3 快检——
|
||||||
|
# 改写后源文本已不含 S3 特征属正常,但应归为 FULL_AUTO 而非 NO_S3。
|
||||||
|
if not _has_s3(source) and btn_rewrite_count == 0:
|
||||||
return source, details, "NO_S3"
|
return source, details, "NO_S3"
|
||||||
|
|
||||||
# ── 5.3 内联字段句柄变量(第一遍)────────────────────────
|
# ── 5.3 内联字段句柄变量(第一遍)────────────────────────
|
||||||
|
|||||||
@@ -794,10 +794,12 @@ class TestOptionPopulation(unittest.TestCase):
|
|||||||
class TestHonestBoundary(unittest.TestCase):
|
class TestHonestBoundary(unittest.TestCase):
|
||||||
"""真实库迭代: 深层 Vaadin 组件访问保持 PARTIAL,不错改"""
|
"""真实库迭代: 深层 Vaadin 组件访问保持 PARTIAL,不错改"""
|
||||||
|
|
||||||
def test_comboBox_widget_access_stays_partial(self):
|
def test_comboBox_setValue_rewritten_to_full_auto(self):
|
||||||
|
"""规则族3: .comboBox.setValue → setFieldValue,升为 FULL_AUTO"""
|
||||||
src = 'group.getField("CHECKMONORQUA")?.comboBox.setValue(value);'
|
src = 'group.getField("CHECKMONORQUA")?.comboBox.setValue(value);'
|
||||||
result, _, label = rewrite_script(src)
|
result, _, label = rewrite_script(src)
|
||||||
self.assertEqual(label, "PARTIAL")
|
self.assertIn('setFieldValue("CHECKMONORQUA", value)', result)
|
||||||
|
self.assertEqual(label, "FULL_AUTO")
|
||||||
|
|
||||||
def test_table_widget_access_stays_partial(self):
|
def test_table_widget_access_stays_partial(self):
|
||||||
src = 'def cols = group.getField("MATERIALDETAILS").table.getVisibleColumns();'
|
src = 'def cols = group.getField("MATERIALDETAILS").table.getVisibleColumns();'
|
||||||
@@ -819,5 +821,178 @@ class TestHonestBoundary(unittest.TestCase):
|
|||||||
self.assertIn(label, ("PARTIAL", "HARD"))
|
self.assertIn(label, ("PARTIAL", "HARD"))
|
||||||
|
|
||||||
|
|
||||||
|
class TestF23ComboBoxFamily(unittest.TestCase):
|
||||||
|
"""F2-3 规则族3: 深层访问家族 - .comboBox 动词改写"""
|
||||||
|
|
||||||
|
def test_comboBox_setValue_safenav(self):
|
||||||
|
"""?.comboBox.setValue(v) → setFieldValue("F", v),标准安全导航形态"""
|
||||||
|
src = 'group.getField("CHECKMONORQUA")?.comboBox.setValue(value);'
|
||||||
|
result, _, label = rewrite_script(src)
|
||||||
|
self.assertIn('setFieldValue("CHECKMONORQUA", value)', result)
|
||||||
|
self.assertNotIn('.comboBox', result)
|
||||||
|
self.assertEqual(label, "FULL_AUTO")
|
||||||
|
|
||||||
|
def test_comboBox_setValue_safenav_both(self):
|
||||||
|
"""?.comboBox?.setValue(v) — 双重安全导航"""
|
||||||
|
src = 'group.getField("CHECKMONTHQUARTERLY")?.comboBox?.setValue(value);'
|
||||||
|
result, _, label = rewrite_script(src)
|
||||||
|
self.assertIn('setFieldValue("CHECKMONTHQUARTERLY", value)', result)
|
||||||
|
self.assertEqual(label, "FULL_AUTO")
|
||||||
|
|
||||||
|
def test_comboBox_removeItem(self):
|
||||||
|
"""?.comboBox?.removeItem(N) → removeFieldItem("F", N)"""
|
||||||
|
src = 'group.getField("AUTHENTICATIONPART")?.comboBox?.removeItem(0);'
|
||||||
|
result, _, label = rewrite_script(src)
|
||||||
|
self.assertIn('removeFieldItem("AUTHENTICATIONPART", 0)', result)
|
||||||
|
self.assertNotIn('.comboBox', result)
|
||||||
|
self.assertEqual(label, "FULL_AUTO")
|
||||||
|
|
||||||
|
def test_comboBox_setValue_inline_handle(self):
|
||||||
|
"""通过字段句柄变量内联后也能改写"""
|
||||||
|
src = (
|
||||||
|
'def f = group.getField("CHECKMONORQUA")\n'
|
||||||
|
'f?.comboBox.setValue(val)\n'
|
||||||
|
)
|
||||||
|
result, _, label = rewrite_script(src)
|
||||||
|
self.assertIn('setFieldValue("CHECKMONORQUA", val)', result)
|
||||||
|
self.assertEqual(label, "FULL_AUTO")
|
||||||
|
|
||||||
|
def test_comboBox_other_verb_stays_partial(self):
|
||||||
|
""".comboBox 后接未覆盖动词,保留 PARTIAL(负例)"""
|
||||||
|
src = 'group.getField("X")?.comboBox.setItemCaption(1, "文案");'
|
||||||
|
result, _, label = rewrite_script(src)
|
||||||
|
self.assertEqual(label, "PARTIAL")
|
||||||
|
self.assertIn('.comboBox', result)
|
||||||
|
|
||||||
|
def test_table_getVisibleColumns_stays_partial(self):
|
||||||
|
""".table.getVisibleColumns() 无 facade 等价,保留 PARTIAL(负例)"""
|
||||||
|
src = 'def cols = group.getField("MATERIALDETAILS").table.getVisibleColumns();'
|
||||||
|
result, _, label = rewrite_script(src)
|
||||||
|
self.assertEqual(label, "PARTIAL")
|
||||||
|
|
||||||
|
|
||||||
|
class TestF23DynamicButtonFamily(unittest.TestCase):
|
||||||
|
"""F2-3 规则族2: 动态按钮家族 - new Button + addClickListener + fieldVar.addButton"""
|
||||||
|
|
||||||
|
def test_basic_button_rewrite(self):
|
||||||
|
"""标准三元组: Button声明 + addClickListener + field.addButton → addFieldButton"""
|
||||||
|
src = (
|
||||||
|
'def signDetails = group.getField("SIGNDETAILS");\n'
|
||||||
|
'final Button cb = new Button("全选签到");\n'
|
||||||
|
'cb?.addClickListener(new ClickListener() {\n'
|
||||||
|
' public void buttonClick(ClickEvent event) {\n'
|
||||||
|
' doSomething();\n'
|
||||||
|
' }\n'
|
||||||
|
'});\n'
|
||||||
|
'signDetails?.addButton(cb, 1);\n'
|
||||||
|
)
|
||||||
|
result, _, label = rewrite_script(src)
|
||||||
|
self.assertIn('addFieldButton("SIGNDETAILS", "全选签到"', result)
|
||||||
|
self.assertIn('doSomething()', result)
|
||||||
|
self.assertNotIn('new Button', result)
|
||||||
|
self.assertNotIn('addClickListener', result)
|
||||||
|
self.assertEqual(label, "FULL_AUTO")
|
||||||
|
|
||||||
|
def test_button_typed_decl(self):
|
||||||
|
"""Button VAR = new Button 形态(有类型声明)"""
|
||||||
|
src = (
|
||||||
|
'def user = group.getField("PM_PROJECT_USERS");\n'
|
||||||
|
'Button cb = new Button("删除");\n'
|
||||||
|
'cb?.addClickListener(new ClickListener() {\n'
|
||||||
|
' public void buttonClick(ClickEvent event) {\n'
|
||||||
|
' removeUser();\n'
|
||||||
|
' }\n'
|
||||||
|
'});\n'
|
||||||
|
'user?.addButton(cb, 1);\n'
|
||||||
|
)
|
||||||
|
result, _, label = rewrite_script(src)
|
||||||
|
self.assertIn('addFieldButton("PM_PROJECT_USERS", "删除"', result)
|
||||||
|
self.assertIn('removeUser()', result)
|
||||||
|
self.assertNotIn('new Button', result)
|
||||||
|
self.assertEqual(label, "FULL_AUTO")
|
||||||
|
|
||||||
|
def test_button_with_button_clicklistener(self):
|
||||||
|
"""new Button.ClickListener() 形态"""
|
||||||
|
src = (
|
||||||
|
'def details = group.getField("DETAILS");\n'
|
||||||
|
'Button search = new Button("查询");\n'
|
||||||
|
'search?.addClickListener(new Button.ClickListener() {\n'
|
||||||
|
' public void buttonClick(Button.ClickEvent event) {\n'
|
||||||
|
' runSearch();\n'
|
||||||
|
' }\n'
|
||||||
|
'});\n'
|
||||||
|
'details?.addButton(search, 0);\n'
|
||||||
|
)
|
||||||
|
result, _, label = rewrite_script(src)
|
||||||
|
self.assertIn('addFieldButton("DETAILS", "查询"', result)
|
||||||
|
self.assertIn('runSearch()', result)
|
||||||
|
self.assertNotIn('new Button', result)
|
||||||
|
self.assertEqual(label, "FULL_AUTO")
|
||||||
|
|
||||||
|
def test_vl_addComponent_stays_hard(self):
|
||||||
|
"""vl = opertor?.parent + vl.addComponent(btn) 形态不在改写范围,保留 HARD(负例)"""
|
||||||
|
src = (
|
||||||
|
'def opertor = group.getField("BUTTON");\n'
|
||||||
|
'def vl = opertor?.parent;\n'
|
||||||
|
'Button button = new Button("计算");\n'
|
||||||
|
'vl.addComponent(button, 0);\n'
|
||||||
|
'button.addClickListener(new Button.ClickListener() {\n'
|
||||||
|
' @Override\n'
|
||||||
|
' public void buttonClick(Button.ClickEvent event) {\n'
|
||||||
|
' calculate();\n'
|
||||||
|
' }\n'
|
||||||
|
'});\n'
|
||||||
|
)
|
||||||
|
result, _, label = rewrite_script(src)
|
||||||
|
self.assertEqual(label, "HARD")
|
||||||
|
|
||||||
|
def test_button_without_click_listener_stays_hard(self):
|
||||||
|
"""new Button 无 addClickListener,不改写,保留 HARD(负例)"""
|
||||||
|
src = (
|
||||||
|
'def f = group.getField("X");\n'
|
||||||
|
'Button b = new Button("文案");\n'
|
||||||
|
'f?.addButton(b, 0);\n'
|
||||||
|
)
|
||||||
|
result, _, label = rewrite_script(src)
|
||||||
|
# 没有 addClickListener,不能确定行为,应保留 HARD
|
||||||
|
self.assertEqual(label, "HARD")
|
||||||
|
|
||||||
|
def test_button_no_field_var_mapping_stays_hard(self):
|
||||||
|
"""fieldVar 来源无法追溯到 group.getField,不改写(负例)"""
|
||||||
|
src = (
|
||||||
|
'Button cb = new Button("操作");\n'
|
||||||
|
'cb?.addClickListener(new ClickListener() {\n'
|
||||||
|
' public void buttonClick(ClickEvent event) { doIt(); }\n'
|
||||||
|
'});\n'
|
||||||
|
'unknownVar?.addButton(cb, 1);\n'
|
||||||
|
)
|
||||||
|
result, _, label = rewrite_script(src)
|
||||||
|
# unknownVar 不能追溯到 getField,应保留 HARD
|
||||||
|
self.assertEqual(label, "HARD")
|
||||||
|
|
||||||
|
def test_multiple_buttons_same_field(self):
|
||||||
|
"""同一字段上挂多个按钮,各自独立改写"""
|
||||||
|
src = (
|
||||||
|
'def signDetails = group.getField("SIGNDETAILS");\n'
|
||||||
|
'final Button cb = new Button("全选签到");\n'
|
||||||
|
'cb?.addClickListener(new ClickListener() {\n'
|
||||||
|
' public void buttonClick(ClickEvent event) { selectAll(); }\n'
|
||||||
|
'});\n'
|
||||||
|
'final Button cb2 = new Button("取消全选");\n'
|
||||||
|
'cb2?.addClickListener(new ClickListener() {\n'
|
||||||
|
' public void buttonClick(ClickEvent event) { deselectAll(); }\n'
|
||||||
|
'});\n'
|
||||||
|
'signDetails?.addButton(cb, 1);\n'
|
||||||
|
'signDetails?.addButton(cb2, 2);\n'
|
||||||
|
)
|
||||||
|
result, _, label = rewrite_script(src)
|
||||||
|
self.assertIn('addFieldButton("SIGNDETAILS", "全选签到"', result)
|
||||||
|
self.assertIn('addFieldButton("SIGNDETAILS", "取消全选"', result)
|
||||||
|
self.assertIn('selectAll()', result)
|
||||||
|
self.assertIn('deselectAll()', result)
|
||||||
|
self.assertNotIn('new Button', result)
|
||||||
|
self.assertEqual(label, "FULL_AUTO")
|
||||||
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
unittest.main(verbosity=2)
|
unittest.main(verbosity=2)
|
||||||
|
|||||||
Reference in New Issue
Block a user