@openrewrite/rewrite 8.99.1 → 8.100.1
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/rpc.d.ts.map +1 -1
- package/dist/java/rpc.js +6 -1
- package/dist/java/rpc.js.map +1 -1
- package/dist/javascript/add-import.d.ts.map +1 -1
- package/dist/javascript/add-import.js +3 -0
- package/dist/javascript/add-import.js.map +1 -1
- package/dist/javascript/binding.d.ts.map +1 -1
- package/dist/javascript/binding.js +8 -1
- package/dist/javascript/binding.js.map +1 -1
- package/dist/javascript/comparator.js +2 -1
- package/dist/javascript/comparator.js.map +1 -1
- package/dist/javascript/format/format.d.ts.map +1 -1
- package/dist/javascript/format/format.js +3 -2
- package/dist/javascript/format/format.js.map +1 -1
- package/dist/javascript/format/minimum-viable-spacing-visitor.js +3 -3
- package/dist/javascript/format/minimum-viable-spacing-visitor.js.map +1 -1
- package/dist/javascript/parser.d.ts.map +1 -1
- package/dist/javascript/parser.js +11 -17
- package/dist/javascript/parser.js.map +1 -1
- package/dist/javascript/print.d.ts +4 -0
- package/dist/javascript/print.d.ts.map +1 -1
- package/dist/javascript/print.js +82 -20
- package/dist/javascript/print.js.map +1 -1
- package/dist/javascript/rpc.js +2 -0
- package/dist/javascript/rpc.js.map +1 -1
- package/dist/javascript/scope.d.ts.map +1 -1
- package/dist/javascript/scope.js +4 -1
- package/dist/javascript/scope.js.map +1 -1
- package/dist/javascript/templating/comparator.d.ts +9 -0
- package/dist/javascript/templating/comparator.d.ts.map +1 -1
- package/dist/javascript/templating/comparator.js +21 -0
- package/dist/javascript/templating/comparator.js.map +1 -1
- package/dist/javascript/templating/debug.d.ts.map +1 -1
- package/dist/javascript/templating/debug.js +2 -0
- package/dist/javascript/templating/debug.js.map +1 -1
- package/dist/javascript/templating/precedence.d.ts.map +1 -1
- package/dist/javascript/templating/precedence.js +36 -39
- package/dist/javascript/templating/precedence.js.map +1 -1
- package/dist/javascript/templating/types.d.ts +4 -4
- package/dist/javascript/tree-debug.d.ts.map +1 -1
- package/dist/javascript/tree-debug.js +6 -3
- package/dist/javascript/tree-debug.js.map +1 -1
- package/dist/javascript/tree.d.ts +4 -1
- package/dist/javascript/tree.d.ts.map +1 -1
- package/dist/javascript/tree.js +2 -0
- package/dist/javascript/tree.js.map +1 -1
- package/dist/javascript/type-mapping.d.ts +1 -0
- package/dist/javascript/type-mapping.d.ts.map +1 -1
- package/dist/javascript/type-mapping.js +12 -1
- package/dist/javascript/type-mapping.js.map +1 -1
- package/dist/json/edit.d.ts +150 -0
- package/dist/json/edit.d.ts.map +1 -0
- package/dist/json/edit.js +667 -0
- package/dist/json/edit.js.map +1 -0
- 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/parser.d.ts +1 -1
- package/dist/parser.d.ts.map +1 -1
- package/dist/print.d.ts +16 -0
- package/dist/print.d.ts.map +1 -1
- package/dist/print.js +33 -0
- package/dist/print.js.map +1 -1
- package/dist/rewrite-javascript-version.txt +1 -1
- package/dist/rpc/request/get-languages.d.ts.map +1 -1
- package/dist/rpc/request/get-languages.js +1 -0
- package/dist/rpc/request/get-languages.js.map +1 -1
- package/dist/rpc/request/parse.d.ts.map +1 -1
- package/dist/rpc/request/parse.js +4 -0
- package/dist/rpc/request/parse.js.map +1 -1
- package/dist/rpc/server.d.ts +1 -0
- package/dist/rpc/server.d.ts.map +1 -1
- package/dist/rpc/server.js +1 -0
- package/dist/rpc/server.js.map +1 -1
- package/dist/xml/assertions.d.ts +4 -0
- package/dist/xml/assertions.d.ts.map +1 -0
- package/dist/xml/assertions.js +31 -0
- package/dist/xml/assertions.js.map +1 -0
- package/dist/xml/index.d.ts +8 -0
- package/dist/xml/index.d.ts.map +1 -0
- package/dist/xml/index.js +39 -0
- package/dist/xml/index.js.map +1 -0
- package/dist/xml/namespaces.d.ts +36 -0
- package/dist/xml/namespaces.d.ts.map +1 -0
- package/dist/xml/namespaces.js +98 -0
- package/dist/xml/namespaces.js.map +1 -0
- package/dist/xml/parser.d.ts +15 -0
- package/dist/xml/parser.d.ts.map +1 -0
- package/dist/xml/parser.js +677 -0
- package/dist/xml/parser.js.map +1 -0
- package/dist/xml/print.d.ts +2 -0
- package/dist/xml/print.d.ts.map +1 -0
- package/dist/xml/print.js +209 -0
- package/dist/xml/print.js.map +1 -0
- package/dist/xml/rpc.d.ts +2 -0
- package/dist/xml/rpc.d.ts.map +1 -0
- package/dist/xml/rpc.js +313 -0
- package/dist/xml/rpc.js.map +1 -0
- package/dist/xml/tree.d.ts +244 -0
- package/dist/xml/tree.d.ts.map +1 -0
- package/dist/xml/tree.js +265 -0
- package/dist/xml/tree.js.map +1 -0
- package/dist/xml/visitor.d.ts +35 -0
- package/dist/xml/visitor.d.ts.map +1 -0
- package/dist/xml/visitor.js +203 -0
- package/dist/xml/visitor.js.map +1 -0
- package/package.json +5 -1
- package/src/java/rpc.ts +6 -1
- package/src/javascript/add-import.ts +3 -0
- package/src/javascript/binding.ts +10 -1
- package/src/javascript/comparator.ts +2 -1
- package/src/javascript/format/format.ts +3 -2
- package/src/javascript/format/minimum-viable-spacing-visitor.ts +3 -3
- package/src/javascript/parser.ts +12 -17
- package/src/javascript/print.ts +85 -23
- package/src/javascript/rpc.ts +2 -0
- package/src/javascript/scope.ts +4 -1
- package/src/javascript/templating/comparator.ts +23 -0
- package/src/javascript/templating/debug.ts +1 -0
- package/src/javascript/templating/precedence.ts +17 -20
- package/src/javascript/templating/types.ts +4 -4
- package/src/javascript/tree-debug.ts +7 -3
- package/src/javascript/tree.ts +4 -1
- package/src/javascript/type-mapping.ts +15 -1
- package/src/json/edit.ts +750 -0
- package/src/json/index.ts +1 -0
- package/src/parser.ts +1 -1
- package/src/print.ts +42 -0
- package/src/rpc/request/get-languages.ts +1 -0
- package/src/rpc/request/parse.ts +3 -0
- package/src/rpc/server.ts +1 -0
- package/src/xml/assertions.ts +28 -0
- package/src/xml/index.ts +23 -0
- package/src/xml/namespaces.ts +109 -0
- package/src/xml/parser.ts +692 -0
- package/src/xml/print.ts +225 -0
- package/src/xml/rpc.ts +370 -0
- package/src/xml/tree.ts +455 -0
- package/src/xml/visitor.ts +227 -0
package/src/xml/tree.ts
ADDED
|
@@ -0,0 +1,455 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* Copyright 2026 the original author or authors.
|
|
3
|
+
* <p>
|
|
4
|
+
* Licensed under the Moderne Source Available License (the "License");
|
|
5
|
+
* you may not use this file except in compliance with the License.
|
|
6
|
+
* You may obtain a copy of the License at
|
|
7
|
+
* <p>
|
|
8
|
+
* https://docs.moderne.io/licensing/moderne-source-available-license
|
|
9
|
+
* <p>
|
|
10
|
+
* Unless required by applicable law or agreed to in writing, software
|
|
11
|
+
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
12
|
+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
13
|
+
* See the License for the specific language governing permissions and
|
|
14
|
+
* limitations under the License.
|
|
15
|
+
*/
|
|
16
|
+
import {emptyMarkers, Markers} from "../markers";
|
|
17
|
+
import {SourceFile, Tree} from "../tree";
|
|
18
|
+
import {randomId} from "../uuid";
|
|
19
|
+
|
|
20
|
+
/**
|
|
21
|
+
* An XML LST element, mirroring `org.openrewrite.xml.tree.Xml`. Every element keeps the
|
|
22
|
+
* whitespace before it in `prefix`, so that a document prints back exactly as it was parsed.
|
|
23
|
+
*/
|
|
24
|
+
export interface Xml extends Tree {
|
|
25
|
+
readonly prefix: string
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
export namespace Xml {
|
|
29
|
+
export const Kind = {
|
|
30
|
+
Document: "org.openrewrite.xml.tree.Xml$Document",
|
|
31
|
+
Prolog: "org.openrewrite.xml.tree.Xml$Prolog",
|
|
32
|
+
XmlDecl: "org.openrewrite.xml.tree.Xml$XmlDecl",
|
|
33
|
+
ProcessingInstruction: "org.openrewrite.xml.tree.Xml$ProcessingInstruction",
|
|
34
|
+
Tag: "org.openrewrite.xml.tree.Xml$Tag",
|
|
35
|
+
TagClosing: "org.openrewrite.xml.tree.Xml$Tag$Closing",
|
|
36
|
+
Attribute: "org.openrewrite.xml.tree.Xml$Attribute",
|
|
37
|
+
AttributeValue: "org.openrewrite.xml.tree.Xml$Attribute$Value",
|
|
38
|
+
CharData: "org.openrewrite.xml.tree.Xml$CharData",
|
|
39
|
+
Comment: "org.openrewrite.xml.tree.Xml$Comment",
|
|
40
|
+
DocTypeDecl: "org.openrewrite.xml.tree.Xml$DocTypeDecl",
|
|
41
|
+
DocTypeDeclExternalSubsets: "org.openrewrite.xml.tree.Xml$DocTypeDecl$ExternalSubsets",
|
|
42
|
+
Element: "org.openrewrite.xml.tree.Xml$Element",
|
|
43
|
+
Ident: "org.openrewrite.xml.tree.Xml$Ident",
|
|
44
|
+
JspDirective: "org.openrewrite.xml.tree.Xml$JspDirective",
|
|
45
|
+
JspScriptlet: "org.openrewrite.xml.tree.Xml$JspScriptlet",
|
|
46
|
+
JspExpression: "org.openrewrite.xml.tree.Xml$JspExpression",
|
|
47
|
+
JspDeclaration: "org.openrewrite.xml.tree.Xml$JspDeclaration",
|
|
48
|
+
JspComment: "org.openrewrite.xml.tree.Xml$JspComment",
|
|
49
|
+
} as const;
|
|
50
|
+
|
|
51
|
+
export const MarkersKind = {
|
|
52
|
+
/**
|
|
53
|
+
* An HTML void element such as `<br>`, which prints with a bare `>` and has neither content nor a closing tag.
|
|
54
|
+
*/
|
|
55
|
+
HtmlVoidElement: "org.openrewrite.xml.marker.HtmlVoidElement",
|
|
56
|
+
} as const;
|
|
57
|
+
|
|
58
|
+
/**
|
|
59
|
+
* What may appear between a tag's start and end, mirroring `org.openrewrite.xml.tree.Content`.
|
|
60
|
+
*/
|
|
61
|
+
export type Content = Tag | CharData | Comment | ProcessingInstruction |
|
|
62
|
+
JspDirective | JspScriptlet | JspExpression | JspDeclaration | JspComment;
|
|
63
|
+
|
|
64
|
+
/**
|
|
65
|
+
* What may appear in the prolog, mirroring `org.openrewrite.xml.tree.Misc`.
|
|
66
|
+
*/
|
|
67
|
+
export type Misc = Comment | DocTypeDecl | ProcessingInstruction | XmlDecl | JspDeclaration | JspComment;
|
|
68
|
+
|
|
69
|
+
export interface Document extends SourceFile, Xml {
|
|
70
|
+
readonly kind: typeof Kind.Document
|
|
71
|
+
readonly prolog: Prolog
|
|
72
|
+
/**
|
|
73
|
+
* Absent from a document without a root element, such as an empty file or one holding only a prolog,
|
|
74
|
+
* which the Java parser accepts as well.
|
|
75
|
+
*/
|
|
76
|
+
readonly root?: Tag
|
|
77
|
+
readonly eof: string
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
export interface Prolog extends Xml {
|
|
81
|
+
readonly kind: typeof Kind.Prolog
|
|
82
|
+
readonly xmlDecl?: XmlDecl
|
|
83
|
+
readonly misc: Misc[]
|
|
84
|
+
readonly jspDirectives: JspDirective[]
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
export interface XmlDecl extends Xml {
|
|
88
|
+
readonly kind: typeof Kind.XmlDecl
|
|
89
|
+
readonly name: string
|
|
90
|
+
readonly attributes: Attribute[]
|
|
91
|
+
/**
|
|
92
|
+
* The space before `?>`.
|
|
93
|
+
*/
|
|
94
|
+
readonly beforeTagDelimiterPrefix: string
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
export interface ProcessingInstruction extends Xml {
|
|
98
|
+
readonly kind: typeof Kind.ProcessingInstruction
|
|
99
|
+
readonly name: string
|
|
100
|
+
/**
|
|
101
|
+
* The instruction's data, absent from `<?target?>`.
|
|
102
|
+
*/
|
|
103
|
+
readonly processingInstructions?: CharData
|
|
104
|
+
/**
|
|
105
|
+
* The space before `?>`.
|
|
106
|
+
*/
|
|
107
|
+
readonly beforeTagDelimiterPrefix: string
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
export interface Tag extends Xml {
|
|
111
|
+
readonly kind: typeof Kind.Tag
|
|
112
|
+
readonly name: string
|
|
113
|
+
readonly attributes: Attribute[]
|
|
114
|
+
/**
|
|
115
|
+
* Absent from a self-closing tag such as `<a/>`, and empty for `<a></a>`.
|
|
116
|
+
*/
|
|
117
|
+
readonly content?: Content[]
|
|
118
|
+
/**
|
|
119
|
+
* Absent from a self-closing tag.
|
|
120
|
+
*/
|
|
121
|
+
readonly closing?: TagClosing
|
|
122
|
+
/**
|
|
123
|
+
* The space before `>` or `/>`.
|
|
124
|
+
*/
|
|
125
|
+
readonly beforeTagDelimiterPrefix: string
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
export interface TagClosing extends Xml {
|
|
129
|
+
readonly kind: typeof Kind.TagClosing
|
|
130
|
+
readonly name: string
|
|
131
|
+
/**
|
|
132
|
+
* The space before `>`.
|
|
133
|
+
*/
|
|
134
|
+
readonly beforeTagDelimiterPrefix: string
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
export interface Attribute extends Xml {
|
|
138
|
+
readonly kind: typeof Kind.Attribute
|
|
139
|
+
readonly key: Ident
|
|
140
|
+
readonly beforeEquals: string
|
|
141
|
+
readonly value: AttributeValue
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
export type Quote = "Double" | "Single";
|
|
145
|
+
|
|
146
|
+
export interface AttributeValue extends Xml {
|
|
147
|
+
readonly kind: typeof Kind.AttributeValue
|
|
148
|
+
readonly quote: Quote
|
|
149
|
+
/**
|
|
150
|
+
* The text between the quotes, with any entity references left as written.
|
|
151
|
+
*/
|
|
152
|
+
readonly value: string
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
export interface CharData extends Xml {
|
|
156
|
+
readonly kind: typeof Kind.CharData
|
|
157
|
+
readonly cdata: boolean
|
|
158
|
+
readonly text: string
|
|
159
|
+
readonly afterText: string
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
export interface Comment extends Xml {
|
|
163
|
+
readonly kind: typeof Kind.Comment
|
|
164
|
+
readonly text: string
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
export interface DocTypeDecl extends Xml {
|
|
168
|
+
readonly kind: typeof Kind.DocTypeDecl
|
|
169
|
+
readonly name: Ident
|
|
170
|
+
/**
|
|
171
|
+
* The `DOCTYPE` keyword as written, which may be in any case.
|
|
172
|
+
*/
|
|
173
|
+
readonly documentDeclaration: string
|
|
174
|
+
readonly externalId?: Ident
|
|
175
|
+
readonly internalSubset: Ident[]
|
|
176
|
+
readonly externalSubsets?: DocTypeDeclExternalSubsets
|
|
177
|
+
/**
|
|
178
|
+
* The space before `>`.
|
|
179
|
+
*/
|
|
180
|
+
readonly beforeTagDelimiterPrefix: string
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
export interface DocTypeDeclExternalSubsets extends Xml {
|
|
184
|
+
readonly kind: typeof Kind.DocTypeDeclExternalSubsets
|
|
185
|
+
readonly elements: Element[]
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
export interface Element extends Xml {
|
|
189
|
+
readonly kind: typeof Kind.Element
|
|
190
|
+
readonly subset: Ident[]
|
|
191
|
+
/**
|
|
192
|
+
* The space before `]` on the last element of a subset.
|
|
193
|
+
*/
|
|
194
|
+
readonly beforeTagDelimiterPrefix: string
|
|
195
|
+
}
|
|
196
|
+
|
|
197
|
+
export interface Ident extends Xml {
|
|
198
|
+
readonly kind: typeof Kind.Ident
|
|
199
|
+
readonly name: string
|
|
200
|
+
}
|
|
201
|
+
|
|
202
|
+
export interface JspDirective extends Xml {
|
|
203
|
+
readonly kind: typeof Kind.JspDirective
|
|
204
|
+
readonly beforeTypePrefix: string
|
|
205
|
+
readonly type: string
|
|
206
|
+
readonly attributes: Attribute[]
|
|
207
|
+
/**
|
|
208
|
+
* The space before `%>`.
|
|
209
|
+
*/
|
|
210
|
+
readonly beforeDirectiveEndPrefix: string
|
|
211
|
+
}
|
|
212
|
+
|
|
213
|
+
export interface JspScriptlet extends Xml {
|
|
214
|
+
readonly kind: typeof Kind.JspScriptlet
|
|
215
|
+
readonly content: string
|
|
216
|
+
}
|
|
217
|
+
|
|
218
|
+
export interface JspExpression extends Xml {
|
|
219
|
+
readonly kind: typeof Kind.JspExpression
|
|
220
|
+
readonly content: string
|
|
221
|
+
}
|
|
222
|
+
|
|
223
|
+
export interface JspDeclaration extends Xml {
|
|
224
|
+
readonly kind: typeof Kind.JspDeclaration
|
|
225
|
+
readonly content: string
|
|
226
|
+
}
|
|
227
|
+
|
|
228
|
+
export interface JspComment extends Xml {
|
|
229
|
+
readonly kind: typeof Kind.JspComment
|
|
230
|
+
readonly content: string
|
|
231
|
+
}
|
|
232
|
+
}
|
|
233
|
+
|
|
234
|
+
const xmlKindValues = new Set<string>(Object.values(Xml.Kind));
|
|
235
|
+
|
|
236
|
+
export function isXml(tree: any): tree is Xml {
|
|
237
|
+
return xmlKindValues.has(tree?.["kind"]);
|
|
238
|
+
}
|
|
239
|
+
|
|
240
|
+
export function isDocument(xml: Xml): xml is Xml.Document {
|
|
241
|
+
return xml.kind === Xml.Kind.Document;
|
|
242
|
+
}
|
|
243
|
+
|
|
244
|
+
export function isTag(xml: Xml): xml is Xml.Tag {
|
|
245
|
+
return xml.kind === Xml.Kind.Tag;
|
|
246
|
+
}
|
|
247
|
+
|
|
248
|
+
export function isAttribute(xml: Xml): xml is Xml.Attribute {
|
|
249
|
+
return xml.kind === Xml.Kind.Attribute;
|
|
250
|
+
}
|
|
251
|
+
|
|
252
|
+
export function isCharData(xml: Xml): xml is Xml.CharData {
|
|
253
|
+
return xml.kind === Xml.Kind.CharData;
|
|
254
|
+
}
|
|
255
|
+
|
|
256
|
+
export function isComment(xml: Xml): xml is Xml.Comment {
|
|
257
|
+
return xml.kind === Xml.Kind.Comment;
|
|
258
|
+
}
|
|
259
|
+
|
|
260
|
+
/**
|
|
261
|
+
* The child tags of a tag, optionally only those with the given name.
|
|
262
|
+
*/
|
|
263
|
+
export function getChildren(tag: Xml.Tag, name?: string): Xml.Tag[] {
|
|
264
|
+
return (tag.content ?? []).filter((c): c is Xml.Tag => isTag(c) && (name === undefined || c.name === name));
|
|
265
|
+
}
|
|
266
|
+
|
|
267
|
+
export function getChild(tag: Xml.Tag, name: string): Xml.Tag | undefined {
|
|
268
|
+
return tag.content?.find((c): c is Xml.Tag => isTag(c) && c.name === name);
|
|
269
|
+
}
|
|
270
|
+
|
|
271
|
+
/**
|
|
272
|
+
* The character data of a tag whose content is only character data, as `Xml.Tag.getValue()` returns it.
|
|
273
|
+
*/
|
|
274
|
+
export function getValue(tag: Xml.Tag): string | undefined {
|
|
275
|
+
if (!tag.content) {
|
|
276
|
+
return undefined;
|
|
277
|
+
}
|
|
278
|
+
if (tag.content.length === 1 && isCharData(tag.content[0])) {
|
|
279
|
+
return tag.content[0].text;
|
|
280
|
+
}
|
|
281
|
+
if (tag.content.every(isCharData)) {
|
|
282
|
+
return tag.content.map(c => unescapeXml((c as Xml.CharData).text)).join("");
|
|
283
|
+
}
|
|
284
|
+
return undefined;
|
|
285
|
+
}
|
|
286
|
+
|
|
287
|
+
export function getChildValue(tag: Xml.Tag, name: string): string | undefined {
|
|
288
|
+
const child = getChild(tag, name);
|
|
289
|
+
return child && getValue(child);
|
|
290
|
+
}
|
|
291
|
+
|
|
292
|
+
export function getAttribute(tag: Xml.Tag, key: string): Xml.Attribute | undefined {
|
|
293
|
+
return tag.attributes.find(a => a.key.name === key);
|
|
294
|
+
}
|
|
295
|
+
|
|
296
|
+
/**
|
|
297
|
+
* The value of an attribute as written between its quotes, with entity references left as they are.
|
|
298
|
+
*/
|
|
299
|
+
export function getAttributeValue(tag: Xml.Tag, key: string): string | undefined {
|
|
300
|
+
return getAttribute(tag, key)?.value.value;
|
|
301
|
+
}
|
|
302
|
+
|
|
303
|
+
/**
|
|
304
|
+
* The tag renamed, together with its closing tag, like `Xml.Tag.withName(String)`.
|
|
305
|
+
*/
|
|
306
|
+
export function withName(tag: Xml.Tag, name: string): Xml.Tag {
|
|
307
|
+
if (name !== name.trim()) {
|
|
308
|
+
throw new Error("Tag name must not contain leading or trailing whitespace");
|
|
309
|
+
}
|
|
310
|
+
if (tag.name === name) {
|
|
311
|
+
return tag;
|
|
312
|
+
}
|
|
313
|
+
return {
|
|
314
|
+
...tag,
|
|
315
|
+
name,
|
|
316
|
+
closing: tag.closing && {...tag.closing, name}
|
|
317
|
+
};
|
|
318
|
+
}
|
|
319
|
+
|
|
320
|
+
/**
|
|
321
|
+
* The tag with the value of an existing attribute replaced, or the tag itself when it has no such attribute.
|
|
322
|
+
* The value is plain text, escaped here for the quotes the attribute already uses.
|
|
323
|
+
*/
|
|
324
|
+
export function withAttributeValue(tag: Xml.Tag, key: string, value: string): Xml.Tag {
|
|
325
|
+
const index = tag.attributes.findIndex(a => a.key.name === key);
|
|
326
|
+
if (index === -1) {
|
|
327
|
+
return tag;
|
|
328
|
+
}
|
|
329
|
+
const attribute = tag.attributes[index];
|
|
330
|
+
const escaped = escapeXml(value, attribute.value.quote);
|
|
331
|
+
if (attribute.value.value === escaped) {
|
|
332
|
+
return tag;
|
|
333
|
+
}
|
|
334
|
+
const attributes = [...tag.attributes];
|
|
335
|
+
attributes[index] = {...attribute, value: {...attribute.value, value: escaped}};
|
|
336
|
+
return {...tag, attributes};
|
|
337
|
+
}
|
|
338
|
+
|
|
339
|
+
/**
|
|
340
|
+
* The tag with an attribute added after its last one, or with the value replaced when it has the attribute
|
|
341
|
+
* already. The value is plain text and gets escaped. The new attribute takes the whitespace before the last
|
|
342
|
+
* attribute, so that it lines up with attributes written one per line, or a single space on a tag without any.
|
|
343
|
+
*/
|
|
344
|
+
export function addAttribute(tag: Xml.Tag, key: string, value: string, quote: Xml.Quote = "Double"): Xml.Tag {
|
|
345
|
+
if (tag.attributes.some(a => a.key.name === key)) {
|
|
346
|
+
return withAttributeValue(tag, key, value);
|
|
347
|
+
}
|
|
348
|
+
const last = tag.attributes[tag.attributes.length - 1];
|
|
349
|
+
const attribute: Xml.Attribute = {
|
|
350
|
+
kind: Xml.Kind.Attribute,
|
|
351
|
+
id: randomId(),
|
|
352
|
+
prefix: last?.prefix ?? " ",
|
|
353
|
+
markers: emptyMarkers,
|
|
354
|
+
key: {kind: Xml.Kind.Ident, id: randomId(), prefix: "", markers: emptyMarkers, name: key},
|
|
355
|
+
beforeEquals: "",
|
|
356
|
+
value: {
|
|
357
|
+
kind: Xml.Kind.AttributeValue,
|
|
358
|
+
id: randomId(),
|
|
359
|
+
prefix: "",
|
|
360
|
+
markers: emptyMarkers,
|
|
361
|
+
quote,
|
|
362
|
+
value: escapeXml(value, quote)
|
|
363
|
+
}
|
|
364
|
+
};
|
|
365
|
+
return {...tag, attributes: [...tag.attributes, attribute]};
|
|
366
|
+
}
|
|
367
|
+
|
|
368
|
+
export function removeAttribute(tag: Xml.Tag, key: string): Xml.Tag {
|
|
369
|
+
const attributes = tag.attributes.filter(a => a.key.name !== key);
|
|
370
|
+
return attributes.length === tag.attributes.length ? tag : {...tag, attributes};
|
|
371
|
+
}
|
|
372
|
+
|
|
373
|
+
/**
|
|
374
|
+
* The tag with its content replaced by the given text, like `Xml.Tag.withValue(String)`. The text is
|
|
375
|
+
* written as is, so a `<` or `&` in it has to be escaped first, for example with {@link escapeXml}.
|
|
376
|
+
*/
|
|
377
|
+
export function withValue(tag: Xml.Tag, value: string): Xml.Tag {
|
|
378
|
+
const content = tag.content;
|
|
379
|
+
if (content?.length === 1 && isCharData(content[0]) && content[0].text === value) {
|
|
380
|
+
return tag;
|
|
381
|
+
}
|
|
382
|
+
const charData: Xml.CharData = content && content.length > 0 && isCharData(content[0]) ?
|
|
383
|
+
{...content[0], text: value} :
|
|
384
|
+
{
|
|
385
|
+
kind: Xml.Kind.CharData,
|
|
386
|
+
id: randomId(),
|
|
387
|
+
prefix: "",
|
|
388
|
+
markers: emptyMarkers,
|
|
389
|
+
cdata: false,
|
|
390
|
+
text: value,
|
|
391
|
+
afterText: ""
|
|
392
|
+
};
|
|
393
|
+
return {
|
|
394
|
+
...tag,
|
|
395
|
+
markers: tag.markers.markers.some(m => m.kind === Xml.MarkersKind.HtmlVoidElement) ? {
|
|
396
|
+
...tag.markers,
|
|
397
|
+
markers: tag.markers.markers.filter(m => m.kind !== Xml.MarkersKind.HtmlVoidElement)
|
|
398
|
+
} : tag.markers,
|
|
399
|
+
content: [charData],
|
|
400
|
+
closing: tag.closing ?? {
|
|
401
|
+
kind: Xml.Kind.TagClosing,
|
|
402
|
+
id: randomId(),
|
|
403
|
+
prefix: "",
|
|
404
|
+
markers: emptyMarkers,
|
|
405
|
+
name: tag.name,
|
|
406
|
+
beforeTagDelimiterPrefix: ""
|
|
407
|
+
}
|
|
408
|
+
};
|
|
409
|
+
}
|
|
410
|
+
|
|
411
|
+
const XML_ENTITIES: { [name: string]: string } = {
|
|
412
|
+
amp: "&",
|
|
413
|
+
lt: "<",
|
|
414
|
+
gt: ">",
|
|
415
|
+
quot: "\"",
|
|
416
|
+
apos: "'"
|
|
417
|
+
};
|
|
418
|
+
|
|
419
|
+
/**
|
|
420
|
+
* Escapes plain text for XML. Without a `quote`, it is escaped for character data, where `&`, `<` and `>`
|
|
421
|
+
* become entity references. Given the quotes of an attribute value, `&`, `<` and that quote character are
|
|
422
|
+
* escaped, and a `>` is left as it is, as in a UI5 expression binding such as `{= ${a} > 1 }`.
|
|
423
|
+
*/
|
|
424
|
+
export function escapeXml(text: string, quote?: Xml.Quote): string {
|
|
425
|
+
const pattern = quote === "Double" ? /[&<"]/g : quote === "Single" ? /[&<']/g : /[&<>]/g;
|
|
426
|
+
return text.replace(pattern, c => {
|
|
427
|
+
switch (c) {
|
|
428
|
+
case "&":
|
|
429
|
+
return "&";
|
|
430
|
+
case "<":
|
|
431
|
+
return "<";
|
|
432
|
+
case ">":
|
|
433
|
+
return ">";
|
|
434
|
+
case "\"":
|
|
435
|
+
return """;
|
|
436
|
+
default:
|
|
437
|
+
return "'";
|
|
438
|
+
}
|
|
439
|
+
});
|
|
440
|
+
}
|
|
441
|
+
|
|
442
|
+
/**
|
|
443
|
+
* Decodes the predefined entities and character references in XML text, the reverse of {@link escapeXml}.
|
|
444
|
+
* Use it on {@link getAttributeValue}, which returns a value as written, before passing it to
|
|
445
|
+
* {@link withAttributeValue} or {@link addAttribute}, which take plain text.
|
|
446
|
+
*/
|
|
447
|
+
export function unescapeXml(text: string): string {
|
|
448
|
+
return text.replace(/&(#x[0-9a-fA-F]+|#[0-9]+|[A-Za-z]+);/g, (entity, ref: string) => {
|
|
449
|
+
if (ref.startsWith("#")) {
|
|
450
|
+
const codePoint = ref.startsWith("#x") ? parseInt(ref.substring(2), 16) : parseInt(ref.substring(1), 10);
|
|
451
|
+
return codePoint <= 0x10FFFF ? String.fromCodePoint(codePoint) : entity;
|
|
452
|
+
}
|
|
453
|
+
return XML_ENTITIES[ref] ?? entity;
|
|
454
|
+
});
|
|
455
|
+
}
|
|
@@ -0,0 +1,227 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* Copyright 2026 the original author or authors.
|
|
3
|
+
* <p>
|
|
4
|
+
* Licensed under the Moderne Source Available License (the "License");
|
|
5
|
+
* you may not use this file except in compliance with the License.
|
|
6
|
+
* You may obtain a copy of the License at
|
|
7
|
+
* <p>
|
|
8
|
+
* https://docs.moderne.io/licensing/moderne-source-available-license
|
|
9
|
+
* <p>
|
|
10
|
+
* Unless required by applicable law or agreed to in writing, software
|
|
11
|
+
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
12
|
+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
13
|
+
* See the License for the specific language governing permissions and
|
|
14
|
+
* limitations under the License.
|
|
15
|
+
*/
|
|
16
|
+
import {mapAsync, updateIfChanged} from "../util";
|
|
17
|
+
import {TreeVisitor, ValidRecipeReturnType} from "../visitor";
|
|
18
|
+
import {SourceFile} from "../tree";
|
|
19
|
+
import {isXml, Xml} from "./tree";
|
|
20
|
+
import {create, Draft} from "mutative";
|
|
21
|
+
|
|
22
|
+
/**
|
|
23
|
+
* Visits an XML LST, mirroring `org.openrewrite.xml.XmlVisitor`.
|
|
24
|
+
*/
|
|
25
|
+
export class XmlVisitor<P> extends TreeVisitor<Xml, P> {
|
|
26
|
+
async isAcceptable(sourceFile: SourceFile): Promise<boolean> {
|
|
27
|
+
return isXml(sourceFile);
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
protected async visitDocument(document: Xml.Document, p: P): Promise<Xml | undefined> {
|
|
31
|
+
return updateIfChanged(document, {
|
|
32
|
+
markers: await this.visitMarkers(document.markers, p),
|
|
33
|
+
prolog: await this.visitDefined<Xml.Prolog>(document.prolog, p),
|
|
34
|
+
root: document.root && await this.visitDefined<Xml.Tag>(document.root, p)
|
|
35
|
+
});
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
protected async visitProlog(prolog: Xml.Prolog, p: P): Promise<Xml | undefined> {
|
|
39
|
+
return updateIfChanged(prolog, {
|
|
40
|
+
markers: await this.visitMarkers(prolog.markers, p),
|
|
41
|
+
xmlDecl: prolog.xmlDecl && await this.visit<Xml.XmlDecl>(prolog.xmlDecl, p),
|
|
42
|
+
misc: await mapAsync(prolog.misc, m => this.visit<Xml.Misc>(m, p)),
|
|
43
|
+
jspDirectives: await mapAsync(prolog.jspDirectives, j => this.visit<Xml.JspDirective>(j, p))
|
|
44
|
+
});
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
protected async visitXmlDecl(xmlDecl: Xml.XmlDecl, p: P): Promise<Xml | undefined> {
|
|
48
|
+
return updateIfChanged(xmlDecl, {
|
|
49
|
+
markers: await this.visitMarkers(xmlDecl.markers, p),
|
|
50
|
+
attributes: await mapAsync(xmlDecl.attributes, a => this.visit<Xml.Attribute>(a, p))
|
|
51
|
+
});
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
protected async visitProcessingInstruction(pi: Xml.ProcessingInstruction, p: P): Promise<Xml | undefined> {
|
|
55
|
+
return updateIfChanged(pi, {
|
|
56
|
+
markers: await this.visitMarkers(pi.markers, p),
|
|
57
|
+
processingInstructions: pi.processingInstructions &&
|
|
58
|
+
await this.visit<Xml.CharData>(pi.processingInstructions, p)
|
|
59
|
+
});
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
protected async visitTag(tag: Xml.Tag, p: P): Promise<Xml | undefined> {
|
|
63
|
+
return updateIfChanged(tag, {
|
|
64
|
+
markers: await this.visitMarkers(tag.markers, p),
|
|
65
|
+
attributes: await mapAsync(tag.attributes, a => this.visit<Xml.Attribute>(a, p)),
|
|
66
|
+
content: tag.content && await mapAsync(tag.content, c => this.visit<Xml.Content>(c, p)),
|
|
67
|
+
closing: tag.closing && await this.visit<Xml.TagClosing>(tag.closing, p)
|
|
68
|
+
});
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
protected async visitTagClosing(closing: Xml.TagClosing, p: P): Promise<Xml | undefined> {
|
|
72
|
+
return updateIfChanged(closing, {
|
|
73
|
+
markers: await this.visitMarkers(closing.markers, p)
|
|
74
|
+
});
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
protected async visitAttribute(attribute: Xml.Attribute, p: P): Promise<Xml | undefined> {
|
|
78
|
+
return updateIfChanged(attribute, {
|
|
79
|
+
markers: await this.visitMarkers(attribute.markers, p),
|
|
80
|
+
value: await this.visitDefined<Xml.AttributeValue>(attribute.value, p)
|
|
81
|
+
});
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
protected async visitAttributeValue(value: Xml.AttributeValue, p: P): Promise<Xml | undefined> {
|
|
85
|
+
return updateIfChanged(value, {
|
|
86
|
+
markers: await this.visitMarkers(value.markers, p)
|
|
87
|
+
});
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
protected async visitCharData(charData: Xml.CharData, p: P): Promise<Xml | undefined> {
|
|
91
|
+
return updateIfChanged(charData, {
|
|
92
|
+
markers: await this.visitMarkers(charData.markers, p)
|
|
93
|
+
});
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
protected async visitComment(comment: Xml.Comment, p: P): Promise<Xml | undefined> {
|
|
97
|
+
return updateIfChanged(comment, {
|
|
98
|
+
markers: await this.visitMarkers(comment.markers, p)
|
|
99
|
+
});
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
protected async visitDocTypeDecl(docTypeDecl: Xml.DocTypeDecl, p: P): Promise<Xml | undefined> {
|
|
103
|
+
return updateIfChanged(docTypeDecl, {
|
|
104
|
+
markers: await this.visitMarkers(docTypeDecl.markers, p),
|
|
105
|
+
internalSubset: await mapAsync(docTypeDecl.internalSubset, i => this.visit<Xml.Ident>(i, p)),
|
|
106
|
+
externalSubsets: docTypeDecl.externalSubsets &&
|
|
107
|
+
await this.visit<Xml.DocTypeDeclExternalSubsets>(docTypeDecl.externalSubsets, p)
|
|
108
|
+
});
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
protected async visitDocTypeDeclExternalSubsets(externalSubsets: Xml.DocTypeDeclExternalSubsets, p: P): Promise<Xml | undefined> {
|
|
112
|
+
return updateIfChanged(externalSubsets, {
|
|
113
|
+
markers: await this.visitMarkers(externalSubsets.markers, p),
|
|
114
|
+
elements: await mapAsync(externalSubsets.elements, e => this.visit<Xml.Element>(e, p))
|
|
115
|
+
});
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
protected async visitElement(element: Xml.Element, p: P): Promise<Xml | undefined> {
|
|
119
|
+
return updateIfChanged(element, {
|
|
120
|
+
markers: await this.visitMarkers(element.markers, p),
|
|
121
|
+
subset: await mapAsync(element.subset, i => this.visit<Xml.Ident>(i, p))
|
|
122
|
+
});
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
protected async visitIdent(ident: Xml.Ident, p: P): Promise<Xml | undefined> {
|
|
126
|
+
return updateIfChanged(ident, {
|
|
127
|
+
markers: await this.visitMarkers(ident.markers, p)
|
|
128
|
+
});
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
protected async visitJspDirective(jspDirective: Xml.JspDirective, p: P): Promise<Xml | undefined> {
|
|
132
|
+
return updateIfChanged(jspDirective, {
|
|
133
|
+
markers: await this.visitMarkers(jspDirective.markers, p),
|
|
134
|
+
attributes: await mapAsync(jspDirective.attributes, a => this.visit<Xml.Attribute>(a, p))
|
|
135
|
+
});
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
protected async visitJspScriptlet(jspScriptlet: Xml.JspScriptlet, p: P): Promise<Xml | undefined> {
|
|
139
|
+
return updateIfChanged(jspScriptlet, {
|
|
140
|
+
markers: await this.visitMarkers(jspScriptlet.markers, p)
|
|
141
|
+
});
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
protected async visitJspExpression(jspExpression: Xml.JspExpression, p: P): Promise<Xml | undefined> {
|
|
145
|
+
return updateIfChanged(jspExpression, {
|
|
146
|
+
markers: await this.visitMarkers(jspExpression.markers, p)
|
|
147
|
+
});
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
protected async visitJspDeclaration(jspDeclaration: Xml.JspDeclaration, p: P): Promise<Xml | undefined> {
|
|
151
|
+
return updateIfChanged(jspDeclaration, {
|
|
152
|
+
markers: await this.visitMarkers(jspDeclaration.markers, p)
|
|
153
|
+
});
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
protected async visitJspComment(jspComment: Xml.JspComment, p: P): Promise<Xml | undefined> {
|
|
157
|
+
return updateIfChanged(jspComment, {
|
|
158
|
+
markers: await this.visitMarkers(jspComment.markers, p)
|
|
159
|
+
});
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
/**
|
|
163
|
+
* Edits an element as a draft, with its markers visited first.
|
|
164
|
+
*/
|
|
165
|
+
protected async produceXml<X extends Xml>(
|
|
166
|
+
before: Xml | undefined,
|
|
167
|
+
p: P,
|
|
168
|
+
recipe?: (draft: Draft<X>) =>
|
|
169
|
+
ValidRecipeReturnType<Draft<X>> |
|
|
170
|
+
PromiseLike<ValidRecipeReturnType<Draft<X>>>
|
|
171
|
+
): Promise<X | undefined> {
|
|
172
|
+
if (before === undefined) {
|
|
173
|
+
return undefined;
|
|
174
|
+
}
|
|
175
|
+
const [draft, finishDraft] = create(before as X);
|
|
176
|
+
(draft as Draft<Xml>).markers = await this.visitMarkers(before.markers, p);
|
|
177
|
+
if (recipe) {
|
|
178
|
+
await recipe(draft);
|
|
179
|
+
}
|
|
180
|
+
return finishDraft() as X;
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
protected accept(t: Xml, p: P): Promise<Xml | undefined> {
|
|
184
|
+
switch (t.kind) {
|
|
185
|
+
case Xml.Kind.Document:
|
|
186
|
+
return this.visitDocument(t as Xml.Document, p);
|
|
187
|
+
case Xml.Kind.Prolog:
|
|
188
|
+
return this.visitProlog(t as Xml.Prolog, p);
|
|
189
|
+
case Xml.Kind.XmlDecl:
|
|
190
|
+
return this.visitXmlDecl(t as Xml.XmlDecl, p);
|
|
191
|
+
case Xml.Kind.ProcessingInstruction:
|
|
192
|
+
return this.visitProcessingInstruction(t as Xml.ProcessingInstruction, p);
|
|
193
|
+
case Xml.Kind.Tag:
|
|
194
|
+
return this.visitTag(t as Xml.Tag, p);
|
|
195
|
+
case Xml.Kind.TagClosing:
|
|
196
|
+
return this.visitTagClosing(t as Xml.TagClosing, p);
|
|
197
|
+
case Xml.Kind.Attribute:
|
|
198
|
+
return this.visitAttribute(t as Xml.Attribute, p);
|
|
199
|
+
case Xml.Kind.AttributeValue:
|
|
200
|
+
return this.visitAttributeValue(t as Xml.AttributeValue, p);
|
|
201
|
+
case Xml.Kind.CharData:
|
|
202
|
+
return this.visitCharData(t as Xml.CharData, p);
|
|
203
|
+
case Xml.Kind.Comment:
|
|
204
|
+
return this.visitComment(t as Xml.Comment, p);
|
|
205
|
+
case Xml.Kind.DocTypeDecl:
|
|
206
|
+
return this.visitDocTypeDecl(t as Xml.DocTypeDecl, p);
|
|
207
|
+
case Xml.Kind.DocTypeDeclExternalSubsets:
|
|
208
|
+
return this.visitDocTypeDeclExternalSubsets(t as Xml.DocTypeDeclExternalSubsets, p);
|
|
209
|
+
case Xml.Kind.Element:
|
|
210
|
+
return this.visitElement(t as Xml.Element, p);
|
|
211
|
+
case Xml.Kind.Ident:
|
|
212
|
+
return this.visitIdent(t as Xml.Ident, p);
|
|
213
|
+
case Xml.Kind.JspDirective:
|
|
214
|
+
return this.visitJspDirective(t as Xml.JspDirective, p);
|
|
215
|
+
case Xml.Kind.JspScriptlet:
|
|
216
|
+
return this.visitJspScriptlet(t as Xml.JspScriptlet, p);
|
|
217
|
+
case Xml.Kind.JspExpression:
|
|
218
|
+
return this.visitJspExpression(t as Xml.JspExpression, p);
|
|
219
|
+
case Xml.Kind.JspDeclaration:
|
|
220
|
+
return this.visitJspDeclaration(t as Xml.JspDeclaration, p);
|
|
221
|
+
case Xml.Kind.JspComment:
|
|
222
|
+
return this.visitJspComment(t as Xml.JspComment, p);
|
|
223
|
+
default:
|
|
224
|
+
throw new Error(`Unexpected XML kind ${t.kind}`);
|
|
225
|
+
}
|
|
226
|
+
}
|
|
227
|
+
}
|