prettier / prettier

Prettier is an opinionated code formatter.
https://prettier.io
MIT License
49.3k stars 4.34k forks source link

Extra spaces in html tagged templates #7096

Closed DiogoDoreto closed 4 years ago

DiogoDoreto commented 4 years ago

Prettier 1.19.1 Playground link

--parser babel

Input:

const label = html`<span>${text}</span>`;

Output:

const label = html`
  <span>${text}</span>
`;

Expected behavior:

Adding whitespace inside the tagged template literal affects the output. To avoid the spaces around my components, the only way right now is to add ignore comments.

alexander-akait commented 4 years ago

/cc @saschanaz please don't repeat issues, put examples here

saschanaz commented 4 years ago

@evilebottnawi For me #7101 wasn't exactly this issue though, because hyperHTML automatically trim out the newlines if single-rooted and thus won't make a breaking change.

Edit: But anyway it looks similar, and 100% sorry for #7100 which is exactly same as this one 😞

saschanaz commented 4 years ago

What should be done here? Any formatting outside a tag is potentially breaking. I think we should introduce one of these option to:

DiogoDoreto commented 4 years ago

@saschanaz I'd vote to keep it simple: if the user tries to have it as one-liner and the line is not too long, avoid adding the line breaks. Otherwise leave the default formatting behavior.

This will create an incentive to the user to simplify their content, possibly extracting it into simple variables that would be easier to read.

It then will behave in a similar way as this example:

// the line below is left as-is
const obj = [{ test: true }];

// the code below will be reformatted with line breaks
// input:
const obj = [{ 
  test: true }];
// output:
const obj = [
  {
    test: true
  }
];

What do you think?

saschanaz commented 4 years ago

That doesn't even need an option then! Sounds good to me 👍

saschanaz commented 4 years ago

@evilebottnawi Do prettier maintainers think https://github.com/prettier/prettier/issues/7096#issuecomment-564444006 is good? Is it possible to implement that?

LarsDenBakker commented 4 years ago

I would really like to see this as well.

alexander-akait commented 4 years ago

@saschanaz it is out of scope this bug, we should not add extra space in this case, so PR welcome should be not hard

alexander-akait commented 4 years ago

Fixed #7208