eclipse-langium / langium

Next-gen language engineering / DSL framework
https://langium.org/
MIT License
663 stars 61 forks source link

Respect the declared/returned type of data type rules, which is relevant for the default value converter #1500

Closed JohannesMeierSE closed 2 weeks ago

JohannesMeierSE commented 1 month ago

This PR contributes:

This issue was found in a real-world application, where a data type rule (with returns number) which referred to a terminal rule (with returns number) produced a string value at runtime in the AST (while the generated property in the TypeScript interface had the correct type number). This conforms to the "number case" in the new test case. The cases for string and boolean are added only to be more complete.

This issue is not related to https://github.com/eclipse-langium/langium/pull/1478.

JohannesMeierSE commented 2 weeks ago

Thanks @msujew for your review! I created two functions, one to be used at runtime and the other one at development time.

However, there are already test cases which check the generated code for data type rules with return type: https://github.com/eclipse-langium/langium/blob/main/packages/langium-cli/test/generator/ast-generator.test.ts#L170 But I don't understand, why they didn't show the regression.

Any ideas?

msujew commented 2 weeks ago

@JohannesMeierSE These tests don't test the generated property types. For example:

Value: id=ID;
ID returns string: ...;

is supposed to generate:

export interface Value extends AstNode {
  id: ID // <-- this is the part that broke
}
export type ID = string;
JohannesMeierSE commented 2 weeks ago

Thanks for the suggestion @msujew! I added two test cases which test properties with data types which have an explicit return type.