abstract-document 15.1.6 → 15.1.8

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 (37) hide show
  1. package/CHANGELOG.md +4 -0
  2. package/lib/abstract-document-xml/abstract-doc-of-xml/abstract-doc-of-xml.d.ts +2 -1
  3. package/lib/abstract-document-xml/abstract-doc-of-xml/abstract-doc-of-xml.d.ts.map +1 -1
  4. package/lib/abstract-document-xml/abstract-doc-of-xml/abstract-doc-of-xml.js +4 -0
  5. package/lib/abstract-document-xml/abstract-doc-of-xml/abstract-doc-of-xml.js.map +1 -1
  6. package/lib/abstract-document-xml/index.d.ts +0 -1
  7. package/lib/abstract-document-xml/index.d.ts.map +1 -1
  8. package/lib/abstract-document-xml/index.js +0 -1
  9. package/lib/abstract-document-xml/index.js.map +1 -1
  10. package/lib/abstract-document-xml/xsd-template/xsd-template.d.ts +2 -2
  11. package/lib/abstract-document-xml/xsd-template/xsd-template.d.ts.map +1 -1
  12. package/lib/abstract-document-xml/xsd-template/xsd-template.js +3 -3
  13. package/lib/abstract-document-xml/xsd-template/xsd-template.js.map +1 -1
  14. package/package.json +4 -3
  15. package/src/abstract-document-xml/abstract-doc-of-xml/abstract-doc-of-xml.ts +3 -1
  16. package/src/abstract-document-xml/index.ts +0 -1
  17. package/src/abstract-document-xml/xsd-template/xsd-template.ts +1 -1
  18. package/lib/abstract-document-xml/handlebars-xml/helpers.d.ts +0 -2
  19. package/lib/abstract-document-xml/handlebars-xml/helpers.d.ts.map +0 -1
  20. package/lib/abstract-document-xml/handlebars-xml/helpers.js +0 -67
  21. package/lib/abstract-document-xml/handlebars-xml/helpers.js.map +0 -1
  22. package/lib/abstract-document-xml/handlebars-xml/index.d.ts +0 -3
  23. package/lib/abstract-document-xml/handlebars-xml/index.d.ts.map +0 -1
  24. package/lib/abstract-document-xml/handlebars-xml/index.js +0 -19
  25. package/lib/abstract-document-xml/handlebars-xml/index.js.map +0 -1
  26. package/lib/abstract-document-xml/handlebars-xml/parse-mustache-xml.d.ts +0 -21
  27. package/lib/abstract-document-xml/handlebars-xml/parse-mustache-xml.d.ts.map +0 -1
  28. package/lib/abstract-document-xml/handlebars-xml/parse-mustache-xml.js +0 -148
  29. package/lib/abstract-document-xml/handlebars-xml/parse-mustache-xml.js.map +0 -1
  30. package/lib/abstract-document-xml/handlebars-xml/validate-mustache-xml.d.ts +0 -21
  31. package/lib/abstract-document-xml/handlebars-xml/validate-mustache-xml.d.ts.map +0 -1
  32. package/lib/abstract-document-xml/handlebars-xml/validate-mustache-xml.js +0 -195
  33. package/lib/abstract-document-xml/handlebars-xml/validate-mustache-xml.js.map +0 -1
  34. package/src/abstract-document-xml/handlebars-xml/helpers.ts +0 -73
  35. package/src/abstract-document-xml/handlebars-xml/index.ts +0 -2
  36. package/src/abstract-document-xml/handlebars-xml/parse-mustache-xml.ts +0 -163
  37. package/src/abstract-document-xml/handlebars-xml/validate-mustache-xml.ts +0 -272
