langium icon indicating copy to clipboard operation
langium copied to clipboard

Name collision in generated ast.ts

Open ydaveluy opened this issue 1 year ago • 2 comments

When defining in mydsl.langium file an interface named Reference, the generated ast.ts does not compile.

Langium version: 3.1 Package name: langium

Steps To Reproduce

define an interface named 'Reference' in mydsl.langium

interface Reference {
name:string
}

Build the project

The current behavior

The project fails to build due to a conflict with imported types from langium in src/language/generated/ast.ts: import type { AstNode, Reference, ReferenceInfo, TypeMetaData } from 'langium';

The expected behavior

Definition of interfaces like AstNode, Reference, ReferenceInfo or TypeMetaData in langium file should not collide with imports.

ydaveluy avatar Jul 21 '24 14:07 ydaveluy

We have a validation for protecting against name collisions with the JavaScript runtime. See here. We would need a separate list for reserved names of the Langium imports.

msujew avatar Jul 21 '24 14:07 msujew

Another alternative would be to import types with a specific prefix (e.g. _langium_) and to prohibit rule names in the grammar from starting with this prefix ?

e.g:

import type { AstNode as _langium_AstNode, 
Reference as _langium_Reference, 
ReferenceInfo as _langium_ReferenceInfo, 
TypeMetaData as _langium_TypeMetaData } from 'langium';

I don't know if this concept would be valid for other reserved keywords.

ydaveluy avatar Jul 22 '24 12:07 ydaveluy

Hey @msujew,

I can try to provide a PR for this issue.

I think that doing a single import like

import * as langium from 'langium';

and adding the 'langium' keyword to reserved names would be more generic.

ydaveluy avatar Mar 12 '25 17:03 ydaveluy

Sure, that sounds reasonable to me 👍

msujew avatar Mar 12 '25 17:03 msujew