Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -210,18 +210,30 @@ function plainToPropertyType(type: PlainPropertyType, union: UnionType | undefin
}

export function mergePropertyTypes(first: PlainPropertyType, second: PlainPropertyType): PlainPropertyType {
const flattenedFirst = flattenPlainType(first);
const flattenedSecond = flattenPlainType(second);
for (const second of flattenedSecond) {
if (!includesType(flattenedFirst, second)) {
flattenedFirst.push(second);
const { union: flattenedFirstUnion, array: flattenedFirstArray } = flattenPlainType(first);
const { union: flattenedSecondUnion, array: flattenedSecondArray } = flattenPlainType(second);
for (const second of flattenedSecondUnion) {
if (!includesType(flattenedFirstUnion, second)) {
flattenedFirstUnion.push(second);
}
}
if (flattenedFirst.length === 1) {
return flattenedFirst[0];
for (const second of flattenedSecondArray) {
if (!includesType(flattenedFirstArray, second)) {
flattenedFirstArray.push(second);
}
}
if (flattenedFirstArray.length > 0) {
flattenedFirstUnion.push({
elementType: flattenedFirstArray.length === 1 ? flattenedFirstArray[0] : {
types: flattenedFirstArray
}
});
}
if (flattenedFirstUnion.length === 1) {
return flattenedFirstUnion[0];
} else {
return {
types: flattenedFirst
types: flattenedFirstUnion
};
}
}
Expand All @@ -244,10 +256,22 @@ function typeEquals(first: PlainPropertyType, second: PlainPropertyType): boolea
}
}

export function flattenPlainType(type: PlainPropertyType): PlainPropertyType[] {
export function flattenPlainType(type: PlainPropertyType): { union: PlainPropertyType[], array: PlainPropertyType[] } {
if (isPlainPropertyUnion(type)) {
return type.types.flatMap(e => flattenPlainType(e));
const flattened = type.types.flatMap(e => flattenPlainType(e));
return {
union: flattened.map(e => e.union).flat(),
array: flattened.map(e => e.array).flat()
};
} else if (isPlainArrayType(type)) {
return {
array: flattenPlainType(type.elementType).union,
union: []
};
} else {
return [type];
return {
array: [],
union: [type]
};
}
}
2 changes: 1 addition & 1 deletion packages/langium/src/grammar/validation/validator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -823,7 +823,7 @@ export class LangiumGrammarValidator {
const flattened = flattenPlainType(plainType);
let hasRef = false;
let hasNonRef = false;
for (const flat of flattened) {
for (const flat of flattened.union.concat(flattened.array)) {
if (isPlainReferenceType(flat)) {
hasRef = true;
} else if (!isPlainReferenceType(flat)) {
Expand Down
24 changes: 24 additions & 0 deletions packages/langium/test/grammar/type-system/inferred-types.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -798,6 +798,30 @@ describe('types of `$container` and `$type` are correct', () => {
}
`);
});

test('Should merge types of array properties', async () => {
const expected = expandToString`
export interface A extends AstNode {
readonly $type: 'A';
values: Array<number | string>;
}
`;
await expectTypes(`
A: values+=ID values+=NUMBER;
terminal ID returns string: /string/;
terminal NUMBER returns number: /number/;
`, expected);
await expectTypes(`
A: (values+=ID | values+=NUMBER)+;
terminal ID returns string: /string/;
terminal NUMBER returns number: /number/;
`, expected);
await expectTypes(`
A: values+=(ID | NUMBER)+;
terminal ID returns string: /string/;
terminal NUMBER returns number: /number/;
`, expected);
});
});

// https://github.com/eclipse-langium/langium/issues/744
Expand Down