@plannotator/sem-jiff 0.2.0 → 0.2.2
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/CHANGELOG.md +41 -0
- package/README.md +20 -8
- package/dist/index.d.ts +6 -3
- package/dist/index.js +94 -6
- package/dist/inline.js +245 -9
- package/dist/linediff.d.ts +2 -1
- package/dist/linediff.js +44 -2
- package/dist/pairing.d.ts +1 -1
- package/dist/pairing.js +137 -5
- package/dist/parse.d.ts +4 -3
- package/dist/parse.js +27 -8
- package/dist/runtime.d.ts +2 -0
- package/dist/runtime.js +9 -0
- package/dist/structure.d.ts +11 -1
- package/dist/structure.js +222 -5
- package/dist/symbol-types.d.ts +18 -0
- package/dist/symbol-types.js +1 -0
- package/dist/symbols.d.ts +6 -0
- package/dist/symbols.js +458 -0
- package/dist/syntax.d.ts +3 -1
- package/dist/syntax.js +42 -4
- package/package.json +1 -1
package/dist/symbols.js
ADDED
|
@@ -0,0 +1,458 @@
|
|
|
1
|
+
import { functionsIn } from "./syntax.js";
|
|
2
|
+
const FUNCTION_KINDS = new Set(['function', 'method', 'constructor']);
|
|
3
|
+
const line = (n) => n.startPosition.row + 1;
|
|
4
|
+
const lastLine = (n) => n.endPosition.row + 1;
|
|
5
|
+
/** First line of the comments and attributes directly above a declaration (no blank line between). */
|
|
6
|
+
function attachedStart(n, cfg) {
|
|
7
|
+
let at = line(n);
|
|
8
|
+
for (let c = n.previousSibling; c && cfg.attachedTypes.has(c.type); c = c.previousSibling) {
|
|
9
|
+
const end = c.endPosition.column === 0 && c.endPosition.row > c.startPosition.row ? c.endPosition.row : c.endPosition.row + 1;
|
|
10
|
+
if (end !== at - 1)
|
|
11
|
+
break;
|
|
12
|
+
at = line(c);
|
|
13
|
+
}
|
|
14
|
+
return at;
|
|
15
|
+
}
|
|
16
|
+
const bareType = (t) => (t.match(/[A-Za-z_][\w]*/g) ?? []).filter((w) => !/^(where|extension|impl|for)$/.test(w))[0] ?? t;
|
|
17
|
+
/** Name and container prefix of a declaration, per language: receivers, namespaces and `self.` are taken off the name. */
|
|
18
|
+
function nameOf(d, cfg) {
|
|
19
|
+
const raw = d.name;
|
|
20
|
+
if (d.kind === 'import' || d.kind === 'export-from') {
|
|
21
|
+
const key = raw.slice(raw.lastIndexOf(' ') + 1);
|
|
22
|
+
return key ? { name: key } : null;
|
|
23
|
+
}
|
|
24
|
+
switch (cfg.id) {
|
|
25
|
+
case 'kotlin': {
|
|
26
|
+
if (d.kind === 'companion')
|
|
27
|
+
return { name: raw === 'companion' ? 'Companion' : raw.slice('companion '.length) };
|
|
28
|
+
const dot = d.kind.startsWith('function') || d.kind === 'property' ? raw.lastIndexOf('.') : -1;
|
|
29
|
+
return dot > 0 ? { name: raw.slice(dot + 1), receiver: raw.slice(0, dot) } : { name: raw };
|
|
30
|
+
}
|
|
31
|
+
case 'go': {
|
|
32
|
+
const m = raw.match(/^\(\s*\*?\s*([A-Za-z_]\w*)[^)]*\)\.(.+)$/);
|
|
33
|
+
return m ? { name: m[2], receiver: m[1] } : { name: raw };
|
|
34
|
+
}
|
|
35
|
+
case 'php':
|
|
36
|
+
return { name: raw.slice(raw.lastIndexOf('\\') + 1) };
|
|
37
|
+
case 'ruby':
|
|
38
|
+
return { name: raw.replace(/^self\./, '') };
|
|
39
|
+
default:
|
|
40
|
+
return { name: raw };
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
/** The kind of a declaration (null: give no symbol), from its config kind and node type. */
|
|
44
|
+
function kindOf(d, parent, cfg) {
|
|
45
|
+
const k = d.kind;
|
|
46
|
+
const t = d.node.type;
|
|
47
|
+
if (k === 'import' || k === 'export-from')
|
|
48
|
+
return 'import';
|
|
49
|
+
const inType = !!parent && parent.kind !== null && ['class', 'interface', 'enum', 'struct', 'other'].includes(parent.kind);
|
|
50
|
+
const fn = () => (inType ? 'method' : 'function');
|
|
51
|
+
switch (cfg.id) {
|
|
52
|
+
case 'typescript':
|
|
53
|
+
case 'tsx': {
|
|
54
|
+
const decl = t === 'export_statement' ? (d.node.childForFieldName('declaration') ?? d.node) : d.node;
|
|
55
|
+
const dt = decl.type;
|
|
56
|
+
if (/^(function_declaration|generator_function_declaration|function_signature)$/.test(dt))
|
|
57
|
+
return 'function';
|
|
58
|
+
if (dt === 'method_definition' || dt === 'method_signature' || dt === 'abstract_method_signature')
|
|
59
|
+
return d.name === 'constructor' ? 'constructor' : 'method';
|
|
60
|
+
if (dt === 'class_declaration' || dt === 'abstract_class_declaration')
|
|
61
|
+
return 'class';
|
|
62
|
+
if (dt === 'interface_declaration')
|
|
63
|
+
return 'interface';
|
|
64
|
+
if (dt === 'enum_declaration')
|
|
65
|
+
return 'enum';
|
|
66
|
+
if (dt === 'type_alias_declaration')
|
|
67
|
+
return 'type';
|
|
68
|
+
if (dt === 'internal_module' || dt === 'module')
|
|
69
|
+
return 'namespace';
|
|
70
|
+
if (dt === 'lexical_declaration' || dt === 'variable_declaration') {
|
|
71
|
+
const value = decl.namedChildren.find((c) => c?.type === 'variable_declarator')?.childForFieldName('value');
|
|
72
|
+
return value && /^(arrow_function|function_expression|function|generator_function)$/.test(value.type) ? 'function' : 'variable';
|
|
73
|
+
}
|
|
74
|
+
if (dt === 'public_field_definition') {
|
|
75
|
+
const value = decl.childForFieldName('value');
|
|
76
|
+
return value && /^(arrow_function|function_expression)$/.test(value.type) ? 'method' : 'field';
|
|
77
|
+
}
|
|
78
|
+
return null;
|
|
79
|
+
}
|
|
80
|
+
case 'python': {
|
|
81
|
+
const def = t === 'decorated_definition' ? d.node.childForFieldName('definition') : d.node;
|
|
82
|
+
if (def?.type === 'function_definition')
|
|
83
|
+
return inType ? (d.name === '__init__' ? 'constructor' : 'method') : 'function';
|
|
84
|
+
if (def?.type === 'class_definition')
|
|
85
|
+
return 'class';
|
|
86
|
+
if (k === 'assignment')
|
|
87
|
+
return inType ? 'field' : 'variable';
|
|
88
|
+
return null;
|
|
89
|
+
}
|
|
90
|
+
case 'go': {
|
|
91
|
+
if (t === 'function_declaration')
|
|
92
|
+
return 'function';
|
|
93
|
+
if (t === 'method_declaration')
|
|
94
|
+
return 'method';
|
|
95
|
+
if (t === 'type_declaration') {
|
|
96
|
+
const spec = d.node.namedChildren.find((c) => c?.type === 'type_spec' || c?.type === 'type_alias');
|
|
97
|
+
const ty = spec?.childForFieldName('type')?.type;
|
|
98
|
+
return ty === 'struct_type' ? 'struct' : ty === 'interface_type' ? 'interface' : 'type';
|
|
99
|
+
}
|
|
100
|
+
if (t === 'const_declaration' || t === 'var_declaration')
|
|
101
|
+
return 'variable';
|
|
102
|
+
return null;
|
|
103
|
+
}
|
|
104
|
+
case 'rust': {
|
|
105
|
+
if (t === 'function_item' || t === 'function_signature_item')
|
|
106
|
+
return parent && (parent.node.type === 'impl_item' || parent.node.type === 'trait_item') ? 'method' : 'function';
|
|
107
|
+
if (t === 'struct_item' || t === 'union_item')
|
|
108
|
+
return 'struct';
|
|
109
|
+
if (t === 'enum_item')
|
|
110
|
+
return 'enum';
|
|
111
|
+
if (t === 'trait_item')
|
|
112
|
+
return 'interface';
|
|
113
|
+
if (t === 'mod_item')
|
|
114
|
+
return 'module';
|
|
115
|
+
if (t === 'const_item' || t === 'static_item')
|
|
116
|
+
return 'variable';
|
|
117
|
+
if (t === 'type_item')
|
|
118
|
+
return 'type';
|
|
119
|
+
return null; // impl blocks, macros, extern crates
|
|
120
|
+
}
|
|
121
|
+
case 'java': {
|
|
122
|
+
if (t === 'method_declaration')
|
|
123
|
+
return 'method';
|
|
124
|
+
if (t === 'constructor_declaration' || t === 'compact_constructor_declaration')
|
|
125
|
+
return 'constructor';
|
|
126
|
+
if (t === 'class_declaration' || t === 'record_declaration')
|
|
127
|
+
return 'class';
|
|
128
|
+
if (t === 'interface_declaration' || t === 'annotation_type_declaration')
|
|
129
|
+
return 'interface';
|
|
130
|
+
if (t === 'enum_declaration')
|
|
131
|
+
return 'enum';
|
|
132
|
+
if (k === 'field_declaration')
|
|
133
|
+
return 'field';
|
|
134
|
+
return null;
|
|
135
|
+
}
|
|
136
|
+
case 'csharp': {
|
|
137
|
+
if (t === 'method_declaration' || t === 'local_function_statement')
|
|
138
|
+
return t === 'method_declaration' ? 'method' : 'function';
|
|
139
|
+
if (t === 'constructor_declaration')
|
|
140
|
+
return 'constructor';
|
|
141
|
+
if (t === 'class_declaration' || t === 'record_declaration')
|
|
142
|
+
return 'class';
|
|
143
|
+
if (t === 'struct_declaration' || t === 'record_struct_declaration')
|
|
144
|
+
return 'struct';
|
|
145
|
+
if (t === 'interface_declaration')
|
|
146
|
+
return 'interface';
|
|
147
|
+
if (t === 'enum_declaration')
|
|
148
|
+
return 'enum';
|
|
149
|
+
if (t === 'property_declaration')
|
|
150
|
+
return 'property';
|
|
151
|
+
if (k === 'field_declaration')
|
|
152
|
+
return 'field';
|
|
153
|
+
if (t === 'delegate_declaration')
|
|
154
|
+
return 'type';
|
|
155
|
+
if (t === 'namespace_declaration')
|
|
156
|
+
return 'namespace';
|
|
157
|
+
return null;
|
|
158
|
+
}
|
|
159
|
+
case 'php': {
|
|
160
|
+
if (t === 'function_definition')
|
|
161
|
+
return 'function';
|
|
162
|
+
if (t === 'method_declaration')
|
|
163
|
+
return d.name === '__construct' ? 'constructor' : 'method';
|
|
164
|
+
if (t === 'class_declaration' || t === 'trait_declaration')
|
|
165
|
+
return 'class';
|
|
166
|
+
if (t === 'interface_declaration')
|
|
167
|
+
return 'interface';
|
|
168
|
+
if (t === 'enum_declaration')
|
|
169
|
+
return 'enum';
|
|
170
|
+
if (t === 'enum_case')
|
|
171
|
+
return 'field';
|
|
172
|
+
if (t === 'property_declaration')
|
|
173
|
+
return 'property';
|
|
174
|
+
if (t === 'const_declaration')
|
|
175
|
+
return inType ? 'field' : 'variable';
|
|
176
|
+
if (k === 'namespace')
|
|
177
|
+
return 'namespace';
|
|
178
|
+
if (k === 'use_declaration')
|
|
179
|
+
return 'other';
|
|
180
|
+
return null;
|
|
181
|
+
}
|
|
182
|
+
case 'ruby': {
|
|
183
|
+
if (k === 'method' || k === 'define_method')
|
|
184
|
+
return fn();
|
|
185
|
+
if (k === 'class')
|
|
186
|
+
return 'class';
|
|
187
|
+
if (k === 'module')
|
|
188
|
+
return 'module';
|
|
189
|
+
if (k === 'constant')
|
|
190
|
+
return 'variable';
|
|
191
|
+
if (/^attr(_reader|_writer|_accessor)?$/.test(k))
|
|
192
|
+
return d.name.includes(',') ? null : 'property';
|
|
193
|
+
return null; // `class << self`, visibility lines
|
|
194
|
+
}
|
|
195
|
+
case 'kotlin': {
|
|
196
|
+
if (k.startsWith('function('))
|
|
197
|
+
return fn();
|
|
198
|
+
if (k.startsWith('constructor('))
|
|
199
|
+
return 'constructor';
|
|
200
|
+
if (k === 'class' || k === 'object' || k === 'companion')
|
|
201
|
+
return 'class';
|
|
202
|
+
if (k === 'interface')
|
|
203
|
+
return 'interface';
|
|
204
|
+
if (k === 'enum class')
|
|
205
|
+
return 'enum';
|
|
206
|
+
if (k === 'property')
|
|
207
|
+
return d.name.startsWith('(') ? null : 'property';
|
|
208
|
+
if (k === 'type_alias')
|
|
209
|
+
return 'type';
|
|
210
|
+
if (k === 'enum_entry')
|
|
211
|
+
return 'field';
|
|
212
|
+
if (k === 'init')
|
|
213
|
+
return 'other';
|
|
214
|
+
return null;
|
|
215
|
+
}
|
|
216
|
+
case 'swift': {
|
|
217
|
+
if (k.startsWith('func(') || k.startsWith('requirement func(')) {
|
|
218
|
+
if (t === 'init_declaration')
|
|
219
|
+
return 'constructor';
|
|
220
|
+
if (t === 'subscript_declaration')
|
|
221
|
+
return null;
|
|
222
|
+
return fn();
|
|
223
|
+
}
|
|
224
|
+
if (k === 'class' || k === 'actor')
|
|
225
|
+
return 'class';
|
|
226
|
+
if (k === 'struct')
|
|
227
|
+
return 'struct';
|
|
228
|
+
if (k === 'enum')
|
|
229
|
+
return 'enum';
|
|
230
|
+
if (t === 'protocol_declaration' || k === 'protocol')
|
|
231
|
+
return 'interface';
|
|
232
|
+
if (k === 'extension')
|
|
233
|
+
return 'other';
|
|
234
|
+
if (k === 'property' || k === 'requirement property')
|
|
235
|
+
return inType ? 'property' : 'variable';
|
|
236
|
+
if (k === 'case')
|
|
237
|
+
return d.name.includes(',') ? null : 'field';
|
|
238
|
+
if (t === 'typealias_declaration' || t === 'associatedtype_declaration')
|
|
239
|
+
return 'type';
|
|
240
|
+
return null;
|
|
241
|
+
}
|
|
242
|
+
}
|
|
243
|
+
return null;
|
|
244
|
+
}
|
|
245
|
+
/** Every declaration in a tree, as the engine's config sees them, with its kind, container and children. */
|
|
246
|
+
function declarations(root, cfg) {
|
|
247
|
+
const all = [];
|
|
248
|
+
const visit = (container, parent) => {
|
|
249
|
+
for (const d of cfg.declarationsIn(container)) {
|
|
250
|
+
const named = nameOf(d, cfg);
|
|
251
|
+
const kind = named ? kindOf(d, parent, cfg) : null;
|
|
252
|
+
const outer = parent?.scope;
|
|
253
|
+
const container_ = named?.receiver ? (outer ? `${outer}.${bareType(named.receiver)}` : bareType(named.receiver)) : outer;
|
|
254
|
+
const node = d.node;
|
|
255
|
+
const end = lastLine(d.tail ?? node);
|
|
256
|
+
const decl = { node, start: attachedStart(node, cfg), bodyStart: line(node), end, name: named?.name ?? d.name, kind, container: container_, parent, children: [] };
|
|
257
|
+
// Members of an extension or impl belong to the type it extends or implements.
|
|
258
|
+
const own = cfg.id === 'swift' && d.kind === 'extension' ? bareType(d.name) : node.type === 'impl_item' ? bareType((node.childForFieldName('type')?.text ?? d.name).replace(/<.*$/s, '')) : decl.name;
|
|
259
|
+
decl.scope = cfg.id === 'swift' && d.kind === 'extension' ? own : outer ? `${outer}.${own}` : own;
|
|
260
|
+
parent?.children.push(decl);
|
|
261
|
+
all.push(decl);
|
|
262
|
+
if (d.members)
|
|
263
|
+
visit(d.members, decl);
|
|
264
|
+
}
|
|
265
|
+
};
|
|
266
|
+
visit(root, undefined);
|
|
267
|
+
return all;
|
|
268
|
+
}
|
|
269
|
+
const symbolOf = (d) => d.kind === null || !d.name ? null : { name: d.name, kind: d.kind, ...(d.container ? { container: d.container } : {}) };
|
|
270
|
+
/** Lines of a side's ranges, blank lines dropped. */
|
|
271
|
+
const nonBlank = (ranges, lines) => {
|
|
272
|
+
const out = [];
|
|
273
|
+
for (const r of ranges)
|
|
274
|
+
for (let l = r.start; l <= r.end; l++)
|
|
275
|
+
if ((lines[l - 1] ?? '').trim())
|
|
276
|
+
out.push(l);
|
|
277
|
+
return out;
|
|
278
|
+
};
|
|
279
|
+
/** The one declaration a single range is exactly (from its first or attached line to its last). */
|
|
280
|
+
function whole(ranges, decls, lines) {
|
|
281
|
+
const ls = nonBlank(ranges, lines);
|
|
282
|
+
if (!ls.length)
|
|
283
|
+
return null;
|
|
284
|
+
const lo = ls[0];
|
|
285
|
+
const hi = ls.at(-1);
|
|
286
|
+
const hits = decls.filter((d) => (d.start === lo || d.bodyStart === lo) && d.end === hi);
|
|
287
|
+
if (hits.length === 1)
|
|
288
|
+
return hits[0];
|
|
289
|
+
// A declaration and its only member on the same lines: the outer one is what the lines are.
|
|
290
|
+
const outer = hits.filter((d) => !hits.some((o) => o !== d && o === d.parent));
|
|
291
|
+
return hits.length && outer.length === 1 ? outer[0] : null;
|
|
292
|
+
}
|
|
293
|
+
/** The innermost declaration holding every line, unless the lines also reach into more than one of its members. */
|
|
294
|
+
function holder(ranges, decls, lines) {
|
|
295
|
+
const ls = nonBlank(ranges, lines);
|
|
296
|
+
if (!ls.length)
|
|
297
|
+
return null;
|
|
298
|
+
const lo = ls[0];
|
|
299
|
+
const hi = ls.at(-1);
|
|
300
|
+
const holding = decls.filter((d) => d.start <= lo && hi <= d.end);
|
|
301
|
+
if (!holding.length)
|
|
302
|
+
return null;
|
|
303
|
+
// The smallest holder; when a declaration and its member cover the same lines (`object A { val x = 1 }`), the outer one.
|
|
304
|
+
const inner = holding.reduce((a, b) => (b.end - b.start < a.end - a.start || (b.end - b.start === a.end - a.start && a.parent === b) ? b : a));
|
|
305
|
+
// Lines in the holder that also reach into one of its members (and not only that member) are about more than one thing.
|
|
306
|
+
if (ls.some((l) => inner.children.some((c) => c.start <= l && l <= c.end)) && !(lo <= inner.start && inner.end <= hi))
|
|
307
|
+
return null;
|
|
308
|
+
return inner;
|
|
309
|
+
}
|
|
310
|
+
const same = (a, b) => !!a && !!b && a.name === b.name && a.kind === b.kind && a.container === b.container;
|
|
311
|
+
const agree = (a, b) => (a === undefined ? b ?? null : b === undefined ? a : same(a, b) ? a : null);
|
|
312
|
+
/** The text a span range covers, on one line. */
|
|
313
|
+
function textAt(lines, r) {
|
|
314
|
+
if (r.start.line !== r.end.line)
|
|
315
|
+
return null;
|
|
316
|
+
return (lines[r.start.line - 1] ?? '').slice(r.start.character, r.end.character) || null;
|
|
317
|
+
}
|
|
318
|
+
const VARIABLE_PARENT = /parameter|variable_declarator|variable_declaration|let_declaration|short_var_declaration|assignment|pattern|catch|for_statement|for_in|range_clause|lambda|closure_parameters|block_parameters|method_parameters|value_binding|property_declaration|multi_variable/;
|
|
319
|
+
/** The kind of a renamed name, from where it is declared (on the old side): a declaration, or a local or parameter. */
|
|
320
|
+
function renamedSymbol(op, oldDecls, newDecls, oldRoot, oldLines, newLines, cfg) {
|
|
321
|
+
if (op.spans.length !== 1)
|
|
322
|
+
return null;
|
|
323
|
+
const span = op.spans[0];
|
|
324
|
+
const olds = 'old' in span ? (span.old ?? []) : [];
|
|
325
|
+
const news = 'new' in span ? (span.new ?? []) : [];
|
|
326
|
+
const oldNames = new Set(olds.map((r) => textAt(oldLines, r)));
|
|
327
|
+
const newNames = new Set(news.map((r) => textAt(newLines, r)));
|
|
328
|
+
if (oldNames.size !== 1 || newNames.size !== 1)
|
|
329
|
+
return null;
|
|
330
|
+
const oldName = [...oldNames][0];
|
|
331
|
+
const newName = [...newNames][0];
|
|
332
|
+
if (!oldName || !newName || oldName === newName)
|
|
333
|
+
return null;
|
|
334
|
+
const declared = oldDecls.filter((d) => d.name === oldName && d.kind !== null && d.kind !== 'import');
|
|
335
|
+
if (declared.length) {
|
|
336
|
+
const kinds = new Set(declared.map((d) => `${d.kind}\u0000${d.container ?? ''}`));
|
|
337
|
+
if (kinds.size !== 1)
|
|
338
|
+
return null;
|
|
339
|
+
const d = declared[0];
|
|
340
|
+
const after = newDecls.filter((x) => x.name === newName && x.kind === d.kind && x.container === d.container);
|
|
341
|
+
if (!after.length)
|
|
342
|
+
return null;
|
|
343
|
+
return { name: newName, kind: d.kind, oldName, ...(d.container ? { container: d.container } : {}) };
|
|
344
|
+
}
|
|
345
|
+
// Not a declaration the config lists: a local or a parameter, when one of the old name's uses declares it.
|
|
346
|
+
let local = false;
|
|
347
|
+
let other = false;
|
|
348
|
+
const walk = (n) => {
|
|
349
|
+
if (n.childCount === 0) {
|
|
350
|
+
if (cfg.identifierTypes.has(n.type) && n.text === oldName && n.parent) {
|
|
351
|
+
const p = n.parent;
|
|
352
|
+
if (/type_parameter/.test(p.type))
|
|
353
|
+
other = true;
|
|
354
|
+
else if (VARIABLE_PARENT.test(p.type) || VARIABLE_PARENT.test(p.parent?.type ?? ''))
|
|
355
|
+
local = true;
|
|
356
|
+
}
|
|
357
|
+
return;
|
|
358
|
+
}
|
|
359
|
+
for (const c of n.children)
|
|
360
|
+
if (c)
|
|
361
|
+
walk(c);
|
|
362
|
+
};
|
|
363
|
+
walk(oldRoot);
|
|
364
|
+
if (!local || other)
|
|
365
|
+
return null;
|
|
366
|
+
const holderFn = holder(op.lines.old, oldDecls, oldLines);
|
|
367
|
+
const container = holderFn && FUNCTION_KINDS.has(holderFn.kind ?? 'other') ? [holderFn.container, holderFn.name].filter(Boolean).join('.') : undefined;
|
|
368
|
+
return { name: newName, kind: 'variable', oldName, ...(container ? { container } : {}) };
|
|
369
|
+
}
|
|
370
|
+
/** Functions on one side lying wholly inside an operation's ranges. */
|
|
371
|
+
function functionsWithin(ranges, decls) {
|
|
372
|
+
const covered = new Set();
|
|
373
|
+
for (const r of ranges)
|
|
374
|
+
for (let l = r.start; l <= r.end; l++)
|
|
375
|
+
covered.add(l);
|
|
376
|
+
return decls.filter((d) => {
|
|
377
|
+
if (d.kind === null || !FUNCTION_KINDS.has(d.kind))
|
|
378
|
+
return false;
|
|
379
|
+
for (let l = d.bodyStart; l <= d.end; l++)
|
|
380
|
+
if (!covered.has(l))
|
|
381
|
+
return false;
|
|
382
|
+
return true;
|
|
383
|
+
});
|
|
384
|
+
}
|
|
385
|
+
/** Set `symbol` on every operation the rules name a declaration for (docs/symbols.md); never changes anything else. */
|
|
386
|
+
export function attachSymbols(ops, oldRoot, newRoot, oldText, newText, cfg) {
|
|
387
|
+
const oldDecls = declarations(oldRoot, cfg);
|
|
388
|
+
const newDecls = declarations(newRoot, cfg);
|
|
389
|
+
// Functions the config does not list as declarations (nested ones): known only by name, as functions.
|
|
390
|
+
const nested = (root, decls) => {
|
|
391
|
+
const out = [];
|
|
392
|
+
// A function the config already lists (perhaps under an `export` or a decorator) is not a nested one.
|
|
393
|
+
const listed = (f) => decls.some((d) => d.node.startIndex <= f.startIndex && f.endIndex <= d.node.endIndex && d.bodyStart <= line(f) && lastLine(f) === d.end && (d.node.startIndex === f.startIndex || d.node.endIndex === f.endIndex));
|
|
394
|
+
for (const f of functionsIn(root, cfg)) {
|
|
395
|
+
if (listed(f.node) || f.node.type === 'pair' || f.node.type === 'variable_declarator' || f.node.type === 'public_field_definition')
|
|
396
|
+
continue;
|
|
397
|
+
const outer = decls.filter((d) => d.start <= line(f.node) && lastLine(f.node) <= d.end).sort((a, b) => b.start - a.start || a.end - b.end)[0];
|
|
398
|
+
if (!outer || !FUNCTION_KINDS.has(outer.kind ?? 'other'))
|
|
399
|
+
continue;
|
|
400
|
+
const d = { node: f.node, start: line(f.node), bodyStart: line(f.node), end: lastLine(f.node), name: f.name, kind: 'function', container: [outer.container, outer.name].filter(Boolean).join('.'), parent: outer, children: [] };
|
|
401
|
+
outer.children.push(d);
|
|
402
|
+
out.push(d);
|
|
403
|
+
}
|
|
404
|
+
return out;
|
|
405
|
+
};
|
|
406
|
+
oldDecls.push(...nested(oldRoot, oldDecls));
|
|
407
|
+
newDecls.push(...nested(newRoot, newDecls));
|
|
408
|
+
const oldLines = oldText.split('\n');
|
|
409
|
+
const newLines = newText.split('\n');
|
|
410
|
+
const sideSymbol = (ranges, decls, lines, pick) => {
|
|
411
|
+
if (!ranges.length)
|
|
412
|
+
return undefined;
|
|
413
|
+
if (pick === 'whole') {
|
|
414
|
+
const d = whole(ranges, decls, lines);
|
|
415
|
+
return d ? symbolOf(d) : null;
|
|
416
|
+
}
|
|
417
|
+
if (pick === 'holder') {
|
|
418
|
+
const d = holder(ranges, decls, lines);
|
|
419
|
+
return d ? symbolOf(d) : null;
|
|
420
|
+
}
|
|
421
|
+
const w = whole(ranges, decls, lines);
|
|
422
|
+
if (w)
|
|
423
|
+
return symbolOf(w);
|
|
424
|
+
const h = holder(ranges, decls, lines);
|
|
425
|
+
return h && FUNCTION_KINDS.has(h.kind ?? 'other') ? symbolOf(h) : null;
|
|
426
|
+
};
|
|
427
|
+
for (const op of ops) {
|
|
428
|
+
let s = null;
|
|
429
|
+
switch (op.type) {
|
|
430
|
+
case 'moved':
|
|
431
|
+
case 'movedWithEdit':
|
|
432
|
+
s = agree(sideSymbol(op.lines.old, oldDecls, oldLines, 'moved'), sideSymbol(op.lines.new, newDecls, newLines, 'moved'));
|
|
433
|
+
break;
|
|
434
|
+
case 'reformatted':
|
|
435
|
+
case 'edited':
|
|
436
|
+
s = agree(sideSymbol(op.lines.old, oldDecls, oldLines, 'holder'), sideSymbol(op.lines.new, newDecls, newLines, 'holder'));
|
|
437
|
+
break;
|
|
438
|
+
case 'added':
|
|
439
|
+
s = op.lines.old.length ? null : sideSymbol(op.lines.new, newDecls, newLines, 'whole');
|
|
440
|
+
break;
|
|
441
|
+
case 'deleted':
|
|
442
|
+
s = op.lines.new.length ? null : sideSymbol(op.lines.old, oldDecls, oldLines, 'whole');
|
|
443
|
+
break;
|
|
444
|
+
case 'extractedFunction':
|
|
445
|
+
case 'inlinedFunction': {
|
|
446
|
+
const [ranges, decls] = op.type === 'extractedFunction' ? [op.lines.new, newDecls] : [op.lines.old, oldDecls];
|
|
447
|
+
const fns = functionsWithin(ranges, decls).filter((f) => !functionsWithin(ranges, decls).some((g) => g !== f && g.start <= f.start && f.end <= g.end));
|
|
448
|
+
s = fns.length === 1 ? symbolOf(fns[0]) : null;
|
|
449
|
+
break;
|
|
450
|
+
}
|
|
451
|
+
case 'renamed':
|
|
452
|
+
s = renamedSymbol(op, oldDecls, newDecls, oldRoot, oldLines, newLines, cfg);
|
|
453
|
+
break;
|
|
454
|
+
}
|
|
455
|
+
if (s)
|
|
456
|
+
op.symbol = s;
|
|
457
|
+
}
|
|
458
|
+
}
|
package/dist/syntax.d.ts
CHANGED
|
@@ -60,8 +60,10 @@ export declare function keepWeights(newStarts: number[], lines: number[], unchan
|
|
|
60
60
|
* imports from one module) pair by similarity, not position, so inserting one
|
|
61
61
|
* overload first does not shift every pairing by one.
|
|
62
62
|
*/
|
|
63
|
-
export declare function pairSameNamed<T>(olds: T[], news: T[], base: (t: T) => string, toks: (t: T) => string[]): Array<[T, T]>;
|
|
63
|
+
export declare function pairSameNamed<T>(olds: T[], news: T[], base: (t: T) => string, toks: (t: T) => string[], kept?: (a: T, b: T) => boolean, exact?: (t: T) => string | null): Array<[T, T]>;
|
|
64
64
|
/** The function is a method (of a class, impl, receiver type), so `x.name()` can call it. */
|
|
65
65
|
export declare function isMethod(fn: Node): boolean;
|
|
66
|
+
/** The call reaches the parent class's version: `super.f()`, `super.init(...)`, Ruby `superclass.f` / `superclass&.f`. */
|
|
67
|
+
export declare function callsParent(call: Node): boolean;
|
|
66
68
|
/** Text of the comments and attributes directly above a declaration (they belong to it). */
|
|
67
69
|
export declare function attachedAbove(n: Node, attached: ReadonlySet<string>): string;
|
package/dist/syntax.js
CHANGED
|
@@ -310,7 +310,7 @@ export function keepWeights(newStarts, lines, unchanged) {
|
|
|
310
310
|
* imports from one module) pair by similarity, not position, so inserting one
|
|
311
311
|
* overload first does not shift every pairing by one.
|
|
312
312
|
*/
|
|
313
|
-
export function pairSameNamed(olds, news, base, toks) {
|
|
313
|
+
export function pairSameNamed(olds, news, base, toks, kept, exact) {
|
|
314
314
|
const group = (xs) => {
|
|
315
315
|
const m = new Map();
|
|
316
316
|
for (const x of xs)
|
|
@@ -327,6 +327,34 @@ export function pairSameNamed(olds, news, base, toks) {
|
|
|
327
327
|
// Huge groups (generated code repeating one name thousands of times) pair in
|
|
328
328
|
// order: comparing every pair would take time and memory in the square of the size.
|
|
329
329
|
if ((as.length === 1 && bs.length === 1) || as.length * bs.length > 2500) {
|
|
330
|
+
if (exact && as.length * bs.length > 2500) {
|
|
331
|
+
// With `exact` (trial FN_PAIRING): a key found once on each side pairs first; the rest pair in order,
|
|
332
|
+
// so one deleted overload among hundreds does not shift every pair after it.
|
|
333
|
+
const once = (xs) => {
|
|
334
|
+
const m = new Map();
|
|
335
|
+
for (const x of xs) {
|
|
336
|
+
const e = exact(x);
|
|
337
|
+
if (e !== null)
|
|
338
|
+
m.set(e, [...(m.get(e) ?? []), x]);
|
|
339
|
+
}
|
|
340
|
+
return m;
|
|
341
|
+
};
|
|
342
|
+
const ea = once(as);
|
|
343
|
+
const eb = once(bs);
|
|
344
|
+
const done = new Set();
|
|
345
|
+
for (const [e, xs] of ea) {
|
|
346
|
+
const ys = eb.get(e);
|
|
347
|
+
if (xs.length === 1 && ys?.length === 1) {
|
|
348
|
+
out.push([xs[0], ys[0]]);
|
|
349
|
+
done.add(xs[0]).add(ys[0]);
|
|
350
|
+
}
|
|
351
|
+
}
|
|
352
|
+
const ra = as.filter((x) => !done.has(x));
|
|
353
|
+
const rb = bs.filter((x) => !done.has(x));
|
|
354
|
+
for (let i = 0; i < Math.min(ra.length, rb.length); i++)
|
|
355
|
+
out.push([ra[i], rb[i]]);
|
|
356
|
+
continue;
|
|
357
|
+
}
|
|
330
358
|
for (let i = 0; i < Math.min(as.length, bs.length); i++)
|
|
331
359
|
out.push([as[i], bs[i]]);
|
|
332
360
|
continue;
|
|
@@ -334,9 +362,11 @@ export function pairSameNamed(olds, news, base, toks) {
|
|
|
334
362
|
const ta = as.map(toks);
|
|
335
363
|
const tb = bs.map(toks);
|
|
336
364
|
const cand = [];
|
|
337
|
-
as.forEach((
|
|
338
|
-
// Most similar first; ties keep their order (the i-th old with the i-th new).
|
|
339
|
-
|
|
365
|
+
as.forEach((a, i) => bs.forEach((b, j) => cand.push([dice(ta[i], tb[j]), i, j, kept?.(a, b) ? 1 : 0])));
|
|
366
|
+
// Most similar first; ties keep their order (the i-th old with the i-th new). With `kept` (trial DUP_TIEBREAK),
|
|
367
|
+
// equally similar copies go first to the pair the line diff keeps unchanged in place (the original of a
|
|
368
|
+
// duplicated `require`, not the copy added above it).
|
|
369
|
+
cand.sort((x, y) => y[0] - x[0] || y[3] - x[3] || Math.abs(x[1] - x[2]) - Math.abs(y[1] - y[2]) || x[1] - y[1]);
|
|
340
370
|
const ua = new Set();
|
|
341
371
|
const ub = new Set();
|
|
342
372
|
for (const [sim, i, j] of cand) {
|
|
@@ -389,6 +419,14 @@ function thisCall(call) {
|
|
|
389
419
|
return call.namedChild(0).childForFieldName('target')?.type === 'self_expression';
|
|
390
420
|
return call.childForFieldName('function')?.childForFieldName('object')?.type === 'this';
|
|
391
421
|
}
|
|
422
|
+
/** The call reaches the parent class's version: `super.f()`, `super.init(...)`, Ruby `superclass.f` / `superclass&.f`. */
|
|
423
|
+
export function callsParent(call) {
|
|
424
|
+
if (!memberCall(call))
|
|
425
|
+
return false;
|
|
426
|
+
const f = call.childForFieldName('function') ?? (call.type === 'call_expression' ? call.namedChild(0) : null);
|
|
427
|
+
const r = call.childForFieldName('receiver') ?? call.childForFieldName('object') ?? f?.childForFieldName('target') ?? f?.childForFieldName('object') ?? f?.namedChild(0);
|
|
428
|
+
return !!r && (/^super/.test(r.type) || /^(super|superclass)$/.test(r.text));
|
|
429
|
+
}
|
|
392
430
|
function memberCall(call) {
|
|
393
431
|
if (call.childForFieldName('object'))
|
|
394
432
|
return true; // Java
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@plannotator/sem-jiff",
|
|
3
|
-
"version": "0.2.
|
|
3
|
+
"version": "0.2.2",
|
|
4
4
|
"description": "Semantic diffs for code review: finds moved, renamed, reformatted, extracted and inlined code with tree-sitter, for any diff viewer.",
|
|
5
5
|
"license": "MIT OR Apache-2.0",
|
|
6
6
|
"type": "module",
|