Skip to content

Commit 89c0a9d

Browse files
authored
do not require properties with default values in rules (#1863)
1 parent 081caa0 commit 89c0a9d

File tree

2 files changed

+17
-2
lines changed

2 files changed

+17
-2
lines changed

packages/langium/src/grammar/validation/types-validator.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -413,7 +413,7 @@ function validatePropertiesConsistency(
413413
const missingProps = new Set<string>();
414414
for (const [name, expectedProperties] of declaredProps.entries()) {
415415
const foundProperty = allInferredProps.get(name);
416-
if (!foundProperty && !isOptionalProperty(expectedProperties)) {
416+
if (!foundProperty && !isOptionalProperty(expectedProperties) && expectedProperties.defaultValue === undefined) {
417417
missingProps.add(name);
418418
}
419419
}

packages/langium/test/grammar/type-system/type-validator.test.ts

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,22 @@ describe('validate params in types', () => {
3939
expect(d.range.start).toEqual({ character: 8, line: 4 });
4040
expect(d.range.end).toEqual({ character: 10, line: 4 });
4141
});
42-
42+
// verifies that properties with default value are not required
43+
test('verify property with default value not required, for single rule', async () => {
44+
const prog = `
45+
interface B {
46+
name:string=''
47+
count?:string
48+
}
49+
X2 returns B: count=ID;
50+
terminal ID: /[a-zA-Z_][\\w_]*/;
51+
`.trim();
52+
// verify we don't have error on 'name' property
53+
const document = await parseDocument(grammarServices, prog);
54+
let diagnostics: Diagnostic[] = await grammarServices.validation.DocumentValidator.validateDocument(document);
55+
diagnostics = diagnostics.filter(d => d.severity === DiagnosticSeverity.Error);
56+
expect(diagnostics).toHaveLength(0);
57+
});
4358
// verifies that missing required params use the right msg & position
4459
test('verify missing required param error is present for the correct rule', async () => {
4560
const prog = `

0 commit comments

Comments
 (0)