@openrewrite/rewrite 8.103.0 → 8.104.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/dist/java/tree.d.ts +3 -0
- package/dist/java/tree.d.ts.map +1 -1
- package/dist/java/tree.js +3 -1
- package/dist/java/tree.js.map +1 -1
- package/dist/javascript/add-import.d.ts +28 -0
- package/dist/javascript/add-import.d.ts.map +1 -1
- package/dist/javascript/add-import.js +48 -25
- package/dist/javascript/add-import.js.map +1 -1
- package/dist/javascript/assertions.d.ts.map +1 -1
- package/dist/javascript/assertions.js +60 -67
- package/dist/javascript/assertions.js.map +1 -1
- package/dist/javascript/binding.d.ts +5 -2
- package/dist/javascript/binding.d.ts.map +1 -1
- package/dist/javascript/binding.js +58 -14
- package/dist/javascript/binding.js.map +1 -1
- package/dist/javascript/format/prettier-config-loader.d.ts +1 -0
- package/dist/javascript/format/prettier-config-loader.d.ts.map +1 -1
- package/dist/javascript/format/prettier-config-loader.js +23 -4
- package/dist/javascript/format/prettier-config-loader.js.map +1 -1
- package/dist/javascript/member-spellings.d.ts +88 -0
- package/dist/javascript/member-spellings.d.ts.map +1 -0
- package/dist/javascript/member-spellings.js +357 -0
- package/dist/javascript/member-spellings.js.map +1 -0
- package/dist/javascript/package-manager.d.ts +3 -0
- package/dist/javascript/package-manager.d.ts.map +1 -1
- package/dist/javascript/package-manager.js +49 -0
- package/dist/javascript/package-manager.js.map +1 -1
- package/dist/javascript/project-parser.d.ts.map +1 -1
- package/dist/javascript/project-parser.js +70 -109
- package/dist/javascript/project-parser.js.map +1 -1
- package/dist/javascript/scope.d.ts +2 -0
- package/dist/javascript/scope.d.ts.map +1 -1
- package/dist/javascript/scope.js +2 -1
- package/dist/javascript/scope.js.map +1 -1
- package/dist/json/index.d.ts +1 -0
- package/dist/json/index.d.ts.map +1 -1
- package/dist/json/index.js +1 -0
- package/dist/json/index.js.map +1 -1
- package/dist/json/path-matcher.d.ts +29 -0
- package/dist/json/path-matcher.d.ts.map +1 -0
- package/dist/json/path-matcher.js +974 -0
- package/dist/json/path-matcher.js.map +1 -0
- package/dist/rewrite-javascript-version.txt +1 -1
- package/dist/rpc/request/parse-project.d.ts.map +1 -1
- package/dist/rpc/request/parse-project.js +20 -23
- package/dist/rpc/request/parse-project.js.map +1 -1
- package/package.json +1 -1
- package/src/java/tree.ts +2 -0
- package/src/javascript/add-import.ts +58 -30
- package/src/javascript/assertions.ts +43 -57
- package/src/javascript/binding.ts +72 -17
- package/src/javascript/format/prettier-config-loader.ts +22 -3
- package/src/javascript/member-spellings.ts +374 -0
- package/src/javascript/package-manager.ts +51 -0
- package/src/javascript/project-parser.ts +17 -50
- package/src/javascript/scope.ts +2 -2
- package/src/json/index.ts +1 -0
- package/src/json/path-matcher.ts +1092 -0
- package/src/rpc/request/parse-project.ts +22 -32
|
@@ -0,0 +1,974 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.JsonPathMatcher = void 0;
|
|
4
|
+
/*
|
|
5
|
+
* Copyright 2026 the original author or authors.
|
|
6
|
+
* <p>
|
|
7
|
+
* Licensed under the Moderne Source Available License (the "License");
|
|
8
|
+
* you may not use this file except in compliance with the License.
|
|
9
|
+
* You may obtain a copy of the License at
|
|
10
|
+
* <p>
|
|
11
|
+
* https://docs.moderne.io/licensing/moderne-source-available-license
|
|
12
|
+
* <p>
|
|
13
|
+
* Unless required by applicable law or agreed to in writing, software
|
|
14
|
+
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
15
|
+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
16
|
+
* See the License for the specific language governing permissions and
|
|
17
|
+
* limitations under the License.
|
|
18
|
+
*/
|
|
19
|
+
const tree_1 = require("../tree");
|
|
20
|
+
const tree_2 = require("./tree");
|
|
21
|
+
/**
|
|
22
|
+
* Matches a cursor location against a JsonPath expression, such as `$.sap.ui5.dependencies.libs.*`.
|
|
23
|
+
*
|
|
24
|
+
* A port of `org.openrewrite.json.JsonPathMatcher`, supporting the same subset of JsonPath with the same results:
|
|
25
|
+
* `$`, `.key`, `['key']`, `*`, `..`, array indexes and slices, and `?()` filters using `==`, `!=`, `=~`,
|
|
26
|
+
* `contains`, `!`, `&&` and `||`.
|
|
27
|
+
*/
|
|
28
|
+
class JsonPathMatcher {
|
|
29
|
+
constructor(jsonPath) {
|
|
30
|
+
this.jsonPath = jsonPath.endsWith(".") && jsonPath.length > 1 ?
|
|
31
|
+
jsonPath.substring(0, jsonPath.length - 1) :
|
|
32
|
+
jsonPath;
|
|
33
|
+
}
|
|
34
|
+
/**
|
|
35
|
+
* What the expression selects, evaluated along the path from the document root to the cursor: a
|
|
36
|
+
* `Json` element, an array of them, or `undefined` when nothing matches.
|
|
37
|
+
*
|
|
38
|
+
* @throws Error when the expression is not valid JsonPath
|
|
39
|
+
*/
|
|
40
|
+
find(cursor) {
|
|
41
|
+
return this.find0(cursor, resolvedAncestors(cursor));
|
|
42
|
+
}
|
|
43
|
+
/**
|
|
44
|
+
* Whether the element at the cursor is one that the expression selects.
|
|
45
|
+
*
|
|
46
|
+
* @throws Error when the expression is not valid JsonPath
|
|
47
|
+
*/
|
|
48
|
+
matches(cursor) {
|
|
49
|
+
const result = this.find0(cursor, resolvedAncestors(cursor));
|
|
50
|
+
if (result === undefined) {
|
|
51
|
+
return false;
|
|
52
|
+
}
|
|
53
|
+
return Array.isArray(result) ?
|
|
54
|
+
result.some(r => javaEquals(r, cursor.value)) :
|
|
55
|
+
javaEquals(result, cursor.value);
|
|
56
|
+
}
|
|
57
|
+
find0(cursor, cursorPath) {
|
|
58
|
+
var _a;
|
|
59
|
+
if (cursorPath.length === 0) {
|
|
60
|
+
return undefined;
|
|
61
|
+
}
|
|
62
|
+
const start = this.jsonPath.startsWith(".") && !this.jsonPath.startsWith("..") ?
|
|
63
|
+
cursor.value :
|
|
64
|
+
cursorPath[0];
|
|
65
|
+
const parsed = this.parse();
|
|
66
|
+
return (_a = new Evaluator(cursorPath, start, false).visit(parsed)) !== null && _a !== void 0 ? _a : undefined;
|
|
67
|
+
}
|
|
68
|
+
parse() {
|
|
69
|
+
if (!this.parsed) {
|
|
70
|
+
this.parsed = new JsonPathParser(this.jsonPath).parse();
|
|
71
|
+
}
|
|
72
|
+
return this.parsed;
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
exports.JsonPathMatcher = JsonPathMatcher;
|
|
76
|
+
function resolvedAncestors(cursor) {
|
|
77
|
+
const path = [];
|
|
78
|
+
for (let c = cursor; c; c = c.parent) {
|
|
79
|
+
if ((0, tree_1.isTree)(c.value)) {
|
|
80
|
+
path.unshift(c.value);
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
return path;
|
|
84
|
+
}
|
|
85
|
+
function javaEquals(a, b) {
|
|
86
|
+
if (a === b) {
|
|
87
|
+
return true;
|
|
88
|
+
}
|
|
89
|
+
if (Array.isArray(a) && Array.isArray(b)) {
|
|
90
|
+
return a.length === b.length && a.every((e, i) => javaEquals(e, b[i]));
|
|
91
|
+
}
|
|
92
|
+
return (0, tree_1.isTree)(a) && (0, tree_1.isTree)(b) && a.kind === b.kind && a.id === b.id;
|
|
93
|
+
}
|
|
94
|
+
const DECIMAL = /^([+-]?)(\d*)(?:\.(\d*))?(?:[eE]([+-]?\d+))?$/;
|
|
95
|
+
class JsonPathNumber {
|
|
96
|
+
constructor(text) {
|
|
97
|
+
this.text = text;
|
|
98
|
+
const [, sign, int, fraction = "", exponent = "0"] = DECIMAL.exec(text);
|
|
99
|
+
const digits = (int + fraction).replace(/^0+/, "");
|
|
100
|
+
const significant = digits.replace(/0+$/, "");
|
|
101
|
+
this.canonical = significant.length === 0 ? "0" :
|
|
102
|
+
`${sign === "-" ? "-" : ""}${significant}e${Number(exponent) - fraction.length + digits.length - significant.length}`;
|
|
103
|
+
}
|
|
104
|
+
equals(other) {
|
|
105
|
+
return this.canonical === other.canonical;
|
|
106
|
+
}
|
|
107
|
+
toString() {
|
|
108
|
+
return this.text;
|
|
109
|
+
}
|
|
110
|
+
}
|
|
111
|
+
function literalValue(literal) {
|
|
112
|
+
var _a;
|
|
113
|
+
if (typeof literal.value === "number" && Number.isFinite(literal.value) &&
|
|
114
|
+
/\d/.test(literal.source) && DECIMAL.test(literal.source)) {
|
|
115
|
+
return new JsonPathNumber(literal.source);
|
|
116
|
+
}
|
|
117
|
+
return (_a = literal.value) !== null && _a !== void 0 ? _a : null;
|
|
118
|
+
}
|
|
119
|
+
function is(o, kind) {
|
|
120
|
+
return o != null && typeof o === "object" && !Array.isArray(o) && o.kind === tree_2.Json.Kind[kind];
|
|
121
|
+
}
|
|
122
|
+
function membersOf(object) {
|
|
123
|
+
return object.members.map(m => m.element);
|
|
124
|
+
}
|
|
125
|
+
function valuesOf(array) {
|
|
126
|
+
return array.values.map(v => v.element);
|
|
127
|
+
}
|
|
128
|
+
function keyOf(member) {
|
|
129
|
+
const key = (0, tree_2.getMemberKeyName)(member);
|
|
130
|
+
return key === undefined ? undefined : escapeUnpairedSurrogates(key);
|
|
131
|
+
}
|
|
132
|
+
function flatten(results) {
|
|
133
|
+
return results.flatMap(r => Array.isArray(r) ? r : [r]);
|
|
134
|
+
}
|
|
135
|
+
function getResultFromList(results) {
|
|
136
|
+
if (Array.isArray(results)) {
|
|
137
|
+
if (results.length === 0) {
|
|
138
|
+
return undefined;
|
|
139
|
+
}
|
|
140
|
+
else if (results.length === 1) {
|
|
141
|
+
return results[0];
|
|
142
|
+
}
|
|
143
|
+
}
|
|
144
|
+
return results;
|
|
145
|
+
}
|
|
146
|
+
function collectDescendants(node, descendants) {
|
|
147
|
+
if (Array.isArray(node)) {
|
|
148
|
+
for (const o of node) {
|
|
149
|
+
collectDescendants(o, descendants);
|
|
150
|
+
}
|
|
151
|
+
return;
|
|
152
|
+
}
|
|
153
|
+
for (const child of childrenOf(node)) {
|
|
154
|
+
descendants.push(child);
|
|
155
|
+
collectDescendants(child, descendants);
|
|
156
|
+
}
|
|
157
|
+
}
|
|
158
|
+
function childrenOf(node) {
|
|
159
|
+
if (is(node, "Member")) {
|
|
160
|
+
return childrenOf(node.value);
|
|
161
|
+
}
|
|
162
|
+
else if (is(node, "Object")) {
|
|
163
|
+
return membersOf(node);
|
|
164
|
+
}
|
|
165
|
+
else if (is(node, "Array")) {
|
|
166
|
+
return valuesOf(node);
|
|
167
|
+
}
|
|
168
|
+
return [];
|
|
169
|
+
}
|
|
170
|
+
function notEmpty(result) {
|
|
171
|
+
return result != null && (!Array.isArray(result) || result.length > 0);
|
|
172
|
+
}
|
|
173
|
+
class Evaluator {
|
|
174
|
+
constructor(cursorPath, scope, isRecursiveDescent) {
|
|
175
|
+
this.cursorPath = cursorPath;
|
|
176
|
+
this.scope = scope;
|
|
177
|
+
this.isRecursiveDescent = isRecursiveDescent;
|
|
178
|
+
}
|
|
179
|
+
visit(node) {
|
|
180
|
+
switch (node.kind) {
|
|
181
|
+
case "jsonPath":
|
|
182
|
+
return this.visitJsonPath(node);
|
|
183
|
+
case "expression":
|
|
184
|
+
case "dotOperator":
|
|
185
|
+
case "filterExpression":
|
|
186
|
+
return this.visitChildren([node.child]);
|
|
187
|
+
case "filter":
|
|
188
|
+
return this.visitChildren(node.expressions);
|
|
189
|
+
case "recursiveDescent":
|
|
190
|
+
return this.visitRecursiveDescent(node);
|
|
191
|
+
case "bracket":
|
|
192
|
+
return this.visitBracketOperator(node);
|
|
193
|
+
case "property":
|
|
194
|
+
return this.visitProperty(node);
|
|
195
|
+
case "wildcard":
|
|
196
|
+
return this.visitWildcard(node);
|
|
197
|
+
case "slice":
|
|
198
|
+
return this.visitSlice(node);
|
|
199
|
+
case "binary":
|
|
200
|
+
return this.visitBinaryExpression(node);
|
|
201
|
+
case "regex":
|
|
202
|
+
return this.visitRegexExpression(node);
|
|
203
|
+
case "contains":
|
|
204
|
+
return this.visitContainsExpression(node);
|
|
205
|
+
case "negation":
|
|
206
|
+
return this.visitNegationExpression(node);
|
|
207
|
+
case "unary":
|
|
208
|
+
return this.visitUnaryExpression(node);
|
|
209
|
+
case "literal":
|
|
210
|
+
return node.value;
|
|
211
|
+
}
|
|
212
|
+
}
|
|
213
|
+
visitChildren(children) {
|
|
214
|
+
let result = this.scope;
|
|
215
|
+
for (const child of children) {
|
|
216
|
+
if (this.scope == null) {
|
|
217
|
+
break;
|
|
218
|
+
}
|
|
219
|
+
result = this.scope = this.visit(child);
|
|
220
|
+
}
|
|
221
|
+
return result;
|
|
222
|
+
}
|
|
223
|
+
visitJsonPath(node) {
|
|
224
|
+
var _a, _b;
|
|
225
|
+
if (node.root || node.startsWithBracket) {
|
|
226
|
+
this.scope = (_a = this.cursorPath.find(t => is(t, "Object"))) !== null && _a !== void 0 ? _a : (_b = this.cursorPath.find(t => is(t, "Document") && is(t.value, "Object"))) === null || _b === void 0 ? void 0 : _b.value;
|
|
227
|
+
}
|
|
228
|
+
return this.visitChildren(node.expressions);
|
|
229
|
+
}
|
|
230
|
+
visitRecursiveDescent(node) {
|
|
231
|
+
if (this.scope == null) {
|
|
232
|
+
return undefined;
|
|
233
|
+
}
|
|
234
|
+
const wildcard = node.child.child.kind === "wildcard";
|
|
235
|
+
if (node.first) {
|
|
236
|
+
const results = [];
|
|
237
|
+
for (const path of this.cursorPath) {
|
|
238
|
+
const result = new Evaluator(this.cursorPath, path, false).visit(node.child);
|
|
239
|
+
if (wildcard && Array.isArray(result)) {
|
|
240
|
+
results.push(...result);
|
|
241
|
+
}
|
|
242
|
+
else if (result != null) {
|
|
243
|
+
results.push(result);
|
|
244
|
+
}
|
|
245
|
+
}
|
|
246
|
+
return results;
|
|
247
|
+
}
|
|
248
|
+
else if (wildcard) {
|
|
249
|
+
const descendants = [];
|
|
250
|
+
collectDescendants(this.scope, descendants);
|
|
251
|
+
return getResultFromList(descendants);
|
|
252
|
+
}
|
|
253
|
+
return new Evaluator(this.cursorPath, this.scope, true).visit(node.child);
|
|
254
|
+
}
|
|
255
|
+
visitBracketOperator(node) {
|
|
256
|
+
if (node.properties.length > 0) {
|
|
257
|
+
if (node.properties.length === 1) {
|
|
258
|
+
return this.visitProperty(node.properties[0]);
|
|
259
|
+
}
|
|
260
|
+
return node.properties.map(p => this.visitProperty(p));
|
|
261
|
+
}
|
|
262
|
+
else if (node.slice) {
|
|
263
|
+
return this.visitSlice(node.slice);
|
|
264
|
+
}
|
|
265
|
+
else if (node.indexes) {
|
|
266
|
+
return this.visitIndexes(node.indexes);
|
|
267
|
+
}
|
|
268
|
+
else if (node.filter) {
|
|
269
|
+
return this.visit(node.filter);
|
|
270
|
+
}
|
|
271
|
+
return undefined;
|
|
272
|
+
}
|
|
273
|
+
elementsInScope() {
|
|
274
|
+
if (Array.isArray(this.scope)) {
|
|
275
|
+
return this.scope;
|
|
276
|
+
}
|
|
277
|
+
else if (is(this.scope, "Array")) {
|
|
278
|
+
return valuesOf(this.scope);
|
|
279
|
+
}
|
|
280
|
+
else if (is(this.scope, "Member")) {
|
|
281
|
+
this.scope = this.scope.value;
|
|
282
|
+
return this.elementsInScope();
|
|
283
|
+
}
|
|
284
|
+
return [];
|
|
285
|
+
}
|
|
286
|
+
visitSlice(node) {
|
|
287
|
+
const results = this.elementsInScope();
|
|
288
|
+
let start = 0;
|
|
289
|
+
let limit = Infinity;
|
|
290
|
+
if (node.first !== undefined) {
|
|
291
|
+
limit = node.first;
|
|
292
|
+
}
|
|
293
|
+
else if (node.last !== undefined) {
|
|
294
|
+
start = Math.max(0, results.length - node.last);
|
|
295
|
+
}
|
|
296
|
+
else if (node.start !== undefined) {
|
|
297
|
+
start = node.start;
|
|
298
|
+
limit = node.end !== undefined ? node.end + 1 : limit;
|
|
299
|
+
}
|
|
300
|
+
return results.slice(start, start + limit);
|
|
301
|
+
}
|
|
302
|
+
visitIndexes(indexes) {
|
|
303
|
+
const results = this.elementsInScope();
|
|
304
|
+
const selected = [];
|
|
305
|
+
for (const index of indexes) {
|
|
306
|
+
for (let i = 0; i < results.length; i++) {
|
|
307
|
+
if (index === String(i)) {
|
|
308
|
+
selected.push(results[i]);
|
|
309
|
+
}
|
|
310
|
+
}
|
|
311
|
+
}
|
|
312
|
+
return getResultFromList(selected);
|
|
313
|
+
}
|
|
314
|
+
mapScope(elements, visit) {
|
|
315
|
+
return elements
|
|
316
|
+
.map(o => {
|
|
317
|
+
this.scope = o;
|
|
318
|
+
return visit();
|
|
319
|
+
})
|
|
320
|
+
.filter(r => r != null);
|
|
321
|
+
}
|
|
322
|
+
visitProperty(node) {
|
|
323
|
+
const scope = this.scope;
|
|
324
|
+
if (is(scope, "Member")) {
|
|
325
|
+
const member = scope;
|
|
326
|
+
const key = keyOf(member);
|
|
327
|
+
if (this.isRecursiveDescent) {
|
|
328
|
+
const matches = [];
|
|
329
|
+
if (key === node.name) {
|
|
330
|
+
matches.push(member);
|
|
331
|
+
}
|
|
332
|
+
this.scope = member.value;
|
|
333
|
+
const result = getResultFromList(this.visitProperty(node));
|
|
334
|
+
if (result != null) {
|
|
335
|
+
matches.push(result);
|
|
336
|
+
}
|
|
337
|
+
return getResultFromList(matches);
|
|
338
|
+
}
|
|
339
|
+
else if (is(member.value, "Literal")) {
|
|
340
|
+
return key === node.name ? member : undefined;
|
|
341
|
+
}
|
|
342
|
+
this.scope = member.value;
|
|
343
|
+
return this.visitProperty(node);
|
|
344
|
+
}
|
|
345
|
+
else if (is(scope, "Object")) {
|
|
346
|
+
const object = scope;
|
|
347
|
+
if (this.isRecursiveDescent) {
|
|
348
|
+
this.scope = membersOf(object);
|
|
349
|
+
return getResultFromList(this.visitProperty(node));
|
|
350
|
+
}
|
|
351
|
+
for (const json of membersOf(object)) {
|
|
352
|
+
if (is(json, "Member") && keyOf(json) === node.name) {
|
|
353
|
+
return json;
|
|
354
|
+
}
|
|
355
|
+
}
|
|
356
|
+
}
|
|
357
|
+
else if (is(scope, "Array")) {
|
|
358
|
+
return getResultFromList(this.mapScope(valuesOf(scope), () => this.visitProperty(node)));
|
|
359
|
+
}
|
|
360
|
+
else if (Array.isArray(scope)) {
|
|
361
|
+
return getResultFromList(flatten(this.mapScope(scope, () => this.visitProperty(node))));
|
|
362
|
+
}
|
|
363
|
+
return undefined;
|
|
364
|
+
}
|
|
365
|
+
visitWildcard(node) {
|
|
366
|
+
const scope = this.scope;
|
|
367
|
+
if (is(scope, "Member")) {
|
|
368
|
+
const value = scope.value;
|
|
369
|
+
if (is(value, "Object")) {
|
|
370
|
+
return getResultFromList(membersOf(value));
|
|
371
|
+
}
|
|
372
|
+
else if (is(value, "Array")) {
|
|
373
|
+
return getResultFromList(valuesOf(value));
|
|
374
|
+
}
|
|
375
|
+
return undefined;
|
|
376
|
+
}
|
|
377
|
+
else if (Array.isArray(scope)) {
|
|
378
|
+
return getResultFromList(flatten(this.mapScope(scope, () => this.visitWildcard(node))));
|
|
379
|
+
}
|
|
380
|
+
else if (is(scope, "Object")) {
|
|
381
|
+
return membersOf(scope);
|
|
382
|
+
}
|
|
383
|
+
else if (is(scope, "Array")) {
|
|
384
|
+
return this.mapScope(valuesOf(scope), () => this.visitWildcard(node));
|
|
385
|
+
}
|
|
386
|
+
return undefined;
|
|
387
|
+
}
|
|
388
|
+
visitUnaryExpression(node) {
|
|
389
|
+
if (node.at) {
|
|
390
|
+
const scope = this.scope;
|
|
391
|
+
if (is(scope, "Literal")) {
|
|
392
|
+
if (node.name === undefined) {
|
|
393
|
+
return scope;
|
|
394
|
+
}
|
|
395
|
+
}
|
|
396
|
+
else if (is(scope, "Member")) {
|
|
397
|
+
const member = scope;
|
|
398
|
+
if (node.name !== undefined && keyOf(member) === node.name) {
|
|
399
|
+
return member;
|
|
400
|
+
}
|
|
401
|
+
this.scope = member.value;
|
|
402
|
+
return getResultFromList(this.visitUnaryExpression(node));
|
|
403
|
+
}
|
|
404
|
+
else if (is(scope, "Object")) {
|
|
405
|
+
for (const json of membersOf(scope)) {
|
|
406
|
+
if (is(json, "Member") && node.name !== undefined && keyOf(json) === node.name) {
|
|
407
|
+
return scope;
|
|
408
|
+
}
|
|
409
|
+
}
|
|
410
|
+
}
|
|
411
|
+
else if (is(scope, "Array")) {
|
|
412
|
+
this.scope = valuesOf(scope);
|
|
413
|
+
return this.visitUnaryExpression(node);
|
|
414
|
+
}
|
|
415
|
+
else if (Array.isArray(scope)) {
|
|
416
|
+
return getResultFromList(flatten(this.mapScope(scope, () => this.visitUnaryExpression(node))));
|
|
417
|
+
}
|
|
418
|
+
}
|
|
419
|
+
else if (node.jsonPath) {
|
|
420
|
+
return this.getResultByKey(this.visit(node.jsonPath), node.lastToken);
|
|
421
|
+
}
|
|
422
|
+
return undefined;
|
|
423
|
+
}
|
|
424
|
+
visitRegexExpression(node) {
|
|
425
|
+
if (this.scope == null || Array.isArray(this.scope) && this.scope.length === 0) {
|
|
426
|
+
return undefined;
|
|
427
|
+
}
|
|
428
|
+
const lhs = this.visitUnaryExpression(node.unary);
|
|
429
|
+
if (Array.isArray(lhs)) {
|
|
430
|
+
return lhs.filter(match => this.getOperatorResult(match, "=~", node.pattern) != null);
|
|
431
|
+
}
|
|
432
|
+
return this.getOperatorResult(lhs, "=~", node.pattern);
|
|
433
|
+
}
|
|
434
|
+
visitContainsExpression(node) {
|
|
435
|
+
const originalScope = this.scope;
|
|
436
|
+
if (node.unaryFirst) {
|
|
437
|
+
let lhs = this.visitUnaryExpression(node.unary);
|
|
438
|
+
const rhs = node.literal.value;
|
|
439
|
+
if (is(lhs, "Object") && rhs != null) {
|
|
440
|
+
lhs = this.getResultByKey(lhs, node.unary.thirdToken);
|
|
441
|
+
if (is(lhs, "Member")) {
|
|
442
|
+
const value = lhs.value;
|
|
443
|
+
if (is(value, "Array")) {
|
|
444
|
+
if (valuesOf(value).some(o => is(o, "Literal") &&
|
|
445
|
+
String(literalValue(o)).includes(String(rhs)))) {
|
|
446
|
+
return originalScope;
|
|
447
|
+
}
|
|
448
|
+
}
|
|
449
|
+
else if (is(value, "Literal") && String(literalValue(value)).includes(String(rhs))) {
|
|
450
|
+
return originalScope;
|
|
451
|
+
}
|
|
452
|
+
}
|
|
453
|
+
}
|
|
454
|
+
}
|
|
455
|
+
else {
|
|
456
|
+
const lhs = node.literal.value;
|
|
457
|
+
let rhs = this.visitUnaryExpression(node.unary);
|
|
458
|
+
if (is(rhs, "Object") && lhs != null) {
|
|
459
|
+
rhs = this.getResultByKey(rhs, node.unary.thirdToken);
|
|
460
|
+
if (is(rhs, "Member")) {
|
|
461
|
+
const value = rhs.value;
|
|
462
|
+
if (is(value, "Literal") && String(literalValue(value)).includes(String(lhs))) {
|
|
463
|
+
return originalScope;
|
|
464
|
+
}
|
|
465
|
+
}
|
|
466
|
+
}
|
|
467
|
+
}
|
|
468
|
+
return undefined;
|
|
469
|
+
}
|
|
470
|
+
visitNegationExpression(node) {
|
|
471
|
+
const originalScope = this.scope;
|
|
472
|
+
const inner = () => node.filterExpression ?
|
|
473
|
+
this.visit(node.filterExpression) :
|
|
474
|
+
this.visitUnaryExpression(node.unary);
|
|
475
|
+
let elements;
|
|
476
|
+
if (Array.isArray(originalScope)) {
|
|
477
|
+
elements = originalScope;
|
|
478
|
+
}
|
|
479
|
+
else if (is(originalScope, "Array")) {
|
|
480
|
+
elements = valuesOf(originalScope);
|
|
481
|
+
}
|
|
482
|
+
else if (is(originalScope, "Member") && is(originalScope.value, "Array")) {
|
|
483
|
+
elements = valuesOf(originalScope.value);
|
|
484
|
+
}
|
|
485
|
+
if (elements) {
|
|
486
|
+
const results = [];
|
|
487
|
+
for (const element of elements) {
|
|
488
|
+
this.scope = element;
|
|
489
|
+
if (!notEmpty(inner())) {
|
|
490
|
+
results.push(element);
|
|
491
|
+
}
|
|
492
|
+
}
|
|
493
|
+
this.scope = originalScope;
|
|
494
|
+
return getResultFromList(results);
|
|
495
|
+
}
|
|
496
|
+
const matched = notEmpty(inner());
|
|
497
|
+
this.scope = originalScope;
|
|
498
|
+
return matched ? undefined : originalScope;
|
|
499
|
+
}
|
|
500
|
+
visitBinaryExpression(node) {
|
|
501
|
+
if (node.operator === "&&" || node.operator === "||") {
|
|
502
|
+
const scopeOfLogicalOp = this.scope;
|
|
503
|
+
const lhs = this.visit(node.left);
|
|
504
|
+
this.scope = scopeOfLogicalOp;
|
|
505
|
+
const rhs = this.visit(node.right);
|
|
506
|
+
if (node.operator === "&&" && notEmpty(lhs) && notEmpty(rhs)) {
|
|
507
|
+
if (!Array.isArray(lhs) && (0, tree_1.isTree)(lhs)) {
|
|
508
|
+
return rhs;
|
|
509
|
+
}
|
|
510
|
+
else if (!Array.isArray(rhs) && (0, tree_1.isTree)(rhs)) {
|
|
511
|
+
return lhs;
|
|
512
|
+
}
|
|
513
|
+
if (Array.isArray(lhs) && Array.isArray(rhs) && lhs.length !== rhs.length) {
|
|
514
|
+
return lhs.length < rhs.length ? lhs : rhs;
|
|
515
|
+
}
|
|
516
|
+
return scopeOfLogicalOp;
|
|
517
|
+
}
|
|
518
|
+
else if (node.operator === "||" && (notEmpty(lhs) || notEmpty(rhs))) {
|
|
519
|
+
return scopeOfLogicalOp;
|
|
520
|
+
}
|
|
521
|
+
return undefined;
|
|
522
|
+
}
|
|
523
|
+
const originalScope = this.scope;
|
|
524
|
+
const lhs = this.visit(node.left);
|
|
525
|
+
const rhs = this.visit(node.right);
|
|
526
|
+
if (Array.isArray(lhs)) {
|
|
527
|
+
return lhs.filter(match => this.getOperatorResult(match, node.operator, rhs) != null);
|
|
528
|
+
}
|
|
529
|
+
else if (is(originalScope, "Member")) {
|
|
530
|
+
if (this.getOperatorResult(lhs, node.operator, rhs) != null) {
|
|
531
|
+
return originalScope;
|
|
532
|
+
}
|
|
533
|
+
return undefined;
|
|
534
|
+
}
|
|
535
|
+
return this.getOperatorResult(lhs, node.operator, rhs);
|
|
536
|
+
}
|
|
537
|
+
getOperatorResult(lhs, operator, rhs) {
|
|
538
|
+
if (is(lhs, "Member")) {
|
|
539
|
+
const value = lhs.value;
|
|
540
|
+
if (is(value, "Literal") && this.checkObjectEquality(literalValue(value), operator, rhs)) {
|
|
541
|
+
return lhs;
|
|
542
|
+
}
|
|
543
|
+
}
|
|
544
|
+
else if (is(lhs, "Object")) {
|
|
545
|
+
for (const json of membersOf(lhs)) {
|
|
546
|
+
if (is(json, "Member")) {
|
|
547
|
+
const value = json.value;
|
|
548
|
+
if (is(value, "Literal") && this.checkObjectEquality(literalValue(value), operator, rhs)) {
|
|
549
|
+
return lhs;
|
|
550
|
+
}
|
|
551
|
+
}
|
|
552
|
+
}
|
|
553
|
+
}
|
|
554
|
+
else if (is(lhs, "Literal")) {
|
|
555
|
+
if (this.checkObjectEquality(literalValue(lhs), operator, rhs)) {
|
|
556
|
+
return lhs;
|
|
557
|
+
}
|
|
558
|
+
}
|
|
559
|
+
return undefined;
|
|
560
|
+
}
|
|
561
|
+
checkObjectEquality(lhs, operator, rhs) {
|
|
562
|
+
switch (operator) {
|
|
563
|
+
case "==":
|
|
564
|
+
return valueEquals(lhs, rhs);
|
|
565
|
+
case "!=":
|
|
566
|
+
return !valueEquals(lhs, rhs);
|
|
567
|
+
case "=~":
|
|
568
|
+
return lhs != null && rhs.test(String(lhs));
|
|
569
|
+
}
|
|
570
|
+
return false;
|
|
571
|
+
}
|
|
572
|
+
getResultByKey(result, key) {
|
|
573
|
+
if (is(result, "Object")) {
|
|
574
|
+
for (const json of membersOf(result)) {
|
|
575
|
+
if (is(json, "Member") && is(json.key.element, "Literal") &&
|
|
576
|
+
keyOf(json) === key) {
|
|
577
|
+
return json;
|
|
578
|
+
}
|
|
579
|
+
}
|
|
580
|
+
}
|
|
581
|
+
else if (is(result, "Member")) {
|
|
582
|
+
const member = result;
|
|
583
|
+
if (is(member.value, "Literal")) {
|
|
584
|
+
return keyOf(member) === key ? member : undefined;
|
|
585
|
+
}
|
|
586
|
+
}
|
|
587
|
+
else if (Array.isArray(result)) {
|
|
588
|
+
for (const o of result) {
|
|
589
|
+
const r = this.getResultByKey(o, key);
|
|
590
|
+
if (r != null) {
|
|
591
|
+
return r;
|
|
592
|
+
}
|
|
593
|
+
}
|
|
594
|
+
}
|
|
595
|
+
return undefined;
|
|
596
|
+
}
|
|
597
|
+
}
|
|
598
|
+
function valueEquals(lhs, rhs) {
|
|
599
|
+
if (lhs instanceof JsonPathNumber && rhs instanceof JsonPathNumber) {
|
|
600
|
+
return lhs.equals(rhs);
|
|
601
|
+
}
|
|
602
|
+
return lhs === rhs;
|
|
603
|
+
}
|
|
604
|
+
const KEYWORDS = new Map([["contains", "contains"], ["true", "true"], ["false", "false"], ["null", "null"]]);
|
|
605
|
+
function tokenize(input) {
|
|
606
|
+
var _a;
|
|
607
|
+
const tokens = [];
|
|
608
|
+
const error = (offset) => new Error(`Syntax error at offset ${offset} in JsonPath '${input}'`);
|
|
609
|
+
let i = 0;
|
|
610
|
+
while (i < input.length) {
|
|
611
|
+
const c = input[i];
|
|
612
|
+
const start = i;
|
|
613
|
+
if (/[ \t\n\r\f,]/.test(c)) {
|
|
614
|
+
i++;
|
|
615
|
+
continue;
|
|
616
|
+
}
|
|
617
|
+
const rest = input.substring(i);
|
|
618
|
+
let match;
|
|
619
|
+
if ((match = /^=~[ \t]?'/.exec(rest))) {
|
|
620
|
+
const close = input.indexOf("'", i + match[0].length);
|
|
621
|
+
if (close < 0) {
|
|
622
|
+
throw error(i);
|
|
623
|
+
}
|
|
624
|
+
tokens.push({ type: "regex", text: input.substring(i + match[0].length, close).replace(/^[ \t]+/, ""), offset: start });
|
|
625
|
+
i = close + 1;
|
|
626
|
+
}
|
|
627
|
+
else if (rest.startsWith("&&") || rest.startsWith("||")) {
|
|
628
|
+
tokens.push({ type: "logical", text: rest.substring(0, 2), offset: start });
|
|
629
|
+
i += 2;
|
|
630
|
+
}
|
|
631
|
+
else if (rest.startsWith("==") || rest.startsWith("!=")) {
|
|
632
|
+
tokens.push({ type: "equality", text: rest.substring(0, 2), offset: start });
|
|
633
|
+
i += 2;
|
|
634
|
+
}
|
|
635
|
+
else if (rest.startsWith("..")) {
|
|
636
|
+
tokens.push({ type: "..", text: "..", offset: start });
|
|
637
|
+
i += 2;
|
|
638
|
+
}
|
|
639
|
+
else if ("$.*[]()@?:!".includes(c)) {
|
|
640
|
+
tokens.push({ type: c, text: c, offset: start });
|
|
641
|
+
i++;
|
|
642
|
+
}
|
|
643
|
+
else if (c === "'" || c === '"') {
|
|
644
|
+
let j = i + 1;
|
|
645
|
+
while (j < input.length && input[j] !== c) {
|
|
646
|
+
j += input[j] === "\\" ? 2 : 1;
|
|
647
|
+
}
|
|
648
|
+
if (j >= input.length) {
|
|
649
|
+
throw error(i);
|
|
650
|
+
}
|
|
651
|
+
tokens.push({ type: "string", text: input.substring(i, j + 1), offset: start });
|
|
652
|
+
i = j + 1;
|
|
653
|
+
}
|
|
654
|
+
else if ((match = /^(-?)\d+([eE][+-]?\d+)?/.exec(rest))) {
|
|
655
|
+
const type = match[2] ? "numeric" : match[1] ? "negative" : "positive";
|
|
656
|
+
tokens.push({ type, text: match[0], offset: start });
|
|
657
|
+
i += match[0].length;
|
|
658
|
+
}
|
|
659
|
+
else if ((match = /^[_A-Za-z][_A-Za-z0-9-]*/.exec(rest))) {
|
|
660
|
+
tokens.push({ type: (_a = KEYWORDS.get(match[0])) !== null && _a !== void 0 ? _a : "identifier", text: match[0], offset: start });
|
|
661
|
+
i += match[0].length;
|
|
662
|
+
}
|
|
663
|
+
else {
|
|
664
|
+
throw error(i);
|
|
665
|
+
}
|
|
666
|
+
}
|
|
667
|
+
tokens.push({ type: "eof", text: "", offset: input.length });
|
|
668
|
+
return tokens;
|
|
669
|
+
}
|
|
670
|
+
class JsonPathParser {
|
|
671
|
+
constructor(input) {
|
|
672
|
+
this.input = input;
|
|
673
|
+
this.pos = 0;
|
|
674
|
+
this.tokens = tokenize(input);
|
|
675
|
+
}
|
|
676
|
+
parse() {
|
|
677
|
+
const jsonPath = this.jsonPath();
|
|
678
|
+
if (this.peek().type !== "eof") {
|
|
679
|
+
throw this.error();
|
|
680
|
+
}
|
|
681
|
+
return jsonPath;
|
|
682
|
+
}
|
|
683
|
+
peek(ahead = 0) {
|
|
684
|
+
return this.tokens[Math.min(this.pos + ahead, this.tokens.length - 1)];
|
|
685
|
+
}
|
|
686
|
+
next() {
|
|
687
|
+
return this.tokens[this.pos++];
|
|
688
|
+
}
|
|
689
|
+
expect(type) {
|
|
690
|
+
if (this.peek().type !== type) {
|
|
691
|
+
throw this.error();
|
|
692
|
+
}
|
|
693
|
+
return this.next();
|
|
694
|
+
}
|
|
695
|
+
error() {
|
|
696
|
+
const token = this.peek();
|
|
697
|
+
return new Error(token.type === "eof" ?
|
|
698
|
+
`Syntax error: unexpected end of JsonPath '${this.input}'` :
|
|
699
|
+
`Syntax error: unexpected '${token.text}' at offset ${token.offset} in JsonPath '${this.input}'`);
|
|
700
|
+
}
|
|
701
|
+
jsonPath() {
|
|
702
|
+
const root = this.peek().type === "$";
|
|
703
|
+
if (root) {
|
|
704
|
+
this.next();
|
|
705
|
+
}
|
|
706
|
+
const node = {
|
|
707
|
+
kind: "jsonPath",
|
|
708
|
+
root,
|
|
709
|
+
startsWithBracket: !root && this.peek().type === "[",
|
|
710
|
+
expressions: []
|
|
711
|
+
};
|
|
712
|
+
while ([".", "..", "["].includes(this.peek().type)) {
|
|
713
|
+
node.expressions.push(this.expression(node.expressions.length === 0));
|
|
714
|
+
}
|
|
715
|
+
if (!root && node.expressions.length === 0) {
|
|
716
|
+
throw this.error();
|
|
717
|
+
}
|
|
718
|
+
return node;
|
|
719
|
+
}
|
|
720
|
+
expression(first) {
|
|
721
|
+
const token = this.next();
|
|
722
|
+
if (token.type === ".") {
|
|
723
|
+
return { kind: "expression", child: this.dotOperator() };
|
|
724
|
+
}
|
|
725
|
+
else if (token.type === "..") {
|
|
726
|
+
return { kind: "expression", child: { kind: "recursiveDescent", first, child: this.dotOperator() } };
|
|
727
|
+
}
|
|
728
|
+
this.pos--;
|
|
729
|
+
return { kind: "expression", child: this.bracketOperator() };
|
|
730
|
+
}
|
|
731
|
+
dotOperator() {
|
|
732
|
+
const token = this.peek();
|
|
733
|
+
if (token.type === "[") {
|
|
734
|
+
return { kind: "dotOperator", child: this.bracketOperator() };
|
|
735
|
+
}
|
|
736
|
+
else if (token.type === "*") {
|
|
737
|
+
this.next();
|
|
738
|
+
return { kind: "dotOperator", child: { kind: "wildcard" } };
|
|
739
|
+
}
|
|
740
|
+
return { kind: "dotOperator", child: this.property() };
|
|
741
|
+
}
|
|
742
|
+
property() {
|
|
743
|
+
const token = this.next();
|
|
744
|
+
if (token.type === "string") {
|
|
745
|
+
return { kind: "property", name: decode(token.text) };
|
|
746
|
+
}
|
|
747
|
+
else if (token.type === "identifier" || KEYWORDS.has(token.text)) {
|
|
748
|
+
return { kind: "property", name: token.text };
|
|
749
|
+
}
|
|
750
|
+
this.pos--;
|
|
751
|
+
throw this.error();
|
|
752
|
+
}
|
|
753
|
+
bracketOperator() {
|
|
754
|
+
this.expect("[");
|
|
755
|
+
const node = { kind: "bracket", properties: [] };
|
|
756
|
+
const token = this.peek();
|
|
757
|
+
if (token.type === "?") {
|
|
758
|
+
this.next();
|
|
759
|
+
this.expect("(");
|
|
760
|
+
const expressions = [];
|
|
761
|
+
do {
|
|
762
|
+
expressions.push(this.filterExpression());
|
|
763
|
+
} while (this.peek().type !== ")");
|
|
764
|
+
this.expect(")");
|
|
765
|
+
node.filter = { kind: "filter", expressions };
|
|
766
|
+
}
|
|
767
|
+
else if (token.type === ":") {
|
|
768
|
+
this.next();
|
|
769
|
+
node.slice = { kind: "slice", first: Number(this.expect("positive").text) };
|
|
770
|
+
}
|
|
771
|
+
else if (token.type === "negative" && this.peek(1).type === ":") {
|
|
772
|
+
this.pos += 2;
|
|
773
|
+
node.slice = { kind: "slice", last: -Number(token.text) };
|
|
774
|
+
}
|
|
775
|
+
else if (token.type === "positive" && this.peek(1).type === ":") {
|
|
776
|
+
this.pos += 2;
|
|
777
|
+
const end = this.peek().type === "positive" ? Number(this.next().text) : undefined;
|
|
778
|
+
node.slice = { kind: "slice", start: Number(token.text), end };
|
|
779
|
+
}
|
|
780
|
+
else if (token.type === "positive") {
|
|
781
|
+
node.indexes = [];
|
|
782
|
+
while (this.peek().type === "positive") {
|
|
783
|
+
node.indexes.push(this.next().text);
|
|
784
|
+
}
|
|
785
|
+
}
|
|
786
|
+
else if (token.type === "*") {
|
|
787
|
+
this.next();
|
|
788
|
+
node.slice = { kind: "slice" };
|
|
789
|
+
}
|
|
790
|
+
else {
|
|
791
|
+
do {
|
|
792
|
+
node.properties.push(this.property());
|
|
793
|
+
} while (this.peek().type !== "]");
|
|
794
|
+
}
|
|
795
|
+
this.expect("]");
|
|
796
|
+
return node;
|
|
797
|
+
}
|
|
798
|
+
filterExpression() {
|
|
799
|
+
let operand = this.operand();
|
|
800
|
+
if (this.peek().type === "logical") {
|
|
801
|
+
if (operand.kind === "unary") {
|
|
802
|
+
throw this.error();
|
|
803
|
+
}
|
|
804
|
+
while (this.peek().type === "logical") {
|
|
805
|
+
const operator = this.next().text;
|
|
806
|
+
const right = this.operand();
|
|
807
|
+
if (right.kind === "unary") {
|
|
808
|
+
throw this.error();
|
|
809
|
+
}
|
|
810
|
+
operand = { kind: "binary", operator, left: operand, right };
|
|
811
|
+
}
|
|
812
|
+
}
|
|
813
|
+
return { kind: "filterExpression", child: operand };
|
|
814
|
+
}
|
|
815
|
+
operand() {
|
|
816
|
+
const token = this.peek();
|
|
817
|
+
if (token.type === "!") {
|
|
818
|
+
this.next();
|
|
819
|
+
if (this.peek().type === "(") {
|
|
820
|
+
this.next();
|
|
821
|
+
const filterExpression = this.filterExpression();
|
|
822
|
+
this.expect(")");
|
|
823
|
+
return { kind: "negation", filterExpression };
|
|
824
|
+
}
|
|
825
|
+
return { kind: "negation", unary: this.unary() };
|
|
826
|
+
}
|
|
827
|
+
else if (["string", "positive", "negative", "numeric", "true", "false", "null"].includes(token.type)) {
|
|
828
|
+
const literal = this.literal();
|
|
829
|
+
const operator = this.next();
|
|
830
|
+
if (operator.type === "equality") {
|
|
831
|
+
return { kind: "binary", operator: operator.text, left: literal, right: this.unary() };
|
|
832
|
+
}
|
|
833
|
+
else if (operator.type === "contains") {
|
|
834
|
+
return { kind: "contains", unaryFirst: false, unary: this.unary(), literal };
|
|
835
|
+
}
|
|
836
|
+
this.pos--;
|
|
837
|
+
throw this.error();
|
|
838
|
+
}
|
|
839
|
+
const unary = this.unary();
|
|
840
|
+
const operator = this.peek();
|
|
841
|
+
if (operator.type === "equality") {
|
|
842
|
+
this.next();
|
|
843
|
+
return { kind: "binary", operator: operator.text, left: unary, right: this.literal() };
|
|
844
|
+
}
|
|
845
|
+
else if (operator.type === "contains") {
|
|
846
|
+
this.next();
|
|
847
|
+
return { kind: "contains", unaryFirst: true, unary, literal: this.literal() };
|
|
848
|
+
}
|
|
849
|
+
else if (operator.type === "regex") {
|
|
850
|
+
this.next();
|
|
851
|
+
return { kind: "regex", unary, pattern: new RegExp(`^(?:${operator.text})$`) };
|
|
852
|
+
}
|
|
853
|
+
return unary;
|
|
854
|
+
}
|
|
855
|
+
unary() {
|
|
856
|
+
const start = this.pos;
|
|
857
|
+
let node;
|
|
858
|
+
if (this.peek().type === "@") {
|
|
859
|
+
this.next();
|
|
860
|
+
node = { kind: "unary", at: true, lastToken: "" };
|
|
861
|
+
if (this.peek().type === "." && this.peek(1).type !== "[") {
|
|
862
|
+
this.next();
|
|
863
|
+
node.name = this.property().name;
|
|
864
|
+
}
|
|
865
|
+
else if (this.peek().type === "[" || this.peek().type === "." && this.peek(1).type === "[") {
|
|
866
|
+
if (this.peek().type === ".") {
|
|
867
|
+
this.next();
|
|
868
|
+
}
|
|
869
|
+
this.expect("[");
|
|
870
|
+
node.name = decode(this.expect("string").text);
|
|
871
|
+
this.expect("]");
|
|
872
|
+
}
|
|
873
|
+
node.thirdToken = this.pos - start > 2 ? this.tokens[start + 2].text : undefined;
|
|
874
|
+
}
|
|
875
|
+
else {
|
|
876
|
+
node = { kind: "unary", at: false, jsonPath: this.jsonPath(), lastToken: "" };
|
|
877
|
+
}
|
|
878
|
+
node.lastToken = this.tokens[this.pos - 1].text;
|
|
879
|
+
return node;
|
|
880
|
+
}
|
|
881
|
+
literal() {
|
|
882
|
+
const token = this.next();
|
|
883
|
+
switch (token.type) {
|
|
884
|
+
case "string":
|
|
885
|
+
return { kind: "literal", value: decode(token.text) };
|
|
886
|
+
case "positive":
|
|
887
|
+
case "negative":
|
|
888
|
+
case "numeric":
|
|
889
|
+
return { kind: "literal", value: new JsonPathNumber(token.text) };
|
|
890
|
+
case "true":
|
|
891
|
+
case "false":
|
|
892
|
+
return { kind: "literal", value: token.type === "true" };
|
|
893
|
+
case "null":
|
|
894
|
+
return { kind: "literal", value: null };
|
|
895
|
+
}
|
|
896
|
+
this.pos--;
|
|
897
|
+
throw this.error();
|
|
898
|
+
}
|
|
899
|
+
}
|
|
900
|
+
/**
|
|
901
|
+
* The string that a single- or double-quoted JSON5 literal stands for. Mirrors
|
|
902
|
+
* `org.openrewrite.json.internal.JsonString.decode`.
|
|
903
|
+
*/
|
|
904
|
+
function decode(literal) {
|
|
905
|
+
const body = literal.substring(1, literal.length - 1);
|
|
906
|
+
if (!body.includes("\\")) {
|
|
907
|
+
return body;
|
|
908
|
+
}
|
|
909
|
+
let out = "";
|
|
910
|
+
for (let i = 0; i < body.length; i++) {
|
|
911
|
+
const c = body[i];
|
|
912
|
+
if (c !== "\\" || i + 1 === body.length) {
|
|
913
|
+
out += c;
|
|
914
|
+
continue;
|
|
915
|
+
}
|
|
916
|
+
const escaped = body[++i];
|
|
917
|
+
switch (escaped) {
|
|
918
|
+
case "b":
|
|
919
|
+
out += "\b";
|
|
920
|
+
break;
|
|
921
|
+
case "f":
|
|
922
|
+
out += "\f";
|
|
923
|
+
break;
|
|
924
|
+
case "n":
|
|
925
|
+
out += "\n";
|
|
926
|
+
break;
|
|
927
|
+
case "r":
|
|
928
|
+
out += "\r";
|
|
929
|
+
break;
|
|
930
|
+
case "t":
|
|
931
|
+
out += "\t";
|
|
932
|
+
break;
|
|
933
|
+
case "v":
|
|
934
|
+
out += "\v";
|
|
935
|
+
break;
|
|
936
|
+
case "0":
|
|
937
|
+
out += "\0";
|
|
938
|
+
break;
|
|
939
|
+
case "x":
|
|
940
|
+
case "u": {
|
|
941
|
+
const digits = escaped === "x" ? 2 : 4;
|
|
942
|
+
const hex = body.substring(i + 1, i + 1 + digits);
|
|
943
|
+
if (hex.length === digits && /^[0-9a-fA-F]+$/.test(hex)) {
|
|
944
|
+
out += String.fromCharCode(parseInt(hex, 16));
|
|
945
|
+
i += digits;
|
|
946
|
+
}
|
|
947
|
+
else {
|
|
948
|
+
out += "\\" + escaped;
|
|
949
|
+
}
|
|
950
|
+
break;
|
|
951
|
+
}
|
|
952
|
+
case "\r":
|
|
953
|
+
if (body[i + 1] === "\n") {
|
|
954
|
+
i++;
|
|
955
|
+
}
|
|
956
|
+
break;
|
|
957
|
+
case "\n":
|
|
958
|
+
case "
":
|
|
959
|
+
case "
":
|
|
960
|
+
break;
|
|
961
|
+
default:
|
|
962
|
+
out += escaped;
|
|
963
|
+
}
|
|
964
|
+
}
|
|
965
|
+
return escapeUnpairedSurrogates(out);
|
|
966
|
+
}
|
|
967
|
+
/**
|
|
968
|
+
* Keeps a surrogate with no partner as a lower-case `\uXXXX` escape, as `JsonString.decode` and the parser's
|
|
969
|
+
* literal values do. `getMemberKeyName` decodes keys from source and so yields the bare surrogate.
|
|
970
|
+
*/
|
|
971
|
+
function escapeUnpairedSurrogates(value) {
|
|
972
|
+
return value.replace(/[\ud800-\udfff]/gu, surrogate => `\\u${surrogate.charCodeAt(0).toString(16)}`);
|
|
973
|
+
}
|
|
974
|
+
//# sourceMappingURL=path-matcher.js.map
|