Repository navigation
Refactored AST reflection - #1942
Conversation
|
An alternative would be to move the property name constants to a separate structure so we can access them like |
|
Hey @spoenemann, And what about prefixing the metadata properties Something like: BinaryExpression: {
$name: BinaryExpression,
$properties: {
left: {
name: 'left'
},
operator: {
name: 'operator'
},
right: {
name: 'right'
}
},
$superTypes: ['Expression'],
// Property name constants
left: 'left',
operator: 'operator',
right: 'right'
}, |
|
Rethinking this, the property constants serve a very different purpose than the AstReflection service. The former are used in language-specific code to have stable access to AST types so you notice it with compiler errors when something has changed in the grammar spec. The latter is used for generic services that should work for all Langium languages. I like @guiliannewiger's proposal in #1184 (comment) and updated this PR according to that. I first thought it could be a problematic change to generate objects instead of string constants, but if you already have code like The solution is to change the code to |
21e3dab to
6ead8a3
Compare
msujew
left a comment
There was a problem hiding this comment.
Looks great, thank you. I'll mention this in detail when writing the changelog for version 4.0 of Langium.
| export const ${name} = { | ||
| $type: '${name}'${properties.length > 0 ? ',' : ''} | ||
| ${joinToNode(properties.sort((a, b) => a.name.localeCompare(b.name)), prop => `${prop.name}: '${escapeQuotes(prop.name, "'")}'`, { separator: ',', appendNewLineIfNotEmpty: true })} | ||
| } as const; |
There was a problem hiding this comment.
Suggestion: Do we want to add a create function in here as a factory method? This would resolve #1755. However, I'm fine with adding this later in a separate PR.
There was a problem hiding this comment.
Wouldn't you just use normal object notation to create AST nodes programmatically?
Closes #1184.
The new implementation of AstReflection no longer needs overriding any of the generic methods. All the necessary metadata is in the
typesfield.In addition, the new structure is useful for type-safe access to AST property names, e.g.
reflection.types.MyType._myProperty. The underscore prefix is necessary to separate the property name constants from the metadata properties.