Skip to content

fix(language): remove this grammar ambiguity in expressions - #2885

Merged
ymc9 merged 1 commit into
devfrom
fix/this-keyword-grammar-ambiguity
Oct 22, 2026
Merged

ymc9 merged 1 commit into
devfrom
fix/this-keyword-grammar-ambiguity

Conversation

@ymc9

@ymc9 ymc9 commented Oct 22, 2026 •

Copy link
Copy Markdown
Member

Summary

Follow-up to #2852. Adding 'this' to RegularID (so a primitive type def can declare its this field) made this ambiguous in PrimaryExpr: it could parse as either ThisExpr or a ReferenceExpr (via RegularIDWithTypeNames). Parsing still picked ThisExpr, but because the language module runs in development mode, Chevrotain printed this warning whenever a schema used this in an expression, including ordinary policies and validation rules (auth() == this, @@validate(this.age > 0)):

Ambiguous Alternatives Detected: <1, 7> in <OR1> inside <PrimaryExpr> Rule,
<this, ), )> may appears as a prefix path in all these alternatives.

This PR removes 'this' from RegularID and allows it only as a data field name through a new DataFieldName rule. Expressions never need to refer to a field named this by name, because this always parses as ThisExpr. The linker already resolves that to the this field's type.

Test plan

  • zen generate on a schema using this in a primitive type def (@@validate(isEmail(this))) and in a model (@@validate(this.age > 0)): the warning is gone and generation succeeds
  • packages/language tests pass (13 files, 132 tests)

🤖 Generated with Claude Code

Summary by CodeRabbit

  • New Features
    • Data-field names can now use this alongside existing identifier forms.
  • Bug Fixes
    • this is no longer accepted as a regular identifier in other grammar contexts.

Allow `this` only as a data field name (for primitive type defs' `this` field)
instead of in `RegularID`, which made `this` ambiguous between `ThisExpr` and
`ReferenceExpr` and caused Chevrotain to print an "Ambiguous Alternatives"
warning whenever a schema used `this` in an expression.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
@coderabbitai

coderabbitai Bot commented Oct 22, 2026 •

Copy link
Copy Markdown
Contributor

Review in Change Stack →

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration
📥 Commits

Reviewing files that changed from the base of the PR and between 151524b and 91ac87c.

⛔ Files ignored due to path filters: 2
  • packages/language/src/generated/ast.ts is excluded by !**/generated/**
  • packages/language/src/generated/grammar.ts is excluded by !**/generated/**
📒 Files selected for processing: 1
  • packages/language/src/zmodel.langium

Included review availability: This review used your included allowance. Your plan provides up to 8 included reviews per hour; 7 remain after this review.

📝 Walkthrough

The grammar now accepts this as a data-field name. Other grammar positions that use RegularID no longer accept this.

Changes: Data field identifier parsing

Layer / File(s) Summary
Define data-field names
packages/language/src/zmodel.langium
DataField now uses DataFieldName. This rule accepts the existing identifier alternatives and this; RegularID no longer accepts this.

Priority: ⬇️ Low

Estimated code review effort: 2 (Simple) | ~8 minutes

Change: Bug fix

Suggested reviewers: evgenovalov, sanny-io

Merge Risk: ⚪ Minimal · up to 91ac8

The grammar change is mergeable based on the supplied implementation and review context.

Pre-merge checks: Passed 5
Check name Status Explanation
Description Check Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check Passed The title accurately describes the main change: removing the grammar ambiguity caused by this in expressions.
Docstring Coverage Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check. Docstring coverage is scoped to functions touched by this diff. Analyzed 0 functions across 0…
Linked Issues check Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check Passed Check skipped because no linked issues were found for this pull request.
Full details: Docstring Coverage

Explanation

No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check. Docstring coverage is scoped to functions touched by this diff. Analyzed 0 functions across 0 files. (1 skipped: 1 unsupported.)

✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Commit to this branch
  • Create a new PR
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@ymc9
ymc9 merged commit db24ecf into dev Oct 22, 2026
8 checks passed
@ymc9
ymc9 deleted the fix/this-keyword-grammar-ambiguity branch October 22, 2026 20:02
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant