Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(zmodel): allow type names to be used as declaration names #1424

Merged
merged 1 commit into from
May 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
"@typescript-eslint/parser": "^7.6.0",
"concurrently": "^7.4.0",
"copyfiles": "^2.4.1",
"eslint": "^8.56.0",
"eslint": "^8.57.0",
"eslint-plugin-jest": "^28.2.0",
"jest": "^29.7.0",
"replace-in-file": "^7.0.1",
Expand Down
4 changes: 2 additions & 2 deletions packages/language/src/generated/ast.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,10 +78,10 @@ export function isReferenceTarget(item: unknown): item is ReferenceTarget {
return reflection.isInstance(item, ReferenceTarget);
}

export type RegularID = 'abstract' | 'attribute' | 'datasource' | 'enum' | 'import' | 'in' | 'model' | 'plugin' | 'view' | string;
export type RegularID = 'Any' | 'BigInt' | 'Boolean' | 'Bytes' | 'DateTime' | 'Decimal' | 'Float' | 'Int' | 'Json' | 'Null' | 'Object' | 'String' | 'Unsupported' | 'abstract' | 'attribute' | 'datasource' | 'enum' | 'import' | 'in' | 'model' | 'plugin' | 'view' | string;

export function isRegularID(item: unknown): item is RegularID {
return item === 'model' || item === 'enum' || item === 'attribute' || item === 'datasource' || item === 'plugin' || item === 'abstract' || item === 'in' || item === 'view' || item === 'import' || (typeof item === 'string' && (/[_a-zA-Z][\w_]*/.test(item)));
return item === 'model' || item === 'enum' || item === 'attribute' || item === 'datasource' || item === 'plugin' || item === 'abstract' || item === 'in' || item === 'view' || item === 'import' || item === 'String' || item === 'Boolean' || item === 'Int' || item === 'BigInt' || item === 'Float' || item === 'Decimal' || item === 'DateTime' || item === 'Json' || item === 'Bytes' || item === 'Null' || item === 'Object' || item === 'Any' || item === 'Unsupported' || (typeof item === 'string' && (/[_a-zA-Z][\w_]*/.test(item)));
}

export type TypeDeclaration = DataModel | Enum;
Expand Down
52 changes: 52 additions & 0 deletions packages/language/src/generated/grammar.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2701,6 +2701,58 @@ export const ZModelGrammar = (): Grammar => loadedZModelGrammar ?? (loadedZModel
{
"$type": "Keyword",
"value": "import"
},
{
"$type": "Keyword",
"value": "String"
},
{
"$type": "Keyword",
"value": "Boolean"
},
{
"$type": "Keyword",
"value": "Int"
},
{
"$type": "Keyword",
"value": "BigInt"
},
{
"$type": "Keyword",
"value": "Float"
},
{
"$type": "Keyword",
"value": "Decimal"
},
{
"$type": "Keyword",
"value": "DateTime"
},
{
"$type": "Keyword",
"value": "Json"
},
{
"$type": "Keyword",
"value": "Bytes"
},
{
"$type": "Keyword",
"value": "Null"
},
{
"$type": "Keyword",
"value": "Object"
},
{
"$type": "Keyword",
"value": "Any"
},
{
"$type": "Keyword",
"value": "Unsupported"
}
]
},
Expand Down
2 changes: 1 addition & 1 deletion packages/language/src/zmodel.langium
Original file line number Diff line number Diff line change
Expand Up @@ -224,7 +224,7 @@ FunctionParamType:
// https://github.com/langium/langium/discussions/1012
RegularID returns string:
// include keywords that we'd like to work as ID in most places
ID | 'model' | 'enum' | 'attribute' | 'datasource' | 'plugin' | 'abstract' | 'in' | 'view' | 'import';
ID | 'model' | 'enum' | 'attribute' | 'datasource' | 'plugin' | 'abstract' | 'in' | 'view' | 'import' | 'String' | 'Boolean' | 'Int' | 'BigInt' | 'Float' | 'Decimal' | 'DateTime' | 'Json' | 'Bytes' | 'Null' | 'Object' | 'Any' | 'Unsupported';

// attribute
Attribute:
Expand Down
2 changes: 1 addition & 1 deletion pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

32 changes: 16 additions & 16 deletions tests/integration/tests/e2e/type-coverage.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,14 @@ describe('Type Coverage Tests', () => {
model Foo {
id String @id @default(cuid())

string String
int Int
bigInt BigInt
date DateTime
float Float
decimal Decimal
boolean Boolean
bytes Bytes
String String
Int Int
BigInt BigInt
DateTime DateTime
Float Float
Decimal Decimal
Boolean Boolean
Bytes Bytes

@@allow('all', true)
}
Expand All @@ -41,14 +41,14 @@ describe('Type Coverage Tests', () => {
const date = new Date();
const data = {
id: '1',
string: 'string',
int: 100,
bigInt: BigInt(9007199254740991),
date,
float: 1.23,
decimal: new Decimal(1.2345),
boolean: true,
bytes: Buffer.from('hello'),
String: 'string',
Int: 100,
BigInt: BigInt(9007199254740991),
DateTime: date,
Float: 1.23,
Decimal: new Decimal(1.2345),
Boolean: true,
Bytes: Buffer.from('hello'),
};

await db.foo.create({
Expand Down