Skip to content
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 9 additions & 6 deletions .github/workflows/build-test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ on:
branches: ['dev', 'main', 'canary']

jobs:
build:
build-test:
runs-on: ubuntu-latest

strategy:
Expand All @@ -35,11 +35,14 @@ jobs:
cache: 'pnpm'
- run: pnpm install --frozen-lockfile
- run: |
if [[ $GITHUB_REF == 'refs/heads/canary' ]]; then
DEFAULT_NPM_TAG=canary pnpm run build
else
DEFAULT_NPM_TAG=latest pnpm run build
fi
if [[ $GITHUB_REF == 'refs/heads/canary' ]]; then
DEFAULT_NPM_TAG=canary pnpm run build
else
DEFAULT_NPM_TAG=latest pnpm run build
fi

- run: pnpm lint

# install again for internal dependencies
- run: pnpm install --frozen-lockfile
- run: pnpm run test
19 changes: 18 additions & 1 deletion packages/schema/src/language-server/validator/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,11 @@ import {
ExpressionType,
isArrayExpr,
isDataModelField,
isEnum,
isLiteralExpr,
isReferenceExpr,
} from '@zenstackhq/language/ast';
import { resolved } from '@zenstackhq/sdk';
import { AstNode, ValidationAcceptor } from 'langium';

/**
Expand Down Expand Up @@ -99,7 +101,19 @@ export function assignableToAttributeParam(
const dstIsArray = param.type.array;
const dstRef = param.type.reference;

if (dstType) {
if (isEnum(argResolvedType.decl)) {
// enum type

let attrArgDeclType = dstRef?.ref;
if (dstType === 'ContextType' && isDataModelField(attr.$container) && attr.$container?.type?.reference) {
// attribute parameter type is ContextType, need to infer type from
// the attribute's container
attrArgDeclType = resolved(attr.$container?.type?.reference);
}
return attrArgDeclType === argResolvedType.decl && dstIsArray === argResolvedType.array;
} else if (dstType) {
// scalar type

if (typeof argResolvedType?.decl !== 'string') {
// destination type is not a reference, so argument type must be a plain expression
return false;
Expand All @@ -115,6 +129,8 @@ export function assignableToAttributeParam(
return isReferenceExpr(arg.value) && isDataModelField(arg.value.target.ref);
}
} else if (dstType === 'ContextType') {
// attribute parameter type is ContextType, need to infer type from
// the attribute's container
if (isDataModelField(attr.$container)) {
if (!attr.$container?.type?.type) {
return false;
Expand All @@ -129,6 +145,7 @@ export function assignableToAttributeParam(
typeAssignable(dstType, argResolvedType.decl) && (dstType === 'Any' || dstIsArray === argResolvedType.array)
);
} else {
// reference type
return dstRef?.ref === argResolvedType.decl && dstIsArray === argResolvedType.array;
}
}
15 changes: 5 additions & 10 deletions packages/schema/src/language-server/zmodel-code-action.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,7 @@ import {
MaybePromise,
} from 'langium';

import {
CancellationToken,
CodeAction,
CodeActionKind,
CodeActionParams,
Command,
Diagnostic,
} from 'vscode-languageserver';
import { CodeAction, CodeActionKind, CodeActionParams, Command, Diagnostic } from 'vscode-languageserver';
import { IssueCodes } from './constants';
import { ZModelFormatter } from './zmodel-formatter';

Expand All @@ -34,8 +27,7 @@ export class ZModelCodeActionProvider implements CodeActionProvider {

getCodeActions(
document: LangiumDocument,
params: CodeActionParams,
cancelToken?: CancellationToken
params: CodeActionParams
): MaybePromise<Array<Command | CodeAction> | undefined> {
const result: CodeAction[] = [];
const acceptor = (ca: CodeAction | undefined) => ca && result.push(ca);
Expand Down Expand Up @@ -67,6 +59,7 @@ export class ZModelCodeActionProvider implements CodeActionProvider {

const astNode = cstNode?.element as DataModelField;

// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
const oppositeModel = astNode.type.reference!.ref! as DataModel;

const lastField = oppositeModel.fields[oppositeModel.fields.length - 1];
Expand Down Expand Up @@ -109,7 +102,9 @@ export class ZModelCodeActionProvider implements CodeActionProvider {
[document.textDocument.uri]: [
{
range: {
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
start: lastField.$cstNode!.range.end,
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
end: lastField.$cstNode!.range.end,
},
newText:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -314,4 +314,19 @@ describe('Attribute tests', () => {
`)
).toContain('attribute "@length" cannot be used on this type of field');
});

it('enum as default', async () => {
await loadModel(`
${prelude}

enum E {
E1
E2
}

model M {
e E @default(E1)
}
`);
});
});