zenstack 0.4.2 → 0.6.0-pre.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (63) hide show
  1. package/{LICENSE.md → LICENSE} +0 -0
  2. package/bin/cli +1 -1
  3. package/package.json +18 -13
  4. package/bin/post-install.js +0 -24
  5. package/bundle/asset/logo-256-bg.png +0 -0
  6. package/bundle/asset/logo-dark-256.png +0 -0
  7. package/bundle/asset/logo-light-256.png +0 -0
  8. package/bundle/cli/index.js +0 -6952
  9. package/bundle/extension.js +0 -39
  10. package/bundle/language-server/main.js +0 -6208
  11. package/bundle/res/package.template.json +0 -9
  12. package/bundle/res/prism-zmodel.js +0 -22
  13. package/bundle/res/stdlib.zmodel +0 -218
  14. package/bundle/res/tsconfig.template.json +0 -17
  15. package/src/cli/cli-error.ts +0 -4
  16. package/src/cli/cli-util.ts +0 -214
  17. package/src/cli/index.ts +0 -246
  18. package/src/extension.ts +0 -76
  19. package/src/generator/ast-utils.ts +0 -18
  20. package/src/generator/constants.ts +0 -6
  21. package/src/generator/field-constraint/index.ts +0 -304
  22. package/src/generator/index.ts +0 -86
  23. package/src/generator/prisma/expression-writer.ts +0 -360
  24. package/src/generator/prisma/index.ts +0 -44
  25. package/src/generator/prisma/prisma-builder.ts +0 -370
  26. package/src/generator/prisma/query-guard-generator.ts +0 -249
  27. package/src/generator/prisma/schema-generator.ts +0 -313
  28. package/src/generator/prisma/typescript-expression-transformer.ts +0 -108
  29. package/src/generator/react-hooks/index.ts +0 -273
  30. package/src/generator/service/index.ts +0 -113
  31. package/src/generator/tsc/index.ts +0 -59
  32. package/src/generator/types.ts +0 -20
  33. package/src/global.d.ts +0 -3
  34. package/src/language-server/constants.ts +0 -29
  35. package/src/language-server/generated/ast.ts +0 -643
  36. package/src/language-server/generated/grammar.ts +0 -2492
  37. package/src/language-server/generated/module.ts +0 -24
  38. package/src/language-server/langium-ext.d.ts +0 -22
  39. package/src/language-server/lsp/zmodel-definition-provider.ts +0 -87
  40. package/src/language-server/main.ts +0 -13
  41. package/src/language-server/types.ts +0 -25
  42. package/src/language-server/utils.ts +0 -21
  43. package/src/language-server/validator/attribute-validator.ts +0 -11
  44. package/src/language-server/validator/datamodel-validator.ts +0 -426
  45. package/src/language-server/validator/datasource-validator.ts +0 -102
  46. package/src/language-server/validator/enum-validator.ts +0 -14
  47. package/src/language-server/validator/expression-validator.ts +0 -48
  48. package/src/language-server/validator/schema-validator.ts +0 -31
  49. package/src/language-server/validator/utils.ts +0 -158
  50. package/src/language-server/validator/zmodel-validator.ts +0 -91
  51. package/src/language-server/zmodel-linker.ts +0 -457
  52. package/src/language-server/zmodel-module.ts +0 -136
  53. package/src/language-server/zmodel-scope.ts +0 -45
  54. package/src/language-server/zmodel-workspace-manager.ts +0 -23
  55. package/src/language-server/zmodel.langium +0 -207
  56. package/src/res/package.template.json +0 -9
  57. package/src/res/prism-zmodel.js +0 -22
  58. package/src/res/stdlib.zmodel +0 -218
  59. package/src/res/tsconfig.template.json +0 -17
  60. package/src/telemetry.ts +0 -119
  61. package/src/utils/exec-utils.ts +0 -8
  62. package/src/utils/indent-string.ts +0 -9
  63. package/src/utils/pkg-utils.ts +0 -63
