fix: support else if conditional attributes - #1412
Open
akfaew wants to merge 1 commit into
Open
Conversation
Contributor
There was a problem hiding this comment.
Pull request overview
Note
Copilot was unable to run its full agentic suite in this review.
Adds support for else if branches in conditional attributes across parsing, AST representation, formatting, visiting, and code generation.
Changes:
- Extend
ConditionalAttributeAST to includeElseIfsbranches and implement deep-copy support. - Update parser/visitor/generator to traverse and emit
else ifblocks. - Add tests covering parsing, formatting, and generator output for conditional attribute
else ifbranches.
Reviewed changes
Copilot reviewed 7 out of 7 changed files in this pull request and generated 5 comments.
Show a summary per file
| File | Description |
|---|---|
| parser/v2/visitor/visitor.go | Ensures else if branches are visited so downstream processing sees all attributes. |
| parser/v2/types.go | Extends the AST with ElseIfs and updates serialization/copy helpers. |
| parser/v2/elementparser_test.go | Adds formatting + parsing tests validating else if branch handling. |
| parser/v2/conditionalattributeparser.go | Implements parsing of else if branches for conditional attributes. |
| internal/format/attributes_test.go | Updates formatting tests to include an ElseIfs branch. |
| generator/generator_test.go | Adds a generator regression test ensuring else if is emitted. |
| generator/generator.go | Updates generation and attribute analysis to include else if branches. |
Comment on lines
1054
to
1060
| type ConditionalAttribute struct { | ||
| Expression Expression | ||
| Then []Attribute | ||
| ElseIfs []ConditionalElseIfAttribute | ||
| Else []Attribute | ||
| Range Range | ||
| } |
Comment on lines
+100
to
+103
| if _, matched, err = openBraceWithOptionalPadding.Parse(pi); err != nil || !matched { | ||
| err = parse.Error("attribute else if: unterminated (missing closing '{\n')", pi.PositionAt(start)) | ||
| return | ||
| } |
Comment on lines
+109
to
+117
| if r.Then, matched, err = (attributesParser{}).Parse(pi); err != nil || !matched { | ||
| err = parse.Error("attribute if: expected attributes in else if block, but none were found", pi.Position()) | ||
| return | ||
| } | ||
|
|
||
| if len(r.Then) == 0 { | ||
| err = parse.Error("attribute if: invalid content or no attributes were found in the else if block", pi.Position()) | ||
| return | ||
| } |
Comment on lines
+87
to
+97
| if _, matched, err = parse.All(parse.OptionalWhitespace, closeBrace, parse.OptionalWhitespace, parse.String("else if")).Parse(pi); err != nil || !matched { | ||
| pi.Seek(start) | ||
| return | ||
| } | ||
|
|
||
| // Rewind to the start of the `if` statement. | ||
| pi.Seek(pi.Index() - 2) | ||
| // Parse the Go if expression. | ||
| if r.Expression, err = parseGo("attribute else if", pi, goexpression.If); err != nil { | ||
| return | ||
| } |
Comment on lines
+205
to
+214
| ElseIfs: []parser.ConditionalElseIfAttribute{ | ||
| { | ||
| Then: []parser.Attribute{ | ||
| &parser.ConstantAttribute{ | ||
| Key: parser.ConstantAttributeKey{Name: "class"}, | ||
| Value: "else-if-class", | ||
| }, | ||
| }, | ||
| }, | ||
| }, |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Adds first-class parser and AST support for
else ifbranches in conditional attributes.Previously, conditional attributes only modeled
ifand finalelsebranches, so formatting an attribute-level conditional withelse ifcould serialize poorly as a splitelseplus nestedif. This updates conditional attributes to mirror normal templifhandling more closely.Changes
ConditionalElseIfAttributeandConditionalAttribute.ElseIfs.} else if ... { ... }conditional attribute branches before an optional finalelse.} else if ... {on one line.else ifbranches.else ifelse ifbranchesTesting