Skip to content

Commit

Permalink
fix: resolve member access expr only in the context of operand type (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
ymc9 authored Oct 14, 2023
1 parent 8ac0915 commit ccae413
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 9 deletions.
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');
});
});

0 comments on commit ccae413

Please sign in to comment.