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 2151, index syntax without dot on array of arrays #2163

Merged
merged 2 commits into from
Mar 24, 2022
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

### Fixed
* DotGet in quotation should be further indented. [#2154](https://github.com/fsprojects/fantomas/issues/2154)
* Reformatting multiple array index operators (v6 style) adds spaces that are invalid [#2151](https://github.com/fsprojects/fantomas/issues/2151)

## [4.7.3] - 2022-03-12

Expand Down
21 changes: 21 additions & 0 deletions src/Fantomas.Tests/IndexSyntaxTests.fs
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,27 @@ arr[0, 2, 3, 4] <- 2
arr[0, 2, 3, 4]
"""

[<Test>]
let ``index syntax without dot on array of arrays, 2151`` () =
formatSourceString
false
"""
let a = Array.create 10 -1
let b = Array.create 10 a

printfn "%d -> %d" a[0] (b[0][0])
"""
config
|> prepend newline
|> should
equal
"""
let a = Array.create 10 -1
let b = Array.create 10 a

printfn "%d -> %d" a[0] (b[0][0])
"""

[<Test>]
let ``only add spaces when expressions are atomic`` () =
formatSourceString
Expand Down
37 changes: 13 additions & 24 deletions src/Fantomas/SourceParser.fs
Original file line number Diff line number Diff line change
Expand Up @@ -903,20 +903,6 @@ let (|TernaryApp|_|) =
| SynExpr.App (_, _, SynExpr.App (_, _, SynExpr.App (_, true, Var "?<-", e1, _), e2, _), e3, _) -> Some(e1, e2, e3)
| _ -> None

// expr1[expr2]
// ref: https://github.com/fsharp/fslang-design/blob/main/FSharp-6.0/FS-1110-index-syntax.md
let (|IndexWithoutDotExpr|_|) =
function
| SynExpr.App (ExprAtomicFlag.Atomic, false, identifierExpr, SynExpr.ArrayOrListComputed (false, indexExpr, _), _) ->
Some(identifierExpr, indexExpr)
| SynExpr.App (ExprAtomicFlag.NonAtomic,
false,
identifierExpr,
(SynExpr.ArrayOrListComputed (isArray = false; expr = indexExpr) as argExpr),
_) when (RangeHelpers.isAdjacentTo identifierExpr.Range argExpr.Range) ->
Some(identifierExpr, indexExpr)
| _ -> None

let (|MatchLambda|_|) =
function
| SynExpr.MatchLambda (_, keywordRange, pats, _, _) -> Some(keywordRange, pats)
Expand Down Expand Up @@ -1749,21 +1735,24 @@ let rec (|UppercaseSynType|LowercaseSynType|) (synType: SynType) =
| SynType.App (st, _, _, _, _, _, _) -> (|UppercaseSynType|LowercaseSynType|) st
| _ -> failwithf "cannot determine if synType %A is uppercase or lowercase" synType

let (|ElmishReactWithoutChildren|_|) e =
let (|IndexWithoutDotExpr|ElmishReactWithoutChildren|ElmishReactWithChildren|NonAppExpr|) e =
match e with
| IndexWithoutDotExpr _ -> None
| SynExpr.App (ExprAtomicFlag.Atomic, false, identifierExpr, SynExpr.ArrayOrListComputed (false, indexExpr, _), _) ->
IndexWithoutDotExpr(identifierExpr, indexExpr)
| SynExpr.App (ExprAtomicFlag.NonAtomic,
false,
identifierExpr,
(SynExpr.ArrayOrListComputed (isArray = false; expr = indexExpr) as argExpr),
_) when (RangeHelpers.isAdjacentTo identifierExpr.Range argExpr.Range) ->
IndexWithoutDotExpr(identifierExpr, indexExpr)
| SynExpr.App (_, false, OptVar (ident, _, _), ArrayOrList (sr, isArray, children, er, _), _) ->
Some(ident, sr, isArray, children, er)
| _ -> None

let (|ElmishReactWithChildren|_|) (e: SynExpr) =
match e with
ElmishReactWithoutChildren(ident, sr, isArray, children, er)
| SynExpr.App (_,
false,
SynExpr.App (_, false, OptVar ident, (ArrayOrList _ as attributes), _),
ArrayOrList (sr, isArray, children, er, _),
_) -> Some(ident, attributes, (isArray, sr, children, er))
| _ -> None
ArrayOrList (sr, isArray, children, er, r),
_) -> ElmishReactWithChildren(ident, attributes, (isArray, sr, children, er))
| _ -> NonAppExpr

let isIfThenElseWithYieldReturn e =
match e with
Expand Down