/
report.ts
219 lines (182 loc) · 6.47 KB
/
report.ts
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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
import chalk from 'chalk';
import type { BasicOperation } from '../levenshtein';
import type { ParsedTypeDetailed } from './layout';
import type { StorageOperation, StorageItem, StorageField, TypeChange, EnumOperation } from './compare';
import { itemize, itemizeWith } from '../utils/itemize';
import { indent } from '../utils/indent';
import { assert } from '../utils/assert';
export class LayoutCompatibilityReport {
readonly pass: boolean;
constructor(readonly ops: StorageOperation<StorageItem>[]) {
this.pass = ops.length === 0;
}
explain(): string {
const res = [];
for (const op of this.ops) {
const src = 'updated' in op ? op.updated.src : op.original.contract;
res.push(
chalk.bold(src) + ':' + indent(explainStorageOperation(op, { kind: 'layout', allowAppend: true }), 2, 1),
);
}
return res.join('\n\n');
}
}
interface StorageOperationContext {
kind: 'struct' | 'layout';
allowAppend: boolean;
}
function explainStorageOperation(op: StorageOperation<StorageField>, ctx: StorageOperationContext): string {
switch (op.kind) {
case 'typechange': {
const basic = explainTypeChange(op.change);
const details =
ctx.kind === 'layout' // explain details for layout only
? new Set(
getAllTypeChanges(op.change)
.map(explainTypeChangeDetails)
.filter((d?: string): d is string => d !== undefined),
)
: [];
return `Upgraded ${label(op.updated)} to an incompatible type\n` + itemize(basic, ...details);
}
case 'rename':
return `Renamed ${label(op.original)} to ${label(op.updated)}`;
case 'replace':
return `Replaced ${label(op.original)} with ${label(op.updated)} of incompatible type`;
default: {
const title = explainBasicOperation(op, t => t.label);
const hints = [];
switch (op.kind) {
case 'insert': {
if (ctx.kind === 'struct') {
if (ctx.allowAppend) {
hints.push('New struct members should be placed after existing ones');
} else {
hints.push('New struct members are not allowed here. Define a new struct');
}
} else {
hints.push('New variables should be placed after all existing inherited variables');
}
break;
}
case 'delete': {
hints.push('Keep the variable even if unused');
break;
}
}
return title + '\n' + itemizeWith('>', ...hints);
}
}
}
function explainTypeChange(ch: TypeChange): string {
switch (ch.kind) {
case 'obvious mismatch':
case 'struct members':
case 'enum members':
return `Bad upgrade ${describeTransition(ch.original, ch.updated)}`;
case 'enum resize':
return `Bad upgrade ${describeTransition(ch.original, ch.updated)}\nDifferent representation sizes`;
case 'mapping key': {
assert(ch.original.args && ch.updated.args);
const originalKey = ch.original.args[0];
const updatedKey = ch.updated.args[0];
return `In key of ${ch.updated.item.label}\n- Bad upgrade ${describeTransition(originalKey, updatedKey)}`;
}
case 'mapping value':
case 'array value':
return `In ${ch.updated.item.label}\n` + itemize(explainTypeChange(ch.inner));
case 'array shrink':
case 'array grow': {
assert(ch.original.tail && ch.updated.tail);
const originalSize = parseInt(ch.original.tail, 10);
const updatedSize = parseInt(ch.updated.tail, 10);
const note = ch.kind === 'array shrink' ? 'Size cannot decrease' : 'Size cannot increase here';
return `Bad array resize from ${originalSize} to ${updatedSize}\n${note}`;
}
case 'array dynamic': {
assert(ch.original.tail && ch.updated.tail);
const [originalSize, updatedSize] = ch.original.tail === 'dyn' ? ['dynamic', 'fixed'] : ['fixed', 'dynamic'];
return `Bad upgrade from ${originalSize} to ${updatedSize} size array`;
}
case 'missing members': {
const type = ch.updated.head.replace(/^t_/, ''); // t_struct, t_enum -> struct, enum
return `Insufficient data to compare ${type}s\nManually assess compatibility, then use option \`unsafeAllowCustomTypes: true\``;
}
case 'unknown':
return `Unknown type ${ch.updated.item.label}`;
}
}
function getAllTypeChanges(root: TypeChange): TypeChange[] {
const list = [root];
for (const ch of list) {
switch (ch.kind) {
case 'mapping value':
case 'array value':
list.push(ch.inner);
break;
case 'struct members': {
for (const op of ch.ops) {
if (op.kind === 'typechange') {
list.push(op.change);
}
}
break;
}
// We mention all other kinds explicitly to review any future new kinds
case 'obvious mismatch':
case 'enum members':
case 'enum resize':
case 'mapping key':
case 'array shrink':
case 'array grow':
case 'array dynamic':
case 'missing members':
case 'unknown':
break;
}
}
return list;
}
function explainTypeChangeDetails(ch: TypeChange): string | undefined {
switch (ch.kind) {
case 'struct members': {
const { allowAppend } = ch;
return (
`In ${ch.updated.item.label}\n` +
itemize(...ch.ops.map(op => explainStorageOperation(op, { kind: 'struct', allowAppend })))
);
}
case 'enum members':
return `In ${ch.updated.item.label}\n` + itemize(...ch.ops.map(explainEnumOperation));
}
}
function explainEnumOperation(op: EnumOperation): string {
switch (op.kind) {
case 'replace':
return `Replaced \`${op.original}\` with \`${op.updated}\``;
default:
return explainBasicOperation(op, t => t);
}
}
function explainBasicOperation<T>(op: BasicOperation<T>, getName: (t: T) => string): string {
switch (op.kind) {
case 'delete':
return `Deleted \`${getName(op.original)}\``;
case 'insert':
return `Inserted \`${getName(op.updated)}\``;
case 'append':
return `Added \`${getName(op.updated)}\``;
}
}
function describeTransition(original: ParsedTypeDetailed, updated: ParsedTypeDetailed): string {
const originalLabel = original.item.label;
const updatedLabel = updated.item.label;
if (originalLabel === updatedLabel) {
return `to ${updatedLabel}`;
} else {
return `from ${originalLabel} to ${updatedLabel}`;
}
}
function label(variable: { label: string }): string {
return '`' + variable.label + '`';
}