Skip to content

Commit 24bf1f7

Browse files
committed
style: pr review nitpicks
1 parent 7e1ce76 commit 24bf1f7

File tree

11 files changed

+55
-38
lines changed

11 files changed

+55
-38
lines changed

libs/execution/src/lib/constraints/constraint-executor.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,8 @@ export class ConstraintExecutor<
3636
if (properties === undefined) {
3737
context.evaluationContext.setValueForValueKeyword(value);
3838
} else {
39-
const assignment_for_type_system: ValueTypeProperty[] = properties;
40-
for (const property of assignment_for_type_system) {
39+
const assignmentForTypeSystem: ValueTypeProperty[] = properties;
40+
for (const property of assignmentForTypeSystem) {
4141
context.evaluationContext.setValueForReference(property.name, value);
4242
}
4343
}

libs/execution/src/lib/types/value-types/internal-representation-parsing.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ import {
1717
type TextValuetype,
1818
type ValueType,
1919
ValueTypeVisitor,
20-
collapseArray,
20+
onlyElementOrUndefined,
2121
internalValueToString,
2222
isCellRangeLiteral,
2323
} from '@jvalue/jayvee-language-server';
@@ -107,7 +107,7 @@ class InternalRepresentationParserVisitor extends ValueTypeVisitor<
107107
): InternalValidValueRepresentation | InvalidValue {
108108
const containedTypes = valueType.getContainedTypes();
109109
assert(containedTypes !== undefined);
110-
const containedType = collapseArray(containedTypes);
110+
const containedType = onlyElementOrUndefined(containedTypes);
111111
if (containedType === undefined) {
112112
return new InvalidValue(
113113
`Cannot parse value types with multiple properties`,

libs/execution/src/lib/types/value-types/visitors/sql-column-type-visitor.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import { strict as assert } from 'assert';
77

88
import {
99
type AtomicValueType,
10-
collapseArray,
10+
onlyElementOrUndefined,
1111
ValueTypeVisitor,
1212
} from '@jvalue/jayvee-language-server';
1313

@@ -31,7 +31,7 @@ export class SQLColumnTypeVisitor extends ValueTypeVisitor<string> {
3131
override visitAtomicValueType(valueType: AtomicValueType): string {
3232
const containedTypes = valueType.getContainedTypes();
3333
assert(containedTypes !== undefined);
34-
const containedType = collapseArray(containedTypes);
34+
const containedType = onlyElementOrUndefined(containedTypes);
3535
if (containedType === undefined) {
3636
throw new Error(
3737
'Can only determine sql column type for value types with one property',

libs/execution/src/lib/types/value-types/visitors/sql-value-representation-visitor.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import { strict as assert } from 'assert';
88
import {
99
type AtomicValueType,
1010
type BooleanValuetype,
11-
collapseArray,
11+
onlyElementOrUndefined,
1212
type DecimalValuetype,
1313
ERROR_TYPEGUARD,
1414
type IntegerValuetype,
@@ -72,7 +72,7 @@ export class SQLValueRepresentationVisitor extends ValueTypeVisitor<
7272
) => string {
7373
const containedTypes = valueType.getContainedTypes();
7474
assert(containedTypes !== undefined);
75-
const containedType = collapseArray(containedTypes);
75+
const containedType = onlyElementOrUndefined(containedTypes);
7676
if (containedType === undefined) {
7777
throw new Error(
7878
'Can only determine sql value representation for value types with one' +

libs/language-server/src/lib/ast/expressions/type-inference.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ import {
4747
pickCommonAtomicValueType,
4848
pickCommonPrimitiveValuetype,
4949
} from '../wrappers/util/value-type-util';
50-
import { collapseArray } from '../../util';
50+
import { onlyElementOrUndefined } from '../../util';
5151

5252
/**
5353
* @returns The inferred ValueType. `undefined` means that any type could be
@@ -240,7 +240,7 @@ function inferCollectionType(
240240
return valueTypeProvider.EmptyCollection;
241241
}
242242
if (elementValuetypes.length === 1) {
243-
const elementValueType = collapseArray(elementValuetypes);
243+
const elementValueType = onlyElementOrUndefined(elementValuetypes);
244244
assert(elementValueType !== undefined);
245245
return valueTypeProvider.createCollectionValueTypeOf(elementValueType);
246246
}

libs/language-server/src/lib/ast/wrappers/util/value-type-util.ts

Lines changed: 26 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -14,27 +14,36 @@ import {
1414
isPrimitiveValueType,
1515
} from '../value-type/primitive';
1616
import { type ValueType } from '../value-type/value-type';
17-
import { collapseArray } from '../../../util';
17+
import { onlyElementOrUndefined } from '../../../util';
18+
19+
function getBasePrimitiveValueType(
20+
valueType: ValueType,
21+
): PrimitiveValueType | undefined {
22+
while (!isPrimitiveValueType(valueType)) {
23+
const containedTypes = valueType.getContainedTypes();
24+
assert(
25+
containedTypes !== undefined,
26+
'non-primitive value types always have at least one contained type',
27+
);
28+
const containedType = onlyElementOrUndefined(containedTypes);
29+
if (containedType === undefined) {
30+
return undefined;
31+
}
32+
valueType = containedType;
33+
}
34+
return valueType;
35+
}
1836

1937
export function pickCommonPrimitiveValuetype(
2038
valueTypes: ValueType[],
2139
): PrimitiveValueType | undefined {
22-
const primitiveValueTypes = valueTypes.flatMap((valueType) => {
23-
while (!isPrimitiveValueType(valueType)) {
24-
const containedTypes = valueType.getContainedTypes();
25-
if (containedTypes === undefined) {
26-
return [];
27-
}
28-
const containedType = collapseArray(containedTypes);
29-
if (containedType === undefined) {
30-
return [];
31-
}
32-
valueType = containedType;
40+
const primitiveValueTypes: PrimitiveValueType[] = [];
41+
for (const valueType of valueTypes) {
42+
const primitiveValueType = getBasePrimitiveValueType(valueType);
43+
if (primitiveValueType === undefined) {
44+
return undefined;
3345
}
34-
return [valueType];
35-
});
36-
if (primitiveValueTypes.length !== valueTypes.length) {
37-
return undefined;
46+
primitiveValueTypes.push(primitiveValueType);
3847
}
3948

4049
let resultingType = primitiveValueTypes.pop();
@@ -86,7 +95,7 @@ export function pickCommonAtomicValueType(
8695
} else if (isAtomicValueType(valueType)) {
8796
const containedTypes = valueType.getContainedTypes();
8897
assert(containedTypes !== undefined);
89-
const containedType = collapseArray(containedTypes);
98+
const containedType = onlyElementOrUndefined(containedTypes);
9099
if (containedType === undefined) {
91100
return [];
92101
}

libs/language-server/src/lib/ast/wrappers/value-type/abstract-value-type.ts

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ export abstract class AbstractValueType<
1616
abstract acceptVisitor<R>(visitor: ValueTypeVisitor<R>): R;
1717

1818
getContainedTypes(): ValueType[] | undefined {
19-
if (this.typeCycleIndex() !== undefined) {
19+
if (this.getIndexOfFirstPropertyInATypeCycle() !== undefined) {
2020
return undefined;
2121
}
2222
return this.doGetContainedTypes();
@@ -42,15 +42,19 @@ export abstract class AbstractValueType<
4242

4343
abstract getName(): string;
4444

45-
typeCycleIndex(visited: ValueType[] = []): number | undefined {
45+
getIndexOfFirstPropertyInATypeCycle(
46+
visited: ValueType[] = [],
47+
): number | undefined {
4648
const cycleDetected = visited.some((v) => v.equals(this));
4749
if (cycleDetected) {
4850
return -1;
4951
}
5052
visited.push(this);
5153

5254
const idx = this.doGetContainedTypes().findIndex(
53-
(containedType) => containedType.typeCycleIndex(visited) !== undefined,
55+
(containedType) =>
56+
containedType.getIndexOfFirstPropertyInATypeCycle(visited) !==
57+
undefined,
5458
);
5559
return idx !== -1 ? idx : undefined;
5660
}

libs/language-server/src/lib/ast/wrappers/value-type/atomic-value-type.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ import { type WrapperFactoryProvider } from '../wrapper-factory-provider';
2020
import { AbstractValueType } from './abstract-value-type';
2121
import { type ValueTypeProvider } from './primitive';
2222
import { type ValueType, type ValueTypeVisitor } from './value-type';
23-
import { collapseArray } from '../../../util';
23+
import { onlyElementOrUndefined } from '../../../util';
2424

2525
export class AtomicValueType
2626
extends AbstractValueType<InternalValidValueRepresentation>
@@ -88,7 +88,7 @@ export class AtomicValueType
8888
if (containedTypes === undefined) {
8989
return false;
9090
}
91-
const containedType = collapseArray(containedTypes);
91+
const containedType = onlyElementOrUndefined(containedTypes);
9292
if (containedType === undefined) {
9393
return false;
9494
}
@@ -138,7 +138,7 @@ export class AtomicValueType
138138
if (containedTypes === undefined) {
139139
return false;
140140
}
141-
const containedType = collapseArray(containedTypes);
141+
const containedType = onlyElementOrUndefined(containedTypes);
142142
if (containedType === undefined) {
143143
return false;
144144
}

libs/language-server/src/lib/ast/wrappers/value-type/value-type.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,9 @@ export interface ValueType<
6565
/**
6666
* Returns the index of the first contained type that is part of a type cycle
6767
*/
68-
typeCycleIndex(visited?: ValueType[]): number | undefined;
68+
getIndexOfFirstPropertyInATypeCycle(
69+
visited?: ValueType[],
70+
): number | undefined;
6971

7072
isAllowedAsRuntimeParameter(): boolean;
7173
getName(): string;

libs/language-server/src/lib/util/array.ts

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

5-
/// If the array has exactly one element, that element is returned. Otherwise
6-
/// undefined
7-
export function collapseArray<T>(array: T[]): T | undefined {
5+
/**
6+
* If the array has exactly one element, that element is returned. Otherwise
7+
* undefined
8+
*/
9+
export function onlyElementOrUndefined<T>(array: T[]): T | undefined {
810
return array.length == 1 ? array[0] : undefined;
911
}

0 commit comments

Comments
 (0)