@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/parse.d.ts
CHANGED
|
@@ -70,9 +70,9 @@ export interface LanguageConfig {
|
|
|
70
70
|
*/
|
|
71
71
|
contextRuns?: boolean;
|
|
72
72
|
/**
|
|
73
|
-
* Claims this language does not make yet: their lines are shown as plain edits.
|
|
74
|
-
*
|
|
75
|
-
*
|
|
73
|
+
* Claims this language does not make yet: their lines are shown as plain edits (0.2.1: the classes over
|
|
74
|
+
* the bar on the held3 blind set, in Ruby, Kotlin and Swift). Measurement lifts it with the HOLD_BACK
|
|
75
|
+
* rule off (SEMANTIC_NO_HOLD_BACK=1 in the harness).
|
|
76
76
|
*/
|
|
77
77
|
holdBack?: ReadonlySet<string>;
|
|
78
78
|
/**
|
|
@@ -112,6 +112,7 @@ export interface Declaration {
|
|
|
112
112
|
}
|
|
113
113
|
/** The type written before a declaration's name (`String` in `fun String.shout()`), if any. */
|
|
114
114
|
export declare const kotlinReceiver: (n: Node) => Node | null;
|
|
115
|
+
export declare const swiftParams: (fn: Node) => string;
|
|
115
116
|
export declare const LANGUAGES: Record<LanguageId, LanguageConfig>;
|
|
116
117
|
export declare function getParser(id: LanguageId): Promise<Parser>;
|
|
117
118
|
/** Parse both versions, run `fn`, and always free the trees (they live in WASM memory). */
|
package/dist/parse.js
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
// Parser setup and per-language knowledge: what a function is, where its
|
|
2
2
|
// statements live, what a call and a return look like.
|
|
3
3
|
import { Language, Parser } from 'web-tree-sitter';
|
|
4
|
-
import { currentSetup, resolveInNode, rule } from "./runtime.js";
|
|
4
|
+
import { currentSetup, resolveInNode, rule, trial } from "./runtime.js";
|
|
5
5
|
export { languageForPath } from "./languages.js";
|
|
6
6
|
/** Comment node types across grammars (Rust splits line and block comments; Swift calls block comments multiline_comment). */
|
|
7
7
|
export const COMMENT_TYPES = new Set(['comment', 'line_comment', 'block_comment', 'multiline_comment']);
|
|
@@ -647,7 +647,8 @@ function rubyVisibility(body, before) {
|
|
|
647
647
|
const ruby = {
|
|
648
648
|
id: 'ruby',
|
|
649
649
|
wasm: 'tree-sitter-ruby/tree-sitter-ruby.wasm',
|
|
650
|
-
|
|
650
|
+
// held3 blind audit, wrong in n: moved 2/96, renamed 2/146, extract/inline 2/145 (bar 1 in 50).
|
|
651
|
+
holdBack: new Set(['movedWithEdit', 'moved', 'renamed', 'extractedFunction', 'inlinedFunction']),
|
|
651
652
|
functionTypes: new Set(['method', 'singleton_method']),
|
|
652
653
|
callTypes: new Set(['call', 'identifier']),
|
|
653
654
|
blockTypes: RUBY_BLOCKS,
|
|
@@ -881,7 +882,8 @@ const kotlin = {
|
|
|
881
882
|
identifierTypes: new Set(['identifier']),
|
|
882
883
|
harmlessMissing: new Set(['_class_member_semi']),
|
|
883
884
|
contextRuns: true,
|
|
884
|
-
|
|
885
|
+
// held3 blind audit, wrong in n: renamed 2/143, extract/inline 4/142 (bar 1 in 50); moved 0/138 stays.
|
|
886
|
+
holdBack: new Set(['movedWithEdit', 'renamed', 'extractedFunction', 'inlinedFunction']),
|
|
885
887
|
declarationsIn: (container) => {
|
|
886
888
|
const out = [];
|
|
887
889
|
for (const child of container.namedChildren) {
|
|
@@ -948,7 +950,7 @@ const kotlin = {
|
|
|
948
950
|
* A move into or out of a constrained extension, or into another `#if` branch, is an edit (contextAt).
|
|
949
951
|
*/
|
|
950
952
|
const SWIFT_NOT_EXPRESSION = /declaration|statement|assignment|directive|^diagnostic$/;
|
|
951
|
-
const swiftParams = (fn) => fn.namedChildren
|
|
953
|
+
export const swiftParams = (fn) => fn.namedChildren
|
|
952
954
|
.filter((c) => c?.type === 'parameter')
|
|
953
955
|
.map((p) => {
|
|
954
956
|
const label = (p.childForFieldName('external_name') ?? p.childForFieldName('name'))?.text ?? '_';
|
|
@@ -1127,7 +1129,8 @@ const swift = {
|
|
|
1127
1129
|
return out;
|
|
1128
1130
|
},
|
|
1129
1131
|
// Moved-with-edits was wrong 2 times in 10 on the held-out sets: held back in v1, as in Ruby and Kotlin.
|
|
1130
|
-
|
|
1132
|
+
// held3 blind audit, wrong in n: moved 4/131, renamed 3/149, extract/inline 6/142 (bar 1 in 50).
|
|
1133
|
+
holdBack: new Set(['movedWithEdit', 'moved', 'renamed', 'extractedFunction', 'inlinedFunction']),
|
|
1131
1134
|
moveEditedFirst: true,
|
|
1132
1135
|
// A whole `#if` block or extension moved with its directives and doc comment keeps its move.
|
|
1133
1136
|
contextRuns: true,
|
|
@@ -1239,18 +1242,34 @@ export const LANGUAGES = {
|
|
|
1239
1242
|
kotlin,
|
|
1240
1243
|
swift,
|
|
1241
1244
|
};
|
|
1245
|
+
/**
|
|
1246
|
+
* Claims held back in every language, on top of each language's own `holdBack`. movedWithEdit: on the held4 blind
|
|
1247
|
+
* set about 1 in 3 sampled claims paired two different functions or blocks as one moved-and-edited thing (Go about
|
|
1248
|
+
* 8%, Kotlin about 63%). Its lines show as plain edits until a pairing rule passes the bar.
|
|
1249
|
+
*/
|
|
1250
|
+
const HELD_BACK_EVERYWHERE = ['movedWithEdit'];
|
|
1251
|
+
for (const cfg of Object.values(LANGUAGES))
|
|
1252
|
+
cfg.holdBack = new Set([...(cfg.holdBack ?? []), ...HELD_BACK_EVERYWHERE]);
|
|
1242
1253
|
let ready = null;
|
|
1243
1254
|
const loaded = new Map();
|
|
1255
|
+
/**
|
|
1256
|
+
* Trial KOTLIN_GRAMMAR: tree-sitter-kotlin 1.1.0 rebuilt with a patched scanner (vendor/tree-sitter-kotlin,
|
|
1257
|
+
* README): a class header followed on the next line by `@Ann internal constructor(...)`, or in a nested
|
|
1258
|
+
* class by `internal constructor(...)`, keeps its primary constructor instead of ending the class there.
|
|
1259
|
+
* Node only (the source tree); the package's wasm/ folder ships the release grammar.
|
|
1260
|
+
*/
|
|
1261
|
+
const KOTLIN_PATCHED = '@tree-sitter-grammars/tree-sitter-kotlin/tree-sitter-kotlin-patched.wasm';
|
|
1244
1262
|
export async function getParser(id) {
|
|
1245
1263
|
const setup = currentSetup();
|
|
1246
1264
|
ready ??= Parser.init(setup.locateRuntime ? { locateFile: setup.locateRuntime } : undefined);
|
|
1247
1265
|
await ready;
|
|
1248
|
-
|
|
1266
|
+
const patched = id === 'kotlin' && trial('KOTLIN_GRAMMAR');
|
|
1267
|
+
const file = patched ? KOTLIN_PATCHED : LANGUAGES[id].wasm;
|
|
1268
|
+
let lang = loaded.get(file);
|
|
1249
1269
|
if (!lang) {
|
|
1250
|
-
const file = LANGUAGES[id].wasm;
|
|
1251
1270
|
const source = setup.locateGrammar ? await setup.locateGrammar(file, id) : await resolveInNode(file);
|
|
1252
1271
|
lang = await Language.load(source instanceof URL ? source.href : source);
|
|
1253
|
-
loaded.set(
|
|
1272
|
+
loaded.set(file, lang);
|
|
1254
1273
|
}
|
|
1255
1274
|
const parser = new Parser();
|
|
1256
1275
|
parser.setLanguage(lang);
|
package/dist/runtime.d.ts
CHANGED
|
@@ -38,6 +38,8 @@ export declare const flags: {
|
|
|
38
38
|
};
|
|
39
39
|
export declare const rule: (name: string) => boolean;
|
|
40
40
|
export declare function switchRulesOff(names: Iterable<string>): void;
|
|
41
|
+
export declare const trial: (name: string) => boolean;
|
|
42
|
+
export declare function switchTrialsOn(names: Iterable<string>): void;
|
|
41
43
|
/**
|
|
42
44
|
* The default grammar lookup (Node only, loaded only when needed and never bundled): the file in this
|
|
43
45
|
* package's own `wasm/` folder when it is there (an installed package), else Node's resolver from the
|
package/dist/runtime.js
CHANGED
|
@@ -31,6 +31,15 @@ export function switchRulesOff(names) {
|
|
|
31
31
|
for (const n of names)
|
|
32
32
|
rulesOff.add(n);
|
|
33
33
|
}
|
|
34
|
+
// Trial rules: off unless switched on (harness: SEMANTIC_TRY_<NAME>=1), measured on a fresh draw before
|
|
35
|
+
// they may ship. Like the rule switches, not part of the public setup.
|
|
36
|
+
const trialsOn = new Set();
|
|
37
|
+
export const trial = (name) => trialsOn.has(name);
|
|
38
|
+
export function switchTrialsOn(names) {
|
|
39
|
+
trialsOn.clear();
|
|
40
|
+
for (const n of names)
|
|
41
|
+
trialsOn.add(n);
|
|
42
|
+
}
|
|
34
43
|
/**
|
|
35
44
|
* The default grammar lookup (Node only, loaded only when needed and never bundled): the file in this
|
|
36
45
|
* package's own `wasm/` folder when it is there (an installed package), else Node's resolver from the
|
package/dist/structure.d.ts
CHANGED
|
@@ -9,5 +9,15 @@ export interface StructureResult {
|
|
|
9
9
|
start: number;
|
|
10
10
|
end: number;
|
|
11
11
|
}>;
|
|
12
|
+
/**
|
|
13
|
+
* Indices into `operations` of `reformatted` claims whose words changed (rule REFORMAT_WORDS): text inside a
|
|
14
|
+
* node that no token covers, such as a Python string's words around an escape, differs. The caller drops them
|
|
15
|
+
* at the end, like a held-back claim, so every other pass sees the same operations either way.
|
|
16
|
+
*/
|
|
17
|
+
wordsChanged: Set<number>;
|
|
12
18
|
}
|
|
13
|
-
|
|
19
|
+
/**
|
|
20
|
+
* `keptLines` (trial DUP_TIEBREAK): the line diff's unchanged lines, old line to new line. Equally similar same-named
|
|
21
|
+
* declarations pair first where the line diff keeps one unchanged in place.
|
|
22
|
+
*/
|
|
23
|
+
export declare function detectStructure(oldRoot: Node, newRoot: Node, cfg: LanguageConfig, keptLines?: Map<number, number>): StructureResult;
|
package/dist/structure.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { brokenTree, isComment } from "./parse.js";
|
|
2
|
-
import { rule } from "./runtime.js";
|
|
3
|
-
import { attachedAbove, codeLines, endLine, keepWeights, longestInOrder, pairSameNamed, range, rangeOf, startLine } from "./syntax.js";
|
|
2
|
+
import { rule, trial } from "./runtime.js";
|
|
3
|
+
import { attachedAbove, codeLines, dice, endLine, keepWeights, longestInOrder, pairSameNamed, range, rangeOf, startLine } from "./syntax.js";
|
|
4
4
|
function leaves(n, cfg) {
|
|
5
5
|
const tokens = [];
|
|
6
6
|
const identifiers = [];
|
|
@@ -148,6 +148,37 @@ function withoutTrailingCommas(seq) {
|
|
|
148
148
|
return !(seq[j] === ')' || seq[j] === ']' || seq[j] === '}' || seq[j] === '>');
|
|
149
149
|
});
|
|
150
150
|
}
|
|
151
|
+
/**
|
|
152
|
+
* The words no token holds: the text inside a node between its children (a Python string's content around an
|
|
153
|
+
* escape sequence, `"a \\n b"` holds only `\\n` as a token). Words only (letters, digits, `_`), so wrapping and
|
|
154
|
+
* spacing change nothing and any changed word does; symbols there are mostly layout the grammar leaves out of the
|
|
155
|
+
* tree (Swift's optional `;`, Rust's raw-string `#`). Comments are whole tokens already.
|
|
156
|
+
*/
|
|
157
|
+
function gapWords(n) {
|
|
158
|
+
const out = [];
|
|
159
|
+
const stack = [n];
|
|
160
|
+
while (stack.length) {
|
|
161
|
+
const x = stack.pop();
|
|
162
|
+
if (x.childCount === 0 || isComment(x))
|
|
163
|
+
continue;
|
|
164
|
+
const text = x.text;
|
|
165
|
+
let at = x.startIndex;
|
|
166
|
+
const gap = (end) => {
|
|
167
|
+
if (end > at)
|
|
168
|
+
out.push(...(text.slice(at - x.startIndex, end - x.startIndex).match(/[\p{L}\p{N}_]+/gu) ?? []));
|
|
169
|
+
};
|
|
170
|
+
const kids = x.children.filter((c) => !!c);
|
|
171
|
+
for (const c of kids) {
|
|
172
|
+
gap(c.startIndex);
|
|
173
|
+
at = Math.max(at, c.endIndex);
|
|
174
|
+
}
|
|
175
|
+
gap(x.endIndex);
|
|
176
|
+
for (let i = kids.length - 1; i >= 0; i--)
|
|
177
|
+
stack.push(kids[i]);
|
|
178
|
+
}
|
|
179
|
+
return out.join('\u0000');
|
|
180
|
+
}
|
|
181
|
+
const declGapWords = (d) => gapWords(d.node) + (d.tail ? '\u0001' + gapWords(d.tail) : '');
|
|
151
182
|
/** A declaration's text, through its tail (a Ruby heredoc body after it). */
|
|
152
183
|
const declText = (d, source) => (d.tail ? source.slice(d.node.startIndex, d.tail.endIndex) : d.node.text);
|
|
153
184
|
/** Last line of a declaration, through its tail. */
|
|
@@ -184,6 +215,80 @@ function collect(root, cfg) {
|
|
|
184
215
|
visit(root, '', null);
|
|
185
216
|
return out;
|
|
186
217
|
}
|
|
218
|
+
/**
|
|
219
|
+
* Trial EXT_BY_MEMBERS. For deciding which members crossed into another container, the containers of one type (Swift
|
|
220
|
+
* extensions) whose header pairing is missing or ambiguous pair by where their paired members went: an extension whose
|
|
221
|
+
* header changed (`_RopeElement` -> `RopeElement`, a conformance added), or two extensions with one header, are still
|
|
222
|
+
* the container their members stayed in. A container with a header found once on each side keeps its pairing, so a
|
|
223
|
+
* member moved from the type's body into a new extension still crossed. Among the open containers of one type and
|
|
224
|
+
* kind, the old and new ones holding the most paired members in common pair first, one to one (ties: the pairing by
|
|
225
|
+
* header, then file order); an open container left over keeps its pairing by header if that partner is still free.
|
|
226
|
+
* Only `holders` changes.
|
|
227
|
+
*/
|
|
228
|
+
function pairContainersByMembers(pairs, olds, news, holders, cfg) {
|
|
229
|
+
const isContainer = (e) => !!cfg.typeOf(e.decl);
|
|
230
|
+
const oldC = olds.filter(isContainer);
|
|
231
|
+
const newC = news.filter(isContainer);
|
|
232
|
+
const count = (list) => {
|
|
233
|
+
const m = new Map();
|
|
234
|
+
for (const e of list)
|
|
235
|
+
m.set(`${e.parent}\u0000${e.base}`, (m.get(`${e.parent}\u0000${e.base}`) ?? 0) + 1);
|
|
236
|
+
return (e) => m.get(`${e.parent}\u0000${e.base}`) ?? 0;
|
|
237
|
+
};
|
|
238
|
+
const inOld = count(oldC);
|
|
239
|
+
const inNew = count(newC);
|
|
240
|
+
const byHeader = new Map(oldC.flatMap((e) => (holders.has(e) ? [[e, holders.get(e)]] : [])));
|
|
241
|
+
const pairedNew = new Set(byHeader.values());
|
|
242
|
+
const ambiguous = (e) => inOld(e) > 1 || inNew(e) > 1;
|
|
243
|
+
const openOld = new Set(oldC.filter((e) => !byHeader.has(e) || ambiguous(e) || ambiguous(byHeader.get(e))));
|
|
244
|
+
const openNew = new Set(newC.filter((e) => !pairedNew.has(e) || ambiguous(e)));
|
|
245
|
+
for (const [a, b] of byHeader)
|
|
246
|
+
if (openOld.has(a))
|
|
247
|
+
openNew.add(b);
|
|
248
|
+
const typeKey = (e) => `${e.parent}\u0000${e.decl.kind}\u0000${cfg.typeOf(e.decl)}`;
|
|
249
|
+
const flow = new Map();
|
|
250
|
+
for (const [a, b] of pairs) {
|
|
251
|
+
const ha = a.holder;
|
|
252
|
+
const hb = b.holder;
|
|
253
|
+
if (!ha || !hb || !openOld.has(ha) || !openNew.has(hb) || typeKey(ha) !== typeKey(hb))
|
|
254
|
+
continue;
|
|
255
|
+
const m = flow.get(ha) ?? new Map();
|
|
256
|
+
m.set(hb, (m.get(hb) ?? 0) + 1);
|
|
257
|
+
flow.set(ha, m);
|
|
258
|
+
}
|
|
259
|
+
if (!flow.size)
|
|
260
|
+
return;
|
|
261
|
+
const order = new Map([...olds, ...news].map((e, i) => [e, i]));
|
|
262
|
+
const cand = [];
|
|
263
|
+
for (const [ha, m] of flow)
|
|
264
|
+
for (const [hb, n] of m)
|
|
265
|
+
cand.push([n, byHeader.get(ha) === hb ? 1 : 0, ha, hb]);
|
|
266
|
+
cand.sort((x, y) => y[0] - x[0] || y[1] - x[1] || order.get(x[2]) - order.get(y[2]) || order.get(x[3]) - order.get(y[3]));
|
|
267
|
+
const usedNew = new Set();
|
|
268
|
+
const result = new Map();
|
|
269
|
+
for (const [, , ha, hb] of cand) {
|
|
270
|
+
if (result.has(ha) || usedNew.has(hb))
|
|
271
|
+
continue;
|
|
272
|
+
result.set(ha, hb);
|
|
273
|
+
usedNew.add(hb);
|
|
274
|
+
}
|
|
275
|
+
for (const ha of openOld) {
|
|
276
|
+
if (result.has(ha))
|
|
277
|
+
continue;
|
|
278
|
+
const hb = byHeader.get(ha);
|
|
279
|
+
if (hb && !usedNew.has(hb)) {
|
|
280
|
+
result.set(ha, hb);
|
|
281
|
+
usedNew.add(hb);
|
|
282
|
+
}
|
|
283
|
+
}
|
|
284
|
+
for (const ha of openOld) {
|
|
285
|
+
const hb = result.get(ha);
|
|
286
|
+
if (hb)
|
|
287
|
+
holders.set(ha, hb);
|
|
288
|
+
else
|
|
289
|
+
holders.delete(ha);
|
|
290
|
+
}
|
|
291
|
+
}
|
|
187
292
|
/**
|
|
188
293
|
* If `a` becomes `b` by consistently swapping identifiers (each old name maps to
|
|
189
294
|
* exactly one new name), return that mapping; otherwise null.
|
|
@@ -242,7 +347,11 @@ function changedLines(a, b) {
|
|
|
242
347
|
tail++;
|
|
243
348
|
return { old: { start: head + 1, end: la.length - tail }, new: { start: head + 1, end: lb.length - tail } };
|
|
244
349
|
}
|
|
245
|
-
|
|
350
|
+
/**
|
|
351
|
+
* `keptLines` (trial DUP_TIEBREAK): the line diff's unchanged lines, old line to new line. Equally similar same-named
|
|
352
|
+
* declarations pair first where the line diff keeps one unchanged in place.
|
|
353
|
+
*/
|
|
354
|
+
export function detectStructure(oldRoot, newRoot, cfg, keptLines) {
|
|
246
355
|
const olds = collect(oldRoot, cfg);
|
|
247
356
|
const news = collect(newRoot, cfg);
|
|
248
357
|
const pairs = [];
|
|
@@ -273,7 +382,8 @@ export function detectStructure(oldRoot, newRoot, cfg) {
|
|
|
273
382
|
if (levels.has(level))
|
|
274
383
|
return;
|
|
275
384
|
levels.add(level);
|
|
276
|
-
|
|
385
|
+
const inPlace = keptLines ? (a, b) => keptLines.get(startLine(a.decl.node)) === startLine(b.decl.node) : undefined;
|
|
386
|
+
for (const [a, b] of pairSameNamed(oldKids.get(oldParent) ?? [], newKids.get(newParent) ?? [], (e) => e.base, similarBy, inPlace)) {
|
|
277
387
|
pairs.push([a, b]);
|
|
278
388
|
usedOld.add(a);
|
|
279
389
|
usedNew.add(b);
|
|
@@ -286,6 +396,10 @@ export function detectStructure(oldRoot, newRoot, cfg) {
|
|
|
286
396
|
for (const a of olds) {
|
|
287
397
|
if (usedOld.has(a))
|
|
288
398
|
continue;
|
|
399
|
+
// Trial IMPORT_NO_RENAME: an import names something else when its last segment changes
|
|
400
|
+
// (`runtime.ReadOnlyComposable` -> `runtime.currentComposer`): two imports are never one renamed declaration.
|
|
401
|
+
if (a.decl.kind === 'import' && trial('IMPORT_NO_RENAME'))
|
|
402
|
+
continue;
|
|
289
403
|
for (const b of news) {
|
|
290
404
|
if (usedNew.has(b) || b.parent !== a.parent || b.decl.kind !== a.decl.kind)
|
|
291
405
|
continue;
|
|
@@ -314,6 +428,8 @@ export function detectStructure(oldRoot, newRoot, cfg) {
|
|
|
314
428
|
if (p[0].parent === p[1].parent)
|
|
315
429
|
byParent.set(p[0].parent, [...(byParent.get(p[0].parent) ?? []), p]);
|
|
316
430
|
const holders = new Map(pairs);
|
|
431
|
+
if (cfg.typeOf && trial('EXT_BY_MEMBERS'))
|
|
432
|
+
pairContainersByMembers(pairs, olds, news, holders, cfg);
|
|
317
433
|
for (const group of byParent.values()) {
|
|
318
434
|
const ordered = [...group].sort((p, q) => p[0].decl.node.startIndex - q[0].decl.node.startIndex);
|
|
319
435
|
// A member now in another reopening of its Ruby class, or another container of its Swift type, moved, whatever the order says.
|
|
@@ -441,6 +557,7 @@ export function detectStructure(oldRoot, newRoot, cfg) {
|
|
|
441
557
|
return best;
|
|
442
558
|
};
|
|
443
559
|
const accepted = new Map();
|
|
560
|
+
const guards = trial('RENAME_GUARDS');
|
|
444
561
|
for (const [from, to] of renames) {
|
|
445
562
|
if (conflicted.has(from))
|
|
446
563
|
continue;
|
|
@@ -463,6 +580,12 @@ export function detectStructure(oldRoot, newRoot, cfg) {
|
|
|
463
580
|
};
|
|
464
581
|
const oldIds = ownUses(sameKind(oldIndex.get(from)), oldIndex);
|
|
465
582
|
const newIds = ownUses(sameKind(newIndex.get(to)), newIndex);
|
|
583
|
+
if (guards) {
|
|
584
|
+
// Trial RENAME_GUARDS, targeted: renames where the name now stands for something else.
|
|
585
|
+
// A Ruby global (`$stdout` -> `$stderr`) is never declared here: switching it switches what the code talks to.
|
|
586
|
+
if (cfg.id === 'ruby' && from.startsWith('$'))
|
|
587
|
+
continue;
|
|
588
|
+
}
|
|
466
589
|
// Every occurrence must have a counterpart, or this is not a clean rename.
|
|
467
590
|
if (!oldIds.length || oldIds.length !== newIds.length)
|
|
468
591
|
continue;
|
|
@@ -477,6 +600,16 @@ export function detectStructure(oldRoot, newRoot, cfg) {
|
|
|
477
600
|
// (`toPath func(string) Path` -> `caseSensitivity CaseSensitivity`).
|
|
478
601
|
if (oldIds.some((o, i) => isDeclarationName(o) && declaredTypeChanged(o, newIds[i], from, to)))
|
|
479
602
|
continue;
|
|
603
|
+
if (guards) {
|
|
604
|
+
// A parameter that changed place in its list is another parameter (`{ (a, b) in }` -> `{ (b, a) in }`).
|
|
605
|
+
if (oldIds.some((o, i) => { if (!isDeclarationName(o))
|
|
606
|
+
return false; const a = parameterIndex(o); const b = parameterIndex(newIds[i]); return a !== null && b !== null && a !== b; }))
|
|
607
|
+
continue;
|
|
608
|
+
// A value built from another class (`networkObserver = NetworkObserver(...)` -> `systemCallbacks = SystemCallbacks(...)`)
|
|
609
|
+
// was replaced, unless that class was renamed too.
|
|
610
|
+
if (oldIds.some((o, i) => isDeclarationName(o) && constructedTypeChanged(o, newIds[i], renames, cfg)))
|
|
611
|
+
continue;
|
|
612
|
+
}
|
|
480
613
|
// A declaration renamed and also edited is not paired, but is still this rename's own declaration.
|
|
481
614
|
const counterpartOf = (e) => partner.get(e) ?? (e.decl.name.split('.').at(-1) === from ? news.find((x) => x.parent === e.parent && x.decl.kind === e.decl.kind && x.decl.name === e.decl.name.replace(new RegExp(`${from}$`), to)) : undefined);
|
|
482
615
|
if (oldIds.some((o, i) => { const e = innermost(olds, o); const f = innermost(news, newIds[i]); return e ? counterpartOf(e) !== f : f !== null; }))
|
|
@@ -567,6 +700,33 @@ export function detectStructure(oldRoot, newRoot, cfg) {
|
|
|
567
700
|
const root = (k) => (group.get(k) === k ? k : root(group.get(k)));
|
|
568
701
|
const lineOwner = new Map();
|
|
569
702
|
const kept = new Map();
|
|
703
|
+
// Trial RENAME_GUARDS: a function renamed whose body is mostly new (the old helper deleted, a different one called
|
|
704
|
+
// instead) was replaced, not renamed: fewer than half the names in its body alike, renames applied.
|
|
705
|
+
if (guards) {
|
|
706
|
+
for (const [from, { oldIds, newIds }] of [...accepted]) {
|
|
707
|
+
if (innerOnly.has(from))
|
|
708
|
+
continue;
|
|
709
|
+
if (oldIds.some((o, i) => {
|
|
710
|
+
if (!isDeclarationName(o) || !/function|method|constructor/.test(o.parent.type))
|
|
711
|
+
return false;
|
|
712
|
+
const ba = cfg.bodyOf(o.parent);
|
|
713
|
+
const bb = cfg.bodyOf(newIds[i].parent);
|
|
714
|
+
if (!ba || !bb)
|
|
715
|
+
return false;
|
|
716
|
+
const names = (n) => leaves(n, cfg).tokens.filter((t) => /^[\p{L}_$][\p{L}\p{N}_$]*$/u.test(t));
|
|
717
|
+
return dice(names(ba).map((t) => renames.get(t) ?? t), names(bb)) < 0.5;
|
|
718
|
+
}))
|
|
719
|
+
accepted.delete(from);
|
|
720
|
+
}
|
|
721
|
+
}
|
|
722
|
+
// Trial RENAME_GUARDS: a use whose only other change is a negation (`hasRowID = !x` -> `isWithoutRowID = x`,
|
|
723
|
+
// `.hasRowID` -> `.isWithoutRowID == false`) shows the name now means the opposite: no rename at all.
|
|
724
|
+
if (guards) {
|
|
725
|
+
for (const [from, { oldIds, newIds }] of [...accepted]) {
|
|
726
|
+
if (oldIds.some((o, i) => negatedOnly(words(swapped(startLine(o))), words(newText[startLine(newIds[i]) - 1] ?? ''))))
|
|
727
|
+
accepted.delete(from);
|
|
728
|
+
}
|
|
729
|
+
}
|
|
570
730
|
for (const [from, { oldIds, newIds }] of accepted) {
|
|
571
731
|
const occ = oldIds.map((o, i) => [o, newIds[i]]).filter(([o, n]) => clean(o, n));
|
|
572
732
|
if (!occ.length)
|
|
@@ -612,6 +772,7 @@ export function detectStructure(oldRoot, newRoot, cfg) {
|
|
|
612
772
|
// A class or interface whose whole shape is unchanged (`interface P { a: string }` wrapped
|
|
613
773
|
// onto lines) is reformatted as a whole; otherwise its changed members are reported.
|
|
614
774
|
const whole = pairs.filter(([a, b]) => a.decl.members && !moved.has(a) && a.text !== b.text && a.seq === b.seq && !brokenTree(a.decl.node, cfg) && !brokenTree(b.decl.node, cfg) && invisibles(a.text) === invisibles(b.text)).map(([a]) => a.decl.node);
|
|
775
|
+
const wordsChanged = new Set();
|
|
615
776
|
const insideWhole = (a) => whole.some((w) => w.id !== a.decl.node.id && w.startIndex <= a.decl.node.startIndex && a.decl.node.endIndex <= w.endIndex);
|
|
616
777
|
for (const [a, b] of pairs) {
|
|
617
778
|
if (moved.has(a) || a.text === b.text || insideWhole(a))
|
|
@@ -642,6 +803,10 @@ export function detectStructure(oldRoot, newRoot, cfg) {
|
|
|
642
803
|
old: { start: oldStart, end: startLine(a.decl.node) + rel.old.end - 1 },
|
|
643
804
|
new: { start: newStart, end: startLine(b.decl.node) + rel.new.end - 1 },
|
|
644
805
|
};
|
|
806
|
+
// The words must be the same too, inside strings and docstrings: rewrapping a docstring is formatting, changing
|
|
807
|
+
// its words is an edit (held4: Python docstrings with an escape sequence read only the escape as a token).
|
|
808
|
+
if (declGapWords(a.decl) !== declGapWords(b.decl))
|
|
809
|
+
wordsChanged.add(operations.length);
|
|
645
810
|
operations.push({
|
|
646
811
|
type: 'reformatted',
|
|
647
812
|
spans: [
|
|
@@ -657,7 +822,7 @@ export function detectStructure(oldRoot, newRoot, cfg) {
|
|
|
657
822
|
});
|
|
658
823
|
}
|
|
659
824
|
const paired = pairs.map(([a]) => ({ start: docStart(a.decl.node, cfg), end: declEnd(a.decl) }));
|
|
660
|
-
return { operations, folds, paired };
|
|
825
|
+
return { operations, folds, paired, wordsChanged };
|
|
661
826
|
}
|
|
662
827
|
const invisibles = (t) => (t.match(/[\u200b-\u200f\u2028\u2029\u2060\ufeff\u00ad]/g) ?? []).join('');
|
|
663
828
|
const macroArgs = (n) => n.descendantsOfType('token_tree').map((t) => t?.text ?? '').join('\u0000');
|
|
@@ -741,6 +906,58 @@ function declaredType(id) {
|
|
|
741
906
|
return p.parent.namedChildren.find((c) => c?.type === 'type_annotation') ?? null;
|
|
742
907
|
return null;
|
|
743
908
|
}
|
|
909
|
+
/** Position of a parameter in its list (function, closure or block parameters), or null. */
|
|
910
|
+
function parameterIndex(id) {
|
|
911
|
+
let q = id;
|
|
912
|
+
for (let depth = 0; q?.parent && depth < 4; depth++, q = q.parent) {
|
|
913
|
+
const list = q.parent;
|
|
914
|
+
if (/(^|_)parameters$|parameter_list|lambda_function_type_parameters|closure_parameters|block_parameters/.test(list.type)) {
|
|
915
|
+
const items = list.namedChildren.filter((c) => !!c && !isComment(c) && c.type === q.type);
|
|
916
|
+
const i = items.findIndex((c) => c.id === q.id);
|
|
917
|
+
return i < 0 ? null : i;
|
|
918
|
+
}
|
|
919
|
+
}
|
|
920
|
+
return null;
|
|
921
|
+
}
|
|
922
|
+
/** The class a declaration's value is built from (`= Foo(...)`, `= Foo.new(...)`), when it is one. */
|
|
923
|
+
function constructedType(id, cfg) {
|
|
924
|
+
let d = id.parent;
|
|
925
|
+
for (let depth = 0; d && depth < 3 && !/property_declaration|variable_declaration|assignment|lexical_declaration|local_variable_declaration|field_declaration|variable_declarator/.test(d.type); depth++)
|
|
926
|
+
d = d.parent;
|
|
927
|
+
if (d?.type === 'variable_declaration' && d.parent?.type === 'property_declaration')
|
|
928
|
+
d = d.parent; // Kotlin
|
|
929
|
+
if (!d)
|
|
930
|
+
return null;
|
|
931
|
+
const value = d.childForFieldName('value') ?? d.childForFieldName('right') ?? d.namedChildren.filter((c) => !!c && !isComment(c)).at(-1) ?? null;
|
|
932
|
+
if (!value || value.startIndex <= id.startIndex || !cfg.callTypes.has(value.type))
|
|
933
|
+
return null;
|
|
934
|
+
let name = cfg.calleeOf(value);
|
|
935
|
+
if (name === 'new')
|
|
936
|
+
name = (value.childForFieldName('receiver') ?? null)?.text ?? null;
|
|
937
|
+
return name && /^[A-Z]/.test(name) ? name : null;
|
|
938
|
+
}
|
|
939
|
+
function constructedTypeChanged(o, n, renames, cfg) {
|
|
940
|
+
const a = constructedType(o, cfg);
|
|
941
|
+
const b = constructedType(n, cfg);
|
|
942
|
+
return !!a && !!b && a !== b && renames.get(a) !== b;
|
|
943
|
+
}
|
|
944
|
+
/** The two lines differ only by a negation: `!` or `not` added or dropped, or `== false` / `!= true` appended. */
|
|
945
|
+
function negatedOnly(a, b) {
|
|
946
|
+
if (a.join(' ') === b.join(' '))
|
|
947
|
+
return false;
|
|
948
|
+
const count = (xs) => xs.reduce((m, x) => m.set(x, (m.get(x) ?? 0) + 1), new Map());
|
|
949
|
+
const ca = count(a);
|
|
950
|
+
const cb = count(b);
|
|
951
|
+
const diff = [];
|
|
952
|
+
for (const k of new Set([...ca.keys(), ...cb.keys()]))
|
|
953
|
+
for (let i = Math.abs((ca.get(k) ?? 0) - (cb.get(k) ?? 0)); i > 0; i--)
|
|
954
|
+
diff.push(k);
|
|
955
|
+
if (!diff.length || !diff.every((x) => /^(!|=|not|false|true)$/.test(x)))
|
|
956
|
+
return false;
|
|
957
|
+
const n = (re) => diff.filter((x) => re.test(x)).length;
|
|
958
|
+
// `!x` / `not x`, or a comparison appended (`== false`): never a default value (`flag: Boolean = false`).
|
|
959
|
+
return n(/^(!|not)$/) > 0 || (n(/^=$/) === 2 && n(/^(false|true)$/) === 1);
|
|
960
|
+
}
|
|
744
961
|
function declaredTypeChanged(o, n, from, to) {
|
|
745
962
|
// Kotlin writes the type after the name with no field: `val x: Int`, `(by: Double)`.
|
|
746
963
|
const typeOf = (d) => d?.childForFieldName('type') ?? (d && /^(variable_declaration|parameter|class_parameter)$/.test(d.type) ? d.namedChildren.find((c) => !!c && /^(user_type|nullable_type|function_type|parenthesized_type|not_nullable_type)$/.test(c.type)) : null) ?? null;
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import type { SemanticDiffOperation } from './pierre-types.js';
|
|
2
|
+
export type SymbolKind = 'function' | 'method' | 'constructor' | 'class' | 'interface' | 'enum' | 'struct' | 'property' | 'field' | 'variable' | 'type' | 'module' | 'namespace' | 'import' | 'other';
|
|
3
|
+
export interface OperationSymbol {
|
|
4
|
+
/** The declaration's own name as written, without class, receiver or namespace: "greet", not "Greeter.greet". */
|
|
5
|
+
name: string;
|
|
6
|
+
kind: SymbolKind;
|
|
7
|
+
/** renamed only: the name before (`name` is the name after). */
|
|
8
|
+
oldName?: string;
|
|
9
|
+
/**
|
|
10
|
+
* The enclosing declarations, outermost first, joined with "." ("Greeter", "User.Companion"). For a Kotlin
|
|
11
|
+
* extension function or property it is the receiver type; inside a Swift extension, the extended type.
|
|
12
|
+
*/
|
|
13
|
+
container?: string;
|
|
14
|
+
}
|
|
15
|
+
/** An operation as semanticDiffFile returns it: Pierre's operation plus the optional symbol. */
|
|
16
|
+
export type SemDiffOperation = SemanticDiffOperation & {
|
|
17
|
+
symbol?: OperationSymbol;
|
|
18
|
+
};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
import type { Node } from 'web-tree-sitter';
|
|
2
|
+
import type { LanguageConfig } from './parse.js';
|
|
3
|
+
import type { SemDiffOperation } from './symbol-types.js';
|
|
4
|
+
export type { OperationSymbol, SemDiffOperation, SymbolKind } from './symbol-types.js';
|
|
5
|
+
/** Set `symbol` on every operation the rules name a declaration for (docs/symbols.md); never changes anything else. */
|
|
6
|
+
export declare function attachSymbols(ops: SemDiffOperation[], oldRoot: Node, newRoot: Node, oldText: string, newText: string, cfg: LanguageConfig): void;
|