@wildshard/shard-format 0.69.1 → 0.70.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/package.json +8 -2
- package/src/cli/corpus-command.ts +38 -0
- package/src/cli/corpus-usage.ts +85 -0
- package/src/cli/format-diff-data.ts +89 -0
- package/src/cli/format-diff-lock.ts +122 -0
- package/src/cli/format-diff-normal.ts +300 -0
- package/src/cli/format-diff-paths.ts +103 -0
- package/src/cli/format-diff-report.ts +30 -0
- package/src/cli/format-diff-rules.ts +232 -0
- package/src/cli/format-diff.ts +226 -0
- package/src/cli/main.ts +50 -31
- package/src/format/cook-data.ts +22 -3
- package/src/format/cook-preparation.ts +103 -69
- package/src/format/deprecations.ts +12 -0
- package/src/format/format-migrate.ts +3 -4
- package/src/format/format-migration-envelope.ts +21 -0
- package/src/format/format-migrations.ts +2 -0
- package/src/format/memory-armed.json +33 -1
- package/src/format/outputIndex.ts +28 -6
- package/src/format/preparation-checks.ts +100 -0
- package/src/format/preparation-spec.ts +45 -0
- package/src/format/stateLineage.ts +3 -1
- package/src/format/version.ts +32 -3
|
@@ -0,0 +1,103 @@
|
|
|
1
|
+
import { MARKS } from './format-diff-normal.ts'
|
|
2
|
+
|
|
3
|
+
import type { Change } from './format-diff-data.ts'
|
|
4
|
+
import type { Row } from './format-diff-normal.ts'
|
|
5
|
+
|
|
6
|
+
// SHARD-COMPAT FE2: lock paths: their last segment, their subtree, and the
|
|
7
|
+
// `rename?` pairing of a removed and an added part with the same subtree.
|
|
8
|
+
|
|
9
|
+
type Sides = readonly [ReadonlyMap<string, Row>, ReadonlyMap<string, Row>]
|
|
10
|
+
|
|
11
|
+
const DEPRECATIONS = './deprecations#'
|
|
12
|
+
const CHAIN = './format-chain#'
|
|
13
|
+
const LAST_MARK = /[.[|{](?=[^.[|{]*$)/u
|
|
14
|
+
const NONE = 0
|
|
15
|
+
const NEXT = 1
|
|
16
|
+
|
|
17
|
+
/** A deprecation or chain step row: an id, never renamed. */
|
|
18
|
+
function isRegistry(key: string): boolean {
|
|
19
|
+
return key.startsWith(DEPRECATIONS) || key.startsWith(CHAIN)
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
/** The path above a key's last segment, and the mark that starts that segment ('' for an export). */
|
|
23
|
+
function lastSegment(key: string): readonly [string, string] {
|
|
24
|
+
const hash = key.indexOf('#')
|
|
25
|
+
const match = LAST_MARK.exec(key.slice(hash + NEXT))
|
|
26
|
+
if (match === null) {
|
|
27
|
+
return [key.slice(NONE, hash), '']
|
|
28
|
+
}
|
|
29
|
+
const at = hash + NEXT + match.index
|
|
30
|
+
return [key.slice(NONE, at), key.charAt(at)]
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
/** A part's subtree, relative to it, as one comparable string. */
|
|
34
|
+
function subtree(key: string, rows: ReadonlyMap<string, Row>): string {
|
|
35
|
+
const keys = [...rows.keys()]
|
|
36
|
+
.filter(
|
|
37
|
+
(other: string): boolean =>
|
|
38
|
+
other === key ||
|
|
39
|
+
(other.startsWith(key) && MARKS.has(other.charAt(key.length))),
|
|
40
|
+
)
|
|
41
|
+
.toSorted()
|
|
42
|
+
return JSON.stringify(
|
|
43
|
+
keys.map((other: string): readonly [string, Row | undefined] => [
|
|
44
|
+
other.slice(key.length),
|
|
45
|
+
rows.get(other),
|
|
46
|
+
]),
|
|
47
|
+
)
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
function bySignature(
|
|
51
|
+
keys: readonly string[],
|
|
52
|
+
rows: ReadonlyMap<string, Row>,
|
|
53
|
+
): Map<string, string[]> {
|
|
54
|
+
const out = new Map<string, string[]>()
|
|
55
|
+
for (const key of keys.filter((name: string): boolean => !isRegistry(name))) {
|
|
56
|
+
const sign = subtree(key, rows)
|
|
57
|
+
out.set(sign, [...(out.get(sign) ?? []), key])
|
|
58
|
+
}
|
|
59
|
+
return out
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
/** The candidate under the same parent as `key`, else the first. */
|
|
63
|
+
function closest(key: string, found: readonly string[]): string | undefined {
|
|
64
|
+
const [parent] = lastSegment(key)
|
|
65
|
+
return (
|
|
66
|
+
found.find(
|
|
67
|
+
(other: string): boolean => lastSegment(other)[NONE] === parent,
|
|
68
|
+
) ?? found.at(NONE)
|
|
69
|
+
)
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
/** Removed and added parts with the same subtree, as `rename?` pairs (same parent first); the rest stay unpaired. */
|
|
73
|
+
function renames(
|
|
74
|
+
gone: readonly string[],
|
|
75
|
+
added: readonly string[],
|
|
76
|
+
sides: Sides,
|
|
77
|
+
): ReadonlyMap<string, string> {
|
|
78
|
+
const [before, after] = sides
|
|
79
|
+
const candidates = bySignature(added, after)
|
|
80
|
+
const pairs = new Map<string, string>()
|
|
81
|
+
for (const key of gone.filter((name: string): boolean => !isRegistry(name))) {
|
|
82
|
+
const found = candidates.get(subtree(key, before)) ?? []
|
|
83
|
+
const pick = closest(key, found)
|
|
84
|
+
if (pick !== undefined) {
|
|
85
|
+
pairs.set(key, pick)
|
|
86
|
+
found.splice(found.indexOf(pick), NEXT)
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
return pairs
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
function renameChange(from: string, to: string): Change {
|
|
93
|
+
const same = lastSegment(from)[NONE] === lastSegment(to)[NONE]
|
|
94
|
+
return {
|
|
95
|
+
path: from,
|
|
96
|
+
kind: 'breaking',
|
|
97
|
+
what: `rename? ${from} -> ${to}`,
|
|
98
|
+
op: same ? 'rename' : 'moveSection',
|
|
99
|
+
rename: { from, to },
|
|
100
|
+
}
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
export { CHAIN, DEPRECATIONS, lastSegment, renameChange, renames }
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
import { KINDS } from './format-diff.ts'
|
|
2
|
+
|
|
3
|
+
import type { Change, ChangeKind } from './format-diff-data.ts'
|
|
4
|
+
import type { Report } from './format-diff.ts'
|
|
5
|
+
|
|
6
|
+
// SHARD-COMPAT FE2: format-diff's report as text, one change per line.
|
|
7
|
+
|
|
8
|
+
const NONE = 0
|
|
9
|
+
const SHORT_ID = 12
|
|
10
|
+
const KIND_WIDTH = 11
|
|
11
|
+
|
|
12
|
+
/** The report as text, one change per line. */
|
|
13
|
+
function formatDiffReport(report: Readonly<Report>): string {
|
|
14
|
+
const counts = KINDS.map(
|
|
15
|
+
(kind: ChangeKind): string =>
|
|
16
|
+
`${String(report.changes.filter((found: Readonly<Change>): boolean => found.kind === kind).length)} ${kind}`,
|
|
17
|
+
)
|
|
18
|
+
const chain =
|
|
19
|
+
report.chain === undefined
|
|
20
|
+
? 'chain id unchanged'
|
|
21
|
+
: `chain id ${report.chain.before.slice(NONE, SHORT_ID)} -> ${report.chain.after.slice(NONE, SHORT_ID)}`
|
|
22
|
+
const lines = report.changes.map(
|
|
23
|
+
(found: Readonly<Change>): string =>
|
|
24
|
+
`${found.kind.padEnd(KIND_WIDTH)} ${found.path} ${found.what}${found.op === undefined ? '' : ` (op: ${found.op})`}`,
|
|
25
|
+
)
|
|
26
|
+
const head = `format-diff: ${String(report.changes.length)} changes (${counts.join(', ')}); ${chain}; worst ${report.worst}`
|
|
27
|
+
return [head, ...lines].join('\n')
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
export { formatDiffReport }
|
|
@@ -0,0 +1,232 @@
|
|
|
1
|
+
import {
|
|
2
|
+
dataFinding,
|
|
3
|
+
directed,
|
|
4
|
+
finding,
|
|
5
|
+
setFinding,
|
|
6
|
+
} from './format-diff-data.ts'
|
|
7
|
+
import { isRecord } from './format-diff-lock.ts'
|
|
8
|
+
|
|
9
|
+
import type { Finding } from './format-diff-data.ts'
|
|
10
|
+
import type { Json } from './format-diff-lock.ts'
|
|
11
|
+
import type { Row, SchemaRow } from './format-diff-normal.ts'
|
|
12
|
+
|
|
13
|
+
// SHARD-COMPAT FE2: how one changed row is classified. "Additive" means an
|
|
14
|
+
// old file still reads on the new reader (never the reverse: every schema is
|
|
15
|
+
// strict). "Breaking" means some old file is refused or means something else.
|
|
16
|
+
// "Review" is what no rule can decide (a predicate's code, a default, a
|
|
17
|
+
// transform). The suggested op is an FE8 migration op.
|
|
18
|
+
|
|
19
|
+
interface Action {
|
|
20
|
+
readonly kind: string
|
|
21
|
+
readonly type: string
|
|
22
|
+
readonly requirement: Json | undefined
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
const LOOSE = 0
|
|
26
|
+
const PLAIN = 1
|
|
27
|
+
const STRICT = 2
|
|
28
|
+
/** Strictness per shape family: a lower level admits more. */
|
|
29
|
+
const LOOSENESS: Readonly<Record<string, readonly [string, number]>> = {
|
|
30
|
+
loose_object: ['object', LOOSE],
|
|
31
|
+
object: ['object', PLAIN],
|
|
32
|
+
object_with_rest: ['object', LOOSE],
|
|
33
|
+
strict_object: ['object', STRICT],
|
|
34
|
+
loose_tuple: ['tuple', LOOSE],
|
|
35
|
+
tuple: ['tuple', PLAIN],
|
|
36
|
+
tuple_with_rest: ['tuple', LOOSE],
|
|
37
|
+
strict_tuple: ['tuple', STRICT],
|
|
38
|
+
}
|
|
39
|
+
const OPTIONALS: ReadonlySet<string> = new Set([
|
|
40
|
+
'exact_optional',
|
|
41
|
+
'nullish',
|
|
42
|
+
'optional',
|
|
43
|
+
'undefinedable',
|
|
44
|
+
])
|
|
45
|
+
const NULLABLES: ReadonlySet<string> = new Set(['nullable', 'nullish'])
|
|
46
|
+
const UNIONS: ReadonlySet<string> = new Set(['picklist', 'union', 'variant'])
|
|
47
|
+
const KIND_FIELD = /\.(?:kind|type)$/u
|
|
48
|
+
const SKIPPED_ACTIONS: ReadonlySet<string> = new Set(['metadata'])
|
|
49
|
+
const NONE = 0
|
|
50
|
+
|
|
51
|
+
function text(value: Json | undefined): string {
|
|
52
|
+
if (value === undefined) {
|
|
53
|
+
return 'none'
|
|
54
|
+
}
|
|
55
|
+
return typeof value === 'string' ? value : JSON.stringify(value)
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
/** A type change: within a shape family by looseness, into a union for review, else a break. */
|
|
59
|
+
function typeFinding(before: SchemaRow, after: SchemaRow): readonly Finding[] {
|
|
60
|
+
if (before.type === after.type) {
|
|
61
|
+
return []
|
|
62
|
+
}
|
|
63
|
+
const [familyBefore, levelBefore] = LOOSENESS[before.type] ?? ['', PLAIN]
|
|
64
|
+
const [familyAfter, levelAfter] = LOOSENESS[after.type] ?? ['', PLAIN]
|
|
65
|
+
const what = `type ${before.type} -> ${after.type}`
|
|
66
|
+
if (familyBefore !== '' && familyBefore === familyAfter) {
|
|
67
|
+
return [directed(levelAfter < levelBefore, what)]
|
|
68
|
+
}
|
|
69
|
+
if (UNIONS.has(after.type) && !UNIONS.has(before.type)) {
|
|
70
|
+
// It may still admit the old shape as one option: a person checks.
|
|
71
|
+
return [finding('review', `${what} (does an option admit the old?)`)]
|
|
72
|
+
}
|
|
73
|
+
return [finding('breaking', what, 'map')]
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
function admits(wrap: readonly string[], set: ReadonlySet<string>): boolean {
|
|
77
|
+
return wrap.some((type: string): boolean => set.has(type))
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
/** Wrapper changes: gaining optional or nullable admits more; losing it refuses old files. */
|
|
81
|
+
function wrapFindings(before: SchemaRow, after: SchemaRow): readonly Finding[] {
|
|
82
|
+
if (before.wrap.join('/') === after.wrap.join('/')) {
|
|
83
|
+
return []
|
|
84
|
+
}
|
|
85
|
+
const out: Finding[] = []
|
|
86
|
+
for (const [set, name] of [
|
|
87
|
+
[OPTIONALS, 'optional'],
|
|
88
|
+
[NULLABLES, 'nullable'],
|
|
89
|
+
] as const) {
|
|
90
|
+
const now = admits(after.wrap, set)
|
|
91
|
+
if (admits(before.wrap, set) !== now) {
|
|
92
|
+
out.push(
|
|
93
|
+
now
|
|
94
|
+
? finding('additive', `now ${name}`)
|
|
95
|
+
: finding('breaking', `no longer ${name}`, 'fill'),
|
|
96
|
+
)
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
const moved = `wrappers ${before.wrap.join('/')} -> ${after.wrap.join('/')}`
|
|
100
|
+
return out.length > NONE ? out : [finding('review', moved)]
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
function actionOf(action: Json): Action | undefined {
|
|
104
|
+
if (!isRecord(action)) {
|
|
105
|
+
return undefined
|
|
106
|
+
}
|
|
107
|
+
return {
|
|
108
|
+
kind: text(action['kind']),
|
|
109
|
+
type: text(action['type']),
|
|
110
|
+
requirement: action['requirement'],
|
|
111
|
+
}
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
function actionsOf(pipe: Json | undefined): readonly Action[] {
|
|
115
|
+
const actions = Array.isArray(pipe) ? pipe : []
|
|
116
|
+
return actions
|
|
117
|
+
.map((action: Json): Action | undefined => actionOf(action))
|
|
118
|
+
.filter(
|
|
119
|
+
(action: Action | undefined): action is Action =>
|
|
120
|
+
action !== undefined && !SKIPPED_ACTIONS.has(action.kind),
|
|
121
|
+
)
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
/** One validation kept but with a new requirement. */
|
|
125
|
+
function requirementFinding(
|
|
126
|
+
type: string,
|
|
127
|
+
before: Json | undefined,
|
|
128
|
+
after: Json | undefined,
|
|
129
|
+
): Finding {
|
|
130
|
+
const what = `${type} ${text(before)} -> ${text(after)}`
|
|
131
|
+
if (typeof before === 'number' && typeof after === 'number') {
|
|
132
|
+
if (type.startsWith('max_')) {
|
|
133
|
+
return directed(after > before, what)
|
|
134
|
+
}
|
|
135
|
+
return type.startsWith('min_')
|
|
136
|
+
? directed(after < before, what)
|
|
137
|
+
: finding('breaking', what)
|
|
138
|
+
}
|
|
139
|
+
if (Array.isArray(before) && Array.isArray(after)) {
|
|
140
|
+
return setFinding(type, before, after)
|
|
141
|
+
}
|
|
142
|
+
return finding('review', what)
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
function sameAction(left: Action, right: Action): boolean {
|
|
146
|
+
return left.kind === right.kind && left.type === right.type
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
function edge(action: Action, verb: 'adds' | 'drops'): Finding {
|
|
150
|
+
if (action.kind !== 'validation') {
|
|
151
|
+
return finding('review', `${verb} ${action.type}`)
|
|
152
|
+
}
|
|
153
|
+
// An added check narrows what reads; a dropped one widens it.
|
|
154
|
+
return directed(verb === 'drops', `${verb} ${action.type}`)
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
/** Pipe changes: an added check narrows, a dropped one widens, a moved limit by its direction. */
|
|
158
|
+
function pipeFindings(before: SchemaRow, after: SchemaRow): readonly Finding[] {
|
|
159
|
+
const was = actionsOf(before.facts['pipe'])
|
|
160
|
+
const now = actionsOf(after.facts['pipe'])
|
|
161
|
+
const kept = now.flatMap((action: Action): readonly Finding[] => {
|
|
162
|
+
const old = was.find((other: Action): boolean => sameAction(other, action))
|
|
163
|
+
if (old === undefined) {
|
|
164
|
+
return [edge(action, 'adds')]
|
|
165
|
+
}
|
|
166
|
+
return text(old.requirement) === text(action.requirement)
|
|
167
|
+
? []
|
|
168
|
+
: [requirementFinding(action.type, old.requirement, action.requirement)]
|
|
169
|
+
})
|
|
170
|
+
const dropped = was
|
|
171
|
+
.filter(
|
|
172
|
+
(action: Action): boolean =>
|
|
173
|
+
!now.some((other: Action): boolean => sameAction(other, action)),
|
|
174
|
+
)
|
|
175
|
+
.map((action: Action): Finding => edge(action, 'drops'))
|
|
176
|
+
return [...kept, ...dropped]
|
|
177
|
+
}
|
|
178
|
+
|
|
179
|
+
/** The other facts: a literal is a break, a default, getter, predicate or async flag is for review. */
|
|
180
|
+
function factFindings(
|
|
181
|
+
path: string,
|
|
182
|
+
before: SchemaRow,
|
|
183
|
+
after: SchemaRow,
|
|
184
|
+
): readonly Finding[] {
|
|
185
|
+
const keys = new Set([
|
|
186
|
+
...Object.keys(before.facts),
|
|
187
|
+
...Object.keys(after.facts),
|
|
188
|
+
])
|
|
189
|
+
return [...keys]
|
|
190
|
+
.filter(
|
|
191
|
+
(key: string): boolean =>
|
|
192
|
+
key !== 'pipe' && text(before.facts[key]) !== text(after.facts[key]),
|
|
193
|
+
)
|
|
194
|
+
.toSorted()
|
|
195
|
+
.map((key: string): Finding => {
|
|
196
|
+
const moved = `${key} ${text(before.facts[key])} -> ${text(after.facts[key])}`
|
|
197
|
+
if (key !== 'literal') {
|
|
198
|
+
return finding('review', moved)
|
|
199
|
+
}
|
|
200
|
+
return finding(
|
|
201
|
+
'breaking',
|
|
202
|
+
moved,
|
|
203
|
+
KIND_FIELD.test(path) ? 'renameKind' : 'map',
|
|
204
|
+
)
|
|
205
|
+
})
|
|
206
|
+
}
|
|
207
|
+
|
|
208
|
+
/** Every finding for one row present in both locks. */
|
|
209
|
+
function changedFindings(
|
|
210
|
+
path: string,
|
|
211
|
+
before: Row,
|
|
212
|
+
after: Row,
|
|
213
|
+
): readonly Finding[] {
|
|
214
|
+
if (before.kind === 'data' && after.kind === 'data') {
|
|
215
|
+
return text(before.value) === text(after.value)
|
|
216
|
+
? []
|
|
217
|
+
: [dataFinding(path, before, after)]
|
|
218
|
+
}
|
|
219
|
+
if (before.kind === 'schema' && after.kind === 'schema') {
|
|
220
|
+
const type = typeFinding(before, after)
|
|
221
|
+
const same = type.length === NONE
|
|
222
|
+
return [
|
|
223
|
+
...type,
|
|
224
|
+
...wrapFindings(before, after),
|
|
225
|
+
...(same ? pipeFindings(before, after) : []),
|
|
226
|
+
...(same ? factFindings(path, before, after) : []),
|
|
227
|
+
]
|
|
228
|
+
}
|
|
229
|
+
return [finding('breaking', 'a schema became plain data, or the reverse')]
|
|
230
|
+
}
|
|
231
|
+
|
|
232
|
+
export { changedFindings, OPTIONALS }
|
|
@@ -0,0 +1,226 @@
|
|
|
1
|
+
import { finding } from './format-diff-data.ts'
|
|
2
|
+
import { MARKS, normalRows } from './format-diff-normal.ts'
|
|
3
|
+
import {
|
|
4
|
+
CHAIN,
|
|
5
|
+
DEPRECATIONS,
|
|
6
|
+
lastSegment,
|
|
7
|
+
renameChange,
|
|
8
|
+
renames,
|
|
9
|
+
} from './format-diff-paths.ts'
|
|
10
|
+
import { changedFindings, OPTIONALS } from './format-diff-rules.ts'
|
|
11
|
+
|
|
12
|
+
import type {
|
|
13
|
+
Change,
|
|
14
|
+
ChangeKind,
|
|
15
|
+
Finding,
|
|
16
|
+
MigrationOp,
|
|
17
|
+
} from './format-diff-data.ts'
|
|
18
|
+
import type { LockData } from './format-diff-lock.ts'
|
|
19
|
+
import type { Row } from './format-diff-normal.ts'
|
|
20
|
+
|
|
21
|
+
// SHARD-COMPAT FE2: `shard-format format-diff <a> <b>` classifies each
|
|
22
|
+
// change between two format locks (FE1) as additive, deprecation, breaking
|
|
23
|
+
// or review, pairs a removed and an added part with the same subtree as
|
|
24
|
+
// `rename?`, and suggests an FE8 migration op. Only the topmost added or
|
|
25
|
+
// removed path is reported: a new optional object is one change, not one
|
|
26
|
+
// per field inside it. Rules per row: format-diff-rules.ts.
|
|
27
|
+
|
|
28
|
+
interface Report {
|
|
29
|
+
readonly chain:
|
|
30
|
+
| { readonly before: string; readonly after: string }
|
|
31
|
+
| undefined
|
|
32
|
+
readonly changes: readonly Change[]
|
|
33
|
+
readonly worst: ChangeKind | 'none'
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
const KINDS: readonly ChangeKind[] = [
|
|
37
|
+
'breaking',
|
|
38
|
+
'review',
|
|
39
|
+
'deprecation',
|
|
40
|
+
'additive',
|
|
41
|
+
]
|
|
42
|
+
const NONE = 0
|
|
43
|
+
const NEXT = 1
|
|
44
|
+
const BEFORE = -1
|
|
45
|
+
const REMOVED: Readonly<
|
|
46
|
+
Record<string, readonly [string, MigrationOp | undefined]>
|
|
47
|
+
> = {
|
|
48
|
+
'': ['export removed', undefined],
|
|
49
|
+
'.': ['field removed', 'drop'],
|
|
50
|
+
'|': ['union option removed', 'map'],
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
/** Whether some proper prefix of `key`, cut at a mark, is in `set`. */
|
|
54
|
+
function covered(key: string, set: ReadonlySet<string>): boolean {
|
|
55
|
+
for (let at = NEXT; at < key.length; at += NEXT) {
|
|
56
|
+
if (MARKS.has(key.charAt(at)) && set.has(key.slice(NONE, at))) {
|
|
57
|
+
return true
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
return false
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
function topmost(keys: ReadonlySet<string>): readonly string[] {
|
|
64
|
+
return [...keys]
|
|
65
|
+
.filter((key: string): boolean => !covered(key, keys))
|
|
66
|
+
.toSorted()
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
function fieldFinding(parent: string, row: Row | undefined): Finding {
|
|
70
|
+
if (
|
|
71
|
+
row?.kind === 'schema' &&
|
|
72
|
+
row.wrap.some((type: string): boolean => OPTIONALS.has(type))
|
|
73
|
+
) {
|
|
74
|
+
return finding('additive', 'new optional field')
|
|
75
|
+
}
|
|
76
|
+
return finding('breaking', `new required field in ${parent}`, 'fill')
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
function addedFinding(key: string, rows: ReadonlyMap<string, Row>): Finding {
|
|
80
|
+
if (key.startsWith(CHAIN)) {
|
|
81
|
+
return finding('additive', `new chain step ${key.slice(CHAIN.length)}`)
|
|
82
|
+
}
|
|
83
|
+
if (key.startsWith(DEPRECATIONS)) {
|
|
84
|
+
return finding(
|
|
85
|
+
'deprecation',
|
|
86
|
+
`deprecates ${key.slice(DEPRECATIONS.length)}`,
|
|
87
|
+
)
|
|
88
|
+
}
|
|
89
|
+
const [parent, mark] = lastSegment(key)
|
|
90
|
+
const plain: Readonly<Record<string, Finding>> = {
|
|
91
|
+
'': finding('additive', 'new export'),
|
|
92
|
+
'|': finding('additive', 'new union option'),
|
|
93
|
+
}
|
|
94
|
+
if (mark === '.') {
|
|
95
|
+
return fieldFinding(parent, rows.get(key))
|
|
96
|
+
}
|
|
97
|
+
const tuple = mark === '[' && !key.endsWith('[]')
|
|
98
|
+
return (
|
|
99
|
+
plain[mark] ??
|
|
100
|
+
(tuple
|
|
101
|
+
? finding('breaking', 'new tuple item', 'fill')
|
|
102
|
+
: finding('review', 'new part'))
|
|
103
|
+
)
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
function removedFinding(key: string): Finding {
|
|
107
|
+
if (key.startsWith(CHAIN)) {
|
|
108
|
+
return finding(
|
|
109
|
+
'breaking',
|
|
110
|
+
`deletes chain step ${key.slice(CHAIN.length)} (FE13 gate)`,
|
|
111
|
+
)
|
|
112
|
+
}
|
|
113
|
+
if (key.startsWith(DEPRECATIONS)) {
|
|
114
|
+
return finding(
|
|
115
|
+
'breaking',
|
|
116
|
+
`deletes deprecated ${key.slice(DEPRECATIONS.length)} (FE13 gate)`,
|
|
117
|
+
)
|
|
118
|
+
}
|
|
119
|
+
const [, mark] = lastSegment(key)
|
|
120
|
+
const [text, op] = REMOVED[mark] ?? ['part removed', undefined]
|
|
121
|
+
return finding('breaking', text, op)
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
function withPath(path: string, found: Finding): Change {
|
|
125
|
+
return { path, ...found }
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
/** Rows present on both sides whose facts moved. */
|
|
129
|
+
function changedRows(
|
|
130
|
+
before: ReadonlyMap<string, Row>,
|
|
131
|
+
after: ReadonlyMap<string, Row>,
|
|
132
|
+
): readonly Change[] {
|
|
133
|
+
return [...after].flatMap(
|
|
134
|
+
([key, row]: readonly [string, Row]): readonly Change[] => {
|
|
135
|
+
const old = before.get(key)
|
|
136
|
+
if (old === undefined || JSON.stringify(old) === JSON.stringify(row)) {
|
|
137
|
+
return []
|
|
138
|
+
}
|
|
139
|
+
return changedFindings(key, old, row).map((found: Finding): Change =>
|
|
140
|
+
withPath(key, found),
|
|
141
|
+
)
|
|
142
|
+
},
|
|
143
|
+
)
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
/** Exported functions (FE1 excludes them with a reason): a new one adds, a gone one breaks importers. */
|
|
147
|
+
function excludedChanges(before: LockData, after: LockData): readonly Change[] {
|
|
148
|
+
const added = Object.keys(after.excluded).filter(
|
|
149
|
+
(key: string): boolean => !(key in before.excluded),
|
|
150
|
+
)
|
|
151
|
+
const gone = Object.keys(before.excluded).filter(
|
|
152
|
+
(key: string): boolean => !(key in after.excluded),
|
|
153
|
+
)
|
|
154
|
+
return [
|
|
155
|
+
...added.map((key: string): Change =>
|
|
156
|
+
withPath(key, finding('additive', 'new export (not locked: function)')),
|
|
157
|
+
),
|
|
158
|
+
...gone.map((key: string): Change =>
|
|
159
|
+
withPath(key, finding('breaking', 'export removed (was a function)')),
|
|
160
|
+
),
|
|
161
|
+
]
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
function worstOf(changes: readonly Change[]): ChangeKind | 'none' {
|
|
165
|
+
return (
|
|
166
|
+
KINDS.find((kind: ChangeKind): boolean =>
|
|
167
|
+
changes.some((found: Readonly<Change>): boolean => found.kind === kind),
|
|
168
|
+
) ?? 'none'
|
|
169
|
+
)
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
function order(left: Readonly<Change>, right: Readonly<Change>): number {
|
|
173
|
+
const rank = KINDS.indexOf(left.kind) - KINDS.indexOf(right.kind)
|
|
174
|
+
if (rank !== NONE) {
|
|
175
|
+
return rank
|
|
176
|
+
}
|
|
177
|
+
return left.path < right.path ? BEFORE : NEXT
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
/** Keys only one side has, topmost only; an option list (`x|`) whose node both sides hold is that node's change. */
|
|
181
|
+
function onlyIn(
|
|
182
|
+
side: ReadonlyMap<string, Row>,
|
|
183
|
+
other: ReadonlyMap<string, Row>,
|
|
184
|
+
): readonly string[] {
|
|
185
|
+
const keys = [...side.keys()].filter(
|
|
186
|
+
(key: string): boolean =>
|
|
187
|
+
!other.has(key) &&
|
|
188
|
+
!(
|
|
189
|
+
key.endsWith('|') &&
|
|
190
|
+
other.has(key.slice(NONE, BEFORE)) &&
|
|
191
|
+
side.has(key.slice(NONE, BEFORE))
|
|
192
|
+
),
|
|
193
|
+
)
|
|
194
|
+
return topmost(new Set(keys))
|
|
195
|
+
}
|
|
196
|
+
|
|
197
|
+
/** Every change from one lock to the next, classified. */
|
|
198
|
+
function formatDiff(before: LockData, after: LockData): Report {
|
|
199
|
+
const was = normalRows(before.rows)
|
|
200
|
+
const now = normalRows(after.rows)
|
|
201
|
+
const added = onlyIn(now, was)
|
|
202
|
+
const gone = onlyIn(was, now)
|
|
203
|
+
const pairs = renames(gone, added, [was, now])
|
|
204
|
+
const paired = new Set(pairs.values())
|
|
205
|
+
const changes = [
|
|
206
|
+
...[...pairs].map(([from, to]: readonly [string, string]): Change =>
|
|
207
|
+
renameChange(from, to),
|
|
208
|
+
),
|
|
209
|
+
...gone
|
|
210
|
+
.filter((key: string): boolean => !pairs.has(key))
|
|
211
|
+
.map((key: string): Change => withPath(key, removedFinding(key))),
|
|
212
|
+
...added
|
|
213
|
+
.filter((key: string): boolean => !paired.has(key))
|
|
214
|
+
.map((key: string): Change => withPath(key, addedFinding(key, now))),
|
|
215
|
+
...changedRows(was, now),
|
|
216
|
+
...excludedChanges(before, after),
|
|
217
|
+
].toSorted(order)
|
|
218
|
+
const chain =
|
|
219
|
+
before.chainId === after.chainId
|
|
220
|
+
? undefined
|
|
221
|
+
: { before: before.chainId, after: after.chainId }
|
|
222
|
+
return { chain, changes, worst: worstOf(changes) }
|
|
223
|
+
}
|
|
224
|
+
|
|
225
|
+
export { formatDiff, KINDS }
|
|
226
|
+
export type { Report }
|