Closed tatumalenko closed 4 years ago
Hey @tatumalenko, thanks for reporting this. Formatting these nested if/elif/else constructions can be pretty tricky. Because the AST doesn't always tell you where the keyword reside.
I should debug these examples to find what is going wrong. And thank you for taking the time to find multiple examples.
After some debugging, I found out I made a wrong assumption that only the last else if
could have a wrong range.
Turns out every SynExpr.IfThenElse
can potentially have a wrong range (ignoring the else keyword).
ImplFile
(ParsedImplFileInput
("Script.fsx",true,QualifiedNameOfFile Script$fsx,[],[],
[SynModuleOrNamespace
([Script],false,AnonModule,
[DoExpr
(NoSequencePointAtDoBinding,
IfThenElse
(App
(NonAtomic,false,
App
(NonAtomic,true,Ident op_LessThan,Ident v1,
Script.fsx (1,3--1,7) IsSynthetic=false),Ident v2,
Script.fsx (1,3--1,10) IsSynthetic=false),
Const (Int32 -1,Script.fsx (2,4--2,6) IsSynthetic=false),
Some
(IfThenElse
(App
(NonAtomic,false,
App
(NonAtomic,true,Ident op_GreaterThan,Ident v1,
Script.fsx (3,5--3,9) IsSynthetic=false),Ident v2,
Script.fsx (3,5--3,12) IsSynthetic=false),
Const (Int32 1,Script.fsx (4,4--4,5) IsSynthetic=false),
Some
(IfThenElse
(App
(NonAtomic,false,
App
(NonAtomic,true,Ident op_LessThan,Ident t1,
Script.fsx (6,7--6,11) IsSynthetic=false),
Ident t2,
Script.fsx (6,7--6,14) IsSynthetic=false),
Const
(Int32 -1,
Script.fsx (7,8--7,10) IsSynthetic=false),
Some
(IfThenElse
(App
(NonAtomic,false,
App
(NonAtomic,true,Ident op_GreaterThan,
Ident t1,
Script.fsx (8,9--8,13) IsSynthetic=false),
Ident t2,
Script.fsx (8,9--8,16) IsSynthetic=false),
Const
(Int32 1,
Script.fsx (9,8--9,9) IsSynthetic=false),
Some
(Const
(Int32 0,
Script.fsx (11,8--11,9) IsSynthetic=false)),
SequencePointAtBinding
Script.fsx (8,4--8,21) IsSynthetic=false,
false,
Script.fsx (8,4--8,21) IsSynthetic=false,
Script.fsx (8,4--11,9) IsSynthetic=false)),
SequencePointAtBinding
Script.fsx (6,4--6,19) IsSynthetic=false,false,
Script.fsx (6,4--6,19) IsSynthetic=false,
Script.fsx (6,4--11,9) IsSynthetic=false)),
SequencePointAtBinding
Script.fsx (3,0--3,17) IsSynthetic=false,false,
Script.fsx (3,0--3,17) IsSynthetic=false,
Script.fsx (3,0--11,9) IsSynthetic=false)),
SequencePointAtBinding Script.fsx (1,0--1,15) IsSynthetic=false,
false,Script.fsx (1,0--1,15) IsSynthetic=false,
Script.fsx (1,0--11,9) IsSynthetic=false),
Script.fsx (1,0--11,9) IsSynthetic=false)],PreXmlDocEmpty,[],None,
Script.fsx (1,0--11,9) IsSynthetic=false)],(true, true)))
Hmm, not sure how to tackle this one yet. Ongoing investigation.
I was able to find a way to correct all elif ranges.
Issue created from fantomas-ui
Exception throws when formatting certain patterns of nested else if / elif blocks. Likely related to [#675] which was recently closed and fixed in [#678].
Code Example 1
Code Example 2
Another example slightly tweaked taken from [#675]:
Changing the
elif
into anelse if
in the previous example will format successfully.Code Example 3
Yet another example, again similar to the previous one, here the issue still arises but no
elif
were involved:The above still throws even if
if la > lb then a' else b'
is replaced simply witha'
(i.e. nesting only inside theelse
).Note
In all cases, if strict mode is enabled, the format succeeds.
Error
Options
Fantomas Next - 3.3.0-3/2/2020
IndentOnTryWith
false
IndentSpaceNum
2
KeepNewlineAfter
false
MaxIfThenElseShortWidth
40
PageWidth
90
ReorderOpenDeclaration
false
SemicolonAtEndOfLine
false
SpaceAfterComma
true
SpaceAfterSemicolon
true
SpaceAroundDelimiter
true
SpaceBeforeArgument
true
SpaceBeforeColon
false
SpaceBeforeSemicolon
false
StrictMode
false