Skip to content

Commit 45ca2a5

Browse files
authored
refactor: remove usages of radash functions (#660)
### Summary of Changes We were only using three functions from `radash` (`isEmpty`, `group`, `last`). These are now replaced by custom implementations and the dependency to `radash` is removed.
1 parent c52b5e6 commit 45ca2a5

32 files changed

+211
-149
lines changed

.github/dependabot.yml

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -20,15 +20,3 @@ updates:
2020
directory: '/docs'
2121
schedule:
2222
interval: 'monthly'
23-
24-
# DSL
25-
- package-ecosystem: 'npm'
26-
directory: '/DSL'
27-
schedule:
28-
interval: 'monthly'
29-
30-
# Runner
31-
- package-ecosystem: 'pip'
32-
directory: '/Runner'
33-
schedule:
34-
interval: 'monthly'

package-lock.json

Lines changed: 0 additions & 14 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,6 @@
106106
"commander": "^11.1.0",
107107
"glob": "^10.3.10",
108108
"langium": "^2.0.2",
109-
"radash": "^11.0.0",
110109
"true-myth": "^7.1.0",
111110
"vscode-languageclient": "^9.0.1",
112111
"vscode-languageserver": "^9.0.1"

src/cli/generator.ts

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,6 @@ import {
6666
moduleMembersOrEmpty,
6767
statementsOrEmpty,
6868
} from '../language/helpers/nodeProperties.js';
69-
import { group } from 'radash';
7069
import { IdManager } from '../language/helpers/idManager.js';
7170
import { isInStubFile } from '../language/helpers/fileExtensions.js';
7271
import {
@@ -76,6 +75,7 @@ import {
7675
NullConstant,
7776
StringConstant,
7877
} from '../language/partialEvaluation/model.js';
78+
import { groupBy } from '../helpers/collectionUtils.js';
7979

8080
export const CODEGEN_PREFIX = '__gen_';
8181
const BLOCK_LAMBDA_PREFIX = `${CODEGEN_PREFIX}block_lambda_`;
@@ -229,16 +229,16 @@ const generatePipeline = function (
229229
};
230230

231231
const generateImports = function (importSet: ImportData[]): string[] {
232-
const qualifiedImports = Array.from(importSet)
232+
const qualifiedImports = importSet
233233
.filter((importStmt) => importStmt.declarationName === undefined)
234234
.sort((a, b) => a.importPath.localeCompare(b.importPath))
235235
.map(generateQualifiedImport);
236-
const groupedImports = Object.entries(
237-
group(
238-
Array.from(importSet).filter((importStmt) => importStmt.declarationName !== undefined),
239-
(importStmt) => importStmt.importPath,
240-
),
241-
).sort(([key1, _value1], [key2, _value2]) => key1.localeCompare(key2));
236+
const groupedImports = groupBy(
237+
importSet.filter((importStmt) => importStmt.declarationName !== undefined),
238+
(importStmt) => importStmt.importPath,
239+
)
240+
.toArray()
241+
.sort(([key1, _value1], [key2, _value2]) => key1.localeCompare(key2));
242242
const declaredImports: string[] = [];
243243
for (const [key, value] of groupedImports) {
244244
const importedDecls =

src/helpers/collectionUtils.ts

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
import { Stream, stream } from 'langium';
2+
3+
/**
4+
* Returns the values of the iterable that are labeled the same as a previous value. The first value with a label is
5+
* not included. Neither are values with an `undefined` label.
6+
*
7+
* @example
8+
* const id = (value: any) => value;
9+
* duplicatesBy([1, 2, 1, 3], id) // [1]
10+
*/
11+
export const duplicatesBy = <T, K>(iterable: Iterable<T>, labeler: (element: T) => K | undefined): Stream<T> => {
12+
return stream(duplicatedByGenerator(iterable, labeler));
13+
};
14+
15+
const duplicatedByGenerator = function* <T, K>(
16+
iterable: Iterable<T>,
17+
labeler: (element: T) => K | undefined,
18+
): Generator<T, void> {
19+
const knownLabels = new Set<K>();
20+
21+
for (const value of iterable) {
22+
const label = labeler(value);
23+
if (label === undefined) {
24+
continue;
25+
}
26+
27+
if (knownLabels.has(label)) {
28+
yield value;
29+
} else {
30+
knownLabels.add(label);
31+
}
32+
}
33+
};
34+
35+
/**
36+
* Returns the values of the iterable grouped by their label. Values with an `undefined` label are not included.
37+
*/
38+
export const groupBy = <T, K>(iterable: Iterable<T>, labeler: (element: T) => K | undefined): Stream<[K, T[]]> => {
39+
const groups = new Map<K, T[]>();
40+
41+
for (const value of iterable) {
42+
const label = labeler(value);
43+
if (label === undefined) {
44+
continue;
45+
}
46+
47+
const group = groups.get(label);
48+
if (group === undefined) {
49+
groups.set(label, [value]);
50+
} else {
51+
group.push(value);
52+
}
53+
}
54+
55+
return stream(groups.entries());
56+
};
57+
58+
/**
59+
* Returns whether the iterable has no values.
60+
*/
61+
export const isEmpty = (iterable: Iterable<unknown>): boolean => {
62+
return iterable[Symbol.iterator]().next().done === true;
63+
};
64+
65+
/**
66+
* Returns the last element of the array, or `undefined` if the array is empty.
67+
*/
68+
export const last = <T>(array: T[]): T | undefined => {
69+
return array[array.length - 1];
70+
};
71+
72+
/**
73+
* Returns the unique value in the iterable, or `undefined` if none or multiple exist.
74+
*/
75+
export const uniqueOrUndefined = <T>(iterable: Iterable<T>): T | undefined => {
76+
const iterator = iterable[Symbol.iterator]();
77+
const { value: first } = iterator.next();
78+
const { done } = iterator.next();
79+
80+
if (done) {
81+
return first;
82+
} else {
83+
return undefined;
84+
}
85+
};
File renamed without changes.

src/language/helpers/collectionUtils.ts

Lines changed: 0 additions & 34 deletions
This file was deleted.

src/language/helpers/idManager.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/**
2-
* Handles the mapping of objects, usually nodes of an Safe-DS AST, to their IDs.
2+
* Handles the mapping of objects, usually nodes of a Safe-DS AST, to their IDs.
33
*/
44
export class IdManager<T extends WeakKey> {
55
/**

src/language/lsp/safe-ds-formatter.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,12 @@ import {
1010
} from 'langium';
1111
import * as ast from '../generated/ast.js';
1212
import { annotationCallsOrEmpty, literalsOrEmpty, typeArgumentsOrEmpty } from '../helpers/nodeProperties.js';
13-
import { last } from 'radash';
1413
import noSpace = Formatting.noSpace;
1514
import newLine = Formatting.newLine;
1615
import newLines = Formatting.newLines;
1716
import oneSpace = Formatting.oneSpace;
1817
import indent = Formatting.indent;
18+
import { last } from '../../helpers/collectionUtils.js';
1919

2020
const newLinesWithIndent = function (count: number, options?: FormattingActionOptions): FormattingAction {
2121
return {

src/language/partialEvaluation/model.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import {
1010
} from '../generated/ast.js';
1111
import { stream } from 'langium';
1212
import { parametersOrEmpty } from '../helpers/nodeProperties.js';
13-
import { isEmpty } from 'radash';
13+
import { isEmpty } from '../../helpers/collectionUtils.js';
1414

1515
export type ParameterSubstitutions = Map<SdsParameter, EvaluatedNode>;
1616
export type ResultSubstitutions = Map<SdsAbstractResult, EvaluatedNode>;

0 commit comments

Comments
 (0)