@openrewrite/rewrite 8.100.0 → 8.100.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.
Files changed (158) hide show
  1. package/dist/java/rpc.d.ts.map +1 -1
  2. package/dist/java/rpc.js +6 -1
  3. package/dist/java/rpc.js.map +1 -1
  4. package/dist/javascript/add-import.d.ts.map +1 -1
  5. package/dist/javascript/add-import.js +3 -0
  6. package/dist/javascript/add-import.js.map +1 -1
  7. package/dist/javascript/binding.d.ts.map +1 -1
  8. package/dist/javascript/binding.js +8 -1
  9. package/dist/javascript/binding.js.map +1 -1
  10. package/dist/javascript/comparator.js +2 -1
  11. package/dist/javascript/comparator.js.map +1 -1
  12. package/dist/javascript/format/format.d.ts.map +1 -1
  13. package/dist/javascript/format/format.js +3 -2
  14. package/dist/javascript/format/format.js.map +1 -1
  15. package/dist/javascript/format/minimum-viable-spacing-visitor.js +3 -3
  16. package/dist/javascript/format/minimum-viable-spacing-visitor.js.map +1 -1
  17. package/dist/javascript/node-resolution-result.d.ts +4 -1
  18. package/dist/javascript/node-resolution-result.d.ts.map +1 -1
  19. package/dist/javascript/node-resolution-result.js +47 -16
  20. package/dist/javascript/node-resolution-result.js.map +1 -1
  21. package/dist/javascript/package-json-parser.d.ts.map +1 -1
  22. package/dist/javascript/package-json-parser.js +4 -2
  23. package/dist/javascript/package-json-parser.js.map +1 -1
  24. package/dist/javascript/package-manager.d.ts +2 -0
  25. package/dist/javascript/package-manager.d.ts.map +1 -1
  26. package/dist/javascript/package-manager.js +11 -4
  27. package/dist/javascript/package-manager.js.map +1 -1
  28. package/dist/javascript/parser.d.ts.map +1 -1
  29. package/dist/javascript/parser.js +11 -17
  30. package/dist/javascript/parser.js.map +1 -1
  31. package/dist/javascript/print.d.ts +4 -0
  32. package/dist/javascript/print.d.ts.map +1 -1
  33. package/dist/javascript/print.js +82 -20
  34. package/dist/javascript/print.js.map +1 -1
  35. package/dist/javascript/rpc.js +2 -0
  36. package/dist/javascript/rpc.js.map +1 -1
  37. package/dist/javascript/scope.d.ts.map +1 -1
  38. package/dist/javascript/scope.js +4 -1
  39. package/dist/javascript/scope.js.map +1 -1
  40. package/dist/javascript/templating/comparator.d.ts +9 -0
  41. package/dist/javascript/templating/comparator.d.ts.map +1 -1
  42. package/dist/javascript/templating/comparator.js +21 -0
  43. package/dist/javascript/templating/comparator.js.map +1 -1
  44. package/dist/javascript/templating/debug.d.ts.map +1 -1
  45. package/dist/javascript/templating/debug.js +2 -0
  46. package/dist/javascript/templating/debug.js.map +1 -1
  47. package/dist/javascript/templating/precedence.d.ts.map +1 -1
  48. package/dist/javascript/templating/precedence.js +36 -39
  49. package/dist/javascript/templating/precedence.js.map +1 -1
  50. package/dist/javascript/templating/types.d.ts +4 -4
  51. package/dist/javascript/tree-debug.d.ts.map +1 -1
  52. package/dist/javascript/tree-debug.js +6 -3
  53. package/dist/javascript/tree-debug.js.map +1 -1
  54. package/dist/javascript/tree.d.ts +4 -1
  55. package/dist/javascript/tree.d.ts.map +1 -1
  56. package/dist/javascript/tree.js +2 -0
  57. package/dist/javascript/tree.js.map +1 -1
  58. package/dist/javascript/type-mapping.d.ts +1 -0
  59. package/dist/javascript/type-mapping.d.ts.map +1 -1
  60. package/dist/javascript/type-mapping.js +12 -1
  61. package/dist/javascript/type-mapping.js.map +1 -1
  62. package/dist/json/edit.d.ts +150 -0
  63. package/dist/json/edit.d.ts.map +1 -0
  64. package/dist/json/edit.js +667 -0
  65. package/dist/json/edit.js.map +1 -0
  66. package/dist/json/index.d.ts +1 -0
  67. package/dist/json/index.d.ts.map +1 -1
  68. package/dist/json/index.js +1 -0
  69. package/dist/json/index.js.map +1 -1
  70. package/dist/json/parser.d.ts.map +1 -1
  71. package/dist/json/parser.js +8 -1
  72. package/dist/json/parser.js.map +1 -1
  73. package/dist/parser.d.ts +1 -1
  74. package/dist/parser.d.ts.map +1 -1
  75. package/dist/print.d.ts +16 -0
  76. package/dist/print.d.ts.map +1 -1
  77. package/dist/print.js +33 -0
  78. package/dist/print.js.map +1 -1
  79. package/dist/rewrite-javascript-version.txt +1 -1
  80. package/dist/rpc/request/get-languages.d.ts.map +1 -1
  81. package/dist/rpc/request/get-languages.js +1 -0
  82. package/dist/rpc/request/get-languages.js.map +1 -1
  83. package/dist/rpc/request/parse.d.ts.map +1 -1
  84. package/dist/rpc/request/parse.js +4 -0
  85. package/dist/rpc/request/parse.js.map +1 -1
  86. package/dist/rpc/server.d.ts +1 -0
  87. package/dist/rpc/server.d.ts.map +1 -1
  88. package/dist/rpc/server.js +1 -0
  89. package/dist/rpc/server.js.map +1 -1
  90. package/dist/xml/assertions.d.ts +4 -0
  91. package/dist/xml/assertions.d.ts.map +1 -0
  92. package/dist/xml/assertions.js +31 -0
  93. package/dist/xml/assertions.js.map +1 -0
  94. package/dist/xml/index.d.ts +8 -0
  95. package/dist/xml/index.d.ts.map +1 -0
  96. package/dist/xml/index.js +39 -0
  97. package/dist/xml/index.js.map +1 -0
  98. package/dist/xml/namespaces.d.ts +36 -0
  99. package/dist/xml/namespaces.d.ts.map +1 -0
  100. package/dist/xml/namespaces.js +98 -0
  101. package/dist/xml/namespaces.js.map +1 -0
  102. package/dist/xml/parser.d.ts +15 -0
  103. package/dist/xml/parser.d.ts.map +1 -0
  104. package/dist/xml/parser.js +677 -0
  105. package/dist/xml/parser.js.map +1 -0
  106. package/dist/xml/print.d.ts +2 -0
  107. package/dist/xml/print.d.ts.map +1 -0
  108. package/dist/xml/print.js +209 -0
  109. package/dist/xml/print.js.map +1 -0
  110. package/dist/xml/rpc.d.ts +2 -0
  111. package/dist/xml/rpc.d.ts.map +1 -0
  112. package/dist/xml/rpc.js +313 -0
  113. package/dist/xml/rpc.js.map +1 -0
  114. package/dist/xml/tree.d.ts +244 -0
  115. package/dist/xml/tree.d.ts.map +1 -0
  116. package/dist/xml/tree.js +265 -0
  117. package/dist/xml/tree.js.map +1 -0
  118. package/dist/xml/visitor.d.ts +35 -0
  119. package/dist/xml/visitor.d.ts.map +1 -0
  120. package/dist/xml/visitor.js +203 -0
  121. package/dist/xml/visitor.js.map +1 -0
  122. package/package.json +5 -1
  123. package/src/java/rpc.ts +6 -1
  124. package/src/javascript/add-import.ts +3 -0
  125. package/src/javascript/binding.ts +10 -1
  126. package/src/javascript/comparator.ts +2 -1
  127. package/src/javascript/format/format.ts +3 -2
  128. package/src/javascript/format/minimum-viable-spacing-visitor.ts +3 -3
  129. package/src/javascript/node-resolution-result.ts +49 -15
  130. package/src/javascript/package-json-parser.ts +4 -1
  131. package/src/javascript/package-manager.ts +11 -4
  132. package/src/javascript/parser.ts +12 -17
  133. package/src/javascript/print.ts +85 -23
  134. package/src/javascript/rpc.ts +2 -0
  135. package/src/javascript/scope.ts +4 -1
  136. package/src/javascript/templating/comparator.ts +23 -0
  137. package/src/javascript/templating/debug.ts +1 -0
  138. package/src/javascript/templating/precedence.ts +17 -20
  139. package/src/javascript/templating/types.ts +4 -4
  140. package/src/javascript/tree-debug.ts +7 -3
  141. package/src/javascript/tree.ts +4 -1
  142. package/src/javascript/type-mapping.ts +15 -1
  143. package/src/json/edit.ts +750 -0
  144. package/src/json/index.ts +1 -0
  145. package/src/json/parser.ts +9 -1
  146. package/src/parser.ts +1 -1
  147. package/src/print.ts +42 -0
  148. package/src/rpc/request/get-languages.ts +1 -0
  149. package/src/rpc/request/parse.ts +3 -0
  150. package/src/rpc/server.ts +1 -0
  151. package/src/xml/assertions.ts +28 -0
  152. package/src/xml/index.ts +23 -0
  153. package/src/xml/namespaces.ts +109 -0
  154. package/src/xml/parser.ts +692 -0
  155. package/src/xml/print.ts +225 -0
  156. package/src/xml/rpc.ts +370 -0
  157. package/src/xml/tree.ts +455 -0
  158. package/src/xml/visitor.ts +227 -0
