langium-tools 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +1171 -0
- package/dist/base/arrays.d.ts +6 -0
- package/dist/base/arrays.js +34 -0
- package/dist/base/document-errors.d.ts +56 -0
- package/dist/base/document-errors.js +198 -0
- package/dist/base/document-issues.d.ts +178 -0
- package/dist/base/document-issues.js +306 -0
- package/dist/base/index.d.ts +4 -0
- package/dist/base/index.js +4 -0
- package/dist/base/marker-parser.d.ts +36 -0
- package/dist/base/marker-parser.js +51 -0
- package/dist/base/string.d.ts +89 -0
- package/dist/base/string.js +104 -0
- package/dist/generator/generated-content-manager.d.ts +207 -0
- package/dist/generator/generated-content-manager.js +242 -0
- package/dist/generator/index.d.ts +2 -0
- package/dist/generator/index.js +2 -0
- package/dist/generator/tools.d.ts +9 -0
- package/dist/generator/tools.js +12 -0
- package/dist/index.d.ts +4 -0
- package/dist/index.js +4 -0
- package/dist/lang/index.d.ts +1 -0
- package/dist/lang/index.js +1 -0
- package/dist/lang/java/common.d.ts +1 -0
- package/dist/lang/java/common.js +3 -0
- package/dist/lang/java/generator-tools.d.ts +37 -0
- package/dist/lang/java/generator-tools.js +48 -0
- package/dist/lang/java/import-manager.d.ts +61 -0
- package/dist/lang/java/import-manager.js +88 -0
- package/dist/lang/java/index.d.ts +3 -0
- package/dist/lang/java/index.js +3 -0
- package/dist/testing/common.d.ts +1 -0
- package/dist/testing/common.js +5 -0
- package/dist/testing/generator-testing.d.ts +56 -0
- package/dist/testing/generator-testing.js +184 -0
- package/dist/testing/index.d.ts +4 -0
- package/dist/testing/index.js +4 -0
- package/dist/testing/matchers.d.ts +209 -0
- package/dist/testing/matchers.js +306 -0
- package/dist/testing/parser-tools.d.ts +19 -0
- package/dist/testing/parser-tools.js +18 -0
- package/package.json +59 -0
- package/src/base/arrays.ts +40 -0
- package/src/base/document-issues.ts +430 -0
- package/src/base/index.ts +4 -0
- package/src/base/marker-parser.ts +96 -0
- package/src/base/string.ts +117 -0
- package/src/generator/generated-content-manager.ts +368 -0
- package/src/generator/index.ts +2 -0
- package/src/generator/tools.ts +14 -0
- package/src/index.ts +5 -0
- package/src/lang/index.ts +1 -0
- package/src/lang/java/common.ts +4 -0
- package/src/lang/java/generator-tools.ts +59 -0
- package/src/lang/java/import-manager.ts +98 -0
- package/src/lang/java/index.ts +3 -0
- package/src/testing/common.ts +5 -0
- package/src/testing/generator-testing.ts +266 -0
- package/src/testing/index.ts +4 -0
- package/src/testing/matchers.ts +503 -0
- package/src/testing/parser-tools.ts +34 -0
|
@@ -0,0 +1,306 @@
|
|
|
1
|
+
import 'vitest';
|
|
2
|
+
import { expect } from "vitest";
|
|
3
|
+
import { DocumentIssueSeverity, documentIssueToString, getDocumentIssues, getDocumentIssueSummary } from "../base/document-issues.js";
|
|
4
|
+
import { expandToNode, toString } from 'langium/generate';
|
|
5
|
+
import { sentenceCase } from 'change-case';
|
|
6
|
+
import { EOL } from 'node:os';
|
|
7
|
+
/**
|
|
8
|
+
* Expects a Langium document to have no errors after parsing.
|
|
9
|
+
* Non-error diagnostics are ignored by default.
|
|
10
|
+
*
|
|
11
|
+
* @param document - The Langium document or parsed document to check for errors.
|
|
12
|
+
* @param parameters - Optional parameters to customize which errors to ignore.
|
|
13
|
+
*
|
|
14
|
+
* @example
|
|
15
|
+
* ```typescript
|
|
16
|
+
* import { expect, test } from 'vitest';
|
|
17
|
+
* import { parse } from './your-langium-test-setup';
|
|
18
|
+
*
|
|
19
|
+
* test('document has no errors', async () => {
|
|
20
|
+
* const doc = await parse('const x = 5;');
|
|
21
|
+
* expect(doc).toHaveNoErrors();
|
|
22
|
+
* });
|
|
23
|
+
*
|
|
24
|
+
* test('document has no errors, ignoring lexer errors', async () => {
|
|
25
|
+
* const doc = await parse('const x = @;', { validation: true });
|
|
26
|
+
* expect(doc).toHaveNoErrors({ ignoreLexerErrors: true });
|
|
27
|
+
* });
|
|
28
|
+
* ```
|
|
29
|
+
*/
|
|
30
|
+
export function toHaveNoErrors(document, parameters) {
|
|
31
|
+
const { ignoreParserErrors = false, ignoreLexerErrors = false, ignoreValidationErrors = false, ignoreNonErrorDiagnostics = true, // Default to true for 'toHaveNoErrors'
|
|
32
|
+
} = parameters || {};
|
|
33
|
+
return toHaveNoIssues(document, {
|
|
34
|
+
ignoreParserErrors,
|
|
35
|
+
ignoreLexerErrors,
|
|
36
|
+
ignoreValidationErrors,
|
|
37
|
+
ignoreNonErrorDiagnostics,
|
|
38
|
+
});
|
|
39
|
+
}
|
|
40
|
+
/**
|
|
41
|
+
* Expects a Langium document to have no issues after parsing.
|
|
42
|
+
*
|
|
43
|
+
* @param document - The Langium document or parsed document to check for issues.
|
|
44
|
+
* @param parameters - Optional parameters to customize which issues to ignore.
|
|
45
|
+
*
|
|
46
|
+
* @example
|
|
47
|
+
* ```typescript
|
|
48
|
+
* import { expect, test } from 'vitest';
|
|
49
|
+
* import { parse } from './your-langium-test-setup';
|
|
50
|
+
*
|
|
51
|
+
* test('document has no issues', async () => {
|
|
52
|
+
* const doc = await parse('const x = 5;');
|
|
53
|
+
* expect(doc).toHaveNoIssues();
|
|
54
|
+
* });
|
|
55
|
+
*
|
|
56
|
+
* test('document has no issues, ignoring lexer errors', async () => {
|
|
57
|
+
* const doc = await parse('const x = @;', { validation: true });
|
|
58
|
+
* expect(doc).toHaveNoIssues({ ignoreLexerErrors: true });
|
|
59
|
+
* });
|
|
60
|
+
* ```
|
|
61
|
+
*/
|
|
62
|
+
export function toHaveNoIssues(document, parameters) {
|
|
63
|
+
const { ignoreParserErrors = false, ignoreLexerErrors = false, ignoreValidationErrors = false, ignoreNonErrorDiagnostics = false, } = parameters || {};
|
|
64
|
+
const langiumDocument = 'document' in document ? document.document : document;
|
|
65
|
+
const issues = getDocumentIssueSummary(langiumDocument, {
|
|
66
|
+
skipLexerErrors: ignoreLexerErrors,
|
|
67
|
+
skipParserErrors: ignoreParserErrors,
|
|
68
|
+
skipValidation: ignoreValidationErrors,
|
|
69
|
+
skipNonErrorDiagnostics: ignoreNonErrorDiagnostics,
|
|
70
|
+
});
|
|
71
|
+
return {
|
|
72
|
+
pass: issues.countTotal === 0,
|
|
73
|
+
message: () => issues.message,
|
|
74
|
+
};
|
|
75
|
+
}
|
|
76
|
+
/**
|
|
77
|
+
* Expects a parsed Langium document with markers to have specific issues.
|
|
78
|
+
*
|
|
79
|
+
* @param parsedDocument - The parsed Langium document with markers to check for issues.
|
|
80
|
+
* @param expectedIssues - An array of expected issues.
|
|
81
|
+
* @param parameters - Optional parameters to customize which issues to ignore.
|
|
82
|
+
*
|
|
83
|
+
* @example
|
|
84
|
+
* ```typescript
|
|
85
|
+
* import { expect, test } from 'vitest';
|
|
86
|
+
* import { parseDocument } from './your-langium-test-setup';
|
|
87
|
+
*
|
|
88
|
+
* test('document has expected issues', async () => {
|
|
89
|
+
* const parsedDoc = await parseMarkedDSL(`
|
|
90
|
+
* let [[x: string]] = 5;
|
|
91
|
+
* print(x);
|
|
92
|
+
* `, '[[', ']]');
|
|
93
|
+
* expect(parsedDoc).toHaveDocumentIssues([
|
|
94
|
+
* {
|
|
95
|
+
* message: "Type 'number' is not assignable to type 'string'",
|
|
96
|
+
* markerId: 0
|
|
97
|
+
* }
|
|
98
|
+
* ]);
|
|
99
|
+
* });
|
|
100
|
+
*
|
|
101
|
+
* test('document has a warning, ignoring errors', async () => {
|
|
102
|
+
* const parsedDoc = await parseMarkedDSL(`
|
|
103
|
+
* let [[foo]];
|
|
104
|
+
* `, '[[', ']]');
|
|
105
|
+
* expect(parsedDoc).toHaveDocumentIssues(
|
|
106
|
+
* [
|
|
107
|
+
* {
|
|
108
|
+
* severity: DocumentIssueSeverity.WARNING,
|
|
109
|
+
* message: "Variable 'foo' is never used",
|
|
110
|
+
* markerId: 0
|
|
111
|
+
* }
|
|
112
|
+
* ],
|
|
113
|
+
* { ignoreValidationErrors: true }
|
|
114
|
+
* );
|
|
115
|
+
* });
|
|
116
|
+
* ```
|
|
117
|
+
*/
|
|
118
|
+
export function toHaveDocumentIssues(parsedDocument, expectedIssues, parameters) {
|
|
119
|
+
if (!('document' in parsedDocument)) {
|
|
120
|
+
throw new Error(`Expected ParsedDocument object. Use 'parseMarkedDSL' to parse.`);
|
|
121
|
+
}
|
|
122
|
+
const { ignoreParserErrors = false, ignoreLexerErrors = false, ignoreValidationErrors = false, ignoreNonErrorDiagnostics = false, } = parameters || {};
|
|
123
|
+
const actualIssues = getDocumentIssues(parsedDocument.document, {
|
|
124
|
+
skipLexerErrors: ignoreLexerErrors,
|
|
125
|
+
skipParserErrors: ignoreParserErrors,
|
|
126
|
+
skipValidation: ignoreValidationErrors,
|
|
127
|
+
skipNonErrorDiagnostics: ignoreNonErrorDiagnostics,
|
|
128
|
+
});
|
|
129
|
+
if (!actualIssues || actualIssues.length === 0) {
|
|
130
|
+
if (expectedIssues.length === 0) {
|
|
131
|
+
return {
|
|
132
|
+
pass: true,
|
|
133
|
+
message: () => "",
|
|
134
|
+
};
|
|
135
|
+
}
|
|
136
|
+
return {
|
|
137
|
+
pass: false,
|
|
138
|
+
message: () => `Expected ${expectedIssues.length} issues, but none were present in the document.${EOL}${expectedIssues
|
|
139
|
+
.map((issue) => issueExpectationToString(parsedDocument, issue))
|
|
140
|
+
.join(EOL)}`,
|
|
141
|
+
};
|
|
142
|
+
}
|
|
143
|
+
const unmatchedExpectedIssues = [];
|
|
144
|
+
let remainingActualIssues = [...actualIssues];
|
|
145
|
+
expectedIssues.forEach((expectedIssue) => {
|
|
146
|
+
const matchedActualIssue = remainingActualIssues.find((actualIssue) => matchesIssueExpectation(actualIssue, expectedIssue, parsedDocument));
|
|
147
|
+
if (matchedActualIssue) {
|
|
148
|
+
remainingActualIssues = remainingActualIssues.filter((issue) => issue !== matchedActualIssue);
|
|
149
|
+
}
|
|
150
|
+
else {
|
|
151
|
+
unmatchedExpectedIssues.push(expectedIssue);
|
|
152
|
+
}
|
|
153
|
+
});
|
|
154
|
+
const errors = [];
|
|
155
|
+
if (remainingActualIssues.length > 0) {
|
|
156
|
+
errors.push("Unmatched actual issues:");
|
|
157
|
+
errors.push(...remainingActualIssues.map((issue) => {
|
|
158
|
+
const source = issue.source ? `(${sentenceCase(issue.source)}) ` : '';
|
|
159
|
+
return `${source}${documentIssueToString(issue)}`;
|
|
160
|
+
}));
|
|
161
|
+
}
|
|
162
|
+
if (unmatchedExpectedIssues.length > 0) {
|
|
163
|
+
errors.push("Unmatched expected issues:");
|
|
164
|
+
errors.push(...unmatchedExpectedIssues.map((issue) => issueExpectationToString(parsedDocument, issue)));
|
|
165
|
+
}
|
|
166
|
+
return {
|
|
167
|
+
pass: errors.length === 0,
|
|
168
|
+
message: () => errors.join(EOL),
|
|
169
|
+
};
|
|
170
|
+
}
|
|
171
|
+
/**
|
|
172
|
+
* Expects that a specific issue is present in the Langium document.
|
|
173
|
+
*
|
|
174
|
+
* @param document - The Langium document or parsed document to check for the issue.
|
|
175
|
+
* @param expectedIssue - The expected issue to look for.
|
|
176
|
+
* @param parameters - Optional parameters to customize which issues to ignore.
|
|
177
|
+
*
|
|
178
|
+
* @example
|
|
179
|
+
* ```typescript
|
|
180
|
+
* import { expect, test } from 'vitest';
|
|
181
|
+
* import { parse } from './your-langium-test-setup';
|
|
182
|
+
*
|
|
183
|
+
* test('document contains specific issue', async () => {
|
|
184
|
+
* const doc = await parse('const x = @;', { validation: true });
|
|
185
|
+
* expect(doc).toContainIssue({
|
|
186
|
+
* message: /Unexpected character/,
|
|
187
|
+
* severity: DocumentIssueSeverity.ERROR,
|
|
188
|
+
* source: DocumentIssueSource.LEXER,
|
|
189
|
+
* });
|
|
190
|
+
* });
|
|
191
|
+
* ```
|
|
192
|
+
*/
|
|
193
|
+
export function toContainIssue(document, expectedIssue, parameters) {
|
|
194
|
+
const { ignoreParserErrors = false, ignoreLexerErrors = false, ignoreValidationErrors = false, ignoreNonErrorDiagnostics = false, } = parameters || {};
|
|
195
|
+
const langiumDocument = 'document' in document ? document.document : document;
|
|
196
|
+
const actualIssues = getDocumentIssues(langiumDocument, {
|
|
197
|
+
skipLexerErrors: ignoreLexerErrors,
|
|
198
|
+
skipParserErrors: ignoreParserErrors,
|
|
199
|
+
skipValidation: ignoreValidationErrors,
|
|
200
|
+
skipNonErrorDiagnostics: ignoreNonErrorDiagnostics,
|
|
201
|
+
});
|
|
202
|
+
if (!actualIssues || actualIssues.length === 0) {
|
|
203
|
+
return {
|
|
204
|
+
pass: false,
|
|
205
|
+
message: () => `No issues were found in the document.`,
|
|
206
|
+
};
|
|
207
|
+
}
|
|
208
|
+
const matchedIssue = actualIssues.find((actualIssue) => matchesIssueExpectation(actualIssue, expectedIssue, 'markerData' in document ? document : undefined));
|
|
209
|
+
if (matchedIssue) {
|
|
210
|
+
return {
|
|
211
|
+
pass: true,
|
|
212
|
+
message: () => '',
|
|
213
|
+
};
|
|
214
|
+
}
|
|
215
|
+
else {
|
|
216
|
+
const issueDescription = issueExpectationToString('markerData' in document ? document : undefined, expectedIssue);
|
|
217
|
+
return {
|
|
218
|
+
pass: false,
|
|
219
|
+
message: () => toString(expandToNode `
|
|
220
|
+
Expected issue was not found in the document:
|
|
221
|
+
${issueDescription}
|
|
222
|
+
|
|
223
|
+
Issues in the document:
|
|
224
|
+
${getDocumentIssueSummary(langiumDocument).message}
|
|
225
|
+
`)
|
|
226
|
+
};
|
|
227
|
+
}
|
|
228
|
+
}
|
|
229
|
+
/**
|
|
230
|
+
* Helper function to determine if an actual issue matches an expected issue.
|
|
231
|
+
*
|
|
232
|
+
* @param actualIssue - The actual issue from the document.
|
|
233
|
+
* @param expectedIssue - The expected issue defined by the user.
|
|
234
|
+
* @param parsedDocument - The parsed document containing marker data (optional).
|
|
235
|
+
* @returns `true` if the actual issue matches the expected issue; otherwise, `false`.
|
|
236
|
+
*/
|
|
237
|
+
function matchesIssueExpectation(actualIssue, expectedIssue, parsedDocument) {
|
|
238
|
+
let match = typeof expectedIssue.message === 'string'
|
|
239
|
+
? actualIssue.message === expectedIssue.message
|
|
240
|
+
: expectedIssue.message.test(actualIssue.message);
|
|
241
|
+
match && (match = actualIssue.severity === (expectedIssue.severity || DocumentIssueSeverity.ERROR));
|
|
242
|
+
match && (match = expectedIssue.source === undefined || actualIssue.source === expectedIssue.source);
|
|
243
|
+
if (expectedIssue.markerId !== undefined && parsedDocument) {
|
|
244
|
+
const marker = parsedDocument.markerData.markers[expectedIssue.markerId];
|
|
245
|
+
if (!marker) {
|
|
246
|
+
throw new Error(`Invalid markerId ${expectedIssue.markerId}. Available markers range from 0 to ${parsedDocument.markerData.markers.length - 1}.`);
|
|
247
|
+
}
|
|
248
|
+
if (actualIssue.startLine !== undefined && marker.startLine !== undefined) {
|
|
249
|
+
match && (match = actualIssue.startLine === marker.startLine);
|
|
250
|
+
}
|
|
251
|
+
if (actualIssue.startColumn !== undefined && marker.startColumn !== undefined) {
|
|
252
|
+
match && (match = actualIssue.startColumn === marker.startColumn);
|
|
253
|
+
}
|
|
254
|
+
if (actualIssue.startOffset !== undefined && marker.startOffset !== undefined) {
|
|
255
|
+
match && (match = actualIssue.startOffset === marker.startOffset);
|
|
256
|
+
}
|
|
257
|
+
if (actualIssue.endLine !== undefined && marker.endLine !== undefined) {
|
|
258
|
+
match && (match = actualIssue.endLine === marker.endLine);
|
|
259
|
+
}
|
|
260
|
+
if (actualIssue.endColumn !== undefined && marker.endColumn !== undefined) {
|
|
261
|
+
match && (match = actualIssue.endColumn === marker.endColumn);
|
|
262
|
+
}
|
|
263
|
+
if (actualIssue.endOffset !== undefined && marker.endOffset !== undefined) {
|
|
264
|
+
match && (match = actualIssue.endOffset === marker.endOffset);
|
|
265
|
+
}
|
|
266
|
+
}
|
|
267
|
+
return match;
|
|
268
|
+
}
|
|
269
|
+
/**
|
|
270
|
+
* Extends Vitest's expect function with custom matchers for Langium documents.
|
|
271
|
+
*/
|
|
272
|
+
expect.extend({
|
|
273
|
+
toHaveNoErrors,
|
|
274
|
+
toHaveNoIssues,
|
|
275
|
+
toHaveDocumentIssues,
|
|
276
|
+
toContainIssue,
|
|
277
|
+
});
|
|
278
|
+
/**
|
|
279
|
+
* Converts an issue expectation into a readable string representation.
|
|
280
|
+
*
|
|
281
|
+
* @param parsedDocument - The parsed document containing marker data (optional).
|
|
282
|
+
* @param issue - The issue expectation to convert.
|
|
283
|
+
* @returns A string representation of the issue expectation.
|
|
284
|
+
*/
|
|
285
|
+
function issueExpectationToString(parsedDocument, issue) {
|
|
286
|
+
const severity = issue.severity || DocumentIssueSeverity.ERROR;
|
|
287
|
+
const source = issue.source ? `(${sentenceCase(issue.source)}) ` : '';
|
|
288
|
+
let message = `${source}${sentenceCase(severity)}`;
|
|
289
|
+
if (issue.markerId !== undefined && parsedDocument) {
|
|
290
|
+
if (issue.markerId < 0 || issue.markerId >= parsedDocument.markerData.markers.length) {
|
|
291
|
+
throw new Error(`Invalid markerId ${issue.markerId}. Available markers range from 0 to ${parsedDocument.markerData.markers.length - 1}.`);
|
|
292
|
+
}
|
|
293
|
+
const marker = parsedDocument.markerData.markers[issue.markerId];
|
|
294
|
+
message += ` at ${marker.startLine + 1}:${marker.startColumn + 1}`;
|
|
295
|
+
if (marker.startOffset < marker.endOffset) {
|
|
296
|
+
message += "-";
|
|
297
|
+
if (marker.startLine < marker.endLine) {
|
|
298
|
+
message += `${marker.endLine + 1}:`;
|
|
299
|
+
}
|
|
300
|
+
message += `${marker.endColumn}`;
|
|
301
|
+
}
|
|
302
|
+
}
|
|
303
|
+
const messageText = issue.message instanceof RegExp ? issue.message.toString() : issue.message;
|
|
304
|
+
message += ` - ${messageText}`;
|
|
305
|
+
return message;
|
|
306
|
+
}
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import { LangiumDocument } from "langium";
|
|
2
|
+
import { ParseMarkedTextResult } from "../base/marker-parser.js";
|
|
3
|
+
import { parseHelper } from "langium/test";
|
|
4
|
+
export interface ParsedDocument {
|
|
5
|
+
markedText: string;
|
|
6
|
+
document: LangiumDocument;
|
|
7
|
+
markerData: ParseMarkedTextResult;
|
|
8
|
+
}
|
|
9
|
+
type DoParse = ReturnType<typeof parseHelper>;
|
|
10
|
+
/**
|
|
11
|
+
* Extract markers and parse dsl and run validation.
|
|
12
|
+
*
|
|
13
|
+
* @param doParse parse method returned from `parseHelper` method.
|
|
14
|
+
* @param text DSL with markers
|
|
15
|
+
* @param [endMarker="|}>>"] (optional) begin marker
|
|
16
|
+
* @param [beginMarker="<<{|"] (optional) end marker
|
|
17
|
+
*/
|
|
18
|
+
export declare function parseWithMarks(doParse: DoParse, text: string, beginMarker?: string, endMarker?: string): Promise<ParsedDocument>;
|
|
19
|
+
export {};
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import { parseMarkedText } from "../base/marker-parser.js";
|
|
2
|
+
/**
|
|
3
|
+
* Extract markers and parse dsl and run validation.
|
|
4
|
+
*
|
|
5
|
+
* @param doParse parse method returned from `parseHelper` method.
|
|
6
|
+
* @param text DSL with markers
|
|
7
|
+
* @param [endMarker="|}>>"] (optional) begin marker
|
|
8
|
+
* @param [beginMarker="<<{|"] (optional) end marker
|
|
9
|
+
*/
|
|
10
|
+
export async function parseWithMarks(doParse, text, beginMarker = "<<{|", endMarker = "|}>>") {
|
|
11
|
+
const markerData = parseMarkedText(text, beginMarker, endMarker);
|
|
12
|
+
const document = await doParse(markerData.text, { validation: true });
|
|
13
|
+
return {
|
|
14
|
+
markedText: text,
|
|
15
|
+
document: document,
|
|
16
|
+
markerData: markerData
|
|
17
|
+
};
|
|
18
|
+
}
|
package/package.json
ADDED
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "langium-tools",
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"description": "A set of tools for Langium",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"main": "dist/index.js",
|
|
7
|
+
"types": "dist/index.d.ts",
|
|
8
|
+
"scripts": {
|
|
9
|
+
"build": "tsc",
|
|
10
|
+
"lint": "eslint 'src/**/*.{ts,tsx}'",
|
|
11
|
+
"lint:fix": "eslint 'src/**/*.{ts,tsx}' --fix",
|
|
12
|
+
"test": "vitest run",
|
|
13
|
+
"test:watch": "vitest",
|
|
14
|
+
"docs": "typedoc --plugin typedoc-plugin-extras --favicon icons/32px.png"
|
|
15
|
+
},
|
|
16
|
+
"exports": {
|
|
17
|
+
"./base": "./dist/base/index.js",
|
|
18
|
+
"./lang": "./dist/lang/index.js",
|
|
19
|
+
"./lang/java": "./dist/lang/java/index.js",
|
|
20
|
+
"./generator": "./dist/generator/index.js",
|
|
21
|
+
"./testing": "./dist/testing/index.js"
|
|
22
|
+
},
|
|
23
|
+
"files": [
|
|
24
|
+
"dist/**/*",
|
|
25
|
+
"src/**/*",
|
|
26
|
+
"*.map"
|
|
27
|
+
],
|
|
28
|
+
"author": "Boris Brodski",
|
|
29
|
+
"license": "MIT",
|
|
30
|
+
"dependencies": {
|
|
31
|
+
"@types/node": "^22.0.0",
|
|
32
|
+
"change-case": "^5.4.4",
|
|
33
|
+
"escape-string-regexp": "^5.0.0",
|
|
34
|
+
"langium": "^3.2.0"
|
|
35
|
+
},
|
|
36
|
+
"peerDependencies": {
|
|
37
|
+
"vitest": "~2.0.5"
|
|
38
|
+
},
|
|
39
|
+
"devDependencies": {
|
|
40
|
+
"@eslint/js": "^9.8.0",
|
|
41
|
+
"globals": "^15.8.0",
|
|
42
|
+
"typedoc": "^0.26.7",
|
|
43
|
+
"typedoc-plugin-extras": "^3.1.0",
|
|
44
|
+
"typedoc-plugin-markdown": "^4.2.7",
|
|
45
|
+
"typescript": "^5.5.4",
|
|
46
|
+
"typescript-eslint": "^7.18.0",
|
|
47
|
+
"vitest": "~2.0.5"
|
|
48
|
+
},
|
|
49
|
+
"overrides": {
|
|
50
|
+
"eslint": "^9.8.0"
|
|
51
|
+
},
|
|
52
|
+
"exclude": [
|
|
53
|
+
"tests",
|
|
54
|
+
"docs",
|
|
55
|
+
"*.config.*",
|
|
56
|
+
"package-lock.json",
|
|
57
|
+
"*.json"
|
|
58
|
+
]
|
|
59
|
+
}
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
declare global {
|
|
2
|
+
interface Array<T> {
|
|
3
|
+
sortBy<K extends keyof T, V>(key: K | ((item: T) => V)): this;
|
|
4
|
+
}
|
|
5
|
+
}
|
|
6
|
+
|
|
7
|
+
Array.prototype.sortBy = function <T, K extends keyof T, V>(key: K | ((item: T) => V)): T[] {
|
|
8
|
+
return arraySortBy(this as T[], key);
|
|
9
|
+
};
|
|
10
|
+
|
|
11
|
+
|
|
12
|
+
export function arraySortBy<T, K extends keyof T, V>(list: T[], key: K | ((item: T) => V)): T[] {
|
|
13
|
+
const comparator = typeof key === 'function'
|
|
14
|
+
? key
|
|
15
|
+
: (item: T) => item[key];
|
|
16
|
+
|
|
17
|
+
return list.sort((a: T, b: T) => {
|
|
18
|
+
const valA = comparator(a);
|
|
19
|
+
const valB = comparator(b);
|
|
20
|
+
|
|
21
|
+
// Handle undefined or null values by moving them to the end, nulls then undefines
|
|
22
|
+
if (valA === undefined && valB === null) return 1;
|
|
23
|
+
if (valB === undefined && valA === null) return -1;
|
|
24
|
+
if (valA === undefined || valA === null) return 1;
|
|
25
|
+
if (valB === undefined || valB === null) return -1;
|
|
26
|
+
|
|
27
|
+
if (typeof valA === 'number' && typeof valB === 'number') {
|
|
28
|
+
return valA - valB;
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
if (typeof valA === 'string' && typeof valB === 'string') {
|
|
32
|
+
return valA.localeCompare(valB);
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
// Fallback for other types like Dates, objects with custom compare methods, etc.
|
|
36
|
+
if (valA > valB) return 1;
|
|
37
|
+
if (valA < valB) return -1;
|
|
38
|
+
return 0;
|
|
39
|
+
});
|
|
40
|
+
};
|