Replace hello-world-validator.ts with a basic standard checkUniqueDefs implementation:
import type { ValidationAcceptor, ValidationChecks } from 'langium';
import type { HelloWorldAstType, Model } from './generated/ast.js';
import type { HelloWorldServices } from './hello-world-module.js';
/**
* Register custom validation checks.
*/
export function registerValidationChecks(services: HelloWorldServices) {
const registry = services.validation.ValidationRegistry;
const validator = services.validation.HelloWorldValidator;
const checks: ValidationChecks<HelloWorldAstType> = {
Model: validator.checkUniqueDefs
};
registry.register(checks, validator);
}
/**
* Implementation of custom validations.
*/
export class HelloWorldValidator {
checkUniqueDefs(model: Model, accept: ValidationAcceptor): void {
const reported = new Set();
model.persons.forEach(d => {
if (reported.has(d.name)) {
accept('error', `non-unique name '${d.name}'.`, { node: d, property: 'name' });
}
reported.add(d.name);
});
}
}
Attempt to validate the following file:
header;
See the error in the console
An error occurred during validation: TypeError: Cannot read properties of undefined (reading 'forEach')
at HelloWorldValidator.checkUniqueDefs (/home/david/hello-world/out/language/main.cjs:33902:19)
at /home/david/hello-world/out/language/main.cjs:14698:21
at /home/david/hello-world/out/language/main.cjs:17481:15
at async Promise.all (index 0)
at async DefaultDocumentValidator.validateAst (/home/david/hello-world/out/language/main.cjs:17477:5)
at async DefaultDocumentValidator.validateDocument (/home/david/hello-world/out/language/main.cjs:17394:27)
at async DefaultDocumentBuilder.validate (/home/david/hello-world/out/language/main.cjs:32812:25)
at async DefaultDocumentBuilder.runCancelable (/home/david/hello-world/out/language/main.cjs:32774:7)
at async DefaultDocumentBuilder.buildDocuments (/home/david/hello-world/out/language/main.cjs:32749:5)
at async DefaultDocumentBuilder.update (/home/david/hello-world/out/language/main.cjs:32706:5)
TypeError: Cannot read properties of undefined (reading 'forEach')
at HelloWorldValidator.checkUniqueDefs (/home/david/hello-world/out/language/main.cjs:33902:19)
at /home/david/hello-world/out/language/main.cjs:14698:21
at /home/david/hello-world/out/language/main.cjs:17481:15
at async Promise.all (index 0)
at async DefaultDocumentValidator.validateAst (/home/david/hello-world/out/language/main.cjs:17477:5)
at async DefaultDocumentValidator.validateDocument (/home/david/hello-world/out/language/main.cjs:17394:27)
at async DefaultDocumentBuilder.validate (/home/david/hello-world/out/language/main.cjs:32812:25)
at async DefaultDocumentBuilder.runCancelable (/home/david/hello-world/out/language/main.cjs:32774:7)
at async DefaultDocumentBuilder.buildDocuments (/home/david/hello-world/out/language/main.cjs:32749:5)
at async DefaultDocumentBuilder.update (/home/david/hello-world/out/language/main.cjs:32706:5)
The cause of this error is that model.persons is undefined rather than [] as required by the attribute type. This may be related to to #1358 because the error does not occur if the SEP after the header is any of the following things:
Langium version: 2.1.0 Package name: hello-world
Steps To Reproduce
yo langium
hello-world.langium
with the following:hello-world-validator.ts
with a basic standardcheckUniqueDefs
implementation:The cause of this error is that
model.persons
isundefined
rather than[]
as required by the attribute type. This may be related to to #1358 because the error does not occur if theSEP
after the header is any of the following things:';'