@@ -1,272 +0,0 @@
1
- import { XMLValidator, ValidationError } from "fast-xml-parser";
2
- import { XmlElement, parseXml, findElement } from "./parse-mustache-xml.js";
3
-
4
- enum ErrorType {
5
- warning = 0,
6
- error = 1,
7
- }
8
-
9
- type Range = {
10
- readonly startLineNumber: number;
11
- readonly startColumn: number;
12
- readonly endLineNumber: number;
13
- readonly endColumn: number;
14
- };
15
- type Position = {
16
- readonly lineNumber: number;
17
- readonly column: number;
18
- };
19
-
20
- type ErrorObject = {
21
- readonly range: Range;
22
- readonly options: ErrorOptions;
23
- };
24
-
25
- type ErrorOptions = {
26
- readonly className: string;
27
- // eslint-disable-next-line functional/prefer-readonly-type
28
- readonly hoverMessage: Array<{
29
- readonly value: string;
30
- }>;
31
- };
32
-
33
- type XmlError = {
34
- readonly message: string;
35
- readonly type: ErrorType;
36
- readonly range: Range;
37
- };
38
-
39
- // eslint-disable-next-line functional/prefer-readonly-type
40
- export function validateXml(fullXml: string, xsdSchema: ReadonlyArray<XmlElement>): Array<ErrorObject> {
41
- const errors: Array<XmlError> = [];
42
-
43
- // ignore all mustache brackets
44
- const matchMustacheBrackets = /{{.*}}(?!([\S]))/g;
45
- // Ignore xml comments
46
- const xmlComments = /<!--[^>]*-->/g;
47
-
48
- // Replace matches with spaces of same length
49
- let cleanedXml = fullXml.replace(matchMustacheBrackets, (m) => " ".repeat(m.length));
50
- cleanedXml = cleanedXml.replace(xmlComments, (m) => {
51
- const x = (m.match(/^.*$/gm) || []).map((m2) => " ".repeat(m2.length));
52
- return x.join("\n");
53
- });
54
-
55
- const xml = `<xml>\n${cleanedXml}\n</xml>`;
56
-
57
- if (xml) {
58
- try {
59
- const result = XMLValidator.validate(xml, {
60
- allowBooleanAttributes: true,
61
- });
62
-
63
- if (result !== true) {
64
- errors.push(getErrorFromException(result, xml));
65
- }
66
- const entryPointXml = parseXml(xml)[0]!;
67
-
68
- const entryPointSchema = xsdSchema[0]!;
69
- let pos = 0;
70
- const lines = cleanedXml.split("\n");
71
- const getRangeOfElement = (text: string, incrementPosition: boolean = true): Range => {
72
- if (text === undefined) {
73
- const monacoPosition = getPositionFromIndex(lines, pos);
74
- return toRange(
75
- monacoPosition.lineNumber,
76
- monacoPosition.column,
77
- monacoPosition.lineNumber,
78
- monacoPosition.column + 5
79
- );
80
- }
81
- const position = cleanedXml.indexOf(text, pos);
82
- if (incrementPosition) {
83
- pos = position >= pos ? position + text.length : pos;
84
- }
85
- const monacoPosition = getPositionFromIndex(lines, position);
86
- return toRange(
87
- monacoPosition.lineNumber,
88
- monacoPosition.column,
89
- monacoPosition.lineNumber,
90
- monacoPosition.column + text.length
91
- );
92
- };
93
-
94
- const validationErrors = entryPointXml.children.flatMap((child) =>
95
- validateElements(child, undefined, entryPointSchema, getRangeOfElement)
96
- );
97
- errors.push(...validationErrors);
98
- } catch (e) {
99
- errors.push(createError(e.message, ErrorType.error, toRange(1, 1, 1, 100)));
100
- }
101
- }
102
-
103
- return errors.map((e) => getDecorationsFromError(e));
104
- }
105
-
106
- function validateElements(
107
- element: XmlElement,
108
- schemaElement: XmlElement | undefined,
109
- completeSchema: XmlElement,
110
- getRangeOfElement: (elementName: string, incrementPosition?: boolean) => Range
111
- ): ReadonlyArray<XmlError> {
112
- const errors: Array<XmlError> = [];
113
-
114
- // Validate element name
115
- const tagName = element.tagName;
116
- const range = getRangeOfElement(tagName);
117
-
118
- const slashPosition = getRangeOfElement("/", false);
119
- const closingTagPosition = getRangeOfElement(">", false);
120
- const isClosed = rangeLessThan(slashPosition, closingTagPosition);
121
-
122
- const validElements = Object.values(completeSchema.children);
123
- const schemaName = schemaElement?.attributes.type || tagName;
124
- const foundSchemaElement = findElement(validElements, schemaName);
125
-
126
- if (!foundSchemaElement) {
127
- return [createError(`"${tagName}" is not a valid element`, ErrorType.error, range)];
128
- }
129
-
130
- const possibleAttributes = Array.from(foundSchemaElement.children).flatMap((c) =>
131
- c.tagName === "attribute" ? c : []
132
- );
133
-
134
- // Validate required attributes
135
- for (const possibleAttribute of possibleAttributes) {
136
- const attributeName = possibleAttribute.attributes.name;
137
- const isRequired = possibleAttribute.attributes.use;
138
- if (attributeName && isRequired && isRequired === "required") {
139
- if (element.attributes[attributeName] === undefined) {
140
- errors.push(createError(`"${attributeName}" is a required attribute on "${tagName}"`, ErrorType.error, range));
141
- }
142
- }
143
- }
144
-
145
- // Validate existing attributes
146
- for (const [attrKey, attrVal] of Object.entries(element.attributes)) {
147
- const possibleAttrNames = possibleAttributes.flatMap((p) => p.attributes.name || []);
148
- const attrText = typeof attrVal === "string" ? `${attrKey}="${attrVal}"` : attrKey;
149
- const attrRange = getRangeOfElement(attrText, false);
150
- if (!possibleAttrNames.includes(attrKey)) {
151
- errors.push(createError(`"${attrKey}" is a not a valid attribute on "${tagName}"`, ErrorType.error, attrRange));
152
- }
153
- }
154
-
155
- // Validate that the children are allowed as a child of current element
156
- // and then validate the children themselves
157
- const schemaChildren = Object.values(foundSchemaElement.children);
158
-
159
- for (const child of element.children) {
160
- const childName = child.tagName;
161
- if (!childName) {
162
- continue;
163
- }
164
- const foundChild = findElement(schemaChildren, childName);
165
- if (!foundChild) {
166
- const childRange = getRangeOfElement(childName, false);
167
- if (childRange) {
168
- errors.push(createError(`"${childName}" is not a valid child of "${tagName}"`, ErrorType.error, childRange));
169
- }
170
- }
171
-
172
- const elementErrors = validateElements(child, foundChild, completeSchema, getRangeOfElement);
173
- errors.push(...elementErrors);
174
- }
175
-
176
- if (!isClosed) {
177
- // Increment position to after closing tag
178
- getRangeOfElement(`</${tagName}`, true);
179
- }
180
-
181
- return errors;
182
- }
183
-
184
- function getDecorationsFromError(error: XmlError): ErrorObject {
185
- return {
186
- range: error.range,
187
- options: {
188
- className: getErrorClassNames(error),
189
- hoverMessage: [{ value: getErrorType(error) }, { value: getErrorMessage(error) }],
190
- },
191
- };
192
- }
193
-
194
- function getErrorFromException(result: ValidationError, xml: string): XmlError {
195
- const { col, line, msg } = result.err;
196
- const startLine = line - 1;
197
- const lines = xml.split("\n");
198
- const rowText = lines[startLine] || "";
199
- const length = rowText.indexOf(">") - rowText.indexOf("<") || 4;
200
- const range = toRange(startLine, col, startLine, col + length);
201
-
202
- return createError(msg!, ErrorType.error, range);
203
- }
204
-
205
- function getErrorClassNames(error: XmlError): string {
206
- switch (error.type) {
207
- case ErrorType.warning:
208
- return "xml-lint xml-lint--warning";
209
- case ErrorType.error:
210
- return "xml-lint xml-lint--error";
211
- default:
212
- return "";
213
- }
214
- }
215
-
216
- function getErrorType(error: XmlError): string {
217
- switch (error.type) {
218
- case ErrorType.warning:
219
- return "**Warning**";
220
- case ErrorType.error:
221
- return "**Error**";
222
- default:
223
- return "";
224
- }
225
- }
226
-
227
- const getErrorMessage = (error: XmlError): string => error.message.split(/[\t\n]/g)[1] ?? error.message;
228
-
229
- const createError = (message: string, type: ErrorType, range: Range): XmlError => ({ message, type, range });
230
-
231
- function getPositionFromIndex(lines: ReadonlyArray<string>, index: number): Position {
232
- let totalLength = 0;
233
- for (const [lineNumber, line] of lines.entries()) {
234
- totalLength += line.length;
235
- if (totalLength >= index - lineNumber) {
236
- return {
237
- lineNumber: lineNumber + 1,
238
- column: 1 + index - (totalLength - line.length) - lineNumber || 1, // "- lineNumber" because of \n characters
239
- };
240
- }
241
- }
242
- return { lineNumber: lines.length, column: 1 };
243
- }
244
-
245
- const toRange = (startLineNumber: number, startColumn: number, endLineNumber: number, endColumn: number): Range => ({
246
- startLineNumber,
247
- startColumn,
248
- endLineNumber,
249
- endColumn,
250
- });
251
-
252
- export function errorToReadableText(errors: ReadonlyArray<ErrorObject>, templateName: string = ""): string {
253
- const errorLines: Array<string> = [];
254
- if (templateName) {
255
- errorLines.push(`Error in template ${templateName} \n`);
256
- }
257
- for (const error of errors) {
258
- const hoverErrors = error.options.hoverMessage.map((e) => e.value.replace(/\*/g, ""));
259
- errorLines.push(`${hoverErrors.join("\n")}`);
260
- errorLines.push(`On line ${error.range.startLineNumber}, column ${error.range.startColumn}\n`);
261
- }
262
-
263
- return errorLines.join("\n");
264
- }
265
-
266
- function rangeLessThan(range1: Range, range2: Range): boolean {
267
- return range1.startLineNumber < range2.startLineNumber
268
- ? true
269
- : range1.startLineNumber > range2.startLineNumber
270
- ? false
271
- : range1.startColumn < range2.startColumn;
272
- }