@openrewrite/rewrite 8.97.1 → 8.98.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/README.md +42 -0
- package/dist/javascript/comparator.d.ts +16 -10
- package/dist/javascript/comparator.d.ts.map +1 -1
- package/dist/javascript/comparator.js +10 -8
- package/dist/javascript/comparator.js.map +1 -1
- package/dist/javascript/package-json-parser.d.ts +2 -0
- package/dist/javascript/package-json-parser.d.ts.map +1 -1
- package/dist/javascript/package-json-parser.js +27 -15
- package/dist/javascript/package-json-parser.js.map +1 -1
- package/dist/javascript/package-manager.d.ts +13 -4
- package/dist/javascript/package-manager.d.ts.map +1 -1
- package/dist/javascript/package-manager.js +134 -17
- package/dist/javascript/package-manager.js.map +1 -1
- package/dist/javascript/print.d.ts.map +1 -1
- package/dist/javascript/print.js +1 -1
- package/dist/javascript/print.js.map +1 -1
- package/dist/javascript/recipes/dependencies.d.ts +17 -0
- package/dist/javascript/recipes/dependencies.d.ts.map +1 -1
- package/dist/javascript/recipes/dependencies.js +8 -0
- package/dist/javascript/recipes/dependencies.js.map +1 -1
- package/dist/javascript/templating/comparator.d.ts +32 -79
- package/dist/javascript/templating/comparator.d.ts.map +1 -1
- package/dist/javascript/templating/comparator.js +35 -628
- package/dist/javascript/templating/comparator.js.map +1 -1
- package/dist/javascript/templating/debug.d.ts +143 -0
- package/dist/javascript/templating/debug.d.ts.map +1 -0
- package/dist/javascript/templating/debug.js +769 -0
- package/dist/javascript/templating/debug.js.map +1 -0
- package/dist/javascript/templating/index.d.ts +1 -1
- package/dist/javascript/templating/index.d.ts.map +1 -1
- package/dist/javascript/templating/index.js.map +1 -1
- package/dist/javascript/templating/pattern.d.ts +44 -35
- package/dist/javascript/templating/pattern.d.ts.map +1 -1
- package/dist/javascript/templating/pattern.js +224 -238
- package/dist/javascript/templating/pattern.js.map +1 -1
- package/dist/javascript/templating/rewrite.d.ts +4 -0
- package/dist/javascript/templating/rewrite.d.ts.map +1 -1
- package/dist/javascript/templating/rewrite.js +5 -1
- package/dist/javascript/templating/rewrite.js.map +1 -1
- package/dist/javascript/templating/types.d.ts +38 -35
- package/dist/javascript/templating/types.d.ts.map +1 -1
- package/dist/javascript/templating/utils.d.ts.map +1 -1
- package/dist/javascript/templating/utils.js +15 -0
- package/dist/javascript/templating/utils.js.map +1 -1
- package/dist/rewrite-javascript-version.txt +1 -1
- package/dist/rpc/request/install-recipes.d.ts +10 -0
- package/dist/rpc/request/install-recipes.d.ts.map +1 -1
- package/dist/rpc/request/install-recipes.js +51 -0
- package/dist/rpc/request/install-recipes.js.map +1 -1
- package/dist/rpc/request/prepare-recipe.d.ts +1 -0
- package/dist/rpc/request/prepare-recipe.d.ts.map +1 -1
- package/dist/rpc/request/prepare-recipe.js +13 -6
- package/dist/rpc/request/prepare-recipe.js.map +1 -1
- package/dist/test/rewrite-test.d.ts +1 -16
- package/dist/test/rewrite-test.d.ts.map +1 -1
- package/dist/test/rewrite-test.js +7 -58
- package/dist/test/rewrite-test.js.map +1 -1
- package/dist/util.d.ts +16 -0
- package/dist/util.d.ts.map +1 -1
- package/dist/util.js +53 -0
- package/dist/util.js.map +1 -1
- package/dist/yaml/parser.js +8 -5
- package/dist/yaml/parser.js.map +1 -1
- package/dist/yaml/print.js +9 -3
- package/dist/yaml/print.js.map +1 -1
- package/package.json +1 -1
- package/src/javascript/comparator.ts +66 -53
- package/src/javascript/package-json-parser.ts +25 -16
- package/src/javascript/package-manager.ts +121 -15
- package/src/javascript/print.ts +1 -2
- package/src/javascript/recipes/dependencies.ts +21 -0
- package/src/javascript/templating/comparator.ts +76 -766
- package/src/javascript/templating/debug.ts +806 -0
- package/src/javascript/templating/index.ts +1 -0
- package/src/javascript/templating/pattern.ts +267 -270
- package/src/javascript/templating/rewrite.ts +5 -1
- package/src/javascript/templating/types.ts +40 -37
- package/src/javascript/templating/utils.ts +0 -1
- package/src/rpc/request/install-recipes.ts +54 -0
- package/src/rpc/request/prepare-recipe.ts +18 -15
- package/src/test/rewrite-test.ts +2 -59
- package/src/util.ts +58 -0
- package/src/yaml/parser.ts +9 -5
- package/src/yaml/print.ts +9 -3
|
@@ -0,0 +1,769 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/*
|
|
3
|
+
* Copyright 2025 the original author or authors.
|
|
4
|
+
* <p>
|
|
5
|
+
* Licensed under the Moderne Source Available License (the "License");
|
|
6
|
+
* you may not use this file except in compliance with the License.
|
|
7
|
+
* You may obtain a copy of the License at
|
|
8
|
+
* <p>
|
|
9
|
+
* https://docs.moderne.io/licensing/moderne-source-available-license
|
|
10
|
+
* <p>
|
|
11
|
+
* Unless required by applicable law or agreed to in writing, software
|
|
12
|
+
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
13
|
+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
14
|
+
* See the License for the specific language governing permissions and
|
|
15
|
+
* limitations under the License.
|
|
16
|
+
*/
|
|
17
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
18
|
+
if (k2 === undefined) k2 = k;
|
|
19
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
20
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
21
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
22
|
+
}
|
|
23
|
+
Object.defineProperty(o, k2, desc);
|
|
24
|
+
}) : (function(o, m, k, k2) {
|
|
25
|
+
if (k2 === undefined) k2 = k;
|
|
26
|
+
o[k2] = m[k];
|
|
27
|
+
}));
|
|
28
|
+
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
|
29
|
+
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
|
30
|
+
}) : function(o, v) {
|
|
31
|
+
o["default"] = v;
|
|
32
|
+
});
|
|
33
|
+
var __importStar = (this && this.__importStar) || (function () {
|
|
34
|
+
var ownKeys = function(o) {
|
|
35
|
+
ownKeys = Object.getOwnPropertyNames || function (o) {
|
|
36
|
+
var ar = [];
|
|
37
|
+
for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
|
|
38
|
+
return ar;
|
|
39
|
+
};
|
|
40
|
+
return ownKeys(o);
|
|
41
|
+
};
|
|
42
|
+
return function (mod) {
|
|
43
|
+
if (mod && mod.__esModule) return mod;
|
|
44
|
+
var result = {};
|
|
45
|
+
if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
|
|
46
|
+
__setModuleDefault(result, mod);
|
|
47
|
+
return result;
|
|
48
|
+
};
|
|
49
|
+
})();
|
|
50
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
51
|
+
exports.DebugPatternMatchingComparator = exports.ElementMarkerVisitor = exports.AnsiAwarePrintOutputCapture = exports.PATTERN_DEBUG_MARKER_PRINTER = exports.CaptureHighlightMarker = exports.PatternMismatchMarker = void 0;
|
|
52
|
+
exports.shouldUseEmoji = shouldUseEmoji;
|
|
53
|
+
exports.callSite = callSite;
|
|
54
|
+
exports.mismatchDelimiters = mismatchDelimiters;
|
|
55
|
+
exports.sourceLocation = sourceLocation;
|
|
56
|
+
exports.narrowMark = narrowMark;
|
|
57
|
+
exports.aroundMarks = aroundMarks;
|
|
58
|
+
exports.describe = describe;
|
|
59
|
+
exports.describeKind = describeKind;
|
|
60
|
+
/**
|
|
61
|
+
* Consolidated debug module containing all pattern debugging functionality:
|
|
62
|
+
* - Marker classes and utilities for highlighting mismatches
|
|
63
|
+
* - Custom marker printer for rendering debug markers
|
|
64
|
+
* - Debug-enabled pattern matching comparator with detailed logging
|
|
65
|
+
*/
|
|
66
|
+
const path = __importStar(require("path"));
|
|
67
|
+
const __1 = require("../..");
|
|
68
|
+
const markers_1 = require("../../markers");
|
|
69
|
+
const print_1 = require("../../print");
|
|
70
|
+
const java_1 = require("../../java");
|
|
71
|
+
const index_1 = require("../index");
|
|
72
|
+
const comparator_1 = require("./comparator");
|
|
73
|
+
const utils_1 = require("./utils");
|
|
74
|
+
// ============================================================================
|
|
75
|
+
// Marker Classes and Utilities
|
|
76
|
+
// ============================================================================
|
|
77
|
+
/**
|
|
78
|
+
* Custom marker for highlighting pattern mismatches in debug output.
|
|
79
|
+
* Contains before/after strings to render around the marked element.
|
|
80
|
+
*/
|
|
81
|
+
class PatternMismatchMarker {
|
|
82
|
+
constructor(before, after, id) {
|
|
83
|
+
this.before = before;
|
|
84
|
+
this.after = after;
|
|
85
|
+
this.kind = 'org.openrewrite.javascript.templating.PatternMismatchMarker';
|
|
86
|
+
this.id = id || (0, __1.randomId)();
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
exports.PatternMismatchMarker = PatternMismatchMarker;
|
|
90
|
+
/**
|
|
91
|
+
* Marks the code a capture holds in a successful match, with a circled letter for the capture's number.
|
|
92
|
+
*/
|
|
93
|
+
class CaptureHighlightMarker {
|
|
94
|
+
constructor(captureNumber, id) {
|
|
95
|
+
this.captureNumber = captureNumber;
|
|
96
|
+
this.kind = 'org.openrewrite.javascript.templating.CaptureHighlightMarker';
|
|
97
|
+
this.id = id || (0, __1.randomId)();
|
|
98
|
+
}
|
|
99
|
+
/** Ⓐ for the first capture through Ⓩ for the 26th, and the number in brackets after that. */
|
|
100
|
+
symbol() {
|
|
101
|
+
return this.captureNumber >= 1 && this.captureNumber <= 26 ?
|
|
102
|
+
String.fromCodePoint(0x24B6 + this.captureNumber - 1) : `[${this.captureNumber}]`;
|
|
103
|
+
}
|
|
104
|
+
}
|
|
105
|
+
exports.CaptureHighlightMarker = CaptureHighlightMarker;
|
|
106
|
+
/**
|
|
107
|
+
* Whether debug output marks mismatches with 👉…👈 rather than colours and inverse video. FORCE_COLOR asks
|
|
108
|
+
* for colours, and otherwise NO_COLOR and a Claude Code session ask for markers. Then stderr decides where
|
|
109
|
+
* it is a terminal. A test runner's worker cannot see the terminal, so the terminal's own colour settings
|
|
110
|
+
* decide.
|
|
111
|
+
*/
|
|
112
|
+
function shouldUseEmoji() {
|
|
113
|
+
var _a;
|
|
114
|
+
if (process.env.FORCE_COLOR && process.env.FORCE_COLOR !== '0') {
|
|
115
|
+
return false;
|
|
116
|
+
}
|
|
117
|
+
if (process.env.CLAUDECODE === '1' || process.env.NO_COLOR) {
|
|
118
|
+
return true;
|
|
119
|
+
}
|
|
120
|
+
if (process.stderr.isTTY) {
|
|
121
|
+
return false;
|
|
122
|
+
}
|
|
123
|
+
return !(process.env.COLORTERM || ((_a = process.env.TERM) === null || _a === void 0 ? void 0 : _a.includes('color')));
|
|
124
|
+
}
|
|
125
|
+
/**
|
|
126
|
+
* The first frame of `stack` outside this package, as `file:line:column (function)` with `file` relative
|
|
127
|
+
* to the working directory. The stack's own first frame names the package's root.
|
|
128
|
+
*/
|
|
129
|
+
function callSite(stack) {
|
|
130
|
+
var _a;
|
|
131
|
+
const frames = (stack !== null && stack !== void 0 ? stack : '').split('\n')
|
|
132
|
+
.map(line => /^\s*at (?:async )?(?:(.+?) \()?(?:file:\/\/)?(.+?):(\d+):(\d+)\)?$/.exec(line))
|
|
133
|
+
.filter((frame) => frame !== null);
|
|
134
|
+
const own = (_a = frames[0]) === null || _a === void 0 ? void 0 : _a[2];
|
|
135
|
+
const rootEnd = own ? own.lastIndexOf('/javascript/templating/') : -1;
|
|
136
|
+
const root = rootEnd >= 0 ? own.slice(0, rootEnd + 1) : undefined;
|
|
137
|
+
const caller = frames.find(([, , file]) => !(root && file.startsWith(root)) && !file.startsWith('node:'));
|
|
138
|
+
if (!caller) {
|
|
139
|
+
return undefined;
|
|
140
|
+
}
|
|
141
|
+
const [, fn, file, line, column] = caller;
|
|
142
|
+
return `${path.relative(process.cwd(), file)}:${line}:${column}${fn ? ` (${fn})` : ''}`;
|
|
143
|
+
}
|
|
144
|
+
/** What a mismatch mark prints before and after the node it is on. */
|
|
145
|
+
function mismatchDelimiters(emoji = shouldUseEmoji()) {
|
|
146
|
+
return emoji ? ['👉', '👈'] : ['\x1b[7m', '\x1b[27m'];
|
|
147
|
+
}
|
|
148
|
+
/** Marks where a tree starts, for {@link sourceLocation}. */
|
|
149
|
+
class PositionMarker {
|
|
150
|
+
constructor(index) {
|
|
151
|
+
this.index = index;
|
|
152
|
+
this.kind = 'org.openrewrite.javascript.templating.PositionMarker';
|
|
153
|
+
this.id = (0, __1.randomId)();
|
|
154
|
+
}
|
|
155
|
+
}
|
|
156
|
+
const positionsBySourceFile = new WeakMap();
|
|
157
|
+
/** Where each tree of `sourceFile` starts, from one print of the file with every tree's start marked. */
|
|
158
|
+
async function positionsIn(sourceFile) {
|
|
159
|
+
let positions = positionsBySourceFile.get(sourceFile);
|
|
160
|
+
if (positions) {
|
|
161
|
+
return positions;
|
|
162
|
+
}
|
|
163
|
+
const trees = [];
|
|
164
|
+
const marked = await new class extends index_1.JavaScriptVisitor {
|
|
165
|
+
async visit(tree, p) {
|
|
166
|
+
if (!tree) {
|
|
167
|
+
return tree;
|
|
168
|
+
}
|
|
169
|
+
const index = trees.push(tree) - 1;
|
|
170
|
+
const visited = await super.visit(tree, p);
|
|
171
|
+
return visited && Object.assign(Object.assign({}, visited), { markers: Object.assign(Object.assign({}, visited.markers), { markers: [...visited.markers.markers, new PositionMarker(index)] }) });
|
|
172
|
+
}
|
|
173
|
+
}().visit(sourceFile, undefined);
|
|
174
|
+
const printed = await print_1.TreePrinters.printer(sourceFile).print(marked, new print_1.PrintOutputCapture({
|
|
175
|
+
beforeSyntax: marker => marker instanceof PositionMarker ? `\0${marker.index}\0` : '',
|
|
176
|
+
beforePrefix: () => '',
|
|
177
|
+
afterSyntax: () => ''
|
|
178
|
+
}));
|
|
179
|
+
positions = new Map();
|
|
180
|
+
let line = 1;
|
|
181
|
+
let column = 1;
|
|
182
|
+
for (let i = 0; i < printed.length; i++) {
|
|
183
|
+
if (printed[i] === '\0') {
|
|
184
|
+
const end = printed.indexOf('\0', i + 1);
|
|
185
|
+
positions.set(trees[Number(printed.slice(i + 1, end))], { line, column });
|
|
186
|
+
i = end;
|
|
187
|
+
}
|
|
188
|
+
else if (printed[i] === '\n') {
|
|
189
|
+
line++;
|
|
190
|
+
column = 1;
|
|
191
|
+
}
|
|
192
|
+
else {
|
|
193
|
+
column++;
|
|
194
|
+
}
|
|
195
|
+
}
|
|
196
|
+
positionsBySourceFile.set(sourceFile, positions);
|
|
197
|
+
return positions;
|
|
198
|
+
}
|
|
199
|
+
/**
|
|
200
|
+
* The source file `cursor` leads to, and where `tree` starts in it. A file that does not hold
|
|
201
|
+
* `tree` itself, as for a node a visitor built, gives its path alone.
|
|
202
|
+
*/
|
|
203
|
+
async function sourceLocation(tree, cursor) {
|
|
204
|
+
const sourceFile = (0, __1.isSourceFile)(tree) ? tree : cursor === null || cursor === void 0 ? void 0 : cursor.firstEnclosing(__1.isSourceFile);
|
|
205
|
+
if (!sourceFile) {
|
|
206
|
+
return undefined;
|
|
207
|
+
}
|
|
208
|
+
return Object.assign({ sourcePath: sourceFile.sourcePath }, (await positionsIn(sourceFile)).get(tree));
|
|
209
|
+
}
|
|
210
|
+
/**
|
|
211
|
+
* `printed` with the mark around `element`'s code moved to `focus` within that code. It stays as it
|
|
212
|
+
* is where the mark does not hold exactly what `element` prints on its own.
|
|
213
|
+
*/
|
|
214
|
+
async function narrowMark(printed, element, focus, [before, after]) {
|
|
215
|
+
const printer = print_1.TreePrinters.printer(index_1.JS.Kind.CompilationUnit);
|
|
216
|
+
const code = (await printer.print(element)).trim();
|
|
217
|
+
const at = printed.indexOf(before + code + after);
|
|
218
|
+
let range;
|
|
219
|
+
if (focus === 'start') {
|
|
220
|
+
range = [0, 0];
|
|
221
|
+
}
|
|
222
|
+
else if (focus === 'operator') {
|
|
223
|
+
range = await operatorRange(element, code);
|
|
224
|
+
}
|
|
225
|
+
else {
|
|
226
|
+
const child = (await printer.print(focus.after)).trim();
|
|
227
|
+
const end = code.indexOf(child) + child.length;
|
|
228
|
+
range = child && end >= child.length ? [end, end] : undefined;
|
|
229
|
+
}
|
|
230
|
+
if (at < 0 || !range) {
|
|
231
|
+
return printed;
|
|
232
|
+
}
|
|
233
|
+
const [start, end] = range;
|
|
234
|
+
return printed.slice(0, at) + code.slice(0, start) + before + code.slice(start, end) + after +
|
|
235
|
+
code.slice(end) + printed.slice(at + before.length + code.length + after.length);
|
|
236
|
+
}
|
|
237
|
+
/**
|
|
238
|
+
* The lines within one line of a mark, with `…` for each run of two or more lines left out. A mark
|
|
239
|
+
* over more than six lines keeps its first two and last two.
|
|
240
|
+
*/
|
|
241
|
+
function aroundMarks(lines, [before, after]) {
|
|
242
|
+
let open = 0;
|
|
243
|
+
const marked = lines.map(line => {
|
|
244
|
+
const inside = open > 0 || line.includes(before) || line.includes(after);
|
|
245
|
+
open += line.split(before).length - line.split(after).length;
|
|
246
|
+
return inside;
|
|
247
|
+
});
|
|
248
|
+
if (!marked.includes(true)) {
|
|
249
|
+
return lines;
|
|
250
|
+
}
|
|
251
|
+
const kept = lines.map((_, i) => marked[i] || marked[i - 1] === true || marked[i + 1] === true);
|
|
252
|
+
for (let start = 0; start < lines.length; start++) {
|
|
253
|
+
let end = start;
|
|
254
|
+
while (end < lines.length && marked[end]) {
|
|
255
|
+
end++;
|
|
256
|
+
}
|
|
257
|
+
if (end - start > 6) {
|
|
258
|
+
kept.fill(false, start + 2, end - 2);
|
|
259
|
+
}
|
|
260
|
+
start = end;
|
|
261
|
+
}
|
|
262
|
+
const result = [];
|
|
263
|
+
for (let i = 0; i < lines.length;) {
|
|
264
|
+
let j = i;
|
|
265
|
+
while (j < lines.length && kept[j] === kept[i]) {
|
|
266
|
+
j++;
|
|
267
|
+
}
|
|
268
|
+
result.push(...(!kept[i] && j - i >= 2 ? ['…'] : lines.slice(i, j)));
|
|
269
|
+
i = j;
|
|
270
|
+
}
|
|
271
|
+
return result;
|
|
272
|
+
}
|
|
273
|
+
// ============================================================================
|
|
274
|
+
// Marker Printer
|
|
275
|
+
// ============================================================================
|
|
276
|
+
/**
|
|
277
|
+
* Custom MarkerPrinter for pattern debugging that renders PatternMismatchMarker
|
|
278
|
+
* and CaptureHighlightMarker with custom before/after strings.
|
|
279
|
+
*/
|
|
280
|
+
exports.PATTERN_DEBUG_MARKER_PRINTER = {
|
|
281
|
+
beforeSyntax(marker, cursor, commentWrapper) {
|
|
282
|
+
if (marker instanceof PatternMismatchMarker) {
|
|
283
|
+
return marker.before;
|
|
284
|
+
}
|
|
285
|
+
if (marker instanceof CaptureHighlightMarker) {
|
|
286
|
+
return marker.symbol();
|
|
287
|
+
}
|
|
288
|
+
return print_1.MarkerPrinter.DEFAULT.beforeSyntax(marker, cursor, commentWrapper);
|
|
289
|
+
},
|
|
290
|
+
beforePrefix(marker, cursor, commentWrapper) {
|
|
291
|
+
return print_1.MarkerPrinter.DEFAULT.beforePrefix(marker, cursor, commentWrapper);
|
|
292
|
+
},
|
|
293
|
+
afterSyntax(marker, cursor, commentWrapper) {
|
|
294
|
+
if (marker instanceof PatternMismatchMarker) {
|
|
295
|
+
return marker.after;
|
|
296
|
+
}
|
|
297
|
+
if (marker instanceof CaptureHighlightMarker) {
|
|
298
|
+
return marker.symbol();
|
|
299
|
+
}
|
|
300
|
+
return print_1.MarkerPrinter.DEFAULT.afterSyntax(marker, cursor, commentWrapper);
|
|
301
|
+
}
|
|
302
|
+
};
|
|
303
|
+
// ============================================================================
|
|
304
|
+
// ANSI-Aware Print Output Capture
|
|
305
|
+
// ============================================================================
|
|
306
|
+
/**
|
|
307
|
+
* A print output capture that closes active ANSI escape codes at each newline and resumes them at the
|
|
308
|
+
* next line's first non-blank character. Each line of debug output is then styled on its own, and its
|
|
309
|
+
* indentation stays unstyled so that it can be dedented.
|
|
310
|
+
*/
|
|
311
|
+
class AnsiAwarePrintOutputCapture extends print_1.PrintOutputCapture {
|
|
312
|
+
constructor() {
|
|
313
|
+
super(...arguments);
|
|
314
|
+
this.activeAnsiCodes = [];
|
|
315
|
+
this.resumePending = false;
|
|
316
|
+
}
|
|
317
|
+
append(text) {
|
|
318
|
+
if (!text || text.length === 0) {
|
|
319
|
+
return this;
|
|
320
|
+
}
|
|
321
|
+
if (!this.resumePending && !text.includes('\x1b') && !text.includes('\n')) {
|
|
322
|
+
super.append(text);
|
|
323
|
+
return this;
|
|
324
|
+
}
|
|
325
|
+
// Process the text character by character to handle ANSI codes and newlines
|
|
326
|
+
let i = 0;
|
|
327
|
+
while (i < text.length) {
|
|
328
|
+
// Check for ANSI escape sequence
|
|
329
|
+
if (text[i] === '\x1b' && i + 1 < text.length && text[i + 1] === '[') {
|
|
330
|
+
// Find the end of the ANSI sequence (ends with a letter)
|
|
331
|
+
let j = i + 2;
|
|
332
|
+
while (j < text.length && !/[a-zA-Z]/.test(text[j])) {
|
|
333
|
+
j++;
|
|
334
|
+
}
|
|
335
|
+
if (j < text.length) {
|
|
336
|
+
j++; // Include the terminating letter
|
|
337
|
+
const ansiCode = text.substring(i, j);
|
|
338
|
+
super.append(ansiCode);
|
|
339
|
+
// Track state-changing codes
|
|
340
|
+
// \x1b[7m = inverted colors (start)
|
|
341
|
+
// \x1b[27m = normal colors (end inverted)
|
|
342
|
+
// \x1b[0m = reset all
|
|
343
|
+
if (ansiCode === '\x1b[7m') {
|
|
344
|
+
this.activeAnsiCodes.push(ansiCode);
|
|
345
|
+
}
|
|
346
|
+
else if (ansiCode === '\x1b[27m') {
|
|
347
|
+
// Remove inverted colors from stack
|
|
348
|
+
this.activeAnsiCodes = this.activeAnsiCodes.filter(code => code !== '\x1b[7m');
|
|
349
|
+
}
|
|
350
|
+
else if (ansiCode === '\x1b[0m' || ansiCode === '\x1b[m') {
|
|
351
|
+
// Reset clears all active codes
|
|
352
|
+
this.activeAnsiCodes = [];
|
|
353
|
+
}
|
|
354
|
+
i = j;
|
|
355
|
+
continue;
|
|
356
|
+
}
|
|
357
|
+
}
|
|
358
|
+
// Check for newline
|
|
359
|
+
if (text[i] === '\n') {
|
|
360
|
+
// First, close any active ANSI codes before the newline
|
|
361
|
+
for (let j = this.activeAnsiCodes.length - 1; j >= 0 && !this.resumePending; j--) {
|
|
362
|
+
if (this.activeAnsiCodes[j] === '\x1b[7m') {
|
|
363
|
+
super.append('\x1b[27m');
|
|
364
|
+
}
|
|
365
|
+
}
|
|
366
|
+
super.append('\n');
|
|
367
|
+
this.resumePending = this.activeAnsiCodes.length > 0;
|
|
368
|
+
i++;
|
|
369
|
+
continue;
|
|
370
|
+
}
|
|
371
|
+
if (this.resumePending && text[i] !== ' ' && text[i] !== '\t') {
|
|
372
|
+
for (const code of this.activeAnsiCodes) {
|
|
373
|
+
super.append(code);
|
|
374
|
+
}
|
|
375
|
+
this.resumePending = false;
|
|
376
|
+
}
|
|
377
|
+
// Regular character
|
|
378
|
+
super.append(text[i]);
|
|
379
|
+
i++;
|
|
380
|
+
}
|
|
381
|
+
return this;
|
|
382
|
+
}
|
|
383
|
+
}
|
|
384
|
+
exports.AnsiAwarePrintOutputCapture = AnsiAwarePrintOutputCapture;
|
|
385
|
+
// ============================================================================
|
|
386
|
+
// Element Marker Visitor
|
|
387
|
+
// ============================================================================
|
|
388
|
+
/**
|
|
389
|
+
* Visitor that marks elements with markers for highlighting in debug output.
|
|
390
|
+
* Uses referential equality to find target elements since nodes aren't mutated during matching.
|
|
391
|
+
*
|
|
392
|
+
* Can mark either:
|
|
393
|
+
* - A single element with PatternMismatchMarker (for failures)
|
|
394
|
+
* - Multiple elements with CaptureHighlightMarkers (for successes)
|
|
395
|
+
*/
|
|
396
|
+
class ElementMarkerVisitor extends index_1.JavaScriptVisitor {
|
|
397
|
+
constructor(targetOrCaptures, emoji = shouldUseEmoji()) {
|
|
398
|
+
super();
|
|
399
|
+
this.elementMarkers = new Map();
|
|
400
|
+
if (targetOrCaptures instanceof Map) {
|
|
401
|
+
// Multiple captures - build map of element -> CaptureHighlightMarker
|
|
402
|
+
for (const [captureNum, captured] of targetOrCaptures.entries()) {
|
|
403
|
+
const marker = new CaptureHighlightMarker(captureNum);
|
|
404
|
+
if (Array.isArray(captured)) {
|
|
405
|
+
// For variadic captures, mark each element with the same number
|
|
406
|
+
for (const element of captured) {
|
|
407
|
+
this.elementMarkers.set(element, marker);
|
|
408
|
+
}
|
|
409
|
+
}
|
|
410
|
+
else {
|
|
411
|
+
this.elementMarkers.set(captured, marker);
|
|
412
|
+
}
|
|
413
|
+
}
|
|
414
|
+
}
|
|
415
|
+
else {
|
|
416
|
+
// Single element - create PatternMismatchMarker
|
|
417
|
+
const [before, after] = mismatchDelimiters(emoji);
|
|
418
|
+
this.elementMarkers.set(targetOrCaptures, new PatternMismatchMarker(before, after));
|
|
419
|
+
}
|
|
420
|
+
}
|
|
421
|
+
async visit(tree, p) {
|
|
422
|
+
if (!tree)
|
|
423
|
+
return tree;
|
|
424
|
+
// Check if this element should be marked
|
|
425
|
+
const marker = this.elementMarkers.get(tree);
|
|
426
|
+
if (marker) {
|
|
427
|
+
const updatedMarkers = [...tree.markers.markers, marker];
|
|
428
|
+
const newMarkers = {
|
|
429
|
+
kind: markers_1.MarkersKind.Markers,
|
|
430
|
+
id: tree.markers.id,
|
|
431
|
+
markers: updatedMarkers
|
|
432
|
+
};
|
|
433
|
+
return Object.assign(Object.assign({}, tree), { markers: newMarkers });
|
|
434
|
+
}
|
|
435
|
+
return await super.visit(tree, p);
|
|
436
|
+
}
|
|
437
|
+
async visitContainer(container, p) {
|
|
438
|
+
// Check if this container should be marked
|
|
439
|
+
const marker = this.elementMarkers.get(container);
|
|
440
|
+
if (marker) {
|
|
441
|
+
const updatedMarkers = [...container.markers.markers, marker];
|
|
442
|
+
const newMarkers = {
|
|
443
|
+
kind: markers_1.MarkersKind.Markers,
|
|
444
|
+
id: container.markers.id,
|
|
445
|
+
markers: updatedMarkers
|
|
446
|
+
};
|
|
447
|
+
return Object.assign(Object.assign({}, container), { markers: newMarkers });
|
|
448
|
+
}
|
|
449
|
+
return await super.visitContainer(container, p);
|
|
450
|
+
}
|
|
451
|
+
}
|
|
452
|
+
exports.ElementMarkerVisitor = ElementMarkerVisitor;
|
|
453
|
+
// ============================================================================
|
|
454
|
+
// Debug Pattern Matching Comparator
|
|
455
|
+
// ============================================================================
|
|
456
|
+
/** Whether {@link ElementMarkerVisitor} can mark `value`, which it does for the trees and containers it visits. */
|
|
457
|
+
function isMarkable(value) {
|
|
458
|
+
return !!(value === null || value === void 0 ? void 0 : value.markers) && value.kind !== java_1.J.Kind.LeftPadded && value.kind !== java_1.J.Kind.RightPadded;
|
|
459
|
+
}
|
|
460
|
+
/** The value at a property path such as `lambda.parameters.parameters` or `statements[2]`. */
|
|
461
|
+
function at(node, path) {
|
|
462
|
+
if (!path) {
|
|
463
|
+
return node;
|
|
464
|
+
}
|
|
465
|
+
return path.split(/\.|\[(\d+)]/).filter(Boolean).reduce((current, property) => current === null || current === void 0 ? void 0 : current[property], node);
|
|
466
|
+
}
|
|
467
|
+
/** The code `value` stands for, as one line, or "none" where there is none. */
|
|
468
|
+
async function describe(value) {
|
|
469
|
+
const elements = Array.isArray(value) ? value :
|
|
470
|
+
(value === null || value === void 0 ? void 0 : value.kind) === java_1.J.Kind.Container ? value.elements :
|
|
471
|
+
value === undefined || value === null ? [] : [value];
|
|
472
|
+
const printer = print_1.TreePrinters.printer(index_1.JS.Kind.CompilationUnit);
|
|
473
|
+
const printed = [];
|
|
474
|
+
for (let element of elements) {
|
|
475
|
+
if ((element === null || element === void 0 ? void 0 : element.kind) === java_1.J.Kind.LeftPadded || (element === null || element === void 0 ? void 0 : element.kind) === java_1.J.Kind.RightPadded) {
|
|
476
|
+
element = element.element;
|
|
477
|
+
}
|
|
478
|
+
const text = isMarkable(element) ? (await printer.print(element)).trim() : String(element);
|
|
479
|
+
if (text) {
|
|
480
|
+
printed.push(text.split('\n')[0] + (text.includes('\n') ? ' …' : ''));
|
|
481
|
+
}
|
|
482
|
+
}
|
|
483
|
+
return printed.length > 0 ? printed.join(', ') : 'none';
|
|
484
|
+
}
|
|
485
|
+
/** A tree's kind in words, followed by its kind name, such as `arrow function (JS$ArrowFunction)`. */
|
|
486
|
+
function describeKind(tree) {
|
|
487
|
+
// A function written where an expression goes is held as a statement, which names its kind
|
|
488
|
+
const named = (tree === null || tree === void 0 ? void 0 : tree.kind) === index_1.JS.Kind.StatementExpression ? tree.statement : tree;
|
|
489
|
+
return `${kindInWords(tree)} (${shortKind(named)})`;
|
|
490
|
+
}
|
|
491
|
+
function shortKind(tree) {
|
|
492
|
+
return (tree === null || tree === void 0 ? void 0 : tree.kind) ? tree.kind.substring(tree.kind.lastIndexOf('.') + 1) : 'unknown';
|
|
493
|
+
}
|
|
494
|
+
function kindInWords(tree) {
|
|
495
|
+
var _a;
|
|
496
|
+
// A function written where an expression goes is held as a statement
|
|
497
|
+
return (tree === null || tree === void 0 ? void 0 : tree.kind) === index_1.JS.Kind.StatementExpression && ((_a = tree.statement) === null || _a === void 0 ? void 0 : _a.kind) === java_1.J.Kind.MethodDeclaration ?
|
|
498
|
+
'function expression' : inWords(shortKind(tree).replace(/^[A-Za-z]+\$/, '').replace(/\$/g, ' '));
|
|
499
|
+
}
|
|
500
|
+
function inWords(name) {
|
|
501
|
+
return name.replace(/([a-z])([A-Z])/g, '$1 $2').toLowerCase();
|
|
502
|
+
}
|
|
503
|
+
/** Properties whose names say something other than what TypeScript calls them where they appear. */
|
|
504
|
+
const PROPERTY_WORDS = {
|
|
505
|
+
[`${java_1.J.Kind.MethodInvocation}.typeParameters`]: 'type arguments',
|
|
506
|
+
[`${index_1.JS.Kind.FunctionCall}.typeParameters`]: 'type arguments',
|
|
507
|
+
[`${java_1.J.Kind.NewClass}.typeParameters`]: 'type arguments',
|
|
508
|
+
[`${index_1.JS.Kind.ImportClause}.name`]: 'default import',
|
|
509
|
+
[`${index_1.JS.Kind.ImportClause}.namedBindings`]: 'named imports'
|
|
510
|
+
};
|
|
511
|
+
/**
|
|
512
|
+
* The code `tree` writes besides its operands, such as the `+` of a binary or the `!` of a unary.
|
|
513
|
+
* Each operand prints at the start or the end of its operation, so what remains is the operator.
|
|
514
|
+
*/
|
|
515
|
+
async function operatorOf(tree) {
|
|
516
|
+
const code = (await print_1.TreePrinters.printer(index_1.JS.Kind.CompilationUnit).print(tree)).trim();
|
|
517
|
+
const range = await operatorRange(tree, code);
|
|
518
|
+
return range ? code.slice(...range) : undefined;
|
|
519
|
+
}
|
|
520
|
+
/** Where the operator is in `code`, which is what `tree` prints. */
|
|
521
|
+
async function operatorRange(tree, code) {
|
|
522
|
+
var _a, _b, _c, _d;
|
|
523
|
+
const printer = print_1.TreePrinters.printer(index_1.JS.Kind.CompilationUnit);
|
|
524
|
+
let start = 0;
|
|
525
|
+
let end = code.length;
|
|
526
|
+
const operands = [(_b = (_a = tree.left) !== null && _a !== void 0 ? _a : tree.variable) !== null && _b !== void 0 ? _b : tree.expression, (_d = (_c = tree.right) !== null && _c !== void 0 ? _c : tree.assignment) !== null && _d !== void 0 ? _d : tree.expression];
|
|
527
|
+
for (const operand of operands) {
|
|
528
|
+
const text = operand ? (await printer.print(operand)).trim() : '';
|
|
529
|
+
if (text && code.startsWith(text, start)) {
|
|
530
|
+
start += text.length;
|
|
531
|
+
}
|
|
532
|
+
else if (text && code.slice(start, end).endsWith(text)) {
|
|
533
|
+
end -= text.length;
|
|
534
|
+
}
|
|
535
|
+
}
|
|
536
|
+
while (start < end && /\s/.test(code[start])) {
|
|
537
|
+
start++;
|
|
538
|
+
}
|
|
539
|
+
while (end > start && /\s/.test(code[end - 1])) {
|
|
540
|
+
end--;
|
|
541
|
+
}
|
|
542
|
+
return start < end ? [start, end] : undefined;
|
|
543
|
+
}
|
|
544
|
+
/** How the pattern's own text writes a capture, which the debug output shows as `🪝name`. */
|
|
545
|
+
function placeholder(marker) {
|
|
546
|
+
return `${utils_1.PlaceholderUtils.CAPTURE_PREFIX}${marker.captureName}__`;
|
|
547
|
+
}
|
|
548
|
+
/**
|
|
549
|
+
* Explains why a pattern does not match, and changes nothing about matching itself. It records
|
|
550
|
+
* what the base class reports through its mismatch methods and hooks, with the nodes to mark.
|
|
551
|
+
*/
|
|
552
|
+
class DebugPatternMatchingComparator extends comparator_1.PatternMatchingComparator {
|
|
553
|
+
get debug() {
|
|
554
|
+
return this.matcher.debug;
|
|
555
|
+
}
|
|
556
|
+
/**
|
|
557
|
+
* Extracts the last segment of a kind string (after the last dot).
|
|
558
|
+
* For example: "org.openrewrite.java.tree.J.MethodInvocation" -> "MethodInvocation"
|
|
559
|
+
*/
|
|
560
|
+
formatKind(kind) {
|
|
561
|
+
if (!kind)
|
|
562
|
+
return 'unknown';
|
|
563
|
+
return kind.substring(kind.lastIndexOf('.') + 1);
|
|
564
|
+
}
|
|
565
|
+
/**
|
|
566
|
+
* Formats a value for display in error messages.
|
|
567
|
+
*/
|
|
568
|
+
formatValue(value) {
|
|
569
|
+
if (value === null)
|
|
570
|
+
return 'null';
|
|
571
|
+
if (value === undefined)
|
|
572
|
+
return 'undefined';
|
|
573
|
+
if (typeof value === 'string')
|
|
574
|
+
return `"${value}"`;
|
|
575
|
+
if (typeof value === 'number' || typeof value === 'boolean')
|
|
576
|
+
return String(value);
|
|
577
|
+
if (java_1.Type.isType(value)) {
|
|
578
|
+
return java_1.Type.isFullyQualified(value) ?
|
|
579
|
+
java_1.Type.FullyQualified.getFullyQualifiedName(value) :
|
|
580
|
+
this.formatKind(value.kind);
|
|
581
|
+
}
|
|
582
|
+
if (value.kind === java_1.J.Kind.LeftPadded || value.kind === java_1.J.Kind.RightPadded) {
|
|
583
|
+
return this.formatValue(value.element);
|
|
584
|
+
}
|
|
585
|
+
// For objects with a kind property (LST nodes)
|
|
586
|
+
if (value && typeof value === 'object' && value.kind) {
|
|
587
|
+
const kind = this.formatKind(value.kind);
|
|
588
|
+
// Show key identifying properties for common node types
|
|
589
|
+
if (value.simpleName)
|
|
590
|
+
return `${kind}("${value.simpleName}")`;
|
|
591
|
+
if (value.value !== undefined)
|
|
592
|
+
return `${kind}(${this.formatValue(value.value)})`;
|
|
593
|
+
return kind;
|
|
594
|
+
}
|
|
595
|
+
return String(value);
|
|
596
|
+
}
|
|
597
|
+
/** A property's name and the code it holds, such as `modifiers: async`. */
|
|
598
|
+
async describeProperty(propertyName, value) {
|
|
599
|
+
return propertyName ? `${this.propertyLabel(propertyName)}: ${await describe(value)}` : describe(value);
|
|
600
|
+
}
|
|
601
|
+
/**
|
|
602
|
+
* The property in words: the last name in its path, or for a container's `elements` or a padded
|
|
603
|
+
* value, the name its owner holds it under.
|
|
604
|
+
*/
|
|
605
|
+
propertyLabel(propertyName) {
|
|
606
|
+
var _a, _b, _c, _d, _e, _f, _g, _h;
|
|
607
|
+
let owner = (_a = this.cursor) === null || _a === void 0 ? void 0 : _a.value;
|
|
608
|
+
let name = propertyName === null || propertyName === void 0 ? void 0 : propertyName.replace(/\[\d+]$/, '').split('.').pop();
|
|
609
|
+
const wrapper = (_b = this.cursor) === null || _b === void 0 ? void 0 : _b.value;
|
|
610
|
+
if (propertyName === undefined || propertyName === 'elements' && (wrapper === null || wrapper === void 0 ? void 0 : wrapper.kind) === java_1.J.Kind.Container) {
|
|
611
|
+
owner = (_d = (_c = this.cursor) === null || _c === void 0 ? void 0 : _c.parent) === null || _d === void 0 ? void 0 : _d.value;
|
|
612
|
+
name = owner ? Object.keys(owner).find(key => owner[key] === wrapper) : undefined;
|
|
613
|
+
}
|
|
614
|
+
else if (name === 'statements' && (wrapper === null || wrapper === void 0 ? void 0 : wrapper.kind) === java_1.J.Kind.Block &&
|
|
615
|
+
((_g = (_f = (_e = this.cursor) === null || _e === void 0 ? void 0 : _e.parent) === null || _f === void 0 ? void 0 : _f.value) === null || _g === void 0 ? void 0 : _g.kind) === java_1.J.Kind.NewClass) {
|
|
616
|
+
// An object literal holds its properties as the statements of a block
|
|
617
|
+
return 'properties';
|
|
618
|
+
}
|
|
619
|
+
return name ? (_h = PROPERTY_WORDS[`${owner === null || owner === void 0 ? void 0 : owner.kind}.${name}`]) !== null && _h !== void 0 ? _h : inWords(name) : 'value';
|
|
620
|
+
}
|
|
621
|
+
/**
|
|
622
|
+
* The node a mismatch is shown on. That is `element`, or the element a padding wrapper holds,
|
|
623
|
+
* where it can be marked, and otherwise the nearest node on `cursor` that can.
|
|
624
|
+
*/
|
|
625
|
+
markable(element, cursor) {
|
|
626
|
+
if ((element === null || element === void 0 ? void 0 : element.kind) === java_1.J.Kind.LeftPadded || (element === null || element === void 0 ? void 0 : element.kind) === java_1.J.Kind.RightPadded) {
|
|
627
|
+
element = element.element;
|
|
628
|
+
}
|
|
629
|
+
if (isMarkable(element)) {
|
|
630
|
+
return element;
|
|
631
|
+
}
|
|
632
|
+
for (let c = cursor; c; c = c.parent) {
|
|
633
|
+
if (isMarkable(c.value)) {
|
|
634
|
+
return c.value;
|
|
635
|
+
}
|
|
636
|
+
}
|
|
637
|
+
return undefined;
|
|
638
|
+
}
|
|
639
|
+
/** Records why matching failed, unless an earlier mismatch already did, and stops the match. */
|
|
640
|
+
fail(t, reason, expected, actual, details, patternElement, targetElement, focus) {
|
|
641
|
+
if (this.match) {
|
|
642
|
+
this.explain(reason, expected, actual, details, patternElement, targetElement, focus);
|
|
643
|
+
}
|
|
644
|
+
this.match = false;
|
|
645
|
+
return t;
|
|
646
|
+
}
|
|
647
|
+
explain(reason, expected, actual, details, patternElement, targetElement, focus) {
|
|
648
|
+
this.debug.setExplanation(reason, expected, actual, details, this.markable(patternElement, this.cursor), this.markable(targetElement, this.targetCursor), focus);
|
|
649
|
+
}
|
|
650
|
+
/** An abort that names no values leaves the explanation to the matcher's generic one. */
|
|
651
|
+
abort(t, reason, propertyName, expected, actual) {
|
|
652
|
+
var _a, _b;
|
|
653
|
+
if (reason && (expected !== undefined || actual !== undefined)) {
|
|
654
|
+
return this.fail(t, reason, this.formatValue(expected), this.formatValue(actual), undefined, (_a = this.cursor) === null || _a === void 0 ? void 0 : _a.value, (_b = this.targetCursor) === null || _b === void 0 ? void 0 : _b.value);
|
|
655
|
+
}
|
|
656
|
+
return super.abort(t, reason, propertyName, expected, actual);
|
|
657
|
+
}
|
|
658
|
+
kindMismatch() {
|
|
659
|
+
var _a, _b;
|
|
660
|
+
const pattern = (_a = this.cursor) === null || _a === void 0 ? void 0 : _a.value;
|
|
661
|
+
const target = (_b = this.targetCursor) === null || _b === void 0 ? void 0 : _b.value;
|
|
662
|
+
// Operations of one name in words, such as JavaScript's `===` and Java's `-`, differ in their operators
|
|
663
|
+
if (kindInWords(pattern) === kindInWords(target) && (pattern === null || pattern === void 0 ? void 0 : pattern.operator) && (target === null || target === void 0 ? void 0 : target.operator)) {
|
|
664
|
+
const describeOperator = async (tree) => { var _a; return `operator: ${(_a = await operatorOf(tree)) !== null && _a !== void 0 ? _a : describeKind(tree)}`; };
|
|
665
|
+
return this.fail(pattern, 'kind-mismatch', describeOperator(pattern), describeOperator(target), undefined, pattern, target, { pattern: 'operator', target: 'operator' });
|
|
666
|
+
}
|
|
667
|
+
const names = [java_1.J.Kind.Identifier, java_1.J.Kind.FieldAccess];
|
|
668
|
+
const note = names.includes(pattern === null || pattern === void 0 ? void 0 : pattern.kind) && names.includes(target === null || target === void 0 ? void 0 : target.kind) ?
|
|
669
|
+
'A pattern matches a name as it is written, not what it refers to. ' +
|
|
670
|
+
'Write it as the source does, or put a capture in its place.' : undefined;
|
|
671
|
+
return this.fail(pattern, 'kind-mismatch', describeKind(pattern), describeKind(target), note, pattern, target);
|
|
672
|
+
}
|
|
673
|
+
structuralMismatch(propertyName) {
|
|
674
|
+
var _a, _b;
|
|
675
|
+
const pattern = (_a = this.cursor) === null || _a === void 0 ? void 0 : _a.value;
|
|
676
|
+
const target = (_b = this.targetCursor) === null || _b === void 0 ? void 0 : _b.value;
|
|
677
|
+
const expectedValue = propertyName ? at(pattern, propertyName) : pattern;
|
|
678
|
+
const actualValue = propertyName ? at(target, propertyName) : target;
|
|
679
|
+
const onlyOneSide = (expectedValue === undefined) !== (actualValue === undefined);
|
|
680
|
+
// The side without the property is marked where it would go
|
|
681
|
+
const missing = (node) => propertyName === 'typeParameters' && (node === null || node === void 0 ? void 0 : node.name) ? { after: node.name } :
|
|
682
|
+
propertyName === 'select' ? 'start' : undefined;
|
|
683
|
+
return this.fail(pattern, 'structural-mismatch', this.describeProperty(propertyName, expectedValue), this.describeProperty(propertyName, actualValue), undefined, onlyOneSide ? expectedValue !== null && expectedValue !== void 0 ? expectedValue : pattern : pattern, onlyOneSide ? actualValue !== null && actualValue !== void 0 ? actualValue : target : target, onlyOneSide ? {
|
|
684
|
+
pattern: expectedValue === undefined ? missing(pattern) : undefined,
|
|
685
|
+
target: actualValue === undefined ? missing(target) : undefined
|
|
686
|
+
} : undefined);
|
|
687
|
+
}
|
|
688
|
+
arrayLengthMismatch(propertyName) {
|
|
689
|
+
var _a, _b, _c, _d;
|
|
690
|
+
const pattern = (_a = this.cursor) === null || _a === void 0 ? void 0 : _a.value;
|
|
691
|
+
const target = (_b = this.targetCursor) === null || _b === void 0 ? void 0 : _b.value;
|
|
692
|
+
const label = this.propertyLabel(propertyName);
|
|
693
|
+
const hint = label === 'arguments' || label === 'properties' ?
|
|
694
|
+
`A capture matches one ${label === 'arguments' ? 'argument' : 'property'}. ` +
|
|
695
|
+
'capture({variadic: true}) matches any number of them.' : undefined;
|
|
696
|
+
const expected = (_c = at(pattern, propertyName)) !== null && _c !== void 0 ? _c : [];
|
|
697
|
+
const actual = (_d = at(target, propertyName)) !== null && _d !== void 0 ? _d : [];
|
|
698
|
+
// A modifier only one side has is marked, and the other side where it would go
|
|
699
|
+
const modifier = label === 'modifiers' && (expected.length === 0) !== (actual.length === 0);
|
|
700
|
+
return this.fail(pattern, 'array-length-mismatch', this.describeProperty(propertyName, expected), this.describeProperty(propertyName, actual), hint, modifier && expected.length > 0 ? expected[0] : pattern, modifier && actual.length > 0 ? actual[0] : target, modifier ? {
|
|
701
|
+
pattern: expected.length === 0 ? 'start' : undefined,
|
|
702
|
+
target: actual.length === 0 ? 'start' : undefined
|
|
703
|
+
} : undefined);
|
|
704
|
+
}
|
|
705
|
+
valueMismatch(propertyName, expected, actual) {
|
|
706
|
+
var _a, _b, _c, _d, _e, _f;
|
|
707
|
+
const pattern = (_a = this.cursor) === null || _a === void 0 ? void 0 : _a.value;
|
|
708
|
+
const target = (_b = this.targetCursor) === null || _b === void 0 ? void 0 : _b.value;
|
|
709
|
+
const padded = (pattern === null || pattern === void 0 ? void 0 : pattern.kind) === java_1.J.Kind.LeftPadded || (pattern === null || pattern === void 0 ? void 0 : pattern.kind) === java_1.J.Kind.RightPadded;
|
|
710
|
+
if (!propertyName && padded && typeof expected === 'string') {
|
|
711
|
+
const label = this.propertyLabel(undefined);
|
|
712
|
+
const describeOperator = async (tree, value) => { var _a; return `${label}: ${(_a = await operatorOf(tree)) !== null && _a !== void 0 ? _a : value}`; };
|
|
713
|
+
const patternHolder = (_d = (_c = this.cursor) === null || _c === void 0 ? void 0 : _c.parent) === null || _d === void 0 ? void 0 : _d.value;
|
|
714
|
+
const targetHolder = (_f = (_e = this.targetCursor) === null || _e === void 0 ? void 0 : _e.parent) === null || _f === void 0 ? void 0 : _f.value;
|
|
715
|
+
return this.fail(pattern, 'value-mismatch', describeOperator(patternHolder, expected), describeOperator(targetHolder, actual), undefined, patternHolder, targetHolder, { pattern: 'operator', target: 'operator' });
|
|
716
|
+
}
|
|
717
|
+
const path = propertyName ? propertyName.split('.') : [];
|
|
718
|
+
// The mismatch shows on the node holding the property, and a call's method type on the call's name
|
|
719
|
+
const holder = (pattern === null || pattern === void 0 ? void 0 : pattern.kind) === java_1.J.Kind.MethodInvocation && path[0] === 'methodType' ?
|
|
720
|
+
'name' : path.slice(0, -1).join('.');
|
|
721
|
+
const declared = path[path.length - 1] === 'declaringType';
|
|
722
|
+
const asWritten = (tree) => (tree === null || tree === void 0 ? void 0 : tree.kind) === java_1.J.Kind.Literal && (!propertyName || propertyName === 'value') ?
|
|
723
|
+
tree.valueSource : undefined;
|
|
724
|
+
const describeValue = (value, tree) => {
|
|
725
|
+
var _a;
|
|
726
|
+
return declared ? `declared in ${this.formatValue(value)}` :
|
|
727
|
+
(_a = asWritten(tree)) !== null && _a !== void 0 ? _a : this.formatValue(value);
|
|
728
|
+
};
|
|
729
|
+
const note = declared ? 'A pattern matches a call only where it calls the same declaration. ' +
|
|
730
|
+
'Its context option declares what its own calls refer to.' : undefined;
|
|
731
|
+
return this.fail(pattern, 'value-mismatch', describeValue(expected !== undefined ? expected : at(pattern, propertyName), pattern), describeValue(actual !== undefined ? actual : at(target, propertyName), target), note, at(pattern, holder), at(target, holder));
|
|
732
|
+
}
|
|
733
|
+
satisfiesConstraint(marker, patternElement, target, cursor) {
|
|
734
|
+
var _a, _b;
|
|
735
|
+
const capture = Array.isArray(target) ?
|
|
736
|
+
`variadic capture: ${marker.captureName} (${target.length} elements)` : `capture: ${marker.captureName}`;
|
|
737
|
+
this.debug.log('debug', 'constraint', `Evaluating constraint for ${capture}`);
|
|
738
|
+
let satisfied;
|
|
739
|
+
try {
|
|
740
|
+
satisfied = super.satisfiesConstraint(marker, patternElement, target, cursor);
|
|
741
|
+
}
|
|
742
|
+
catch (e) {
|
|
743
|
+
const on = Array.isArray(target) ? `${target.length} elements` : describeKind(target);
|
|
744
|
+
const message = (_a = e === null || e === void 0 ? void 0 : e.message) !== null && _a !== void 0 ? _a : e;
|
|
745
|
+
throw new Error(`The constraint of capture ${marker.captureName} threw on ${on}: ${message}`);
|
|
746
|
+
}
|
|
747
|
+
if (satisfied) {
|
|
748
|
+
this.debug.log('debug', 'constraint', `Constraint passed for ${capture}`);
|
|
749
|
+
}
|
|
750
|
+
else {
|
|
751
|
+
this.debug.log('info', 'constraint', `Constraint failed for ${capture}`);
|
|
752
|
+
if (this.match) {
|
|
753
|
+
const constraint = String(marker.constraint).split('\n')[0];
|
|
754
|
+
this.explain('constraint-failed', `${placeholder(marker)} satisfying ${constraint}`, describe(target), undefined, patternElement, Array.isArray(target) && target.length !== 1 ? (_b = this.targetCursor) === null || _b === void 0 ? void 0 : _b.value : [target].flat()[0]);
|
|
755
|
+
}
|
|
756
|
+
}
|
|
757
|
+
return satisfied;
|
|
758
|
+
}
|
|
759
|
+
explainCaptureConflict(marker, patternElement, targets) {
|
|
760
|
+
var _a;
|
|
761
|
+
const bound = marker.captureName ? this.matcher.getCapture(marker.captureName) : undefined;
|
|
762
|
+
if (bound === undefined || !this.match) {
|
|
763
|
+
return;
|
|
764
|
+
}
|
|
765
|
+
this.explain('value-mismatch', describe(bound).then(code => `${code} (what ${placeholder(marker)} holds already)`), describe(targets), 'A capture the pattern names twice binds the same code at both places.', patternElement, targets.length === 1 ? targets[0] : (_a = this.targetCursor) === null || _a === void 0 ? void 0 : _a.value);
|
|
766
|
+
}
|
|
767
|
+
}
|
|
768
|
+
exports.DebugPatternMatchingComparator = DebugPatternMatchingComparator;
|
|
769
|
+
//# sourceMappingURL=debug.js.map
|