@@ -0,0 +1,677 @@
1
+ "use strict";
2
+ var __await = (this && this.__await) || function (v) { return this instanceof __await ? (this.v = v, this) : new __await(v); }
3
+ var __asyncGenerator = (this && this.__asyncGenerator) || function (thisArg, _arguments, generator) {
4
+ if (!Symbol.asyncIterator) throw new TypeError("Symbol.asyncIterator is not defined.");
5
+ var g = generator.apply(thisArg, _arguments || []), i, q = [];
6
+ return i = Object.create((typeof AsyncIterator === "function" ? AsyncIterator : Object).prototype), verb("next"), verb("throw"), verb("return", awaitReturn), i[Symbol.asyncIterator] = function () { return this; }, i;
7
+ function awaitReturn(f) { return function (v) { return Promise.resolve(v).then(f, reject); }; }
8
+ function verb(n, f) { if (g[n]) { i[n] = function (v) { return new Promise(function (a, b) { q.push([n, v, a, b]) > 1 || resume(n, v); }); }; if (f) i[n] = f(i[n]); } }
9
+ function resume(n, v) { try { step(g[n](v)); } catch (e) { settle(q[0][3], e); } }
10
+ function step(r) { r.value instanceof __await ? Promise.resolve(r.value.v).then(fulfill, reject) : settle(q[0][2], r); }
11
+ function fulfill(value) { resume("next", value); }
12
+ function reject(value) { resume("throw", value); }
13
+ function settle(f, v) { if (f(v), q.shift(), q.length) resume(q[0][0], q[0][1]); }
14
+ };
15
+ Object.defineProperty(exports, "__esModule", { value: true });
16
+ exports.XmlParser = void 0;
17
+ /*
18
+ * Copyright 2026 the original author or authors.
19
+ * <p>
20
+ * Licensed under the Moderne Source Available License (the "License");
21
+ * you may not use this file except in compliance with the License.
22
+ * You may obtain a copy of the License at
23
+ * <p>
24
+ * https://docs.moderne.io/licensing/moderne-source-available-license
25
+ * <p>
26
+ * Unless required by applicable law or agreed to in writing, software
27
+ * distributed under the License is distributed on an "AS IS" BASIS,
28
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
29
+ * See the License for the specific language governing permissions and
30
+ * limitations under the License.
31
+ */
32
+ const markers_1 = require("../markers");
33
+ const parser_1 = require("../parser");
34
+ const uuid_1 = require("../uuid");
35
+ const tree_1 = require("./tree");
36
+ const ACCEPTED_FILE_EXTENSIONS = new Set([
37
+ "xml", "wsdl", "xhtml", "xsd", "xsl", "xslt", "xmi", "tld", "jxb", "xjb", "jsp", "jspx", "tagx",
38
+ "mapxmi", "tblxmi", "dbxmi", "schxmi",
39
+ "det", "pjb", "qjb", "sjb", "prt", "srt", "psc", "ssc", "tbd", "tfm", "dqs", "stp", "dcn", "pst",
40
+ "csproj", "vbproj", "fsproj", "props", "targets", "slnx",
41
+ "projitems", "shproj", "vcxproj", "sqlproj", "njsproj", "pyproj", "wapproj", "ruleset", "dotsettings",
42
+ "jrxml"
43
+ ]);
44
+ const HTML_VOID_ELEMENTS = new Set([
45
+ "area", "base", "br", "col", "embed", "hr", "img", "input", "link", "meta", "param", "source", "track", "wbr"
46
+ ]);
47
+ /**
48
+ * Parses XML into an {@link Xml.Document} that prints back byte for byte. The tree has the shape
49
+ * `org.openrewrite.xml.XmlParser` gives the same source, down to which element each run of whitespace
50
+ * is the prefix of, so that a recipe sees the same tree whichever side parsed the file.
51
+ */
52
+ class XmlParser extends parser_1.Parser {
53
+ /**
54
+ * Whether `org.openrewrite.xml.XmlParser` takes the file at this path.
55
+ */
56
+ static accept(sourcePath) {
57
+ const fileName = sourcePath.substring(Math.max(sourcePath.lastIndexOf("/"), sourcePath.lastIndexOf("\\")) + 1);
58
+ const dot = fileName.lastIndexOf(".");
59
+ if (0 < dot && dot < fileName.length - 1 &&
60
+ ACCEPTED_FILE_EXTENSIONS.has(fileName.substring(dot + 1).toLowerCase())) {
61
+ return true;
62
+ }
63
+ const lower = fileName.toLowerCase();
64
+ return lower === "nuget.config" || lower === "packages.config" ||
65
+ ((lower.startsWith("app.") || lower.startsWith("web.")) && lower.endsWith(".config"));
66
+ }
67
+ parse(...sourcePaths) {
68
+ return __asyncGenerator(this, arguments, function* parse_1() {
69
+ for (const input of sourcePaths) {
70
+ const sourcePath = this.relativePath(input);
71
+ try {
72
+ const text = (0, parser_1.parserInputRead)(input);
73
+ const bom = text.startsWith("\uFEFF");
74
+ const document = Object.assign(Object.assign({}, new XmlParserReader(bom ? text.substring(1) : text, isHtmlLike(sourcePath)).parse()), { sourcePath, charsetName: "UTF-8", charsetBomMarked: bom });
75
+ yield yield __await(yield __await(this.requirePrintEqualsInput(document, input, text)));
76
+ }
77
+ catch (e) {
78
+ yield yield __await(this.error(input, e instanceof Error ? e : new Error(String(e))));
79
+ }
80
+ }
81
+ });
82
+ }
83
+ }
84
+ exports.XmlParser = XmlParser;
85
+ function isHtmlLike(sourcePath) {
86
+ const p = sourcePath.toLowerCase();
87
+ return p.endsWith(".jsp") || p.endsWith(".html") || p.endsWith(".htm");
88
+ }
89
+ const NAME_START_CHAR = ":A-Z_a-z\\u00C0-\\u00D6\\u00D8-\\u00F6\\u00F8-\\u02FF\\u0370-\\u037D\\u037F-\\u1FFF" +
90
+ "\\u200C-\\u200D\\u2070-\\u218F\\u3001-\\uD7FF\\uF900-\\uFDCF\\uFDF0-\\uFFFD\\u{10000}-\\u{EFFFF}";
91
+ const NAME_CHAR = NAME_START_CHAR + "\\-.0-9\\u00B7\\u0300-\\u036F\\u203F-\\u2040";
92
+ const NAME = `[${NAME_START_CHAR}][${NAME_CHAR}]*`;
93
+ /**
94
+ * The whitespace the lexer skips between the parts of a tag, `S` in the grammar.
95
+ */
96
+ const TAG_WHITESPACE = /[ \t\r\n]*/y;
97
+ /**
98
+ * The whitespace the lexer skips between markup, `WS` in the grammar.
99
+ */
100
+ const MARKUP_WHITESPACE = /^[ \t\n\r\f]+$/;
101
+ const NAME_PATTERN = new RegExp(NAME, "uy");
102
+ const NAME_START_PATTERN = new RegExp(`[${NAME_START_CHAR}]`, "uy");
103
+ const NAME_CHAR_PATTERN = new RegExp(`[${NAME_CHAR}]`, "uy");
104
+ const REFERENCE_PATTERN = new RegExp(`&(?:#[0-9]+|#x[0-9a-fA-F]+|${NAME});`, "uy");
105
+ const PARAMETER_ENTITY_REFERENCE_PATTERN = new RegExp(`%${NAME};`, "uy");
106
+ const TEXT_END = /[<&]/g;
107
+ /**
108
+ * `java.lang.Character.isWhitespace(char)`, by which the Java parser splits the whitespace off character data.
109
+ */
110
+ function isJavaWhitespace(c) {
111
+ return (c >= 0x09 && c <= 0x0D) || (c >= 0x1C && c <= 0x20) || c === 0x1680 ||
112
+ (c >= 0x2000 && c <= 0x2006) || (c >= 0x2008 && c <= 0x200A) ||
113
+ c === 0x2028 || c === 0x2029 || c === 0x205F || c === 0x3000;
114
+ }
115
+ class XmlParserReader {
116
+ constructor(source, htmlMode) {
117
+ this.source = source;
118
+ this.htmlMode = htmlMode;
119
+ this.cursor = 0;
120
+ }
121
+ parse() {
122
+ const prefix = this.markupWhitespace();
123
+ const prolog = this.parseProlog();
124
+ const beforeRoot = this.cursor;
125
+ const rootPrefix = this.markupWhitespace();
126
+ let root;
127
+ if (this.startsWithTag()) {
128
+ root = this.parseTag(rootPrefix);
129
+ }
130
+ else if (this.cursor === this.source.length) {
131
+ this.cursor = beforeRoot;
132
+ }
133
+ else {
134
+ throw this.error("Expected the root element");
135
+ }
136
+ return {
137
+ kind: tree_1.Xml.Kind.Document,
138
+ id: (0, uuid_1.randomId)(),
139
+ prefix,
140
+ markers: markers_1.emptyMarkers,
141
+ prolog,
142
+ root,
143
+ eof: this.source.substring(this.cursor)
144
+ };
145
+ }
146
+ parseProlog() {
147
+ const xmlDecl = this.startsWithXmlDecl() ? this.parseXmlDecl() : undefined;
148
+ const misc = [];
149
+ while (true) {
150
+ const start = this.cursor;
151
+ const prefix = this.markupWhitespace();
152
+ if (this.startsWith("<!--")) {
153
+ misc.push(this.parseComment(prefix));
154
+ }
155
+ else if (this.startsWithDocTypeDecl()) {
156
+ misc.push(this.parseDocTypeDecl(prefix));
157
+ }
158
+ else if (this.startsWith("<?") && !this.startsWithXmlDecl()) {
159
+ misc.push(this.parseProcessingInstruction(prefix));
160
+ }
161
+ else if (this.startsWith("<%--")) {
162
+ misc.push(this.parseJspComment(prefix));
163
+ }
164
+ else if (this.startsWith("<%!")) {
165
+ misc.push(this.parseDelimited(prefix, tree_1.Xml.Kind.JspDeclaration, "<%!", "%>"));
166
+ }
167
+ else {
168
+ this.cursor = start;
169
+ break;
170
+ }
171
+ }
172
+ const jspDirectives = [];
173
+ while (true) {
174
+ const start = this.cursor;
175
+ const prefix = this.markupWhitespace();
176
+ if (this.startsWith("<%@")) {
177
+ jspDirectives.push(this.parseJspDirective(prefix));
178
+ }
179
+ else {
180
+ this.cursor = start;
181
+ break;
182
+ }
183
+ }
184
+ return {
185
+ kind: tree_1.Xml.Kind.Prolog,
186
+ id: (0, uuid_1.randomId)(),
187
+ prefix: "",
188
+ markers: markers_1.emptyMarkers,
189
+ xmlDecl,
190
+ misc,
191
+ jspDirectives
192
+ };
193
+ }
194
+ parseXmlDecl() {
195
+ this.cursor += "<?".length;
196
+ const name = this.name();
197
+ const { attributes, before } = this.parseAttributes();
198
+ this.expect("?>");
199
+ return {
200
+ kind: tree_1.Xml.Kind.XmlDecl,
201
+ id: (0, uuid_1.randomId)(),
202
+ prefix: "",
203
+ markers: markers_1.emptyMarkers,
204
+ name,
205
+ attributes,
206
+ beforeTagDelimiterPrefix: before
207
+ };
208
+ }
209
+ parseProcessingInstruction(prefix) {
210
+ this.cursor += "<?".length;
211
+ const name = this.name();
212
+ const end = this.indexOf("?>", this.cursor, "<?" + name);
213
+ const data = this.source.substring(this.cursor, end);
214
+ this.cursor = end + "?>".length;
215
+ // As the lexer has it, a lone whitespace character is skipped, while any longer run is data.
216
+ const isData = data.length > 1 || (data.length === 1 && !/[ \t\r\n]/.test(data));
217
+ return {
218
+ kind: tree_1.Xml.Kind.ProcessingInstruction,
219
+ id: (0, uuid_1.randomId)(),
220
+ prefix,
221
+ markers: markers_1.emptyMarkers,
222
+ name,
223
+ processingInstructions: isData ? this.charData(data, "") : undefined,
224
+ beforeTagDelimiterPrefix: isData ? "" : data
225
+ };
226
+ }
227
+ parseTag(prefix) {
228
+ var _a, _b;
229
+ const start = this.cursor;
230
+ this.cursor += "<".length;
231
+ const name = this.name();
232
+ const { attributes, before } = this.parseAttributes();
233
+ if (this.startsWith("/>")) {
234
+ this.cursor += "/>".length;
235
+ return this.tag(prefix, markers_1.emptyMarkers, name, attributes, undefined, undefined, before);
236
+ }
237
+ this.expect(">");
238
+ if (this.htmlMode && HTML_VOID_ELEMENTS.has(name.toLowerCase())) {
239
+ return this.tag(prefix, (0, markers_1.markers)({ kind: tree_1.Xml.MarkersKind.HtmlVoidElement, id: (0, uuid_1.randomId)() }), name, attributes, undefined, undefined, before);
240
+ }
241
+ const content = [];
242
+ while (true) {
243
+ const textStart = this.cursor;
244
+ TEXT_END.lastIndex = textStart;
245
+ const textEnd = (_b = (_a = TEXT_END.exec(this.source)) === null || _a === void 0 ? void 0 : _a.index) !== null && _b !== void 0 ? _b : this.source.length;
246
+ this.cursor = textEnd;
247
+ let itemPrefix = "";
248
+ if (textEnd > textStart) {
249
+ const text = this.source.substring(textStart, textEnd);
250
+ if (MARKUP_WHITESPACE.test(text)) {
251
+ itemPrefix = text;
252
+ }
253
+ else {
254
+ content.push(this.charData(text, ""));
255
+ }
256
+ }
257
+ if (this.cursor >= this.source.length) {
258
+ throw this.error(`Unclosed element <${name}>`, start);
259
+ }
260
+ if (this.startsWith("</")) {
261
+ const closing = this.parseClosing(itemPrefix, name);
262
+ return this.tag(prefix, markers_1.emptyMarkers, name, attributes, content, closing, before);
263
+ }
264
+ content.push(this.parseContent(itemPrefix));
265
+ }
266
+ }
267
+ parseClosing(prefix, openName) {
268
+ const start = this.cursor;
269
+ this.cursor += "</".length;
270
+ const name = this.name();
271
+ const before = this.tagWhitespace();
272
+ this.expect(">");
273
+ if (!this.htmlMode && name !== openName) {
274
+ throw this.error(`Mismatched closing tag </${name}> for <${openName}>`, start);
275
+ }
276
+ return {
277
+ kind: tree_1.Xml.Kind.TagClosing,
278
+ id: (0, uuid_1.randomId)(),
279
+ prefix,
280
+ markers: markers_1.emptyMarkers,
281
+ name,
282
+ beforeTagDelimiterPrefix: before
283
+ };
284
+ }
285
+ tag(prefix, tagMarkers, name, attributes, content, closing, before) {
286
+ return {
287
+ kind: tree_1.Xml.Kind.Tag,
288
+ id: (0, uuid_1.randomId)(),
289
+ prefix,
290
+ markers: tagMarkers,
291
+ name,
292
+ attributes,
293
+ content,
294
+ closing,
295
+ beforeTagDelimiterPrefix: before
296
+ };
297
+ }
298
+ parseContent(prefix) {
299
+ if (this.startsWith("<!--")) {
300
+ return this.parseComment(prefix);
301
+ }
302
+ else if (this.startsWith("<![CDATA[")) {
303
+ const end = this.indexOf("]]>", this.cursor + "<![CDATA[".length, "<![CDATA[");
304
+ const text = this.source.substring(this.cursor + "<![CDATA[".length, end);
305
+ this.cursor = end + "]]>".length;
306
+ return {
307
+ kind: tree_1.Xml.Kind.CharData,
308
+ id: (0, uuid_1.randomId)(),
309
+ prefix,
310
+ markers: markers_1.emptyMarkers,
311
+ cdata: true,
312
+ text,
313
+ afterText: ""
314
+ };
315
+ }
316
+ else if (this.startsWith("<%--")) {
317
+ return this.parseJspComment(prefix);
318
+ }
319
+ else if (this.startsWith("<%!")) {
320
+ return this.parseDelimited(prefix, tree_1.Xml.Kind.JspDeclaration, "<%!", "%>");
321
+ }
322
+ else if (this.startsWith("<%=")) {
323
+ return this.parseDelimited(prefix, tree_1.Xml.Kind.JspExpression, "<%=", "%>");
324
+ }
325
+ else if (this.startsWith("<%") && this.cursor + 2 < this.source.length && !"@-".includes(this.source[this.cursor + 2])) {
326
+ return this.parseDelimited(prefix, tree_1.Xml.Kind.JspScriptlet, "<%", "%>");
327
+ }
328
+ else if (this.startsWith("<?") && !this.startsWithXmlDecl()) {
329
+ return this.parseProcessingInstruction(prefix);
330
+ }
331
+ else if (this.startsWithTag()) {
332
+ return this.parseTag(prefix);
333
+ }
334
+ else if (this.startsWith("&")) {
335
+ REFERENCE_PATTERN.lastIndex = this.cursor;
336
+ const reference = REFERENCE_PATTERN.exec(this.source);
337
+ if (!reference) {
338
+ throw this.error("Expected an entity or character reference");
339
+ }
340
+ this.cursor += reference[0].length;
341
+ return {
342
+ kind: tree_1.Xml.Kind.CharData,
343
+ id: (0, uuid_1.randomId)(),
344
+ prefix,
345
+ markers: markers_1.emptyMarkers,
346
+ cdata: false,
347
+ text: reference[0],
348
+ afterText: ""
349
+ };
350
+ }
351
+ throw this.error("Unexpected markup");
352
+ }
353
+ parseComment(prefix) {
354
+ const end = this.indexOf("-->", this.cursor + "<!--".length, "<!--");
355
+ const text = this.source.substring(this.cursor + "<!--".length, end);
356
+ this.cursor = end + "-->".length;
357
+ return {
358
+ kind: tree_1.Xml.Kind.Comment,
359
+ id: (0, uuid_1.randomId)(),
360
+ prefix,
361
+ markers: markers_1.emptyMarkers,
362
+ text
363
+ };
364
+ }
365
+ parseJspComment(prefix) {
366
+ return this.parseDelimited(prefix, tree_1.Xml.Kind.JspComment, "<%--", "--%>");
367
+ }
368
+ parseDelimited(prefix, kind, open, close) {
369
+ // The lexer's non-greedy match needs at least one character between a scriptlet's `<%` and `%>`.
370
+ const contentStart = this.cursor + open.length;
371
+ const end = this.indexOf(close, kind === tree_1.Xml.Kind.JspScriptlet ? contentStart + 1 : contentStart, open);
372
+ const content = this.source.substring(contentStart, end);
373
+ this.cursor = end + close.length;
374
+ return {
375
+ kind,
376
+ id: (0, uuid_1.randomId)(),
377
+ prefix,
378
+ markers: markers_1.emptyMarkers,
379
+ content
380
+ };
381
+ }
382
+ parseJspDirective(prefix) {
383
+ this.cursor += "<%@".length;
384
+ const beforeTypePrefix = this.tagWhitespace();
385
+ const type = this.name();
386
+ const { attributes, before } = this.parseAttributes();
387
+ this.expect("%>");
388
+ return {
389
+ kind: tree_1.Xml.Kind.JspDirective,
390
+ id: (0, uuid_1.randomId)(),
391
+ prefix,
392
+ markers: markers_1.emptyMarkers,
393
+ beforeTypePrefix,
394
+ type,
395
+ attributes,
396
+ beforeDirectiveEndPrefix: before
397
+ };
398
+ }
399
+ parseAttributes() {
400
+ const attributes = [];
401
+ while (true) {
402
+ const prefix = this.tagWhitespace();
403
+ if (!this.startsWithName()) {
404
+ return { attributes, before: prefix };
405
+ }
406
+ attributes.push(this.parseAttribute(prefix));
407
+ }
408
+ }
409
+ parseAttribute(prefix) {
410
+ const key = {
411
+ kind: tree_1.Xml.Kind.Ident,
412
+ id: (0, uuid_1.randomId)(),
413
+ prefix: "",
414
+ markers: markers_1.emptyMarkers,
415
+ name: this.name()
416
+ };
417
+ const beforeEquals = this.tagWhitespace();
418
+ this.expect("=");
419
+ const valuePrefix = this.tagWhitespace();
420
+ const valueStart = this.cursor;
421
+ const quote = this.source[this.cursor];
422
+ if (quote !== '"' && quote !== "'") {
423
+ throw this.error(`Expected a quoted value for attribute ${key.name}`);
424
+ }
425
+ const end = this.source.indexOf(quote, this.cursor + 1);
426
+ const value = end < 0 ? "" : this.source.substring(this.cursor + 1, end);
427
+ if (end < 0 || value.includes("<")) {
428
+ throw this.error(`Malformed value of attribute ${key.name}`, valueStart);
429
+ }
430
+ this.cursor = end + 1;
431
+ return {
432
+ kind: tree_1.Xml.Kind.Attribute,
433
+ id: (0, uuid_1.randomId)(),
434
+ prefix,
435
+ markers: markers_1.emptyMarkers,
436
+ key,
437
+ beforeEquals,
438
+ value: {
439
+ kind: tree_1.Xml.Kind.AttributeValue,
440
+ id: (0, uuid_1.randomId)(),
441
+ prefix: valuePrefix,
442
+ markers: markers_1.emptyMarkers,
443
+ quote: quote === '"' ? "Double" : "Single",
444
+ value
445
+ }
446
+ };
447
+ }
448
+ parseDocTypeDecl(prefix) {
449
+ this.cursor += "<!".length;
450
+ const documentDeclaration = this.source.substring(this.cursor, this.cursor + "DOCTYPE".length);
451
+ this.cursor += documentDeclaration.length;
452
+ const namePrefix = this.tagWhitespace();
453
+ const name = this.ident(namePrefix, this.name());
454
+ let externalId;
455
+ let start = this.cursor;
456
+ const externalIdPrefix = this.tagWhitespace();
457
+ if (this.startsWithName()) {
458
+ externalId = this.ident(externalIdPrefix, this.name());
459
+ }
460
+ else {
461
+ this.cursor = start;
462
+ }
463
+ const internalSubset = [];
464
+ while (true) {
465
+ start = this.cursor;
466
+ const stringPrefix = this.tagWhitespace();
467
+ const quote = this.source[this.cursor];
468
+ if (quote !== '"' && quote !== "'") {
469
+ this.cursor = start;
470
+ break;
471
+ }
472
+ const end = this.source.indexOf(quote, this.cursor + 1);
473
+ if (end < 0 || this.source.substring(this.cursor, end).includes("<")) {
474
+ throw this.error("Malformed string in the document type declaration");
475
+ }
476
+ internalSubset.push(this.ident(stringPrefix, this.source.substring(this.cursor, end + 1)));
477
+ this.cursor = end + 1;
478
+ }
479
+ let externalSubsets;
480
+ start = this.cursor;
481
+ const subsetPrefix = this.tagWhitespace();
482
+ if (this.startsWith("[")) {
483
+ externalSubsets = this.parseExternalSubsets(subsetPrefix);
484
+ }
485
+ else {
486
+ this.cursor = start;
487
+ }
488
+ const before = this.tagWhitespace();
489
+ this.expect(">");
490
+ return {
491
+ kind: tree_1.Xml.Kind.DocTypeDecl,
492
+ id: (0, uuid_1.randomId)(),
493
+ prefix,
494
+ markers: markers_1.emptyMarkers,
495
+ name,
496
+ documentDeclaration,
497
+ externalId,
498
+ internalSubset,
499
+ externalSubsets,
500
+ beforeTagDelimiterPrefix: before
501
+ };
502
+ }
503
+ parseExternalSubsets(prefix) {
504
+ this.cursor += "[".length;
505
+ const elements = [];
506
+ while (true) {
507
+ const elementPrefix = this.tagWhitespace();
508
+ if (this.startsWith("]")) {
509
+ if (elements.length > 0) {
510
+ elements[elements.length - 1] = Object.assign(Object.assign({}, elements[elements.length - 1]), { beforeTagDelimiterPrefix: elementPrefix });
511
+ }
512
+ else if (elementPrefix !== "") {
513
+ throw this.error("An empty internal subset has no element to hold its whitespace");
514
+ }
515
+ this.cursor += "]".length;
516
+ break;
517
+ }
518
+ const start = this.cursor;
519
+ this.skipMarkupDecl();
520
+ elements.push({
521
+ kind: tree_1.Xml.Kind.Element,
522
+ id: (0, uuid_1.randomId)(),
523
+ prefix: "",
524
+ markers: markers_1.emptyMarkers,
525
+ subset: [this.ident(elementPrefix, this.source.substring(start, this.cursor))],
526
+ beforeTagDelimiterPrefix: ""
527
+ });
528
+ }
529
+ return {
530
+ kind: tree_1.Xml.Kind.DocTypeDeclExternalSubsets,
531
+ id: (0, uuid_1.randomId)(),
532
+ prefix,
533
+ markers: markers_1.emptyMarkers,
534
+ elements
535
+ };
536
+ }
537
+ /**
538
+ * Moves past a markup declaration, comment, processing instruction or parameter entity reference
539
+ * of a document type declaration's internal subset, which the tree keeps as text.
540
+ */
541
+ skipMarkupDecl() {
542
+ if (this.startsWith("<!--")) {
543
+ this.cursor = this.indexOf("-->", this.cursor + "<!--".length, "<!--") + "-->".length;
544
+ }
545
+ else if (this.startsWith("<?")) {
546
+ this.cursor = this.indexOf("?>", this.cursor + "<?".length, "<?") + "?>".length;
547
+ }
548
+ else if (this.startsWith("<!")) {
549
+ const start = this.cursor;
550
+ this.cursor += "<!".length;
551
+ while (true) {
552
+ const c = this.source[this.cursor];
553
+ if (c === undefined) {
554
+ throw this.error("Unterminated markup declaration", start);
555
+ }
556
+ else if (c === ">") {
557
+ this.cursor++;
558
+ return;
559
+ }
560
+ else if (c === '"' || c === "'" || c === "[") {
561
+ const end = this.source.indexOf(c === "[" ? "]" : c, this.cursor + 1);
562
+ if (end < 0) {
563
+ throw this.error("Unterminated markup declaration", start);
564
+ }
565
+ this.cursor = end + 1;
566
+ }
567
+ else {
568
+ this.cursor++;
569
+ }
570
+ }
571
+ }
572
+ else {
573
+ PARAMETER_ENTITY_REFERENCE_PATTERN.lastIndex = this.cursor;
574
+ const reference = PARAMETER_ENTITY_REFERENCE_PATTERN.exec(this.source);
575
+ if (!reference) {
576
+ throw this.error("Expected a markup declaration in the internal subset");
577
+ }
578
+ this.cursor += reference[0].length;
579
+ }
580
+ }
581
+ charData(text, prefix) {
582
+ let start = 0;
583
+ while (start < text.length && isJavaWhitespace(text.charCodeAt(start))) {
584
+ start++;
585
+ }
586
+ let end = text.length;
587
+ while (end > start && isJavaWhitespace(text.charCodeAt(end - 1))) {
588
+ end--;
589
+ }
590
+ return {
591
+ kind: tree_1.Xml.Kind.CharData,
592
+ id: (0, uuid_1.randomId)(),
593
+ prefix: prefix + text.substring(0, start),
594
+ markers: markers_1.emptyMarkers,
595
+ cdata: false,
596
+ text: text.substring(start, end),
597
+ afterText: text.substring(end)
598
+ };
599
+ }
600
+ ident(prefix, name) {
601
+ return {
602
+ kind: tree_1.Xml.Kind.Ident,
603
+ id: (0, uuid_1.randomId)(),
604
+ prefix,
605
+ markers: markers_1.emptyMarkers,
606
+ name
607
+ };
608
+ }
609
+ name() {
610
+ NAME_PATTERN.lastIndex = this.cursor;
611
+ const match = NAME_PATTERN.exec(this.source);
612
+ if (!match) {
613
+ throw this.error("Expected a name");
614
+ }
615
+ this.cursor += match[0].length;
616
+ return match[0];
617
+ }
618
+ startsWithName() {
619
+ NAME_START_PATTERN.lastIndex = this.cursor;
620
+ return NAME_START_PATTERN.test(this.source);
621
+ }
622
+ startsWithTag() {
623
+ if (this.source[this.cursor] !== "<") {
624
+ return false;
625
+ }
626
+ NAME_START_PATTERN.lastIndex = this.cursor + 1;
627
+ return NAME_START_PATTERN.test(this.source);
628
+ }
629
+ startsWithXmlDecl() {
630
+ if (!this.startsWith("<?xml")) {
631
+ return false;
632
+ }
633
+ NAME_CHAR_PATTERN.lastIndex = this.cursor + "<?xml".length;
634
+ return !NAME_CHAR_PATTERN.test(this.source);
635
+ }
636
+ startsWithDocTypeDecl() {
637
+ return this.startsWith("<!") &&
638
+ this.source.substring(this.cursor + 2, this.cursor + 2 + "DOCTYPE".length).toUpperCase() === "DOCTYPE";
639
+ }
640
+ tagWhitespace() {
641
+ TAG_WHITESPACE.lastIndex = this.cursor;
642
+ const whitespace = TAG_WHITESPACE.exec(this.source)[0];
643
+ this.cursor += whitespace.length;
644
+ return whitespace;
645
+ }
646
+ markupWhitespace() {
647
+ const start = this.cursor;
648
+ while (this.cursor < this.source.length && " \t\n\r\f".includes(this.source[this.cursor])) {
649
+ this.cursor++;
650
+ }
651
+ return this.source.substring(start, this.cursor);
652
+ }
653
+ startsWith(s) {
654
+ return this.source.startsWith(s, this.cursor);
655
+ }
656
+ expect(s) {
657
+ if (!this.startsWith(s)) {
658
+ throw this.error(`Expected '${s}'`);
659
+ }
660
+ this.cursor += s.length;
661
+ }
662
+ indexOf(s, from, opened) {
663
+ const index = this.source.indexOf(s, from);
664
+ if (index < 0) {
665
+ throw this.error(`Expected '${s}' to close '${opened}'`);
666
+ }
667
+ return index;
668
+ }
669
+ error(message, at = this.cursor) {
670
+ const before = this.source.substring(0, at);
671
+ const line = before.split("\n").length;
672
+ const column = at - before.lastIndexOf("\n");
673
+ return new Error(`${message} at line ${line}, column ${column}`);
674
+ }
675
+ }
676
+ parser_1.Parsers.registerParser("xml", XmlParser);
677
+ //# sourceMappingURL=parser.js.map