InlineIf¶
Replaces IF blocks with inline IF.
Note
Required Robot Framework version: >=5.0
Disabling the formatter
InlineIf is included in the default formatters, but it can be also run separately with:
You can also disable it:
It will only replace IF block if it can fit in one line shorter than line_length parameter (default: 80 characters).
Simple IF blocks that will be replaced by inline IF:
Assignments¶
Assignments are also supported as long as all ELSE and ELSE IF branches have matching return variables and there is ELSE
branch. ELSE branch is required because without it assignment variable would be overwritten with None without
your consent:
*** Keywords ***
Keyword
# assignment variable but missing ELSE
IF $condition
${var} Keyword
END
# assignment variables and ELSE branch
IF $condition
${var} ${var2} Keyword
ELSE
${var} ${var2} Keyword 2
END
# assignment variable and ELSE branch but variable name does not match
IF $condition
${var} Keyword
ELSE
${other_var} Keyword 2
END
*** Keywords ***
Keyword
# assignment variable but missing ELSE
IF $condition
${var} Keyword
END
# assignment variables and ELSE branch
${var} ${var2} IF $condition Keyword ELSE Keyword 2
# assignment variable and ELSE branch but variable name does not match
IF $condition
${var} Keyword
ELSE
${other_var} Keyword 2
END
Line length¶
Inline IF will be only used if resulting IF will be shorter than line_length parameter (default value is 80).
Multi statements IF blocks are also skipped:
*** Keywords ***
Keyword
FOR ${var} IN @{array}
# Infline IF would not fit under --line-length
IF $condition == "some value"
Longer Keyword That Will Not Fit In One Line ${argument} ${argument2}
ELIF $condition == "other value"
Longer Keyword That Will Not Fit In One Line ${argument3} ${argument4}
END
END
# multi statements inside IF
IF $condition
Keyword
Other Keyword
END
*** Keywords ***
Keyword
FOR ${var} IN @{array}
# Infline IF would not fit under --line-length
IF $condition == "some value"
Longer Keyword That Will Not Fit In One Line ${argument} ${argument2}
ELIF $condition == "other value"
Longer Keyword That Will Not Fit In One Line ${argument3} ${argument4}
END
END
# multi statements inside IF
IF $condition
Keyword
Other Keyword
END
line_length parameter is configurable. Note that line_length is different than global --line-length (used
in other formatters such as SplitTooLongLine):
With above configuration InlineIf will be configured to use line_length of 140 character limits and
global --line-length is set to 80 (and is not used by InlineIf).
Too long inline IF¶
Too long inline IFs (over line_length character limit) will be replaced with normal IF block:
Since in above example ${var} and ${var2} variables were used in assignment Robocop added missing ELSE branch
to set variable values to None if no other condition matches. If there is ELSE branch, or there are no assignments
in formatted inline IF, Robocop will not add it.