Skip to content

Commit e98f2dc

Browse files
committed
refactor: move nesting logic into a method
1 parent 4f5350b commit e98f2dc

File tree

2 files changed

+52
-29
lines changed

2 files changed

+52
-29
lines changed

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

Lines changed: 6 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -347,26 +347,14 @@ function inferTypeFromReferenceLiteral(
347347
isBlockTypeProperty(referenced) ||
348348
isValueTypeProperty(referenced)
349349
) {
350-
const valueType = referenced.valueType;
351-
352-
if (valueType === undefined) {
353-
return undefined;
354-
}
355-
let wrappedValueType = wrapperFactories.ValueType.wrap(valueType);
356-
357-
if (isNestedPropertyAccess(expression)) {
358-
wrappedValueType = expression.nestedAccesses.reduce(
359-
(valueType, access) => {
360-
const accessedProperty = isAtomicValueType(valueType)
361-
? valueType.getProperty(access)?.valueType
362-
: undefined;
363-
return wrapperFactories.ValueType.wrap(accessedProperty);
364-
},
365-
wrappedValueType,
366-
);
350+
const valueType = wrapperFactories.ValueType.wrap(referenced.valueType);
351+
if (!isNestedPropertyAccess(expression)) {
352+
return valueType;
367353
}
368354

369-
return wrappedValueType;
355+
assert(isAtomicValueType(valueType));
356+
const accessedProperty = valueType.getProperty(expression);
357+
return wrapperFactories.ValueType.wrap(accessedProperty?.valueType);
370358
}
371359
assertUnreachable(referenced);
372360
}

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

Lines changed: 46 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ import {
1313
type ValueTypeConstraintInlineDefinition,
1414
type ValuetypeDefinition,
1515
isValueTypeConstraintInlineDefinition,
16+
type NestedPropertyAccess,
17+
isNestedPropertyAccess,
1618
} from '../../generated/ast';
1719
import { type AstNodeWrapper } from '../ast-node-wrapper';
1820
import { type WrapperFactoryProvider } from '../wrapper-factory-provider';
@@ -44,21 +46,32 @@ export class AtomicValueType
4446
return this.astNode?.properties;
4547
}
4648

47-
getProperty(name: string): ValueTypeProperty | undefined;
48-
getProperty(
49-
reference: Reference<ValueTypeProperty>,
50-
validationContext?: ValidationContext,
51-
): ValueTypeProperty | undefined;
52-
getProperty(
53-
reference: string | Reference<ValueTypeProperty>,
54-
validationContext?: ValidationContext,
49+
private getNestedProperty(
50+
nestedPropertyAccess: NestedPropertyAccess,
5551
): ValueTypeProperty | undefined {
56-
let propertyName: string | undefined = undefined;
52+
const property = this.getProperty(nestedPropertyAccess.value);
5753

54+
return nestedPropertyAccess.nestedAccesses.reduce(
55+
(property, propertyName) => {
56+
const valueType = this.wrapperFactories.ValueType.wrap(
57+
property?.valueType,
58+
);
59+
return isAtomicValueType(valueType)
60+
? valueType.getProperty(propertyName)
61+
: undefined;
62+
},
63+
property,
64+
);
65+
}
66+
67+
private resolvePropertyReference(
68+
reference: string | Reference<ValueTypeProperty>,
69+
validationContext?: ValidationContext,
70+
): string | undefined {
5871
if (typeof reference === 'string') {
59-
propertyName = reference;
72+
return reference;
6073
} else if (reference.ref !== undefined) {
61-
propertyName = reference.ref.name;
74+
return reference.ref.name;
6275
} else if (
6376
reference.error !== undefined &&
6477
validationContext !== undefined
@@ -72,7 +85,29 @@ export class AtomicValueType
7285
}
7386
validationContext.accept('error', reference.error.message, info);
7487
}
88+
return undefined;
89+
}
90+
91+
getProperty(name: string): ValueTypeProperty | undefined;
92+
getProperty(
93+
reference: Reference<ValueTypeProperty>,
94+
validationContext?: ValidationContext,
95+
): ValueTypeProperty | undefined;
96+
getProperty(
97+
nestedPropertyAccess: NestedPropertyAccess,
98+
): ValueTypeProperty | undefined;
99+
getProperty(
100+
reference: string | Reference<ValueTypeProperty> | NestedPropertyAccess,
101+
validationContext?: ValidationContext,
102+
): ValueTypeProperty | undefined {
103+
if (isNestedPropertyAccess(reference)) {
104+
return this.getNestedProperty(reference);
105+
}
75106

107+
const propertyName = this.resolvePropertyReference(
108+
reference,
109+
validationContext,
110+
);
76111
return this.astNode?.properties.find(
77112
(property) => property.name == propertyName,
78113
);

0 commit comments

Comments
 (0)