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: resolve member access expr only in the context of operand type #761

Merged
merged 2 commits into from
Oct 14, 2023
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
16 changes: 8 additions & 8 deletions packages/schema/src/language-server/zmodel-linker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -92,9 +92,10 @@ export class ZModelLinker extends DefaultLinker {
container: AstNode,
property: string,
document: LangiumDocument,
extraScopes: ScopeProvider[]
extraScopes: ScopeProvider[],
onlyFromExtraScopes = false
) {
if (!this.resolveFromScopeProviders(container, property, document, extraScopes)) {
if (!this.resolveFromScopeProviders(container, property, document, extraScopes) && !onlyFromExtraScopes) {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
const reference: Reference<AstNode> = (container as any)[property];
this.doLink({ reference, container, property }, document);
Expand Down Expand Up @@ -327,12 +328,11 @@ export class ZModelLinker extends DefaultLinker {
if (operandResolved && !operandResolved.array && isDataModel(operandResolved.decl)) {
const modelDecl = operandResolved.decl as DataModel;
const provider = (name: string) => modelDecl.$resolvedFields.find((f) => f.name === name);
extraScopes = [provider, ...extraScopes];
}

this.linkReference(node, 'member', document, extraScopes);
if (node.member.ref) {
this.resolveToDeclaredType(node, node.member.ref.type);
// member access is resolved only in the context of the operand type
this.linkReference(node, 'member', document, [provider], true);
if (node.member.ref) {
this.resolveToDeclaredType(node, node.member.ref.type);
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1017,7 +1017,7 @@ describe('Attribute tests', () => {
@@allow('all', auth().email != null)
}
`)
).toContain(`Could not resolve reference to DataModelField named 'email'.`);
).toContain(`expression cannot be resolved`);
});

it('collection predicate expression check', async () => {
Expand Down
33 changes: 33 additions & 0 deletions tests/integration/tests/regression/issue-756.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import { loadModel, loadModelWithError } from '@zenstackhq/testtools';

describe('Regression: issue 756', () => {
it('regression', async () => {
expect(
await loadModelWithError(
`
generator client {
provider = "prisma-client-js"
}

datasource db {
provider = "postgresql"
url = env("DATABASE_URL")
}

model User {
id Int @id @default(autoincrement())
email Int
posts Post[]
}

model Post {
id Int @id @default(autoincrement())
author User? @relation(fields: [authorId], references: [id])
authorId Int
@@allow('all', auth().posts.authorId == authorId)
}
`
)
).toContain('expression cannot be resolved');
});
});
Loading