Repository navigation
Commit 4f83db5
Fixes #20686
Clause-②: no
The `pure-schema-construction` build plugin
(`packages/spec/tsup.config.ts`) now marks real calls of `lazySchema` /
`strictObject` / `defineForm` only, and never touches text. The
published `@objectstack/spec/migrations` bundle carries the source's
text again. The one affected D3 entry,
`18.dashboard-widget-stage-order-non-funnel-refused`, has no text
change.
## What changed
- **`annotatePureCalls(source, fileName)`** replaces the line-prefix
rule. The TypeScript parser drives the scanner. Every `CallExpression`
whose callee is one of the three names gets the PURE annotation at the
start of the call, whether the callee is bare (`lazySchema(…)`) or a
property-access member (`z.strictObject(…)`). String and template text,
comments, JSDoc and declarations are not call expressions, so they are
never touched. A call inside a template substitution is code, and it is
marked (that behaviour is decided here and pinned). The output is the
source plus inserted annotations, and nothing else.
- The esbuild `Plugin` type is now read off tsup's
`Options['esbuildPlugins']`. This package has no direct `esbuild`
dependency, so a bare `'esbuild'` type import resolves for no type
checker. `ts.resolveModuleName` from this file answers UNRESOLVED. The
new test puts this file in front of `tsconfig.scripts.json`.
- `pureSchemaConstruction` and `mainConfig` are named exports, for the
test. tsup reads only the default export.
- New test `packages/spec/scripts/pure-schema-construction.test.ts`, in
the `local` project (6 cases). It adds no gate.
- `.changeset/20686-pure-plugin-skips-strings.md`: `@objectstack/spec`
patch.
## Why a parse and not a bare `ts.createScanner` pass
Whether `/` starts a regex literal and whether `}` resumes a template
are grammar questions. The scanner answers them only when the parser
asks it to rescan. Measured over `packages/spec/src` at 3693a1b (1774
files, test files excluded): a `createScanner` loop without those
rescans reports 1538 calls where the syntax tree has 1727. It loses 191
real calls, because every regex literal that contains a quote is
mis-lexed. It also marks 2 mentions inside comments: one in the
`defineForm` JSDoc `@example` (`ui/view.zod.ts:7013`) and one in a line
comment (`:7042`). So the positions are read off the syntax tree that
the parser builds with that same scanner. Parse cost: the new rule
parses the 249 files that name a marked constructor (the old rule
rewrote 212). The census that ran both rules over all of `src` took 1.2
s.
## Annotation census
**Source level** (insertion positions over `packages/spec/src` non-test
files, old rule vs new):
| | old rule | new |
|---|---|---|
| insertions | 1730 | 1727 |
The old rule's extra or misplaced insertions, all in non-call positions:
- `migrations/registry.ts:8608`: **inside a string** (the
`acceptanceCriteria` of
`dashboard-widget-stage-order-non-funnel-refused`). This one shipped.
-
`migrations/entries/semantic/18.dashboard-widget-stage-order-non-funnel-refused.ts:59`:
the same string in the entry source. No bundle imports the entry file.
Only the generated registry copy is bundled.
- `ui/view.zod.ts:7032`: `export function defineForm(`, a
**declaration**. esbuild drops a comment there, so it has no effect on
the bundles.
- `data/driver/turso.zod.ts:469`: `z.strictObject({`. The annotation
**moved** from after the `z.` to the start of the call. See the note
below.
No new position exists beyond that move. Real calls with a type argument
or a space before the paren: 0 in `src`. So including them changes
nothing today.
**Bundle level** (tsup JS pass, all 48 `.js` / `.mjs` bundles, base
plugin vs fixed plugin, same source tree):
| | before | after |
|---|---|---|
| PURE annotations, all bundles | 17202 | 17220 (+18) |
| `migrations/index.js` / `.mjs` | 82 / 82 | 81 / 81 (the in-string one
removed) |
| the 20 bundles that reach the Turso schema (10 entries, ESM and CJS) |
n | n + 1 each (the moved member-call annotation) |
| the marker inside `` z.strictObject(DashboardWidgetSchema.shape) `` |
2 | 0 |
| bundles differing once every annotation is stripped | | 0 of 48 |
So the published code is byte-identical apart from annotations: −2 in
the string and +20 at the one member call.
**The member-call annotation (bounded in-place fix).** esbuild 0.28.1
(the version tsup resolves) drops an annotation written after a member
access's dot. Measured on an input carrying both spellings: an unused
`const` initialised from `z.` + PURE + `strictObject({…})` is kept, and
its PURE annotation is gone from the output. An unused `const`
initialised from PURE + `z.strictObject({…})` is dropped. So the old
rule's output at `turso.zod.ts:469` marked nothing. The new rule puts
the annotation where esbuild honours it, because it marks call
expressions. It is the same defect class (the plugin wrote an annotation
at a token that is not a call start). The fix is mechanical, pinned by
the fixture test, in the file this claim holds, and adds no verification
surface.
## Tree-shake control
Measured with a consumer bundle per row: esbuild, `bundle`, `minify`,
`treeShaking`, `zod` external. Sizes are bytes against the before tree,
the after tree, and the before tree with every annotation stripped (what
the plugin buys):
| consumer imports | before | after | no annotations |
|---|---|---|---|
| `FieldSchema` from `./data` | 231,683 | 231,683 | 551,411 |
| `TursoConfigSchema` from `./data` | 173,044 | 173,044 | 551,411 |
| `defineStack` from the root | 954,689 | 954,689 | 968,107 |
| `MIGRATION_SUPPORT_FLOOR` from `./migrations` | 1,548,424 | 1,548,408
| 1,548,521 |
Tree-shaking is unchanged. The −16 bytes on `./migrations` is the
16-character marker that left the string.
## The test, and how it reads the build
No spec test reads the real `packages/spec/dist`. Only gates do, and
they run after the build step in `lint.yml`. turbo's
`@objectstack/spec#test` depends on `^build` only, and spec has no
workspace dependency. So in CI, `dist` is either absent or built in
parallel when the suite runs. A test that read it would be skipped or
racy there. This test therefore runs the real build itself. It calls
tsup's own `build()` with the exported `mainConfig` (the plugin, target
and treeshake included) over `src/migrations/index.ts`. It writes into a
temp directory under the package's `node_modules/.cache` (so the
bundle's `zod` import resolves), imports the bundle and compares:
- `MIGRATIONS_BY_MAJOR`, `MIGRATION_MAJORS`, `MIGRATION_SUPPORT_FLOOR`,
`RETIRED_KEYS_BY_MAJOR` and `RETIRED_DEFS_BY_MAJOR` must equal the
source values exactly. Markers are **not** stripped before comparing:
stripped, the buggy build compares equal too. A control requires at
least one annotation in that bundle, so the equality cannot hold
vacuously for a build that ran no plugin.
- A fixture quotes every marked name in a single-quoted and a
double-quoted string, template text, a template substitution, a regex
literal, JSDoc, and trailing block and line comments, next to a
declaration and the three real call shapes. It is built through the same
`build()`. Each string survives byte-identical, and the real calls carry
the annotation at the call start.
- Four `annotatePureCalls` cases pin positions directly. They cover the
unparsed hand-back, untouched non-calls, every call shape, and that
removing the insertions gives the source back.
## Ablation
The fix was committed first (`7189a7c485`). `node
scripts/ablation-replace.mjs` replaced the plugin's call to
`annotatePureCalls` with the old line-prefix rule. The anchor went from
x1 to x0, and the blob from `7adc1d65404d` to `e73d50e2a72e`. The run,
under the verify lock:
- **2 failed | 4 passed.** The built `MIGRATIONS_BY_MAJOR` differs from
the source at entry `dashboard-widget-stage-order-non-funnel-refused`,
field `acceptanceCriteria`: the live entry.
- The fixture build fails with esbuild `Unexpected "export"`. The old
rule wrote an annotation inside a trailing block comment and closed it
early. That is the comment-in-comment hazard the old docblock cites.
- The four `annotatePureCalls` cases stay green, as expected: they call
the function, not the plugin.
The restore was proven by the tool: blob after restore = blob at HEAD =
`7adc1d65404d`, and `git diff HEAD` is empty. No `dist` is involved. The
test imports the plugin from source and builds in-test, so there was no
rebuild leg.
## Verification (at `7272847e14`: the branch merged with `origin/main`
`5f6b63a6fd`)
- `pnpm --filter @objectstack/spec build`: green. `dist` has 0 in-string
markers and 17220 annotations.
- `vitest run scripts/pure-schema-construction.test.ts`: 6 passed.
- `pnpm --filter @objectstack/spec test`: 585 files passed, 17211 tests
passed, 1 todo.
- `pnpm --filter @objectstack/spec typecheck`: green. `tsc -p
tsconfig.scripts.json --listFiles` includes the new test and
`tsup.config.ts`.
- `dispatch-gates --commands --repo objectstack-ai/objectstack`: 67
derived. `--ran`: 65 run, all exit 0, and **2 NOT MEASURED** (exit 3,
PREREQUISITE NOT MET):
- `check:dual-build-cjs-loads` needs the `dist` of every workspace
package (86 named). Spec's own half was measured directly: all 24
`require` export conditions of `@objectstack/spec` load.
- `check:type-check-debt` needs 30 workspace dependencies built.
`@objectstack/spec` is graduated from that ledger, and its own
`typecheck` is green above.
- Both are declared narrowings, and CI runs them.
- `check:lean-entry-closure` ran against an objectql closure rebuilt at
this head. `check:query-options-erasure` ran in 307 s.
- After this head, `origin/main` moved 4 commits (to `7fa67dada3`). None
touches `packages/spec`. The derivation files that changed
(`scripts/pm/ensure-pm-labels.sh` and two `scripts/pm/fleet-write/`
modules) are unrelated to these paths.
## Acceptance notes
- The card attributes the altered text to the entry's `reason` (printed
as `why:`). It is the entry's `acceptanceCriteria`, which `os migrate
meta` prints as the `verify:` line
(`packages/cli/src/commands/migrate/meta.ts:380`). The premise holds.
Only the field name differs.
- The old rule also rewrote 56 positions in `src/**/*.test.ts` files. No
entry's graph reaches a test file, so none of them shipped.
---
_Generated by [Claude
Code](https://claude.ai/code/session_018fxqvRJW12TaHC7DUQ89Y6)_
---------
Co-authored-by: Claude <noreply@anthropic.com>
1 parent 6073bb9 commit 4f83db5
3 files changed
Lines changed: 280 additions & 33 deletions
File tree
- .changeset
- packages/spec
- scripts
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
| 15 | + | |
| 16 | + | |
| 17 | + | |
| 18 | + | |
| 19 | + | |
| 20 | + | |
| 21 | + | |
| 22 | + | |
| 23 | + | |
| 24 | + | |
| 25 | + | |
| 26 | + | |
| 27 | + | |
| 28 | + | |
| 29 | + | |
| 30 | + | |
| 31 | + | |
| 32 | + | |
| 33 | + | |
| 34 | + | |
| 35 | + | |
| 36 | + | |
| 37 | + | |
| 38 | + | |
| 39 | + | |
| 40 | + | |
| 41 | + | |
| 42 | + | |
| 43 | + | |
| 44 | + | |
| 45 | + | |
| 46 | + | |
| 47 | + | |
| 48 | + | |
| 49 | + | |
| 50 | + | |
| 51 | + | |
| 52 | + | |
| 53 | + | |
| 54 | + | |
| 55 | + | |
| 56 | + | |
| 57 | + | |
| 58 | + | |
| 59 | + | |
| 60 | + | |
| 61 | + | |
| 62 | + | |
| 63 | + | |
| 64 | + | |
| 65 | + | |
| 66 | + | |
| 67 | + | |
| 68 | + | |
| 69 | + | |
| 70 | + | |
| 71 | + | |
| 72 | + | |
| 73 | + | |
| 74 | + | |
| 75 | + | |
| 76 | + | |
| 77 | + | |
| 78 | + | |
| 79 | + | |
| 80 | + | |
| 81 | + | |
| 82 | + | |
| 83 | + | |
| 84 | + | |
| 85 | + | |
| 86 | + | |
| 87 | + | |
| 88 | + | |
| 89 | + | |
| 90 | + | |
| 91 | + | |
| 92 | + | |
| 93 | + | |
| 94 | + | |
| 95 | + | |
| 96 | + | |
| 97 | + | |
| 98 | + | |
| 99 | + | |
| 100 | + | |
| 101 | + | |
| 102 | + | |
| 103 | + | |
| 104 | + | |
| 105 | + | |
| 106 | + | |
| 107 | + | |
| 108 | + | |
| 109 | + | |
| 110 | + | |
| 111 | + | |
| 112 | + | |
| 113 | + | |
| 114 | + | |
| 115 | + | |
| 116 | + | |
| 117 | + | |
| 118 | + | |
| 119 | + | |
| 120 | + | |
| 121 | + | |
| 122 | + | |
| 123 | + | |
| 124 | + | |
| 125 | + | |
| 126 | + | |
| 127 | + | |
| 128 | + | |
| 129 | + | |
| 130 | + | |
| 131 | + | |
| 132 | + | |
| 133 | + | |
| 134 | + | |
| 135 | + | |
| 136 | + | |
| 137 | + | |
| 138 | + | |
| 139 | + | |
| 140 | + | |
| 141 | + | |
| 142 | + | |
| 143 | + | |
| 144 | + | |
| 145 | + | |
| 146 | + | |
| 147 | + | |
| 148 | + | |
| 149 | + | |
| 150 | + | |
| 151 | + | |
| 152 | + | |
| 153 | + | |
| 154 | + | |
| 155 | + | |
| 156 | + | |
| 157 | + | |
| 158 | + | |
| 159 | + | |
| 160 | + | |
| 161 | + | |
| 162 | + | |
| 163 | + | |
| 164 | + | |
| 165 | + | |
| 166 | + | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
2 | 2 | | |
3 | 3 | | |
4 | 4 | | |
| 5 | + | |
5 | 6 | | |
6 | | - | |
7 | 7 | | |
8 | 8 | | |
9 | 9 | | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
| 15 | + | |
| 16 | + | |
| 17 | + | |
| 18 | + | |
| 19 | + | |
| 20 | + | |
| 21 | + | |
| 22 | + | |
| 23 | + | |
| 24 | + | |
| 25 | + | |
| 26 | + | |
| 27 | + | |
| 28 | + | |
| 29 | + | |
| 30 | + | |
| 31 | + | |
| 32 | + | |
| 33 | + | |
| 34 | + | |
| 35 | + | |
| 36 | + | |
| 37 | + | |
| 38 | + | |
| 39 | + | |
| 40 | + | |
| 41 | + | |
| 42 | + | |
| 43 | + | |
| 44 | + | |
| 45 | + | |
| 46 | + | |
| 47 | + | |
| 48 | + | |
| 49 | + | |
| 50 | + | |
| 51 | + | |
| 52 | + | |
| 53 | + | |
| 54 | + | |
| 55 | + | |
| 56 | + | |
| 57 | + | |
| 58 | + | |
| 59 | + | |
| 60 | + | |
| 61 | + | |
| 62 | + | |
| 63 | + | |
| 64 | + | |
| 65 | + | |
| 66 | + | |
| 67 | + | |
| 68 | + | |
| 69 | + | |
| 70 | + | |
| 71 | + | |
| 72 | + | |
| 73 | + | |
| 74 | + | |
| 75 | + | |
| 76 | + | |
| 77 | + | |
| 78 | + | |
| 79 | + | |
| 80 | + | |
| 81 | + | |
| 82 | + | |
| 83 | + | |
| 84 | + | |
| 85 | + | |
| 86 | + | |
| 87 | + | |
| 88 | + | |
| 89 | + | |
| 90 | + | |
| 91 | + | |
| 92 | + | |
| 93 | + | |
| 94 | + | |
| 95 | + | |
| 96 | + | |
| 97 | + | |
| 98 | + | |
| 99 | + | |
| 100 | + | |
| 101 | + | |
10 | 102 | | |
11 | 103 | | |
12 | 104 | | |
| |||
29 | 121 | | |
30 | 122 | | |
31 | 123 | | |
32 | | - | |
33 | | - | |
34 | | - | |
35 | | - | |
| 124 | + | |
| 125 | + | |
| 126 | + | |
| 127 | + | |
36 | 128 | | |
37 | | - | |
| 129 | + | |
38 | 130 | | |
39 | 131 | | |
40 | | - | |
41 | | - | |
42 | | - | |
43 | | - | |
44 | | - | |
45 | | - | |
46 | | - | |
47 | | - | |
48 | | - | |
49 | 132 | | |
50 | 133 | | |
51 | | - | |
52 | | - | |
53 | | - | |
54 | | - | |
55 | | - | |
56 | | - | |
57 | | - | |
58 | | - | |
59 | | - | |
60 | | - | |
61 | | - | |
62 | | - | |
63 | | - | |
64 | | - | |
65 | | - | |
66 | | - | |
67 | | - | |
| 134 | + | |
| 135 | + | |
68 | 136 | | |
69 | 137 | | |
70 | 138 | | |
| |||
245 | 313 | | |
246 | 314 | | |
247 | 315 | | |
248 | | - | |
| 316 | + | |
| 317 | + | |
| 318 | + | |
249 | 319 | | |
250 | 320 | | |
251 | 321 | | |
| |||
0 commit comments