@poveste/shared 0.6.1 → 0.8.0
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.
- package/dist/codegen/const.d.ts +0 -1
- package/dist/codegen/index.d.ts +0 -1
- package/dist/codegen/serialize-js.d.ts +0 -1
- package/dist/codegen/util.d.ts +0 -1
- package/dist/index.d.ts +1 -1
- package/dist/index.js +1 -0
- package/dist/setup.d.ts +0 -1
- package/dist/state.d.ts +0 -1
- package/dist/story-error.d.ts +13 -0
- package/dist/story-error.js +30 -0
- package/dist/story.d.ts +0 -1
- package/dist/type-utils.d.ts +0 -1
- package/dist/types/command.d.ts +0 -1
- package/dist/types/config.d.ts +0 -1
- package/dist/types/index.d.ts +0 -1
- package/dist/types/plugin.d.ts +0 -1
- package/dist/types/prompt.d.ts +0 -1
- package/dist/types/story.d.ts +0 -1
- package/package.json +14 -4
- package/dist/codegen/const.d.ts.map +0 -1
- package/dist/codegen/index.d.ts.map +0 -1
- package/dist/codegen/serialize-js.d.ts.map +0 -1
- package/dist/codegen/util.d.ts.map +0 -1
- package/dist/index.d.ts.map +0 -1
- package/dist/setup.d.ts.map +0 -1
- package/dist/state.d.ts.map +0 -1
- package/dist/story.d.ts.map +0 -1
- package/dist/type-utils.d.ts.map +0 -1
- package/dist/types/command.d.ts.map +0 -1
- package/dist/types/config.d.ts.map +0 -1
- package/dist/types/index.d.ts.map +0 -1
- package/dist/types/plugin.d.ts.map +0 -1
- package/dist/types/prompt.d.ts.map +0 -1
- package/dist/types/story.d.ts.map +0 -1
- package/src/__tests__/setup.spec.ts +0 -60
- package/src/__tests__/state.spec.ts +0 -353
- package/src/codegen/const.ts +0 -17
- package/src/codegen/index.ts +0 -3
- package/src/codegen/serialize-js.ts +0 -143
- package/src/codegen/util.ts +0 -77
- package/src/index.ts +0 -6
- package/src/setup.ts +0 -29
- package/src/state.ts +0 -374
- package/src/story.ts +0 -10
- package/src/type-utils.ts +0 -1
- package/src/types/command.ts +0 -38
- package/src/types/config.ts +0 -288
- package/src/types/index.ts +0 -5
- package/src/types/plugin.ts +0 -135
- package/src/types/prompt.ts +0 -25
- package/src/types/story.ts +0 -221
- package/tsconfig.json +0 -51
|
@@ -1,353 +0,0 @@
|
|
|
1
|
-
import { describe, expect, it } from 'vitest'
|
|
2
|
-
import { applyState, createStateBaseline, diffState, isEquivalent } from '../state.js'
|
|
3
|
-
|
|
4
|
-
describe('isEquivalent', () => {
|
|
5
|
-
it('compares primitives the way Vue decides whether to trigger', () => {
|
|
6
|
-
expect(isEquivalent(1, 1)).toBe(true)
|
|
7
|
-
expect(isEquivalent('a', 'a')).toBe(true)
|
|
8
|
-
expect(isEquivalent(null, null)).toBe(true)
|
|
9
|
-
expect(isEquivalent(undefined, undefined)).toBe(true)
|
|
10
|
-
// `Object.is`, not `===`: matches Vue's `hasChanged` on both of its oddities.
|
|
11
|
-
expect(isEquivalent(Number.NaN, Number.NaN)).toBe(true)
|
|
12
|
-
expect(isEquivalent(0, -0)).toBe(false)
|
|
13
|
-
expect(isEquivalent(1, '1')).toBe(false)
|
|
14
|
-
expect(isEquivalent(0, false)).toBe(false)
|
|
15
|
-
})
|
|
16
|
-
|
|
17
|
-
it('recurses into plain objects and arrays', () => {
|
|
18
|
-
expect(isEquivalent({ a: 1, b: { c: [1, 2] } }, { a: 1, b: { c: [1, 2] } })).toBe(true)
|
|
19
|
-
expect(isEquivalent({ a: 1, b: { c: [1, 2] } }, { a: 1, b: { c: [1, 3] } })).toBe(false)
|
|
20
|
-
expect(isEquivalent([1, 2], [1, 2, 3])).toBe(false)
|
|
21
|
-
expect(isEquivalent({ a: 1 }, { a: 1, b: 2 })).toBe(false)
|
|
22
|
-
expect(isEquivalent({ a: 1, b: 2 }, { a: 1 })).toBe(false)
|
|
23
|
-
// Same key count, different keys.
|
|
24
|
-
expect(isEquivalent({ a: 1 }, { b: 1 })).toBe(false)
|
|
25
|
-
// An array and an object are never equivalent, however similar.
|
|
26
|
-
expect(isEquivalent([], {})).toBe(false)
|
|
27
|
-
})
|
|
28
|
-
|
|
29
|
-
it('refuses to compare anything with a prototype of its own', () => {
|
|
30
|
-
// Both would report an empty key list, so a structural walk would call them
|
|
31
|
-
// equal. Reporting "different" makes the caller write, which is what it did
|
|
32
|
-
// before this guard existed.
|
|
33
|
-
expect(isEquivalent(new Date(0), new Date(1))).toBe(false)
|
|
34
|
-
expect(isEquivalent(new Date(0), new Date(0))).toBe(false)
|
|
35
|
-
expect(isEquivalent(new Map([['a', 1]]), new Map())).toBe(false)
|
|
36
|
-
expect(isEquivalent(new Set([1]), new Set([2]))).toBe(false)
|
|
37
|
-
expect(isEquivalent(/a/, /b/)).toBe(false)
|
|
38
|
-
|
|
39
|
-
class Point { constructor(public x = 1) {} }
|
|
40
|
-
expect(isEquivalent(new Point(), new Point())).toBe(false)
|
|
41
|
-
// ...but the identical instance is still identical.
|
|
42
|
-
const point = new Point()
|
|
43
|
-
expect(isEquivalent(point, point)).toBe(true)
|
|
44
|
-
})
|
|
45
|
-
|
|
46
|
-
it('terminates on cyclic state', () => {
|
|
47
|
-
const a: any = { name: 'a' }
|
|
48
|
-
a.self = a
|
|
49
|
-
const b: any = { name: 'a' }
|
|
50
|
-
b.self = b
|
|
51
|
-
|
|
52
|
-
expect(isEquivalent(a, b)).toBe(true)
|
|
53
|
-
|
|
54
|
-
const c: any = { name: 'c' }
|
|
55
|
-
c.self = c
|
|
56
|
-
expect(isEquivalent(a, c)).toBe(false)
|
|
57
|
-
})
|
|
58
|
-
|
|
59
|
-
it('handles objects created without a prototype', () => {
|
|
60
|
-
const bare = Object.create(null)
|
|
61
|
-
bare.a = 1
|
|
62
|
-
expect(isEquivalent(bare, { a: 1 })).toBe(true)
|
|
63
|
-
})
|
|
64
|
-
})
|
|
65
|
-
|
|
66
|
-
describe('applyState', () => {
|
|
67
|
-
it('copies values across', () => {
|
|
68
|
-
const target: any = { a: 1 }
|
|
69
|
-
applyState(target, { a: 2, b: 3 })
|
|
70
|
-
expect(target).toEqual({ a: 2, b: 3 })
|
|
71
|
-
})
|
|
72
|
-
|
|
73
|
-
it('merges into an existing object rather than replacing it', () => {
|
|
74
|
-
const nested = { a: 1, b: 2 }
|
|
75
|
-
const target: any = { nested }
|
|
76
|
-
applyState(target, { nested: { a: 9 } })
|
|
77
|
-
// Same object, updated in place — the sandbox bridge depends on this.
|
|
78
|
-
expect(target.nested).toBe(nested)
|
|
79
|
-
expect(target.nested).toEqual({ a: 9, b: 2 })
|
|
80
|
-
})
|
|
81
|
-
|
|
82
|
-
it('replaces the object when override is set', () => {
|
|
83
|
-
const nested = { a: 1, b: 2 }
|
|
84
|
-
const target: any = { nested }
|
|
85
|
-
applyState(target, { nested: { a: 9 } }, true)
|
|
86
|
-
expect(target.nested).not.toBe(nested)
|
|
87
|
-
expect(target.nested).toEqual({ a: 9 })
|
|
88
|
-
})
|
|
89
|
-
|
|
90
|
-
it('leaves identity alone when the incoming value is equivalent', () => {
|
|
91
|
-
// The behaviour #95 turns on. `toRawDeep` rebuilds every object on its way
|
|
92
|
-
// through the state sync, so without this the assignment below would land a
|
|
93
|
-
// new identity over an equal one and trigger every watcher on it.
|
|
94
|
-
const list = [1, 2, 3]
|
|
95
|
-
const nested = { a: 1 }
|
|
96
|
-
const target: any = { list, nested, count: 0 }
|
|
97
|
-
|
|
98
|
-
applyState(target, { list: [1, 2, 3], nested: { a: 1 }, count: 0 })
|
|
99
|
-
|
|
100
|
-
expect(target.list).toBe(list)
|
|
101
|
-
expect(target.nested).toBe(nested)
|
|
102
|
-
})
|
|
103
|
-
|
|
104
|
-
it('creates a key the target does not have, even when the value is undefined', () => {
|
|
105
|
-
// `undefined` is equivalent to `undefined`, so the skip has to check that
|
|
106
|
-
// the key exists before it trusts that. A story is allowed to declare its
|
|
107
|
-
// shape upfront — `initState: () => ({ text: 'hi', pending: undefined })` —
|
|
108
|
-
// and the controls panel lists the keys the state actually has.
|
|
109
|
-
const target: any = {}
|
|
110
|
-
applyState(target, { text: 'hi', pending: undefined })
|
|
111
|
-
|
|
112
|
-
expect(Object.keys(target)).toEqual(['text', 'pending'])
|
|
113
|
-
expect('pending' in target).toBe(true)
|
|
114
|
-
})
|
|
115
|
-
|
|
116
|
-
it('still skips an existing key whose value is already undefined', () => {
|
|
117
|
-
const target: any = { pending: undefined }
|
|
118
|
-
expect(applyState(target, { pending: undefined })).toBe(false)
|
|
119
|
-
})
|
|
120
|
-
|
|
121
|
-
it('still writes when only part of an equivalent-looking value differs', () => {
|
|
122
|
-
const target: any = { nested: { a: 1, b: 2 } }
|
|
123
|
-
applyState(target, { nested: { a: 1, b: 3 } })
|
|
124
|
-
expect(target.nested).toEqual({ a: 1, b: 3 })
|
|
125
|
-
})
|
|
126
|
-
|
|
127
|
-
it('replaces an object with a value it cannot merge into it', () => {
|
|
128
|
-
// The merge branch used to be chosen from the *target*'s type alone, so an
|
|
129
|
-
// incoming primitive met `for (const nested in 5)` — no iterations, nothing
|
|
130
|
-
// written, and the write silently dropped. A story swapping an object for a
|
|
131
|
-
// scalar is ordinary; a sync that records the write as done while the far
|
|
132
|
-
// side never got it then reverts that edit on the next pass.
|
|
133
|
-
const target: any = { config: { a: 1 }, list: { a: 1 } }
|
|
134
|
-
|
|
135
|
-
expect(applyState(target, { config: 5 })).toBe(true)
|
|
136
|
-
expect(target.config).toBe(5)
|
|
137
|
-
|
|
138
|
-
expect(applyState(target, { list: [1, 2] })).toBe(true)
|
|
139
|
-
expect(target.list).toEqual([1, 2])
|
|
140
|
-
})
|
|
141
|
-
|
|
142
|
-
it('swallows writes to read-only properties', () => {
|
|
143
|
-
const target: any = {}
|
|
144
|
-
Object.defineProperty(target, 'ro', { get: () => 1, enumerable: true })
|
|
145
|
-
expect(() => applyState(target, { ro: 2 })).not.toThrow()
|
|
146
|
-
expect(target.ro).toBe(1)
|
|
147
|
-
})
|
|
148
|
-
|
|
149
|
-
it('cannot express a key removal', () => {
|
|
150
|
-
// Not a wish list — every state sync in the repo is built around knowing
|
|
151
|
-
// this. `applyState` iterates the incoming keys, so a key the source has
|
|
152
|
-
// dropped is simply never visited and survives on the target.
|
|
153
|
-
const target: any = { a: 1, gone: 2 }
|
|
154
|
-
applyState(target, { a: 1 })
|
|
155
|
-
expect(target.gone).toBe(2)
|
|
156
|
-
})
|
|
157
|
-
})
|
|
158
|
-
|
|
159
|
-
// `plugin-svelte` and the sandbox bridge hold a flag meaning "ignore the next
|
|
160
|
-
// firing, it is the echo of my own write", and clear it when that firing
|
|
161
|
-
// arrives. This return value is what tells them whether a firing is coming at
|
|
162
|
-
// all. Report `true` when nothing moved and the flag stays set forever,
|
|
163
|
-
// swallowing the next real edit — which is #95. (`plugin-vue` has since dropped
|
|
164
|
-
// the flag for a baseline; see `createStateBaseline` below.)
|
|
165
|
-
describe('applyState reporting whether it wrote', () => {
|
|
166
|
-
it('reports a write', () => {
|
|
167
|
-
expect(applyState({ a: 1 }, { a: 2 })).toBe(true)
|
|
168
|
-
expect(applyState({}, { a: 1 })).toBe(true)
|
|
169
|
-
expect(applyState({ list: [1] }, { list: [1, 2] })).toBe(true)
|
|
170
|
-
})
|
|
171
|
-
|
|
172
|
-
it('reports nothing for a state that already matches', () => {
|
|
173
|
-
expect(applyState({ a: 1 }, { a: 1 })).toBe(false)
|
|
174
|
-
expect(applyState({ nested: { a: 1 } }, { nested: { a: 1 } })).toBe(false)
|
|
175
|
-
expect(applyState({ list: [1, 2] }, { list: [1, 2] })).toBe(false)
|
|
176
|
-
expect(applyState({ a: 1 }, {})).toBe(false)
|
|
177
|
-
})
|
|
178
|
-
|
|
179
|
-
it('reports nothing when a merge only drops keys it cannot drop', () => {
|
|
180
|
-
// The exact shape of #95, and the case that makes the naive answer wrong.
|
|
181
|
-
// `{ a: 1, b: 2 }` and `{ a: 1 }` are not equivalent, so this reaches the
|
|
182
|
-
// merge — but merging leaves the target byte for byte as it was, because
|
|
183
|
-
// the only difference is a key the merge has no way to remove.
|
|
184
|
-
const target: any = { items: { a: 1, b: 2 } }
|
|
185
|
-
expect(applyState(target, { items: { a: 1 } })).toBe(false)
|
|
186
|
-
expect(target.items).toEqual({ a: 1, b: 2 })
|
|
187
|
-
})
|
|
188
|
-
|
|
189
|
-
it('reports a write when a merge does change something', () => {
|
|
190
|
-
const target: any = { items: { a: 1, b: 2 } }
|
|
191
|
-
expect(applyState(target, { items: { a: 9 } })).toBe(true)
|
|
192
|
-
expect(target.items).toEqual({ a: 9, b: 2 })
|
|
193
|
-
})
|
|
194
|
-
|
|
195
|
-
it('reports a write it could not actually make', () => {
|
|
196
|
-
// Known and deliberately left alone: a setter that silently drops the value
|
|
197
|
-
// — Vue's read-only `computed` is the one that occurs in practice — still
|
|
198
|
-
// counts. Getting this right means reading back after every write, and the
|
|
199
|
-
// consequence of being wrong here is the pre-existing behaviour rather than
|
|
200
|
-
// a new failure.
|
|
201
|
-
const target: any = {}
|
|
202
|
-
Object.defineProperty(target, 'ro', { get: () => 1, enumerable: true, configurable: true })
|
|
203
|
-
expect(applyState(target, { ro: 2 })).toBe(true)
|
|
204
|
-
})
|
|
205
|
-
})
|
|
206
|
-
|
|
207
|
-
describe('diffState', () => {
|
|
208
|
-
it('reports nothing when the two match', () => {
|
|
209
|
-
expect(diffState({ a: 1 }, { a: 1 })).toBeNull()
|
|
210
|
-
expect(diffState({ nested: { a: 1 } }, { nested: { a: 1 } })).toBeNull()
|
|
211
|
-
expect(diffState({ list: [1, 2] }, { list: [1, 2] })).toBeNull()
|
|
212
|
-
})
|
|
213
|
-
|
|
214
|
-
it('reports only the keys that moved', () => {
|
|
215
|
-
expect(diffState({ a: 1, b: 2 }, { a: 1, b: 3 })).toEqual({ b: 3 })
|
|
216
|
-
})
|
|
217
|
-
|
|
218
|
-
it('reports a key the baseline has never seen', () => {
|
|
219
|
-
expect(diffState({}, { a: 1 })).toEqual({ a: 1 })
|
|
220
|
-
expect(diffState({}, { a: undefined })).toEqual({ a: undefined })
|
|
221
|
-
})
|
|
222
|
-
|
|
223
|
-
it('narrows a nested object to its own changed keys', () => {
|
|
224
|
-
// The point of the whole exercise: what is not sent cannot be clobbered, so
|
|
225
|
-
// the far side keeps its concurrent edit to `b`.
|
|
226
|
-
expect(diffState({ items: { a: 1, b: 2 } }, { items: { a: 9, b: 2 } })).toEqual({ items: { a: 9 } })
|
|
227
|
-
})
|
|
228
|
-
|
|
229
|
-
it('narrows one level and no further, which is as far as applyState merges', () => {
|
|
230
|
-
// `applyState` assigns what it finds at depth two, so a narrowed object there
|
|
231
|
-
// would land as the whole value and drop `y`.
|
|
232
|
-
expect(diffState({ deep: { inner: { x: 1, y: 2 } } }, { deep: { inner: { x: 9, y: 2 } } }))
|
|
233
|
-
.toEqual({ deep: { inner: { x: 9, y: 2 } } })
|
|
234
|
-
})
|
|
235
|
-
|
|
236
|
-
it('carries an `_h` key whole, because applyState replaces rather than merges it', () => {
|
|
237
|
-
// The sandbox bridge needs those replaced outright, so `applyState` sends
|
|
238
|
-
// them down the assignment branch. Narrowing one would empty it.
|
|
239
|
-
expect(diffState({ _hPropState: { a: 1, b: 2 } }, { _hPropState: { a: 9, b: 2 } }))
|
|
240
|
-
.toEqual({ _hPropState: { a: 9, b: 2 } })
|
|
241
|
-
})
|
|
242
|
-
|
|
243
|
-
it('carries an array whole', () => {
|
|
244
|
-
// `applyState` assigns arrays rather than merging them, so a partial one
|
|
245
|
-
// would be read as the entire new value.
|
|
246
|
-
expect(diffState({ list: [1, 2] }, { list: [1, 2, 3] })).toEqual({ list: [1, 2, 3] })
|
|
247
|
-
})
|
|
248
|
-
|
|
249
|
-
it('carries a value whole when the two sides are not both plain objects', () => {
|
|
250
|
-
expect(diffState({ a: 1 }, { a: { b: 2 } })).toEqual({ a: { b: 2 } })
|
|
251
|
-
expect(diffState({ a: { b: 2 } }, { a: 1 })).toEqual({ a: 1 })
|
|
252
|
-
})
|
|
253
|
-
|
|
254
|
-
it('ignores a removal, at any depth', () => {
|
|
255
|
-
// Deliberate, and the reason the baseline stays usable. `applyState` cannot
|
|
256
|
-
// express a removal, so reporting one produces a write that changes nothing
|
|
257
|
-
// — and one the baseline would go on reporting on every later pass.
|
|
258
|
-
expect(diffState({ a: 1, gone: 2 }, { a: 1 })).toBeNull()
|
|
259
|
-
expect(diffState({ items: { a: 1, gone: 2 } }, { items: { a: 1 } })).toBeNull()
|
|
260
|
-
})
|
|
261
|
-
})
|
|
262
|
-
|
|
263
|
-
describe('createStateBaseline', () => {
|
|
264
|
-
it('reports everything the first time, since it has agreed to nothing yet', () => {
|
|
265
|
-
const baseline = createStateBaseline()
|
|
266
|
-
expect(baseline.take({ a: 1, b: 2 })).toEqual({ a: 1, b: 2 })
|
|
267
|
-
})
|
|
268
|
-
|
|
269
|
-
it('reports nothing for a repeat, which is what makes an echo recognisable', () => {
|
|
270
|
-
const baseline = createStateBaseline()
|
|
271
|
-
baseline.take({ a: 1 })
|
|
272
|
-
expect(baseline.take({ a: 1 })).toBeNull()
|
|
273
|
-
})
|
|
274
|
-
|
|
275
|
-
it('reports each side only its own change', () => {
|
|
276
|
-
// #96 in miniature. Both sides changed a different key in the same tick, so
|
|
277
|
-
// each holds one fresh value and one stale one — and is asked about the
|
|
278
|
-
// fresh one alone.
|
|
279
|
-
const baseline = createStateBaseline()
|
|
280
|
-
baseline.take({ a: 0, b: 0 })
|
|
281
|
-
|
|
282
|
-
expect(baseline.take({ a: 1, b: 0 })).toEqual({ a: 1 })
|
|
283
|
-
expect(baseline.take({ a: 1, b: 1 })).toEqual({ b: 1 })
|
|
284
|
-
expect(baseline.take({ a: 1, b: 1 })).toBeNull()
|
|
285
|
-
})
|
|
286
|
-
|
|
287
|
-
it('stops reporting an `_h` key once the far side holds the shrunk value', () => {
|
|
288
|
-
// `_h` keys are replaced whole rather than merged, so the baseline has to
|
|
289
|
-
// record them the same way. Merging instead leaves behind a nested key the
|
|
290
|
-
// real state has dropped, and the baseline can never match again — every
|
|
291
|
-
// later pass reports the same phantom change, forever. Every Vue story
|
|
292
|
-
// carries `_hPropState`, so "forever" means every story whose auto-props
|
|
293
|
-
// ever shrink.
|
|
294
|
-
const baseline = createStateBaseline()
|
|
295
|
-
baseline.take({ _hPropState: { a: 1, b: 2 } })
|
|
296
|
-
|
|
297
|
-
expect(baseline.take({ _hPropState: { a: 9 } })).toEqual({ _hPropState: { a: 9 } })
|
|
298
|
-
expect(baseline.take({ _hPropState: { a: 9 } })).toBeNull()
|
|
299
|
-
})
|
|
300
|
-
|
|
301
|
-
it('keeps a key the far side removed rather than replaying it', () => {
|
|
302
|
-
const baseline = createStateBaseline()
|
|
303
|
-
baseline.take({ a: 1, gone: 2 })
|
|
304
|
-
|
|
305
|
-
expect(baseline.take({ a: 1 })).toBeNull()
|
|
306
|
-
// Still agreed as far as the side that kept it is concerned, so it does not
|
|
307
|
-
// come back the next time that side is asked.
|
|
308
|
-
expect(baseline.take({ a: 1, gone: 2 })).toBeNull()
|
|
309
|
-
})
|
|
310
|
-
|
|
311
|
-
it('does not keep a reference to what it was handed', () => {
|
|
312
|
-
// The caller passes the same object on to `applyState`, which assigns it
|
|
313
|
-
// into a reactive state. Sharing it would let a later write land in the
|
|
314
|
-
// baseline too, and an edit the baseline already knows is an edit dropped.
|
|
315
|
-
const baseline = createStateBaseline()
|
|
316
|
-
const next: any = { list: [1, 2], nested: { a: 1 } }
|
|
317
|
-
baseline.take(next)
|
|
318
|
-
|
|
319
|
-
next.list.push(3)
|
|
320
|
-
next.nested.a = 9
|
|
321
|
-
|
|
322
|
-
expect(baseline.take({ list: [1, 2, 3], nested: { a: 9 } }))
|
|
323
|
-
.toEqual({ list: [1, 2, 3], nested: { a: 9 } })
|
|
324
|
-
})
|
|
325
|
-
|
|
326
|
-
it('survives a cyclic state', () => {
|
|
327
|
-
const baseline = createStateBaseline()
|
|
328
|
-
const next: any = { name: 'a' }
|
|
329
|
-
next.self = next
|
|
330
|
-
|
|
331
|
-
expect(() => baseline.take(next)).not.toThrow()
|
|
332
|
-
expect(baseline.take(next)).toBeNull()
|
|
333
|
-
})
|
|
334
|
-
|
|
335
|
-
it('survives a cyclic state that changes', () => {
|
|
336
|
-
// The harder half. `isEquivalent` short-circuits an unchanged cycle before
|
|
337
|
-
// the diff ever recurses; a changed one gets past it, and then meets the
|
|
338
|
-
// same pair on every lap.
|
|
339
|
-
const baseline = createStateBaseline()
|
|
340
|
-
const first: any = { name: 'a' }
|
|
341
|
-
first.self = first
|
|
342
|
-
baseline.take(first)
|
|
343
|
-
|
|
344
|
-
const second: any = { name: 'b' }
|
|
345
|
-
second.self = second
|
|
346
|
-
|
|
347
|
-
// `self.name` did genuinely move, so it is reported at that path too. The
|
|
348
|
-
// walk terminates because it stops after one level, not because a cycle is
|
|
349
|
-
// detected — below that the value is carried whole, cycle and all.
|
|
350
|
-
expect(baseline.take(second)).toEqual({ name: 'b', self: { name: 'b', self: second } })
|
|
351
|
-
expect(baseline.take(second)).toBeNull()
|
|
352
|
-
})
|
|
353
|
-
})
|
package/src/codegen/const.ts
DELETED
package/src/codegen/index.ts
DELETED
|
@@ -1,143 +0,0 @@
|
|
|
1
|
-
const KEY_ESCAPE_REG = /[\s\-.:|#@$£*%]/
|
|
2
|
-
const MAX_SINGLE_LINE_ARRAY_LENGTH = 3
|
|
3
|
-
|
|
4
|
-
interface Line {
|
|
5
|
-
spaces: number
|
|
6
|
-
line: string
|
|
7
|
-
}
|
|
8
|
-
|
|
9
|
-
export function serializeJs(value: any): string {
|
|
10
|
-
const seen = new Set()
|
|
11
|
-
|
|
12
|
-
if (value === undefined) {
|
|
13
|
-
return 'undefined'
|
|
14
|
-
}
|
|
15
|
-
if (value === null) {
|
|
16
|
-
return 'null'
|
|
17
|
-
}
|
|
18
|
-
if (typeof value === 'string') {
|
|
19
|
-
return `'${value}'`
|
|
20
|
-
}
|
|
21
|
-
if (typeof value === 'boolean') {
|
|
22
|
-
return value ? 'true' : 'false'
|
|
23
|
-
}
|
|
24
|
-
if (Array.isArray(value)) {
|
|
25
|
-
return printLines(arrayToSourceLines(value, seen))
|
|
26
|
-
}
|
|
27
|
-
if (typeof value === 'object') {
|
|
28
|
-
return printLines(objectToSourceLines(value, seen))
|
|
29
|
-
}
|
|
30
|
-
if (value?.__autoBuildingObject) {
|
|
31
|
-
return value
|
|
32
|
-
}
|
|
33
|
-
if (typeof value === 'function' && value.name) {
|
|
34
|
-
return value.name
|
|
35
|
-
}
|
|
36
|
-
return value.toString()
|
|
37
|
-
}
|
|
38
|
-
|
|
39
|
-
function printLines(lines: Line[]) {
|
|
40
|
-
return lines.map(line => ' '.repeat(line.spaces) + line.line).join('\n')
|
|
41
|
-
}
|
|
42
|
-
|
|
43
|
-
function objectToSourceLines(object, seen: Set<unknown>, indentCount = 0) {
|
|
44
|
-
if (seen.has(object)) {
|
|
45
|
-
object = {}
|
|
46
|
-
}
|
|
47
|
-
else {
|
|
48
|
-
seen.add(object)
|
|
49
|
-
}
|
|
50
|
-
|
|
51
|
-
return createLines(indentCount, (lines) => {
|
|
52
|
-
lines.push('{')
|
|
53
|
-
lines.push(...createLines(1, (lines) => {
|
|
54
|
-
for (const key in object) {
|
|
55
|
-
const value = object[key]
|
|
56
|
-
|
|
57
|
-
let printedKey = key
|
|
58
|
-
if (KEY_ESCAPE_REG.test(key)) {
|
|
59
|
-
printedKey = `'${printedKey}'`
|
|
60
|
-
}
|
|
61
|
-
|
|
62
|
-
addLinesFromValue(lines, value, `${printedKey}: `, ',', seen)
|
|
63
|
-
}
|
|
64
|
-
}))
|
|
65
|
-
lines.push('}')
|
|
66
|
-
})
|
|
67
|
-
}
|
|
68
|
-
|
|
69
|
-
function arrayToSourceLines(array: any[], seen: Set<unknown>, indentCount = 0): Array<Line> {
|
|
70
|
-
if (seen.has(array)) {
|
|
71
|
-
array = []
|
|
72
|
-
}
|
|
73
|
-
else {
|
|
74
|
-
seen.add(array)
|
|
75
|
-
}
|
|
76
|
-
|
|
77
|
-
return createLines(indentCount, (lines) => {
|
|
78
|
-
const contentLines = createLines(1, (lines) => {
|
|
79
|
-
for (const value of array) {
|
|
80
|
-
addLinesFromValue(lines, value, '', ',', seen)
|
|
81
|
-
}
|
|
82
|
-
})
|
|
83
|
-
if (contentLines.length === 0) {
|
|
84
|
-
lines.push('[]')
|
|
85
|
-
}
|
|
86
|
-
else if (contentLines.length <= MAX_SINGLE_LINE_ARRAY_LENGTH && !contentLines.some(line => line.spaces > 1)) {
|
|
87
|
-
const [first] = contentLines
|
|
88
|
-
first.line = contentLines.map(({ line }) => line.substring(0, line.length - 1)).join(', ')
|
|
89
|
-
first.line = `[${first.line}]`
|
|
90
|
-
first.spaces--
|
|
91
|
-
lines.push(first)
|
|
92
|
-
}
|
|
93
|
-
else {
|
|
94
|
-
lines.push('[', ...contentLines, ']')
|
|
95
|
-
}
|
|
96
|
-
})
|
|
97
|
-
}
|
|
98
|
-
|
|
99
|
-
function createLines(indentCount: number, handler: (lines: any[]) => unknown): Array<Line> {
|
|
100
|
-
const lines: any[] = []
|
|
101
|
-
handler(lines)
|
|
102
|
-
return lines.map((line) => {
|
|
103
|
-
if (line.spaces != null) {
|
|
104
|
-
line.spaces += indentCount
|
|
105
|
-
return line
|
|
106
|
-
}
|
|
107
|
-
return { spaces: indentCount, line }
|
|
108
|
-
})
|
|
109
|
-
}
|
|
110
|
-
|
|
111
|
-
function addLinesFromValue(lines: Line[], value, before, after, seen) {
|
|
112
|
-
let result
|
|
113
|
-
if (Array.isArray(value)) {
|
|
114
|
-
lines.push(...wrap(arrayToSourceLines(value, seen), before, after))
|
|
115
|
-
return
|
|
116
|
-
}
|
|
117
|
-
else if (value && typeof value === 'object') {
|
|
118
|
-
lines.push(...wrap(objectToSourceLines(value, seen), before, after))
|
|
119
|
-
return
|
|
120
|
-
}
|
|
121
|
-
else if (typeof value === 'string') {
|
|
122
|
-
result = value.includes('\'') ? `\`${value}\`` : `'${value}'`
|
|
123
|
-
}
|
|
124
|
-
else if (typeof value === 'undefined') {
|
|
125
|
-
result = 'undefined'
|
|
126
|
-
}
|
|
127
|
-
else if (value === null) {
|
|
128
|
-
result = 'null'
|
|
129
|
-
}
|
|
130
|
-
else if (typeof value === 'boolean') {
|
|
131
|
-
result = value ? 'true' : 'false'
|
|
132
|
-
}
|
|
133
|
-
else {
|
|
134
|
-
result = value
|
|
135
|
-
}
|
|
136
|
-
lines.push(before + result + after)
|
|
137
|
-
}
|
|
138
|
-
|
|
139
|
-
function wrap(lines: Line[], before: string, after: string) {
|
|
140
|
-
lines[0].line = before + lines[0].line
|
|
141
|
-
lines[lines.length - 1].line += after
|
|
142
|
-
return lines
|
|
143
|
-
}
|
package/src/codegen/util.ts
DELETED
|
@@ -1,77 +0,0 @@
|
|
|
1
|
-
export function indent(lines: string[], count = 1) {
|
|
2
|
-
return lines.map(line => `${' '.repeat(count)}${line}`)
|
|
3
|
-
}
|
|
4
|
-
|
|
5
|
-
export function unindent(code: string) {
|
|
6
|
-
const lines = code.split('\n')
|
|
7
|
-
let indentLevel = -1
|
|
8
|
-
let indentText: string
|
|
9
|
-
const linesToAnalyze = lines.filter(line => line.trim().length > 0)
|
|
10
|
-
for (const line of linesToAnalyze) {
|
|
11
|
-
const match = /^\s*/.exec(line)
|
|
12
|
-
if (match && (indentLevel === -1 || indentLevel > match[0].length)) {
|
|
13
|
-
indentLevel = match[0].length
|
|
14
|
-
indentText = match[0]
|
|
15
|
-
}
|
|
16
|
-
}
|
|
17
|
-
const result: string[] = []
|
|
18
|
-
for (const line of lines) {
|
|
19
|
-
result.push(line.replace(indentText, ''))
|
|
20
|
-
}
|
|
21
|
-
return result.join('\n').trim()
|
|
22
|
-
}
|
|
23
|
-
|
|
24
|
-
interface AutoBuildingOject {
|
|
25
|
-
key: string
|
|
26
|
-
cache: Record<string | symbol, AutoBuildingOject>
|
|
27
|
-
target: any
|
|
28
|
-
proxy: any
|
|
29
|
-
}
|
|
30
|
-
|
|
31
|
-
export function createAutoBuildingObject(format?: (key: string) => string, specialKeysHandler?: (target: any, p: string | symbol) => (() => unknown) | null, key = '', depth = 0): AutoBuildingOject {
|
|
32
|
-
const cache: Record<string | symbol, AutoBuildingOject> = {}
|
|
33
|
-
if (depth > 32) return { key, cache, target: {}, proxy: () => key }
|
|
34
|
-
const target: any = () => {
|
|
35
|
-
const k = `${key}()`
|
|
36
|
-
return format ? format(k) : k
|
|
37
|
-
}
|
|
38
|
-
const proxy = new Proxy(target, {
|
|
39
|
-
get(_, p) {
|
|
40
|
-
if (p === '__autoBuildingObject') {
|
|
41
|
-
return true
|
|
42
|
-
}
|
|
43
|
-
if (p === '__autoBuildingObjectGetKey') {
|
|
44
|
-
return key
|
|
45
|
-
}
|
|
46
|
-
if (specialKeysHandler) {
|
|
47
|
-
const fn = specialKeysHandler(target, p)
|
|
48
|
-
if (fn) {
|
|
49
|
-
return fn()
|
|
50
|
-
}
|
|
51
|
-
}
|
|
52
|
-
if (p === 'toString') {
|
|
53
|
-
const k = `${key}.toString()`
|
|
54
|
-
return () => format ? format(k) : k
|
|
55
|
-
}
|
|
56
|
-
if (p === Symbol.toPrimitive) {
|
|
57
|
-
return () => format ? format(key) : key
|
|
58
|
-
}
|
|
59
|
-
if (!cache[p]) {
|
|
60
|
-
const childKey = key ? `${key}.${p.toString()}` : p.toString()
|
|
61
|
-
const child = createAutoBuildingObject(format, specialKeysHandler, childKey, depth + 1)
|
|
62
|
-
cache[p] = { key: childKey, ...child }
|
|
63
|
-
}
|
|
64
|
-
return cache[p].proxy
|
|
65
|
-
},
|
|
66
|
-
apply(_, thisArg, args) {
|
|
67
|
-
const k = `${key}(${args.join(', ')})`
|
|
68
|
-
return format ? format(k) : k
|
|
69
|
-
},
|
|
70
|
-
})
|
|
71
|
-
return {
|
|
72
|
-
key,
|
|
73
|
-
cache,
|
|
74
|
-
target,
|
|
75
|
-
proxy,
|
|
76
|
-
}
|
|
77
|
-
}
|
package/src/index.ts
DELETED
package/src/setup.ts
DELETED
|
@@ -1,29 +0,0 @@
|
|
|
1
|
-
export type SetupModule = Record<string, unknown> | undefined
|
|
2
|
-
|
|
3
|
-
/**
|
|
4
|
-
* Reads a setup hook (`setupVanilla`, `setupVue3`, ...) out of a setup module.
|
|
5
|
-
*
|
|
6
|
-
* Takes the module as an argument instead of letting callers access it inline:
|
|
7
|
-
* the setup modules are namespace imports of virtual or user-provided files that
|
|
8
|
-
* aren't guaranteed to declare every hook, and a static `namespace.setupVue3`
|
|
9
|
-
* access makes Rollup warn `"setupVue3" is not exported by ...` in every
|
|
10
|
-
* consumer build. Passing the namespace through a call forces it to be
|
|
11
|
-
* materialized, so the lookup happens at runtime where it belongs.
|
|
12
|
-
*/
|
|
13
|
-
export function getSetupHook<T>(mod: SetupModule, name: string | string[]): T | undefined {
|
|
14
|
-
const names = typeof name === 'string' ? [name] : name
|
|
15
|
-
const present = names.filter(candidate => typeof mod?.[candidate] === 'function')
|
|
16
|
-
|
|
17
|
-
// Earlier names win, so a plugin lists its established hook first and a newer
|
|
18
|
-
// alias after it: an existing setup file keeps the exact behaviour it had.
|
|
19
|
-
// Running both instead would apply the same setup twice, which for Vue means
|
|
20
|
-
// a second `app.use()` for every plugin the user registers.
|
|
21
|
-
if (present.length > 1) {
|
|
22
|
-
console.warn(
|
|
23
|
-
`[poveste] Setup file exports ${present.length} interchangeable setup hooks (${present.join(', ')}). `
|
|
24
|
-
+ `Only ${present[0]} runs. Keep one — they are aliases, not separate hooks.`,
|
|
25
|
-
)
|
|
26
|
-
}
|
|
27
|
-
|
|
28
|
-
return present.length > 0 ? mod![present[0]] as T : undefined
|
|
29
|
-
}
|