Skip to content

Commit 368e22c

Browse files
authored
Merge pull request #674 from jvalue/no-structured-clone
[Refractor] Replace `structuredClone()` with a jayvee specific clone method
2 parents 6c0dd9c + 7493f7b commit 368e22c

File tree

4 files changed

+90
-36
lines changed

4 files changed

+90
-36
lines changed

libs/execution/src/lib/types/io-types/table.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import {
1010
type InternalValueRepresentation,
1111
type TextValuetype,
1212
type ValueType,
13+
cloneInternalValue,
1314
} from '@jvalue/jayvee-language-server';
1415

1516
import { SQLColumnTypeVisitor } from '../value-types/visitors/sql-column-type-visitor';
@@ -191,7 +192,7 @@ export class Table implements IOTypeImplementation<IOType.TABLE> {
191192
cloned.numberOfRows = this.numberOfRows;
192193
[...this.columns.entries()].forEach(([columnName, column]) => {
193194
cloned.addColumn(columnName, {
194-
values: structuredClone(column.values),
195+
values: cloneInternalValue(column.values),
195196
valueType: column.valueType,
196197
});
197198
});

libs/language-server/src/lib/ast/expressions/evaluators/in-operator-evaluator.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,13 +31,13 @@ export class InOperatorEvaluator extends DefaultBinaryOperatorEvaluator<
3131

3232
const isLeftOperandMatchingValueRepresentationTypeguard: InternalValueRepresentationTypeguard<
3333
string | number
34-
> = (value: InternalValueRepresentation): value is string | number => {
34+
> = (value) => {
3535
return STRING_TYPEGUARD(value) || NUMBER_TYPEGUARD(value);
3636
};
3737

3838
const isRightOperandMatchingValueRepresentationTypeguard: InternalValueRepresentationTypeguard<
3939
(string | number)[]
40-
> = (value: InternalValueRepresentation): value is (string | number)[] => {
40+
> = (value) => {
4141
return (
4242
Array.isArray(value) &&
4343
value.every(isLeftOperandMatchingValueRepresentationTypeguard)

libs/language-server/src/lib/ast/expressions/internal-value-representation.ts

Lines changed: 32 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
//
33
// SPDX-License-Identifier: AGPL-3.0-only
44

5+
import { assertUnreachable, isAstNode } from 'langium';
6+
57
import {
68
type BlockTypeProperty,
79
type CellRangeLiteral,
@@ -16,10 +18,11 @@ import {
1618
} from '../generated/ast';
1719
import type { WrapperFactoryProvider } from '../wrappers';
1820

21+
import { COLLECTION_TYPEGUARD } from './typeguards';
22+
1923
export type InternalValueRepresentation =
2024
| AtomicInternalValueRepresentation
21-
| InternalValueRepresentation[]
22-
| [];
25+
| InternalValueRepresentation[];
2326

2427
export type AtomicInternalValueRepresentation =
2528
| boolean
@@ -34,7 +37,7 @@ export type AtomicInternalValueRepresentation =
3437

3538
export type InternalValueRepresentationTypeguard<
3639
T extends InternalValueRepresentation,
37-
> = (value: InternalValueRepresentation) => value is T;
40+
> = (value: unknown) => value is T;
3841

3942
export function internalValueToString(
4043
valueRepresentation: InternalValueRepresentation,
@@ -87,7 +90,30 @@ export function internalValueToString(
8790
if (isBlockTypeProperty(valueRepresentation)) {
8891
return valueRepresentation.name;
8992
}
90-
throw new Error(
91-
'Convert of this InternalValueRepresentation is not implemented',
92-
);
93+
assertUnreachable(valueRepresentation);
94+
}
95+
96+
export function cloneInternalValue<T extends InternalValueRepresentation>(
97+
valueRepresentation: T,
98+
): T {
99+
if (COLLECTION_TYPEGUARD(valueRepresentation)) {
100+
return valueRepresentation.map(cloneInternalValue) as T;
101+
}
102+
103+
if (
104+
typeof valueRepresentation === 'boolean' ||
105+
typeof valueRepresentation === 'number' ||
106+
typeof valueRepresentation === 'string'
107+
) {
108+
return structuredClone(valueRepresentation);
109+
}
110+
if (valueRepresentation instanceof RegExp) {
111+
const cloned = structuredClone(valueRepresentation);
112+
cloned.lastIndex = valueRepresentation.lastIndex;
113+
return cloned;
114+
}
115+
if (isAstNode(valueRepresentation)) {
116+
return valueRepresentation;
117+
}
118+
assertUnreachable(valueRepresentation);
93119
}

libs/language-server/src/lib/ast/expressions/typeguards.ts

Lines changed: 54 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -3,57 +3,84 @@
33
// SPDX-License-Identifier: AGPL-3.0-only
44

55
import {
6+
type BlockTypeProperty,
7+
type CellRangeLiteral,
8+
type ConstraintDefinition,
9+
type TransformDefinition,
610
type ValuetypeAssignment,
11+
isBlockTypeProperty,
12+
isCellRangeLiteral,
13+
isConstraintDefinition,
14+
isTransformDefinition,
715
isValuetypeAssignment,
816
} from '../generated/ast';
917

1018
import {
19+
type AtomicInternalValueRepresentation,
1120
type InternalValueRepresentation,
1221
type InternalValueRepresentationTypeguard,
1322
} from './internal-value-representation';
1423

1524
export const INTERNAL_VALUE_REPRESENTATION_TYPEGUARD: InternalValueRepresentationTypeguard<
1625
InternalValueRepresentation
17-
> = (
18-
value: InternalValueRepresentation,
19-
): value is InternalValueRepresentation => {
20-
return true;
26+
> = (value): value is InternalValueRepresentation => {
27+
return (
28+
ATOMIC_INTERNAL_VALUE_REPRESENTATION_TYPEGUARD(value) ||
29+
COLLECTION_TYPEGUARD(value)
30+
);
2131
};
32+
33+
export const ATOMIC_INTERNAL_VALUE_REPRESENTATION_TYPEGUARD: InternalValueRepresentationTypeguard<
34+
AtomicInternalValueRepresentation
35+
> = (value): value is AtomicInternalValueRepresentation =>
36+
BOOLEAN_TYPEGUARD(value) ||
37+
NUMBER_TYPEGUARD(value) ||
38+
STRING_TYPEGUARD(value) ||
39+
CELLRANGELITERAL_TYPEGUARD(value) ||
40+
CONSTRAINTDEFINITION_TYPEGUARD(value) ||
41+
VALUETYPEASSIGNMENT_TYPEGUARD(value) ||
42+
BLOCKTYPEPROPERTY_TYPEGUARD(value) ||
43+
TRANSFORMDEFINITION_TYPEGUARD(value);
44+
2245
export const NUMBER_TYPEGUARD: InternalValueRepresentationTypeguard<number> = (
23-
value: InternalValueRepresentation,
24-
): value is number => {
25-
return typeof value === 'number';
26-
};
46+
value,
47+
) => typeof value === 'number';
2748
export const BOOLEAN_TYPEGUARD: InternalValueRepresentationTypeguard<
2849
boolean
29-
> = (value: InternalValueRepresentation): value is boolean => {
30-
return typeof value === 'boolean';
31-
};
50+
> = (value) => typeof value === 'boolean';
3251
export const STRING_TYPEGUARD: InternalValueRepresentationTypeguard<string> = (
33-
value: InternalValueRepresentation,
34-
): value is string => {
35-
return typeof value === 'string';
36-
};
52+
value,
53+
) => typeof value === 'string';
3754

3855
export const REGEXP_TYPEGUARD: InternalValueRepresentationTypeguard<RegExp> = (
39-
value: InternalValueRepresentation,
40-
): value is RegExp => {
41-
return value instanceof RegExp;
42-
};
56+
value,
57+
) => value instanceof RegExp;
58+
59+
export const CELLRANGELITERAL_TYPEGUARD: InternalValueRepresentationTypeguard<
60+
CellRangeLiteral
61+
> = (value) => isCellRangeLiteral(value);
62+
63+
export const CONSTRAINTDEFINITION_TYPEGUARD: InternalValueRepresentationTypeguard<
64+
ConstraintDefinition
65+
> = (value) => isConstraintDefinition(value);
4366

4467
export const VALUETYPEASSIGNMENT_TYPEGUARD: InternalValueRepresentationTypeguard<
4568
ValuetypeAssignment
46-
> = (value: InternalValueRepresentation): value is ValuetypeAssignment => {
47-
return isValuetypeAssignment(value);
48-
};
69+
> = (value) => isValuetypeAssignment(value);
70+
71+
export const BLOCKTYPEPROPERTY_TYPEGUARD: InternalValueRepresentationTypeguard<
72+
BlockTypeProperty
73+
> = (value) => isBlockTypeProperty(value);
74+
75+
export const TRANSFORMDEFINITION_TYPEGUARD: InternalValueRepresentationTypeguard<
76+
TransformDefinition
77+
> = (value) => isTransformDefinition(value);
4978

5079
export const COLLECTION_TYPEGUARD: InternalValueRepresentationTypeguard<
5180
InternalValueRepresentation[]
52-
> = (
53-
value: InternalValueRepresentation,
54-
): value is InternalValueRepresentation[] => {
55-
return Array.isArray(value);
56-
};
81+
> = (value) =>
82+
Array.isArray(value) &&
83+
value.every((v) => INTERNAL_VALUE_REPRESENTATION_TYPEGUARD(v));
5784

5885
export function isEveryValueDefined<T>(array: (T | undefined)[]): array is T[] {
5986
return array.every((value) => value !== undefined);

0 commit comments

Comments
 (0)