@@ -1,102 +0,0 @@
1
- import { DataSource, isInvocationExpr } from '@lang/generated/ast';
2
- import { AstValidator } from '@lang/types';
3
- import { ValidationAcceptor } from 'langium';
4
- import { getStringLiteral, validateDuplicatedDeclarations } from './utils';
5
- import { SUPPORTED_PROVIDERS } from '../constants';
6
-
7
- const supportedFields = [
8
- 'provider',
9
- 'url',
10
- 'shadowDatabaseUrl',
11
- 'relationMode',
12
- ];
13
-
14
- /**
15
- * Validates data source declarations.
16
- */
17
- export default class DataSourceValidator implements AstValidator<DataSource> {
18
- validate(ds: DataSource, accept: ValidationAcceptor): void {
19
- validateDuplicatedDeclarations(ds.fields, accept);
20
- this.validateFields(ds, accept);
21
- this.validateProvider(ds, accept);
22
- this.validateUrl(ds, accept);
23
- this.validateRelationMode(ds, accept);
24
- }
25
-
26
- private validateFields(ds: DataSource, accept: ValidationAcceptor) {
27
- ds.fields.forEach((f) => {
28
- if (!supportedFields.includes(f.name)) {
29
- accept('error', `Unexpected field "${f.name}"`, { node: f });
30
- }
31
- });
32
- }
33
-
34
- private validateProvider(ds: DataSource, accept: ValidationAcceptor) {
35
- const provider = ds.fields.find((f) => f.name === 'provider');
36
- if (!provider) {
37
- accept('error', 'datasource must include a "provider" field', {
38
- node: ds,
39
- });
40
- return;
41
- }
42
-
43
- const value = getStringLiteral(provider.value);
44
- if (!value) {
45
- accept('error', '"provider" must be set to a string literal', {
46
- node: provider.value,
47
- });
48
- } else if (!SUPPORTED_PROVIDERS.includes(value)) {
49
- accept(
50
- 'error',
51
- `Provider "${value}" is not supported. Choose from ${SUPPORTED_PROVIDERS.map(
52
- (p) => '"' + p + '"'
53
- ).join(' | ')}.`,
54
- { node: provider.value }
55
- );
56
- }
57
- }
58
-
59
- private validateUrl(ds: DataSource, accept: ValidationAcceptor) {
60
- const url = ds.fields.find((f) => f.name === 'url');
61
- if (!url) {
62
- accept('error', 'datasource must include a "url" field', {
63
- node: ds,
64
- });
65
- }
66
-
67
- for (const fieldName of ['url', 'shadowDatabaseUrl']) {
68
- const field = ds.fields.find((f) => f.name === fieldName);
69
- if (!field) {
70
- continue;
71
- }
72
- const value = getStringLiteral(field.value);
73
- if (
74
- !value &&
75
- !(
76
- isInvocationExpr(field.value) &&
77
- field.value.function.ref?.name === 'env'
78
- )
79
- ) {
80
- accept(
81
- 'error',
82
- `"${fieldName}" must be set to a string literal or an invocation of "env" function`,
83
- { node: field.value }
84
- );
85
- }
86
- }
87
- }
88
-
89
- private validateRelationMode(ds: DataSource, accept: ValidationAcceptor) {
90
- const field = ds.fields.find((f) => f.name === 'relationMode');
91
- if (field) {
92
- const val = getStringLiteral(field.value);
93
- if (!val || !['foreignKeys', 'prisma'].includes(val)) {
94
- accept(
95
- 'error',
96
- '"relationMode" must be set to "foreignKeys" or "prisma"',
97
- { node: field.value }
98
- );
99
- }
100
- }
101
- }
102
- }
@@ -1,14 +0,0 @@
1
- import { Enum } from '@lang/generated/ast';
2
- import { AstValidator } from '@lang/types';
3
- import { ValidationAcceptor } from 'langium';
4
- import { validateDuplicatedDeclarations } from './utils';
5
-
6
- /**
7
- * Validates enum declarations.
8
- */
9
- export default class EnumValidator implements AstValidator<Enum> {
10
- // eslint-disable-next-line @typescript-eslint/explicit-module-boundary-types
11
- validate(_enum: Enum, accept: ValidationAcceptor) {
12
- validateDuplicatedDeclarations(_enum.fields, accept);
13
- }
14
- }
@@ -1,48 +0,0 @@
1
- import {
2
- Expression,
3
- isBinaryExpr,
4
- isInvocationExpr,
5
- } from '@lang/generated/ast';
6
- import { AstValidator } from '@lang/types';
7
- import { isFromStdlib } from '@lang/utils';
8
- import { ValidationAcceptor } from 'langium';
9
-
10
- /**
11
- * Validates expressions.
12
- */
13
- export default class ExpressionValidator implements AstValidator<Expression> {
14
- validate(expr: Expression, accept: ValidationAcceptor): void {
15
- if (!expr.$resolvedType) {
16
- if (this.isAuthInvocation(expr)) {
17
- // check was done at link time
18
- accept(
19
- 'error',
20
- 'auth() cannot be resolved because no "User" model is defined',
21
- { node: expr }
22
- );
23
- } else if (this.isCollectionPredicate(expr)) {
24
- accept(
25
- 'error',
26
- 'collection predicate can only be used on an array of model type',
27
- { node: expr }
28
- );
29
- } else {
30
- accept('error', 'expression cannot be resolved', {
31
- node: expr,
32
- });
33
- }
34
- }
35
- }
36
-
37
- private isCollectionPredicate(expr: Expression) {
38
- return isBinaryExpr(expr) && ['?', '!', '^'].includes(expr.operator);
39
- }
40
-
41
- private isAuthInvocation(expr: Expression) {
42
- return (
43
- isInvocationExpr(expr) &&
44
- expr.function.ref?.name === 'auth' &&
45
- isFromStdlib(expr.function.ref)
46
- );
47
- }
48
- }
@@ -1,31 +0,0 @@
1
- import { STD_LIB_MODULE_NAME } from '@lang/constants';
2
- import { isDataSource, Model } from '@lang/generated/ast';
3
- import { AstValidator } from '@lang/types';
4
- import { ValidationAcceptor } from 'langium';
5
- import { validateDuplicatedDeclarations } from './utils';
6
-
7
- /**
8
- * Validates toplevel schema.
9
- */
10
- export default class SchemaValidator implements AstValidator<Model> {
11
- validate(model: Model, accept: ValidationAcceptor): void {
12
- validateDuplicatedDeclarations(model.declarations, accept);
13
-
14
- if (!model.$document?.uri.path.endsWith(STD_LIB_MODULE_NAME)) {
15
- this.validateDataSources(model, accept);
16
- }
17
- }
18
-
19
- private validateDataSources(model: Model, accept: ValidationAcceptor) {
20
- const dataSources = model.declarations.filter((d) => isDataSource(d));
21
- if (dataSources.length === 0) {
22
- accept('error', 'Model must define a datasource', { node: model });
23
- } else if (dataSources.length > 1) {
24
- accept(
25
- 'error',
26
- 'Multiple datasource declarations are not allowed',
27
- { node: dataSources[1] }
28
- );
29
- }
30
- }
31
- }
@@ -1,158 +0,0 @@
1
- import {
2
- AttributeArg,
3
- AttributeParam,
4
- BuiltinType,
5
- DataModelAttribute,
6
- DataModelFieldAttribute,
7
- ExpressionType,
8
- isArrayExpr,
9
- isDataModelField,
10
- isLiteralExpr,
11
- isReferenceExpr,
12
- } from '@lang/generated/ast';
13
- import { AstNode, ValidationAcceptor } from 'langium';
14
-
15
- /**
16
- * Checks if the given declarations have duplicated names
17
- */
18
- export function validateDuplicatedDeclarations(
19
- decls: Array<AstNode & { name: string }>,
20
- accept: ValidationAcceptor
21
- ): void {
22
- const groupByName = decls.reduce<
23
- Record<string, Array<AstNode & { name: string }>>
24
- >((group, decl) => {
25
- group[decl.name] = group[decl.name] ?? [];
26
- group[decl.name].push(decl);
27
- return group;
28
- }, {});
29
-
30
- for (const [name, decls] of Object.entries<AstNode[]>(groupByName)) {
31
- if (decls.length > 1) {
32
- accept('error', `Duplicated declaration name "${name}"`, {
33
- node: decls[1],
34
- });
35
- }
36
- }
37
- }
38
-
39
- /**
40
- * Try getting string value from a potential string literal expression
41
- */
42
- export function getStringLiteral(node: AstNode): string | undefined {
43
- if (isLiteralExpr(node) && typeof node.value === 'string') {
44
- return node.value;
45
- } else {
46
- return undefined;
47
- }
48
- }
49
-
50
- /**
51
- * Determines if the given sourceType is assignable to a destination of destType
52
- */
53
- export function typeAssignable(
54
- destType: ExpressionType,
55
- sourceType: ExpressionType
56
- ): boolean {
57
- switch (destType) {
58
- case 'Any':
59
- return true;
60
- case 'Float':
61
- return (
62
- sourceType === 'Any' ||
63
- sourceType === 'Int' ||
64
- sourceType === 'Float'
65
- );
66
- default:
67
- return sourceType === 'Any' || sourceType === destType;
68
- }
69
- }
70
-
71
- /**
72
- * Maps a ZModel builtin type to expression type
73
- */
74
- export function mapBuiltinTypeToExpressionType(
75
- type: BuiltinType | 'Any' | 'Null'
76
- ): ExpressionType | 'Any' {
77
- switch (type) {
78
- case 'Any':
79
- case 'Boolean':
80
- case 'String':
81
- case 'DateTime':
82
- case 'Int':
83
- case 'Float':
84
- case 'Null':
85
- return type;
86
- case 'BigInt':
87
- return 'Int';
88
- case 'Decimal':
89
- return 'Float';
90
- case 'Json':
91
- case 'Bytes':
92
- return 'Any';
93
- }
94
- }
95
-
96
- /**
97
- * Determines if the given attribute argument is assignable to the given attribute parameter
98
- */
99
- export function assignableToAttributeParam(
100
- arg: AttributeArg,
101
- param: AttributeParam,
102
- attr: DataModelAttribute | DataModelFieldAttribute
103
- ): boolean {
104
- const argResolvedType = arg.$resolvedType;
105
- if (!argResolvedType) {
106
- return false;
107
- }
108
-
109
- let dstType = param.type.type;
110
- const dstIsArray = param.type.array;
111
- const dstRef = param.type.reference;
112
-
113
- if (dstType) {
114
- if (typeof argResolvedType?.decl !== 'string') {
115
- // destination type is not a reference, so argument type must be a plain expression
116
- return false;
117
- }
118
-
119
- if (dstType === 'FieldReference') {
120
- if (dstIsArray) {
121
- return (
122
- isArrayExpr(arg.value) &&
123
- !arg.value.items.find(
124
- (item) =>
125
- !isReferenceExpr(item) ||
126
- !isDataModelField(item.target.ref)
127
- )
128
- );
129
- } else {
130
- return (
131
- isReferenceExpr(arg.value) &&
132
- isDataModelField(arg.value.target.ref)
133
- );
134
- }
135
- } else if (dstType === 'ContextType') {
136
- if (isDataModelField(attr.$container)) {
137
- if (!attr.$container?.type?.type) {
138
- return false;
139
- }
140
- dstType = mapBuiltinTypeToExpressionType(
141
- attr.$container.type.type
142
- );
143
- } else {
144
- dstType = 'Any';
145
- }
146
- }
147
-
148
- return (
149
- typeAssignable(dstType, argResolvedType.decl) &&
150
- dstIsArray === argResolvedType.array
151
- );
152
- } else {
153
- return (
154
- dstRef?.ref === argResolvedType.decl &&
155
- dstIsArray === argResolvedType.array
156
- );
157
- }
158
- }
@@ -1,91 +0,0 @@
1
- import {
2
- AstNode,
3
- LangiumDocument,
4
- ValidationAcceptor,
5
- ValidationChecks,
6
- ValidationRegistry,
7
- } from 'langium';
8
- import {
9
- Attribute,
10
- DataModel,
11
- DataSource,
12
- Enum,
13
- Expression,
14
- Model,
15
- ZModelAstType,
16
- } from '../generated/ast';
17
- import type { ZModelServices } from '../zmodel-module';
18
- import SchemaValidator from './schema-validator';
19
- import DataSourceValidator from './datasource-validator';
20
- import DataModelValidator from './datamodel-validator';
21
- import AttributeValidator from './attribute-validator';
22
- import EnumValidator from './enum-validator';
23
- import ExpressionValidator from './expression-validator';
24
-
25
- /**
26
- * Registry for validation checks.
27
- */
28
- export class ZModelValidationRegistry extends ValidationRegistry {
29
- constructor(services: ZModelServices) {
30
- super(services);
31
- const validator = services.validation.ZModelValidator;
32
- const checks: ValidationChecks<ZModelAstType> = {
33
- Model: validator.checkModel,
34
- DataSource: validator.checkDataSource,
35
- DataModel: validator.checkDataModel,
36
- Enum: validator.checkEnum,
37
- Attribute: validator.checkAttribute,
38
- Expression: validator.checkExpression,
39
- };
40
- this.register(checks, validator);
41
- }
42
- }
43
-
44
- /**
45
- * Implementation of custom validations.
46
- */
47
- export class ZModelValidator {
48
- private shouldCheck(node: AstNode) {
49
- let doc: LangiumDocument | undefined;
50
- let currNode: AstNode | undefined = node;
51
- while (currNode) {
52
- if (currNode.$document) {
53
- doc = currNode.$document;
54
- break;
55
- }
56
- currNode = currNode.$container;
57
- }
58
-
59
- return (
60
- doc?.parseResult.lexerErrors.length === 0 &&
61
- doc?.parseResult.parserErrors.length === 0
62
- );
63
- }
64
-
65
- checkModel(node: Model, accept: ValidationAcceptor): void {
66
- this.shouldCheck(node) && new SchemaValidator().validate(node, accept);
67
- }
68
-
69
- checkDataSource(node: DataSource, accept: ValidationAcceptor): void {
70
- this.shouldCheck(node) &&
71
- new DataSourceValidator().validate(node, accept);
72
- }
73
-
74
- checkDataModel(node: DataModel, accept: ValidationAcceptor): void {
75
- this.shouldCheck(node) &&
76
- new DataModelValidator().validate(node, accept);
77
- }
78
-
79
- checkEnum(node: Enum, accept: ValidationAcceptor): void {
80
- this.shouldCheck(node) && new EnumValidator().validate(node, accept);
81
- }
82
-
83
- checkAttribute(node: Attribute, accept: ValidationAcceptor): void {
84
- this.shouldCheck(node) &&
85
- new AttributeValidator().validate(node, accept);
86
- }
87
-
88
- checkExpression(node: Expression, accept: ValidationAcceptor): void {
89
- new ExpressionValidator().validate(node, accept);
90
- }
91
- }