swiftlang / swiftlang/swift-syntax

Refactor `IfConfigClauseSyntax` to disallow starting an if config clause with `#else`

Open
#1,971 1 comment 0 reactions 0 assignees View on GitHub
Dominant language
Swift
Stars
3.7k
Forks
553
Avg merge
5d 13h
Merged PRs (30d)
16

Description

Currently, `IfConfigClause` can happily represent the following code

```swift
#else
doSomeStuff()
#if true
doSomeStuff()
#elseif true
doSomeStuff()
#if true
doSomeStuff()
#endif
```

While the parser won’t accept it, the syntax tree just shouldn’t be able to represent it. We should refactor the parser to disallow this.

`IfConfigClause` should have a structure that looks like the following (subject to change if I change my mind):

- `IfConfigElementList`: `(CodeBlockItemListSyntax | SwitchCaseListSyntax | MemberBlockItemListSyntax | ExprSyntax | AttributeListSyntax)`

- `IfConfigDeclSyntax`
- poundIfKeyword: `'#if'`
- ifCondition: `ExprSyntax`
- ifElements: `IfConfigElementList`
- elseIfClauses: `IfConfigElseIfClause*`
- elseClause: `IfConfigElseClause?`
- poundEndIfKeyword: `#endif`

- `IfConfigElseIfClause`
- elseIfKeyword: `#elseif`
- condition: `ExprSyntax`
- elements: `IfConfigElementList`

- `IfConfigElseClause`
- elseKeyword: `#else`
- elements: `IfConfigElementList`

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.