gatelight 0.19.1 → 0.19.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "gatelight",
3
3
  "displayName": "gatelight",
4
- "version": "0.19.1",
4
+ "version": "0.19.3",
5
5
  "description": "Gates for coding agents: tests actually ran before code lands, code stays inside its module boundaries, standards (types, lint, format, secrets) are not quietly loosened, plus test-writing conventions and a change-triggered checklist",
6
6
  "author": {
7
7
  "name": "coffeecoproject"
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "gatelight",
3
3
  "displayName": "gatelight",
4
- "version": "0.19.1",
4
+ "version": "0.19.3",
5
5
  "description": "Gates for coding agents: tests actually ran before code lands, code stays inside its module boundaries, standards (types, lint, format, secrets) are not quietly loosened, plus test-writing conventions and a change-triggered checklist",
6
6
  "author": {
7
7
  "name": "coffeecoproject"
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "gatelight",
3
- "version": "0.19.1",
3
+ "version": "0.19.3",
4
4
  "description": "Gates for coding agents: tests actually ran before code lands, code stays inside its module boundaries, standards (types, lint, format, secrets) are not quietly loosened, plus test-writing conventions and a change-triggered checklist",
5
5
  "author": {
6
6
  "name": "coffeecoproject"
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "gatelight",
3
- "version": "0.19.1",
3
+ "version": "0.19.3",
4
4
  "description": "Gates for coding agents: tests actually ran before code lands, code stays inside its module boundaries, standards (types, lint, format, secrets) are not quietly loosened, plus test-writing conventions and a change-triggered checklist",
5
5
  "license": "MIT",
6
6
  "homepage": "https://github.com/coffeecoproject/gatelight#readme",
@@ -22,7 +22,7 @@ description: 把做完的分支合回默认分支:先把测试写上(接入
22
22
  - 这个会话没有自己的分支(比如在主目录里被叫来合),用户又没点名:列出还没合进去的分支(`git branch --no-merged main`)给用户挑。
23
23
  2. **把测试补齐**,然后 `testpace focused` 通过,把这个分支提交干净。
24
24
  - 开发中留着没过的旧测试(因为用户这次改了需求才不过的):照定下来的规则改掉。
25
- - 仓库接入了 testcraft(有 `.testcraft/` 目录)的:照 gatelight:write-tests 技能"什么时候写"一节,把这次定下来的规则列出来,交给一个子代理写新测试,旧测试也交给它一起改。这次没改到规则的不用写。
25
+ - 仓库接入了 testcraft(有 `.testcraft/` 目录)的:照 gatelight:write-tests 技能"什么时候写"一节,把这次定下来的规则列出来,交给一个子代理写新测试,旧测试也交给它一起改。这次没改到规则的不用写。规则写进测试后,把设计文档、README 里写着这几条规则的段落删掉,只留为什么这样定、否掉的做法、还没定的(见 gatelight:write-tests 技能),和测试一起提交。
26
26
  3. **找一个干净的地方合**。主目录在 `main` 上而且没有未提交改动,就在主目录里做;否则开一个新 worktree,按项目说明准备好环境(依赖、`.env`、服务)。不要在别的会话正在用的目录里做。
27
27
  4. **建合并分支并合进来**:从最新的 `main` 建 `merge/<日期时间>`,把要合的分支合进去(`git merge --no-ff --no-edit <分支>`;有几个就一个个合)。
28
28
  - 有冲突时,机械性的(比如两边各加了一段、只是位置挨着)可以解决;涉及逻辑取舍的,停下来告诉用户是哪些文件、和 `main` 上的哪次改动冲突,问怎么办。不要用 `-X ours`、`-X theirs` 这类办法一刀切。
@@ -38,6 +38,7 @@ modwall 的约定:默认模块化,越界由程序拦。模块边界以 `.mod
38
38
  - 样式、图片、JSON 这类文件也算:不直接 import 别的模块里的。图片、JSON 和代码一样从对方入口导出;样式不能经入口导出,几个模块都用的样式搬进共享层,只该对方用的,改成用对方入口导出的组件。
39
39
  - 入口里没有要的东西:在对方入口**新增导出**,作为单独的一小步来做,说清楚为什么要导出。不要为了省事把对方内部的东西全从入口导出:`modwall check` 会列出这次给哪些入口新增了导出。
40
40
  - **不给别的模块的已有函数加分支**:为新功能在对方新增一个函数。改了别人已有的导出函数、同时又在自己模块里新加了对它的调用,`modwall check` 会提示(R4);正常的重构(改签名、同步更新调用处)不用管。
41
+ - **不在别的模块里写上自己的名字**:别的模块的名单、类型、枚举、常量、映射表、`switch`、数据库约束里,不为新功能加上它的名字、它的页面、它的来源。加了,对方就认识了用它的模块,以后每多一个用的地方都要回去改它,看引用还查不出来。改成对方收一个值(比如来源的文字由调用方给),或者由装配层把链接、实现接进来。旧代码里已经有这种写法的,不照着再加一项:顺手改成干净的写法,改动大的先告诉用户。这次新建了模块、又改了已有模块里已有的东西(声明,或它的新迁移里改、删已有的对象),`modwall check` 会提示(R4);正常的重构不用管。
41
42
  - 模块之间**不能成环**(数据库里经入口 schema 的引用也算进去):A 用 B、B 又用 A 时,把真正通用、没有业务含义的东西下沉到共享层,或者通过参数传进来;两个模块确实分不开,就合成一个。不要为了躲开成环把业务类型、常量挪进共享层:成环说明模块要重新划分,拿不准就停下来问用户。
42
43
  - 正式代码不引用 `outside` 里的文件(构建配置、脚本、迁移)和测试文件。
43
44
 
@@ -71,7 +71,7 @@ testpace 不管测试的写法风格(分几层、怎么组织、写多细)
71
71
  ## 步骤
72
72
 
73
73
  1. **找项目已有的命令和测试**:看 `package.json` 的 scripts、Makefile、CI 配置、README、测试框架的配置。三条命令尽量直接用现成的,不另造一套;不是 JavaScript 项目(Go、Python、Swift 等)也一样写三条命令。`full` 放测试以外的全部检查;`tests` 照测试框架实际认的文件写(比如 Vitest 的 include、exclude),项目里有几种测试(单元、数据库、页面、端到端)就要都覆盖到。说不清某种测试怎么跑、或全部测试跑一遍太久要怎么取舍时,用大白话说明情况,请用户定;把整套系统起起来跑的测试排除出整组,不在可取舍的办法里。
74
- 2. **测试用 `node:test`、Vitest 或 Playwright 的,`files` 一律用 `testpace run node-test`、`testpace run vitest` 或 `testpace run playwright`**,不自己写包装、不自己数测试,接入前项目里已有的这类包装和计数报告器删掉:它检查文件都在、给的每个文件和每个 `文件:行号` 都跑到了测试(被项目配置排除、悄悄没跑的文件也查得出来;Playwright 只要别的行有测试,给的行上没有测试、文件里没有测试都照样通过,也查得出来),全被跳过时说一句;Vitest、Playwright 按行跑,node:test 不能按行跑,按行给的整个文件跑、跑完照样核对那一行。项目配置里的报告器(比如 Playwright 的断言检查、Vitest 的 junit)照常起作用。要先起环境、编译、按路径分给不同跑法的,脚本照旧做这些,到"用框架跑这几个文件"那一步调它(`--cwd` 换目录:node 的工作目录,或装着 Vitest、Playwright,放着它们配置的那个目录;`--node-arg`、`--vitest-arg`、`--playwright-arg` 原样交参数给框架,比如 `--node-arg=--import=tsx`、`--vitest-arg=--maxWorkers=2`),自己不再数测试。testpace 跑项目命令时 PATH 上一定有它自己。
74
+ 2. **测试用 `node:test`、Vitest 或 Playwright 的,`files` 一律用 `testpace run node-test`、`testpace run vitest` 或 `testpace run playwright`**,不自己写包装、不自己数测试,接入前项目里已有的这类包装和计数报告器删掉:它检查文件都在、给的每个文件和每个 `文件:行号` 都跑到了测试(被项目配置排除、悄悄没跑的文件也查得出来;Playwright 只要别的行有测试,给的行上没有测试、文件里没有测试都照样通过,也查得出来),全被跳过时说一句;Vitest、Playwright 按行跑,node:test 不能按行跑,按行给的整个文件跑、跑完照样核对那一行。项目配置里的报告器(比如 Playwright 的断言检查、Vitest 的 junit)照常起作用。要先起环境、编译、按路径分给不同跑法的,脚本照旧做这些,到"用框架跑这几个文件"那一步调它(`--cwd` 换目录:node 的工作目录,或装着 Vitest、Playwright,放着它们配置的那个目录;`--node-arg`、`--vitest-arg`、`--playwright-arg` 原样交参数给框架,比如 `--node-arg=--import=tsx`、`--vitest-arg=--maxWorkers=2`),自己不再数测试。testpace 跑项目命令时 PATH 上一定有它自己。整组跑完会核对:交给 `files` 的每个测试文件都要交回至少一条结果(经 `testpace run` 跑的自动交)。不是这三种框架、只能用别的办法跑的(比如一个脚本就是一条数据库契约),自己往环境变量 `TESTPACE_RESULTS` 给的文件里每个测试追加一行 JSON:`{"file":"文件的绝对路径","name":"测试名","status":"passed"}`(`status` 是 `passed`、`failed` 或 `skipped`);不交的,整组会点名这些文件、算没过。
75
75
  **testpace 还没内置的测试框架**(目前是 node:test、Vitest、Playwright 以外的),写一个小脚本包一层(比如 `scripts/testpace.sh files a.test.ts b.test.ts:42`),把参数交给测试框架。测试框架能按行跑的(比如 Playwright、Vitest),按 `文件:行号` 交给它,带列号的先去掉列号;不能的,去掉行号和列号跑整个文件,在输出里说一句。很多测试框架把参数当成筛选条件:给了几个文件、其中一个不存在时,只跑找得到的那些,照样报通过。所以脚本要自己检查每个文件都存在、测试框架加载了它、里面有测试,否则返回非零。按条件跳过的测试(比如只在某个系统上跑)算有测试:整组里它们同样被跳过,`files` 不比整组更严;一个文件的测试全被跳过时,在输出里说一句,让人知道它这次什么都没验证。
76
76
  3. **写 `testpace.json`**,放在仓库根目录,进版本库。然后跑 `testpace status` 看合并门那一行:
77
77
  - 说合并门没配:会话开始时 testpace 会自动配上;用户自己删掉过的,问用户要不要跑 `testpace install` 配回去。
@@ -99,7 +99,7 @@ modwall 让 agent 默认按模块开发,越界由程序拦。它不自己解
99
99
  - **认不出来的应用目录要手动补**:不是工作区包、又不在上面几个目录下的应用(比如放在仓库根目录 `miniprogram/` 下的小程序),`modwall init` 不起草,它下面的代码在输出里列为没有归属。照着这个应用的目录在 `apps` 里手动补一组(应用名取目录名,`roots`、`shared`、`app`、`entry`、`map` 按它搬好的目录写)。草稿是单应用写法的,先把它改成按应用分组的写法(原来那一组放进 `apps`,起个应用名),再补上这一组。补完重跑 `modwall check`,直到没有归属的代码文件都归了某个应用、外面一层的 `shared` 或 `outside`。
100
100
  3. **改到清零**:`modwall check` 列出全部越界,当作待办逐条按改法改(R6 是迁移文件里的数据库越界)。每个模块可以补一个 `MODULE.md`(一句职责、一句不做什么)。用 Postgres 的,在临时库上跑完全部迁移后执行 `modwall db check --psql "<执行 SQL 的命令>"`(比如 `psql $DATABASE_URL`;用 napbox 的项目写 `napbox exec postgres --temp $ENV -- psql`),把它报的也改到清零:改迁移或跑迁移的工具,不直接改库。这条命令用哪个账号连库都行,它只读系统目录。
101
101
  4. **整份配置让用户确认一次**:首次接入时 `modwall check` 会把整份配置列为放宽项。先用白话告诉用户:分了哪几个模块(有几个应用时按应用说,还有哪些代码是几个应用共用的)、共享层和装配层是哪些、哪些路径不管,再调用 `confirm_with_user` 工具(弹窗,用户同意才算;编号和文字照 `modwall check` 输出的填)。确认后 `modwall check` 应当通过。
102
- 5. **库的检查进整组**(用 Postgres 的项目):在项目整组(testpace 的 `full`)里,迁移跑完之后加一步 `modwall db check --psql "<执行 SQL 的命令>"`,用测试的临时库;它不通过时退出码非零,整组就不通过。怎么起库、怎么跑迁移仍归项目,modwall 只读系统目录。不接上的话库这一半没人查:`modwall status` 的"数据库"一行会提醒(它只看得到 `testpace.json` 的 `full` 里直接写的命令,写在脚本里的要自己核对)。
102
+ 5. **库的检查进整组**(用 Postgres 的项目):在项目整组(testpace 的 `full`)里,迁移跑完之后加一步 `modwall db check --psql "<执行 SQL 的命令>"`,用测试的临时库;它不通过时退出码非零,整组就不通过。怎么起库、怎么跑迁移仍归项目,modwall 只读系统目录。写在项目的脚本里、在测试里跑都行,但要在整组里跑到、退出码不能被忽略:整组给每段命令一个环境变量 `MODWALL_DB_RECEIPTS`,`modwall db check` 查完往里交一条凭据,整组跑完时写了 `database` 的应用(有几个应用的,每个用 `--app` 各查一次)没交凭据、或没通过却接着跑完了,整组算没过。所以起库、跑迁移的脚本要把环境变量原样传给它(在容器里跑的也要带进去)。
103
103
  6. **核对**:`modwall status` 应当显示 LayerMap 能用、testpace 接入了、没有越界、放宽项都同意过。
104
104
  7. 提交前先问用户;合回默认分支照 testpace 的节奏(`gatelight:merge`)。
105
105
 
@@ -19,6 +19,7 @@ description: 在接入了 testcraft 的仓库(有 .testcraft/ 目录)里写
19
19
  - **测试没过、它认为是业务代码不对的,不改业务代码**:回来说明是哪条规则、实际结果是什么。
20
20
 
21
21
  子代理回来后跑 `testpace focused`,通过了提交。它说业务代码不对的,照"写完"第 1 条办:先停下来告诉用户。
22
+ - **规则写进测试后,从文档里删掉**(这一步自己做,不交给子代理:哪几段写的是这次的规则,做这个分支的会话最清楚)。设计文档、README 里写着这几条规则的段落删掉,只留代码和测试装不下的:为什么这样定、否掉的做法、还没定的、怎么跑。这次定下来的"待确认"一起拿掉;改规则的经过(原来怎样、哪天改成怎样)不留,git 里有。同一条规则写在文档、测试、代码三处,改规则时文档总有漏改的地方,以后的 agent 照着旧说法做;只留在测试里,就只有一处要改。和测试一起提交。
22
23
  - **一个例外,开发中当场做**:修已定规则(已经合进默认分支的)的 bug,先写能重现它的测试,确认它失败,再修。
23
24
 
24
25
  ## 先分档
@@ -88,7 +89,7 @@ description: 在接入了 testcraft 的仓库(有 .testcraft/ 目录)里写
88
89
  - **还在试**:分支上还没合的都算。不写测试,自己临时跑通就行。
89
90
  - **合并时用户说"这块还在试、先合进去"**:那一块先不写测试,合进去以后也还算在试。它之后修过的 bug 记进 `.testcraft/rules/<编号>.md`(一条一个文件,标题写规则,正文写修过的 bug);用户说定了的时候写测试,把记过的 bug 补成测试,然后删掉那条记录。
90
91
  - 修已定规则的 bug:先写能重现它的测试,确认它失败,再修。
91
- - 要改已定的规则(已定的规则以测试名为准;包括为此删测试、让测试不跑、改期望值):先用白话问用户一次,用户同意了再改。用户这次自己提出要改的不用再问。不用另外写记录。
92
+ - 要改已定的规则(已定的规则以测试名为准,文档里的说法和它不一样时也是;包括为此删测试、让测试不跑、改期望值):先用白话问用户一次,用户同意了再改。用户这次自己提出要改的不用再问。不用另外写记录。
92
93
  - 测试上别的调整(跟着代码改接法、换写法、挪位置、删重复的)自己定,不问用户,也不用写说明。
93
94
  - 整块功能不要了:规则和测试一起删。
94
95
 
package/src/base/full.ts CHANGED
@@ -110,8 +110,11 @@ async function run(ctx: Repo, sessionId: string | undefined, ignore: string[], p
110
110
 
111
111
  // 各模块往记录里加自己的字段,记录写下之后再说它们要说的话
112
112
  const finished = parts.map(part => part.finish?.() ?? {})
113
+ // 各段都过了,再按交回来的东西核对:该跑到的没跑到,整组也算没过
114
+ const problems = execution.exitCode === 0 ? finished.flatMap(done => done.problems ?? []) : []
115
+ if (problems.length) appendFileSync(log, `===== testpace 跑完核对没通过\n${problems.join('\n')}\n`)
113
116
  const treeAtEnd = workTreeId(ctx.root)
114
- const result = execution.exitCode === 0 ? 'pass' : 'fail'
117
+ const result = execution.exitCode === 0 && problems.length === 0 ? 'pass' : 'fail'
115
118
  const before = ctx.store.readRuns()
116
119
  const record: RunRecord = {
117
120
  id,
@@ -153,6 +156,10 @@ async function run(ctx: Repo, sessionId: string | undefined, ignore: string[], p
153
156
  )
154
157
  return EXIT.fail
155
158
  }
159
+ if (problems.length) {
160
+ say([`整组失败:各段都过了,跑完核对没通过(该跑到的没跑到)`, ...problems.map(problem => `- ${problem}`), `用时 ${took} · 日志:${log}`].join('\n'))
161
+ return EXIT.fail
162
+ }
156
163
  const what = failed?.title ?? '整组'
157
164
  const gap = /[A-Za-z0-9]$/.test(what) ? ' ' : ''
158
165
  say(failureText(`整组失败:${what}${gap}没通过${failed === steps.at(-1) ? '' : ',后面的没有跑'}`, log, execution, excerptFrom))
package/src/base/git.ts CHANGED
@@ -233,6 +233,13 @@ export function filesAt(root: string, rev: string, dir: string): string[] {
233
233
  return (out ?? '').split('\0').filter(Boolean)
234
234
  }
235
235
 
236
+ /** 这些路径(文件或目录,相对仓库根目录)里,提交里有的那些。 */
237
+ export function pathsAt(root: string, rev: string, paths: readonly string[]): Set<string> {
238
+ if (paths.length === 0) return new Set()
239
+ const out = gitOr(root, ['ls-tree', '-z', '--name-only', rev, '--', ...paths.map(path => path.replace(/\/+$/, ''))])
240
+ return new Set((out ?? '').split('\0').filter(Boolean))
241
+ }
242
+
236
243
  /** 工作区里这个文件现在的内容。只读普通文件(跟着软链走);目录、不存在的都是 undefined。 */
237
244
  export function fileNow(root: string, path: string): string | undefined {
238
245
  const full = join(root, path)
@@ -30,8 +30,10 @@ export interface FullPart {
30
30
  /**
31
31
  * 各段跑完(没被中断)以后:模块往这次的运行记录里加自己的字段(fields),在记录写下之后说给 agent 的话(notes,
32
32
  * 拿得到写好的记录和这之前的全部记录)。
33
+ * problems:按跑完交回来的东西核对出的问题(比如该跑到的没跑到),一条一句、写明怎么改。各段都过了、却有问题的,整组算没过;
34
+ * 有哪一段没过时不看(后面的没跑,核对不出什么)。
33
35
  */
34
- finish?(): { fields?: Record<string, unknown>; notes?(record: RunRecord, before: RunRecord[]): string[] }
36
+ finish?(): { fields?: Record<string, unknown>; problems?: string[]; notes?(record: RunRecord, before: RunRecord[]): string[] }
35
37
  /** 这一轮结束(不管怎么结束)时收拾模块自己的临时文件。 */
36
38
  dispose?(): void
37
39
  }
@@ -10,7 +10,7 @@ export function guide(label = '这个仓库'): string[] {
10
10
  `${label}用 testcraft 管测试的写法(.testcraft/):`,
11
11
  '1. 什么时候写:分支上开发时不写新测试、不改已有的测试(需求还在试),做完告诉用户做完了、测试还没写。用户说要合并这个分支时才写:把这次定下来的规则用白话一条条列出来,交给一个子代理照 gatelight:write-tests 技能写新测试、改掉因为需求变了而不过的旧测试(没有起子代理的工具就自己写),跑通、提交,再合。例外,当场做:修已合进默认分支的规则的 bug,先写能重现它的测试再修。',
12
12
  '2. 写哪些:小改动(文字、样式、注释、显示用的配置)不写。必跑层:钱、权限、状态与数据(什么状态下能做、数据会不会错)写在接口层、测透,每个功能留主流程的页面测试。细节层:界面细节、样式、提示文字这类人容易漏看的,也值得写。修过的 bug 留一条能重现它的测试。在页面上重复验证接口层守着的规则不写。',
13
- '3. 已定的业务规则(合进默认分支的)以测试名为准。要改已定的业务规则(包括为此删测试、让测试不跑、改期望值),先用白话问用户,用户同意了再改;用户这次自己提出要改的不用再问。测试上别的调整(跟着代码改接法、换写法、挪位置、删重复的)自己定,不问用户,也不用写说明。',
13
+ '3. 已定的业务规则(合进默认分支的)以测试名为准。要改已定的业务规则(包括为此删测试、让测试不跑、改期望值),先用白话问用户,用户同意了再改;用户这次自己提出要改的不用再问。测试上别的调整(跟着代码改接法、换写法、挪位置、删重复的)自己定,不问用户,也不用写说明。文档里不另写已定的规则:设计文档只写代码和测试装不下的(为什么这样定、否掉的做法、还没定的、怎么跑),和测试名说法不一样时以测试为准;合并时规则写进了测试,文档里写着这几条规则的段落就删掉。',
14
14
  '4. 改动命中 .testcraft/checklist/ 里条目的触发条件时(testcraft checklist 列出,落地前的整组里也会说一遍),按条目查一遍,查出问题就改好。修 bug 时,写不成测试、又能从改动看出来的坑,加一条清单。',
15
15
  ]
16
16
  }
package/src/pace/full.ts CHANGED
@@ -7,7 +7,7 @@ import { loadConfig } from './config.ts'
7
7
  import { unconfirmedChanges } from './consent.ts'
8
8
  import type { PaceRun } from './record.ts'
9
9
  import { FULL_STEP, TESTS_STEP } from './report.ts'
10
- import { failedTests, noteResults, readResults, RESULTS_ENV, resultsFile } from './results.ts'
10
+ import { failedTests, noteResults, readResults, RESULTS_ENV, resultsFile, type TestResult } from './results.ts'
11
11
  import { findTests } from './tests.ts'
12
12
 
13
13
  export function fullPart(repo: Repo): FullPart | { problem: string } {
@@ -19,10 +19,11 @@ export function fullPart(repo: Repo): FullPart | { problem: string } {
19
19
  const unconfirmed = unconfirmedChanges(repo)
20
20
  if (unconfirmed) return { problem: unconfirmed }
21
21
  const resultsAt = resultsFile()
22
+ const files = config.files
22
23
  return {
23
24
  steps: [
24
25
  { kind: 'full', name: FULL_STEP, title: FULL_STEP, header: `${FULL_STEP}:${config.full}`, command: config.full, args: [] },
25
- { kind: 'tests', name: TESTS_STEP, title: `${TESTS_STEP}(${tests.length} 个文件)`, header: `${TESTS_STEP}(${tests.length} 个文件):${config.files}`, command: config.files, args: tests },
26
+ { kind: 'tests', name: TESTS_STEP, title: `${TESTS_STEP}(${tests.length} 个文件)`, header: `${TESTS_STEP}(${tests.length} 个文件):${files}`, command: files, args: tests },
26
27
  ],
27
28
  summary: `${tests.length} 个测试文件`,
28
29
  env: { [RESULTS_ENV]: resultsAt.path },
@@ -30,9 +31,29 @@ export function fullPart(repo: Repo): FullPart | { problem: string } {
30
31
  const results = readResults(resultsAt.path, repo.root)
31
32
  return {
32
33
  fields: { tests: tests.length, ...(results.length > 0 ? { failed: failedTests(results) } : {}) },
34
+ problems: silentFiles(tests, results, files),
33
35
  notes: (record, before) => noteResults(repo, { record: record as PaceRun, results, before: before as PaceRun[], whole: tests }),
34
36
  }
35
37
  },
36
38
  dispose: () => resultsAt.dispose(),
37
39
  }
38
40
  }
41
+
42
+ /** 一次最多点名几个没交回结果的文件。 */
43
+ const NAMED = 10
44
+
45
+ /**
46
+ * 交给 files 的每个测试文件都要交回至少一条结果(过、没过、跳过都算)。不读脚本:脚本漏跑了哪一类测试,结果里就少了那些文件。
47
+ * 一条结果都没交回的,是 files 整个没经 testpace run 跑、也没自己写结果。
48
+ */
49
+ export function silentFiles(tests: readonly string[], results: readonly TestResult[], files: string): string[] {
50
+ const reported = new Set(results.map(test => test.file))
51
+ const silent = tests.filter(file => !reported.has(file))
52
+ if (silent.length === 0) return []
53
+ const how = `node:test、Vitest、Playwright 的测试经 testpace run 跑(它会交回结果);别的跑法自己往环境变量 ${RESULTS_ENV} 给的文件里,每个测试追加一行 JSON:{"file":"文件的绝对路径","name":"测试名","status":"passed、failed 或 skipped"}`
54
+ if (results.length === 0) return [`files 命令(${files})跑完,一个测试结果都没交回,整组核对不了跑到了哪些测试。${how}。`]
55
+ const named = silent.slice(0, NAMED).join('、') + (silent.length > NAMED ? ` 等 ${silent.length} 个` : '')
56
+ return [
57
+ `交给 files 命令(${files})的测试文件里,这些一条结果都没交回:${named}。可能是 files 的脚本没把它们交给测试框架、被框架的配置排除了,或跑它们的那一路没交结果。${how}。`,
58
+ ]
59
+ }
package/src/wall/check.ts CHANGED
@@ -6,19 +6,23 @@ import { readFileSync, writeFileSync } from 'node:fs'
6
6
  import { join } from 'node:path'
7
7
  import { askUser } from '../base/confirm.ts'
8
8
  import { EXIT } from '../base/exit.ts'
9
+ import { pathsAt } from '../base/git.ts'
9
10
  import type { Repo } from '../base/repo.ts'
10
11
  import { CONFIG_PATH } from './config.ts'
11
12
  import { unreadSql } from './database.ts'
12
13
  import { codeRoleSwitches, migrationViolations } from './dbrules.ts'
13
14
  import { type ChangeFacts, type ProjectImports, readChanges, readImports } from './layermap.ts'
14
15
  import { miniprogramRefs } from './miniprogram.ts'
15
- import type { Layout } from './layout.ts'
16
+ import type { Layout, Module } from './layout.ts'
16
17
  import { looseningItems } from './loosen.ts'
17
18
  import { layoutOf, type Project } from './project.ts'
19
+ import { statements } from './sql.ts'
18
20
  import {
19
21
  BLOCKING_RULES,
20
22
  behaviorHints,
21
23
  entryExports,
24
+ existingChangeHints,
25
+ type MigrationAlter,
22
26
  type Hint,
23
27
  importViolations,
24
28
  RULE_NAMES,
@@ -121,13 +125,14 @@ export function analyze(project: Project, repo: Repo): Report {
121
125
  }
122
126
  let changes: ChangeFacts | undefined
123
127
  if (project.base && imports.ok) {
124
- const read = readChanges(project.root, project.base.commit)
125
- if (read.ok) {
126
- changes = read.value
128
+ const changed = readChanges(project.root, project.base.commit)
129
+ if (changed.ok) {
130
+ changes = changed.value
127
131
  report.behavior = behaviorHints(layout, changes)
132
+ report.behavior.push(...existingChangeHints(layout, changes, freshModules(project.root, project.base.commit, layout), migrationAlters(project.root, project.base.commit, layout, read)))
128
133
  report.entryExports = entryExports(layout, changes)
129
134
  } else {
130
- report.changesProblem = read.problem
135
+ report.changesProblem = changed.problem
131
136
  }
132
137
  }
133
138
  report.unchecked = [
@@ -138,6 +143,21 @@ export function analyze(project: Project, repo: Repo): Report {
138
143
  return report
139
144
  }
140
145
 
146
+ /** 这次新建的模块:分叉点上还没有它的目录。 */
147
+ function freshModules(root: string, base: string, layout: Layout): Set<string> {
148
+ const existed = pathsAt(root, base, layout.modules.map(module => module.dir))
149
+ return new Set(layout.modules.filter(module => !existed.has(module.dir)).map(module => module.id))
150
+ }
151
+
152
+ /** 已有模块这次新加的迁移里,改、删已有对象的句子(函数正文里的不算)。 */
153
+ function migrationAlters(root: string, base: string, layout: Layout, read: (path: string) => string): MigrationAlter[] {
154
+ const owned = layout.databases.flatMap(database => database.migrations).filter((migration): migration is { path: string; module: Module } => migration.module !== undefined)
155
+ const existed = pathsAt(root, base, owned.map(migration => migration.path))
156
+ return owned
157
+ .filter(migration => !existed.has(migration.path))
158
+ .flatMap(({ path, module }) => statements(read(path)).filter(statement => statement.alters && !statement.inFunction).map(statement => ({ module, path, line: statement.line })))
159
+ }
160
+
141
161
  /** R1–R3、R5、R6 都查了:配置能用、layermap imports 跑成了。只有这样的检查,越界列表才完整。 */
142
162
  function complete(report: Report): boolean {
143
163
  return report.configProblems.length === 0 && !report.removed && report.layermapProblem === undefined
@@ -12,6 +12,7 @@ import { openRepo, type Repo } from '../base/repo.ts'
12
12
  import { analyze, exitCode, formatReport, logCheck, prepare, type Report, violationLines } from './check.ts'
13
13
  import { CONFIG_PATH, MODWALL_DIR, schemeOf } from './config.ts'
14
14
  import { catalogViolations, readCatalog } from './dbcheck.ts'
15
+ import { leaveReceipt } from './receipt.ts'
15
16
  import { draftConfig } from './init.ts'
16
17
  import type { Module } from './layout.ts'
17
18
  import { configText } from './loosen.ts'
@@ -153,6 +154,7 @@ function dbCheck(args: string[]): number {
153
154
  }
154
155
  const violations = catalogViolations(database, catalog.value)
155
156
  const which = database.app.name === undefined ? '' : `(${database.app.name} 应用的库)`
157
+ leaveReceipt(database.app.name, violations.length === 0)
156
158
  if (violations.length === 0) {
157
159
  console.log(`modwall db check:通过${which}。库里没有越界。`)
158
160
  return EXIT.ok
package/src/wall/full.ts CHANGED
@@ -1,6 +1,6 @@
1
1
  // 整组里"模块边界"的部分(base/module.ts 的 fullPart):接入了的仓库在测试之前跑一次 modwall check。
2
2
  // 它随插件一起装,不用在本机另外登记;没接入的仓库没有这一段。
3
- // 库里的现状(modwall db check)不在这一段:要迁移跑完才能查,由项目写在自己的 full 命令里。
3
+ // 库里的现状(modwall db check)不在这一段:要迁移跑完才能查,由项目写在自己的 full 命令里。整组跑完按它交回的凭据核对跑没跑(receipt.ts)。
4
4
 
5
5
  import { fileURLToPath } from 'node:url'
6
6
  import { quoted } from '../base/execute.ts'
@@ -8,6 +8,7 @@ import { forkPoint } from '../base/git.ts'
8
8
  import type { FullPart } from '../base/module.ts'
9
9
  import type { Repo } from '../base/repo.ts'
10
10
  import { joined } from './config.ts'
11
+ import { DB_RECEIPTS_ENV, receiptProblems, receiptsFile } from './receipt.ts'
11
12
 
12
13
  /** 这个插件里的 modwall 命令:用完整路径,不靠 PATH 上碰巧有哪一版。 */
13
14
  const BIN = fileURLToPath(new URL('../../bin/modwall', import.meta.url))
@@ -15,7 +16,11 @@ const BIN = fileURLToPath(new URL('../../bin/modwall', import.meta.url))
15
16
  export function fullPart(repo: Repo): FullPart {
16
17
  // 按分叉点也算接入:删掉 .modwall/ 躲不过检查
17
18
  if (!joined(repo.root, forkPoint(repo.root).commit)) return { steps: [] }
19
+ const receipts = receiptsFile()
18
20
  return {
21
+ env: { [DB_RECEIPTS_ENV]: receipts.path },
22
+ finish: () => ({ problems: receiptProblems(repo.root, receipts.path) }),
23
+ dispose: () => receipts.dispose(),
19
24
  steps: [
20
25
  {
21
26
  kind: 'wall',
@@ -0,0 +1,73 @@
1
+ // modwall db check 在整组里交回的凭据。它由项目写进自己的 full 命令(迁移跑完才能查),脚本里删掉那一行,库的模块边界就不查了。
2
+ // 所以不读脚本,按凭据核对:整组给各段命令一个环境变量(DB_RECEIPTS_ENV),db check 查完往那个文件追加一行(查的是哪个应用、过没过);
3
+ // 整组跑完,配置里每个写了 database 的应用都要有凭据、而且都过了。只防看错:凭据是 agent 写得到的文件,故意伪造拦不住。
4
+
5
+ import { appendFileSync, mkdtempSync, readFileSync, rmSync } from 'node:fs'
6
+ import { tmpdir } from 'node:os'
7
+ import { join } from 'node:path'
8
+ import { fileNow } from '../base/git.ts'
9
+ import { CONFIG_PATH, readConfig, schemeOf } from './config.ts'
10
+
11
+ export const DB_RECEIPTS_ENV = 'MODWALL_DB_RECEIPTS'
12
+
13
+ /** 一次 db check 的凭据。app:查的是哪个应用的库(单应用的写法没有名字,为 null)。 */
14
+ export interface DbReceipt {
15
+ app: string | null
16
+ passed: boolean
17
+ }
18
+
19
+ /** 给一次整组准备收凭据的文件。用完调 dispose。 */
20
+ export function receiptsFile(): { path: string; dispose: () => void } {
21
+ const dir = mkdtempSync(join(tmpdir(), 'modwall-db-receipts-'))
22
+ return { path: join(dir, 'receipts.jsonl'), dispose: () => rmSync(dir, { recursive: true, force: true }) }
23
+ }
24
+
25
+ /** db check 查完时调:在整组里跑的(有那个环境变量),交一条凭据。 */
26
+ export function leaveReceipt(app: string | undefined, passed: boolean): void {
27
+ const path = process.env[DB_RECEIPTS_ENV]
28
+ if (path) appendFileSync(path, `${JSON.stringify({ app: app ?? null, passed } satisfies DbReceipt)}\n`)
29
+ }
30
+
31
+ function readReceipts(path: string): DbReceipt[] {
32
+ let text: string
33
+ try {
34
+ text = readFileSync(path, 'utf8')
35
+ } catch {
36
+ return []
37
+ }
38
+ return text.split('\n').flatMap(line => {
39
+ try {
40
+ const value = JSON.parse(line) as Partial<DbReceipt>
41
+ return typeof value.passed === 'boolean' && (value.app === null || typeof value.app === 'string') ? [{ app: value.app, passed: value.passed }] : []
42
+ } catch {
43
+ return [] // 空行、写到一半的
44
+ }
45
+ })
46
+ }
47
+
48
+ /** 工作区的配置里写了 database 的应用(单应用的写法为 null)。配置有问题的不算:整组里的 modwall check 已经报了。 */
49
+ function databaseApps(root: string): Array<string | null> {
50
+ const { config } = readConfig(fileNow(root, CONFIG_PATH))
51
+ if (!config) return []
52
+ return schemeOf(config).apps.filter(app => app.database).map(app => app.name ?? null)
53
+ }
54
+
55
+ /** 整组跑完:写了 database 的应用,哪个没交凭据、哪个没过却没让整组失败。一条一句、写明怎么改。 */
56
+ export function receiptProblems(root: string, path: string): string[] {
57
+ const receipts = readReceipts(path)
58
+ const problems: string[] = []
59
+ const apps = databaseApps(root)
60
+ for (const app of apps) {
61
+ // 只有一个应用写了 database 时,db check 不用 --app 也查它
62
+ const which = app === null ? '' : apps.length > 1 ? `(${app} 应用的库,用 --app ${app})` : `(${app} 应用的库)`
63
+ const mine = receipts.filter(receipt => receipt.app === app)
64
+ if (mine.length === 0) {
65
+ problems.push(
66
+ `整组里没跑 modwall db check${which}:${CONFIG_PATH} 写了 database,项目的 full 命令要在迁移跑完后跑它,查库里的模块边界(写法见 gatelight:setup-modwall 技能)。`,
67
+ )
68
+ } else if (mine.some(receipt => !receipt.passed)) {
69
+ problems.push(`modwall db check${which}没通过,整组却接着跑完了:项目的 full 命令里不要忽略它的退出码(比如 || true),没通过要让整组失败。`)
70
+ }
71
+ }
72
+ return problems
73
+ }
package/src/wall/rules.ts CHANGED
@@ -3,7 +3,8 @@
3
3
  // R2 循环 模块之间有回环 拦
4
4
  // R3 方向反了 共享层引用模块或装配层;模块引用装配层;被管的代码引用 outside 或测试文件; 拦
5
5
  // 一个应用引用另一个应用的代码;外面一层的共享层引用任何应用的代码
6
- // R4 改已有行为 改了 B 已有导出函数的实现,A 在这次改动里新增了对它的调用 只提示
6
+ // R4 改已有行为 改了 B 已有导出函数的实现,A 在这次改动里新增了对它的调用; 只提示
7
+ // 这次新建了模块,同时改了已有模块里已有的声明、在它的新迁移里改删已有的对象
7
8
  // R5 没有归属 代码文件不在模块、共享层、装配层、映射和 outside 里 拦
8
9
  // R6 数据库越界 迁移文件那部分在 database.ts,库里的现状在 dbcheck.ts 拦
9
10
  // R1、R2、R3、R5、R6 看项目当前的全部代码;R4 和"入口新增导出"看这次改动(和分叉点比)。
@@ -17,7 +18,7 @@ import { type App, type ExceptionRule, field, moduleId, type Scheme } from './co
17
18
  import { posix } from 'node:path'
18
19
  import { inEntryLanguage, isCode, isStylesheet, UNREAD_DIRECTORIES } from './files.ts'
19
20
  import type { ChangeFacts, ProjectImports } from './layermap.ts'
20
- import { label, type Layout, type Owner } from './layout.ts'
21
+ import { label, type Layout, type Module, type Owner } from './layout.ts'
21
22
 
22
23
  export type Rule = 'R1' | 'R2' | 'R3' | 'R5' | 'R6'
23
24
 
@@ -338,19 +339,28 @@ export function unownedFiles(layout: Layout, files: readonly string[]): Violatio
338
339
  })
339
340
  }
340
341
 
342
+ type ChangedDeclaration = ChangeFacts['declarations'][number]
343
+
344
+ /** 这次改动里,同一个应用里别的模块新增的、对这个已有导出声明的调用(应用之间的调用另由 R3 拦)。 */
345
+ function newCallsFromOthers(layout: Layout, changes: ChangeFacts, declaration: ChangedDeclaration): ChangeFacts['calls'] {
346
+ const owner = layout.owner(declaration.path)
347
+ if (!declaration.exported || owner.kind !== 'module') return []
348
+ return changes.calls.filter(call => {
349
+ if (call.to !== declaration.path || call.declaration !== declaration.declaration) return false
350
+ const caller = layout.owner(call.from)
351
+ return caller.kind === 'module' && caller.app === owner.app && caller.name !== owner.name
352
+ })
353
+ }
354
+
341
355
  /** R4:这次改了 B 模块已有的导出函数的实现,同时 A 模块在这次改动里新增了对它的调用。只提示。 */
342
356
  export function behaviorHints(layout: Layout, changes: ChangeFacts): Hint[] {
343
357
  const hints: Hint[] = []
344
358
  for (const declaration of changes.declarations) {
345
- if (!declaration.exported) continue
346
359
  const owner = layout.owner(declaration.path)
347
360
  if (owner.kind !== 'module') continue
348
361
  const callee = moduleId(owner.app, owner.name)
349
- for (const call of changes.calls) {
350
- if (call.to !== declaration.path || call.declaration !== declaration.declaration) continue
351
- // 只看同一个应用里别的模块:应用之间的调用另由 R3 拦
352
- const caller = layout.owner(call.from)
353
- if (caller.kind !== 'module' || caller.app !== owner.app || caller.name === owner.name) continue
362
+ for (const call of newCallsFromOthers(layout, changes, declaration)) {
363
+ const caller = layout.owner(call.from) as Extract<Owner, { kind: 'module' }>
354
364
  hints.push({
355
365
  where: `${callee} 模块的 ${declaration.declaration}(${declaration.path}:${declaration.line})这次改了实现,${moduleId(caller.app, caller.name)} 模块在 ${site(call)} 新增了对它的调用`,
356
366
  fix: `为新功能就在 ${callee} 新增一个函数,不要给旧函数加分支;是正常的重构(比如改了签名、同时更新调用处)就不用管。`,
@@ -360,6 +370,52 @@ export function behaviorHints(layout: Layout, changes: ChangeFacts): Hint[] {
360
370
  return hints
361
371
  }
362
372
 
373
+ /** 新迁移里改、删已有对象的一句:所在的模块、位置。 */
374
+ export interface MigrationAlter {
375
+ module: Module
376
+ path: string
377
+ line: number
378
+ }
379
+
380
+ /**
381
+ * R4 的另一种:这次新建了模块,同一个应用里已有的模块又被改了已有的东西(声明、迁移里改删已有的对象)。
382
+ * 新模块要的应该在对方新增;改对方已有的,常是在对方的名单、类型、常量、约束、映射表里加上新模块的名字,
383
+ * 对方因此认识了用它的模块。这种依赖写在字符串里,看引用查不出来,模块之间经装配层接线时也没有调用可查,所以按"改了已有的"提示。
384
+ * 已经按调用提示过的(上面的 behaviorHints)不再列。
385
+ */
386
+ export function existingChangeHints(layout: Layout, changes: ChangeFacts, fresh: ReadonlySet<string>, alters: readonly MigrationAlter[]): Hint[] {
387
+ const freshIn = (app: string | undefined): string[] => layout.modules.filter(module => module.app === app && fresh.has(module.id)).map(module => module.id)
388
+ const touched = new Map<string, { app: string | undefined; id: string; where: string[] }>()
389
+ // 同一处(key)只说一次,位置写第一次见到的
390
+ const seen = new Set<string>()
391
+ const add = (app: string | undefined, id: string, key: string, where: string): void => {
392
+ if (seen.has(key)) return
393
+ seen.add(key)
394
+ const found = touched.get(id) ?? { app, id, where: [] }
395
+ found.where.push(where)
396
+ touched.set(id, found)
397
+ }
398
+ for (const declaration of changes.declarations) {
399
+ const owner = layout.owner(declaration.path)
400
+ if (owner.kind !== 'module') continue
401
+ const id = moduleId(owner.app, owner.name)
402
+ if (fresh.has(id) || freshIn(owner.app).length === 0) continue
403
+ // 对象字面量的属性报成 PAGES.deposit 这样,同一个文件里按顶层的名字说一次
404
+ const name = declaration.declaration.split('.')[0] as string
405
+ if (newCallsFromOthers(layout, changes, declaration).length) continue
406
+ add(owner.app, id, `${declaration.path}\0${name}`, `${name}(${declaration.path}:${declaration.line})`)
407
+ }
408
+ // 一个迁移文件只说一次,位置写第一句
409
+ for (const alter of alters) {
410
+ if (fresh.has(alter.module.id) || freshIn(alter.module.app).length === 0) continue
411
+ add(alter.module.app, alter.module.id, alter.path, `迁移里改、删了已有的对象(${alter.path}:${alter.line})`)
412
+ }
413
+ return [...touched.values()].map(({ app, id, where }) => ({
414
+ where: `这次新建了 ${freshIn(app).join('、')} 模块,同时改了已有的 ${id} 模块里已有的:${where.join('、')}`,
415
+ fix: `新模块要的在 ${id} 新增,不改它已有的。是在 ${id} 的名单、类型、常量、约束、映射表里加上新模块的名字的(${id} 因此认识了用它的模块),改成 ${id} 收一个值,或者由装配层接进来;是正常的重构就不用管。`,
416
+ }))
417
+ }
418
+
363
419
  /** 这次改动给哪些模块的入口新增了引用(转导出也算 import):不算违规,留作试点数据。 */
364
420
  export function entryExports(layout: Layout, changes: ChangeFacts): Hint[] {
365
421
  return changes.imports
@@ -121,7 +121,7 @@ function singleRules(scheme: Scheme): string[] {
121
121
  return [
122
122
  `- 模块根目录:${or(app.roots)},下面每个子目录是一个模块。新功能默认在模块根目录下新建一个模块目录,不用登记、不用问人。`,
123
123
  `- 要别的模块的能力,只从它的入口(模块目录下第一层的 ${app.entry.join('、')})引用;入口里没有,就在对方入口新增导出,作为单独的一小步。`,
124
- '- 不给别的模块的已有函数加分支:为新功能在对方新增一个函数。模块之间不能成环。',
124
+ '- 不给别的模块的已有函数加分支:为新功能在对方新增一个函数。不在别的模块的名单、类型、常量、映射表、数据库约束里写上新功能的名字:改成对方收一个值,或由装配层接进来。模块之间不能成环。',
125
125
  `- 共享层(${or(app.shared)})只放真正通用的东西,不能引用模块和装配层;装配层(${or(app.app)})把各模块接起来,别人不能引用它。正式代码不引用 outside(${or(scheme.outside)})和测试文件。`,
126
126
  ...databaseLines(scheme),
127
127
  ...CHECK_LINES,
@@ -135,7 +135,7 @@ function groupedRules(scheme: Scheme): string[] {
135
135
  `- 这个仓库有 ${scheme.apps.length} 个应用(${scheme.apps.map(app => app.name).join('、')}),每个应用各有一套模块,模块名只在应用内唯一,写成 应用名/模块名。应用之间不能引用代码(引用对方的模块、共享层、装配层都不行):几个应用共用的代码放进外面一层的共享层(${or(scheme.shared)}),它不能引用任何应用的代码;应用之间通过接口打交道。`,
136
136
  `- 模块根目录(${each(app => or(app.roots))}),下面每个子目录是一个模块。新功能默认在所在应用的模块根目录下新建一个模块目录,不用登记、不用问人。`,
137
137
  `- 要同一个应用里别的模块的能力,只从它的入口(模块目录下第一层的文件,${each(app => or(app.entry))})引用;入口里没有,就在对方入口新增导出,作为单独的一小步。`,
138
- '- 不给别的模块的已有函数加分支:为新功能在对方新增一个函数。同一个应用里的模块之间不能成环。',
138
+ '- 不给别的模块的已有函数加分支:为新功能在对方新增一个函数。不在别的模块的名单、类型、常量、映射表、数据库约束里写上新功能的名字:改成对方收一个值,或由装配层接进来。同一个应用里的模块之间不能成环。',
139
139
  `- 每个应用的共享层(${each(app => or(app.shared))})只放这个应用里真正通用的东西,不能引用模块和装配层;装配层(${each(app => or(app.app))})把这个应用的各模块接起来,别人不能引用它。正式代码不引用 outside(${or(scheme.outside)})和测试文件。`,
140
140
  ...databaseLines(scheme),
141
141
  ...CHECK_LINES,
package/src/wall/sql.ts CHANGED
@@ -11,6 +11,7 @@
11
11
  // setsSearchPath 这句改了 search_path(SET、RESET search_path,set_config('search_path', …))
12
12
  // switchesRole 这句切了身份(SET ROLE、RESET ROLE、SET SESSION AUTHORIZATION、set_config('role', …),也包括角色、库上设的 role;建函数、改函数时设的不算)
13
13
  // schemas 这句建的 schema(CREATE SCHEMA)
14
+ // alters 这句改、删已有的对象(ALTER、DROP、CREATE OR REPLACE 开头)
14
15
  // 不解析完整语法:切词(注释、字符串、带引号的名字、$$ 正文)后按分号分句,在句子里认几种固定的写法。
15
16
  // 函数、DO 块的正文当成 SQL 再读一遍,得到的句子单独列出(别名只在自己的句子里起作用)。
16
17
  // 拼出来再执行的 SQL(EXECUTE '...')是字符串,读不到。
@@ -58,6 +59,8 @@ export interface Statement {
58
59
  switchesRole: boolean
59
60
  /** CREATE SCHEMA 建的 schema。 */
60
61
  schemas: Name[]
62
+ /** 改、删已有的对象:ALTER、DROP、CREATE OR REPLACE 开头的句子。 */
63
+ alters: boolean
61
64
  }
62
65
 
63
66
  const WORD_START = /[A-Za-z_\u0080-\uffff]/
@@ -206,11 +209,12 @@ const KINDS = new Set(['table', 'view', 'function', 'procedure', 'sequence', 'ty
206
209
  const MODIFIERS = new Set(['or', 'replace', 'global', 'local', 'temp', 'temporary', 'unlogged', 'foreign', 'recursive', 'materialized', 'unique', 'concurrently'])
207
210
 
208
211
  function statement(tokens: Token[]): Statement[] {
209
- const found: Statement = { line: tokens[0]?.line ?? 0, refs: [], targets: [], writes: [], tables: [], view: false, aliases: [], relations: [], setsSearchPath: false, switchesRole: false, schemas: [], inFunction: false }
212
+ const found: Statement = { line: tokens[0]?.line ?? 0, refs: [], targets: [], writes: [], tables: [], view: false, aliases: [], relations: [], setsSearchPath: false, switchesRole: false, schemas: [], inFunction: false, alters: false }
210
213
  const nested: Statement[] = []
211
214
  const creates = isWord(tokens[0], 'create')
212
215
  let at = 1
213
216
  if (creates && isWord(tokens[at], 'or') && isWord(tokens[at + 1], 'replace')) at += 2
217
+ found.alters = isWord(tokens[0], 'alter', 'drop') || at === 3
214
218
  const isFunction = creates && isWord(tokens[at], 'function', 'procedure')
215
219
  // DO 块的正文是句子里第一个字符串(DO $$…$$、DO LANGUAGE x '…')
216
220
  let doBody = isWord(tokens[0], 'do')