stylus-supremacy icon indicating copy to clipboard operation
stylus-supremacy copied to clipboard

`insertNewLineAroundBlocks` interprets inline conditions as a "block"

Open maranomynet opened this issue 7 years ago • 4 comments

If insertNewLineAroundBlocks option is true then this block of variable assignments:

$color = red;
$name = 'John';
$greet = 'Hi ' + $name  if ($name);
$err = 'Name missing' unless ($name);
$something = 9000;
$other = 42;

gets broken up as if the if and unless conditions are blocks of their own, like so:

$color = red;
$name = 'John';

$greet = 'Hi ' + $name  if ($name);

$err = 'Name missing' unless ($name);

$something = 9000;
$other = 42;

Which is a bit surprising and can turn ugly quite fast.

maranomynet avatar Oct 17 '18 12:10 maranomynet

There are actually a type of Block regarding the Stylus compiler. Hence, this is not really a bug from Stylus Supremacy, unless people want an exception.

ThisIsManta avatar Oct 23 '18 14:10 ThisIsManta

This is also a simple variable assignment, so the question is which one wins over. :-)

In my experience these sort of lines will be sitting in the middle of other variable assignments, and the resulting breakup patterns will look surprising and out of character compared to the rest of the formatting.

I'd say that if the line starts with variable = ... then treat it as variable assignment, but if it starts with if (...) then treat as a block.

maranomynet avatar Oct 23 '18 15:10 maranomynet

Well, it doesn't matter if the statement starts with if or var =. The Stylus compiler will parse them as if-block anyway. The below code compiles to an AST in exactly the same way.

$var = 123 if ($xxx)

if ($xxx) {
  $var = 123
}

ThisIsManta avatar Oct 23 '18 15:10 ThisIsManta

I see, so even if from the human developer's end there's a difference, the parser can't see the difference?

How come then does the formatter retain the single-line syntax and doesn't normalize it into

if ($xxx) {
  $var = 123
}

?

maranomynet avatar Oct 24 '18 02:10 maranomynet