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

Indent script and style tags content in *.vue files #3888

Closed
GrahamCampbell opened this issue Feb 4, 2018 · 229 comments
Closed

Indent script and style tags content in *.vue files #3888

GrahamCampbell opened this issue Feb 4, 2018 · 229 comments
Labels
lang:vue Issues affecting Vue locked-due-to-inactivity Please open a new issue and fill out the template instead of commenting. type:option request Issues requesting a new option. We generally don’t accept these unless there is technical necessity.

Comments

@GrahamCampbell
Copy link

Prettier 1.10.2
Playground link

When I have a Vue.js file:

<script>
    export default {
        mounted() {
            console.log('Component mounted!')
        }
    }
</script>

How do I stop prettier changing the indentation to:

<script>
export default {
    mounted() {
        console.log("Component mounted!");
    }
};
</script>
@lipis
Copy link
Member

lipis commented Feb 4, 2018

Same with <style>

Prettier 1.10.2
Playground link

--parser vue
--tab-width 4

Input:

<style>
    body {
        color: #fff;
    }
</style>

Output:

<style>
body {
    color: #fff;
}
</style>

Expected behavior:

I would prefer the output to stay unchanged..

@azz
Copy link
Member

azz commented Feb 4, 2018

Why would you want an additional level of indentation there?

@azz azz added type:question Questions and support requests. Please use Stack Overflow for them, not the issue tracker. area:multiparser Issues with printing one language inside another, like CSS-in-JS lang:vue Issues affecting Vue labels Feb 4, 2018
@hawkrives
Copy link
Contributor

For me, I view the contents of a script tag like the contents of any other html tag - I indent its body. So for me, it's consistency.

@lipis
Copy link
Member

lipis commented Feb 5, 2018

same for me..

@azz
Copy link
Member

azz commented Feb 5, 2018

Current behaviour matches the default for eslint-plugin-vue/script-indent. Seems like a small price to pay for an entire indentation level for all your code. So long as all script/style tags are consistently not indented, this seems like a non-issue to me.

@GrahamCampbell
Copy link
Author

It's a blocker for using prettier for me and many others.

@azz
Copy link
Member

azz commented Feb 5, 2018

@GrahamCampbell would you be able to explain why it is a blocker?

@lydell
Copy link
Member

lydell commented Feb 5, 2018

I think we should follow the most common Vue convention here, if there is one.

@lipis
Copy link
Member

lipis commented Feb 5, 2018

Also there are two different schools for HTML:

<html>
  <head>
    <title>This is more consistent</title>
  </head>
  <body>
    <div></div>
  </body>
</html>
<html>
<head>
  <title>than this</title>
</head>
<body>
  <div></div>
</body>
</html>

@lydell
Copy link
Member

lydell commented Feb 5, 2018

Yes, for HTML, but now we’re talking Vue :) Which similar, but not quite the same.

@lipis
Copy link
Member

lipis commented Feb 5, 2018

Just for fun: https://twitter.com/PrettierCode/status/960584198376566784 :)

@vjeux
Copy link
Contributor

vjeux commented Feb 5, 2018

Fwiw, when I implemented this, I followed the vue official website example: https://vuejs.org/v2/guide/single-file-components.html

@lipis
Copy link
Member

lipis commented Feb 5, 2018

I was just checking the documentation for the same reason.. and it looks like in the unit test section they are indenting.. https://vuejs.org/v2/guide/unit-testing.html 😕

@igeligel
Copy link
Contributor

igeligel commented Feb 5, 2018

In the style guide there is one example where the content of the <script> is unindented.

<!-- TodoItem.vue -->
<template>
  <span>
    {{ todo.text }}
    <button @click="removeTodo(todo)">
      X
    </button>
  </span>
</template>

<script>
import { mapActions } from 'vuex'

export default {
  props: {
    todo: {
      type: Object,
      required: true
    }
  },
  methods: mapActions(['removeTodo'])
}
</script>

Same for <style>:

<template>
  <button class="c-Button c-Button--close">X</button>
</template>

<!-- Using the BEM convention -->
<style>
.c-Button {
  border: none;
  border-radius: 2px;
}

.c-Button--close {
  background-color: red;
}
</style>

I think this is controverse and a configuration option should be provided.

@lipis
Copy link
Member

lipis commented Feb 5, 2018

We want less options... not more :)

@felixfbecker
Copy link

Here's an actual argument why it's beneficial to have indentation: It allows you to use (indentation-based) code folding to collapse the style and script tags (as in Visual Studio Code).

@montogeek
Copy link
Contributor

montogeek commented Feb 6, 2018

@felixfbecker Is that the case? Folding is available for tags, it is not really dependant on its content. But looks like VSCode check for indentation to make elements foldable.

There is an issue open about it microsoft/vscode#3422

@igeligel
Copy link
Contributor

igeligel commented Feb 6, 2018

@montogeek

Yes, it is a good reason. From my comment earlier i just wanted to say it is official. I do not care what is used in the end when it is consistent :)

imgur album

Inlined pictures:

@lipis
Copy link
Member

lipis commented Feb 6, 2018

If you have the Vue plugin installed it folds them correctly.. but not out of the box VS Code

@montogeek
Copy link
Contributor

This is not the case on Atom for example:
image
image

@larsenwork
Copy link

I'd vote for indentation too. For widest and easiest editor support for folding

@GrahamCampbell
Copy link
Author

@lipis
Copy link
Member

lipis commented Feb 8, 2018

For what it's worth from the 241 people that voted in the Twitter poll here are the results..

▓▓▓▓▓▓▓▓▓░░░░░░ 57%  Yes       137 people
▓▓▓░░░░░░░░░░░░ 19%  No         46 people
▓▓▓▓░░░░░░░░░░░ 24%  Whatever   58 people

if we remove the whatever then it becomes

▓▓▓▓▓▓▓▓▓▓▓░░░░ 75%  Yes       137 people
▓▓▓▓░░░░░░░░░░░ 25%  No         46 people

and since whatever would do whatever they should be included with the majority..

▓▓▓▓▓▓▓▓▓▓▓▓░░░ 81%  Yes       195 people
▓▓▓░░░░░░░░░░░░ 19%  No         46 people

@azz
Copy link
Member

azz commented Feb 8, 2018

and since whatever would do whatever they should be included with the majority..

What?

@lukepolo
Copy link

lukepolo commented Feb 8, 2018

He is saying that people who say whatever, will follow the herd , and join the yes

@lerit
Copy link

lerit commented Feb 9, 2018

prettier and eslint-plugin-vue conflict for indent rule.they say we should not use it together.
"Both vue/script-indent and prettier change the indentation of your code. Both are formatter. You can't use both together but you can choose which formatter you use."
vuejs/eslint-plugin-vue#369

@GrahamCampbell
Copy link
Author

I think the big split there indicates that configuration is needed.

@pbastowski
Copy link

pbastowski commented Apr 22, 2019

This was going to be a Vue only setting, not affecting other html like code. It would have been implemented and released by now.

However, Someone thought it might be wiser to add html to the scope and thus introduced a whole new level of complexity and interested parties.

And that’s called scope creep. It kills good projects.

A better approach would have been to implement the feature as previously agreed for Vue only.

Only after that was done, we could create a new issue to consider including html as a new feature.

@DCzajkowski
Copy link

@ForsakenHarmony: Probably because it was released and this is not done

This is not how milestones work 😅

Justineo added a commit to Justineo/prettier that referenced this issue Apr 23, 2019
Justineo added a commit to Justineo/prettier that referenced this issue Apr 23, 2019
@Justineo
Copy link

Justineo commented Apr 23, 2019

I've created a PR (#6077) which introduces a new option: html-top-level-indent with the following valid options:

  • "always" - Always apply top-level indent for templates, scripts and styles.
  • "never" - Avoid top-level indent for templates, scripts and styles.
  • "auto" - Avoid top-level indent for scripts and styles inside Vue files. This is the default value and reflects current behavior.

I think we can move the technical discussion over there.

@lydell
Copy link
Member

lydell commented May 26, 2019

In #6077 we learned that making a generic option for all HTML-like languages sounded like a good idea, but it still needed Vue-specific behaviors and didn’t work out very well in practice. So after that exploration we decided to go with a simpler Vue-specific option instead: --vue-indent-script-and-style. It’s up for grabs if you want to make a PR!

I’m really sorry for all the back-and-forth!

@lydell
Copy link
Member

lydell commented Aug 1, 2019

#6157 has been merged, adding --vue-indent-script-and-style. Release guesstimate: September.

@andystroz
Copy link

@lydell looking for this feature in release / specifically in vscode prettier, is there going to be one soon?

@lipis
Copy link
Member

lipis commented Sep 3, 2019

Most likely by the end of this week :)

@ezdotjs

This comment has been minimized.

@Kocal

This comment has been minimized.

@jackkoppa
Copy link

Hi there @lydell & @kamilic - thanks so much for implementing this! Very excited to have this control over components again. Anything we can help with to get Prettier ready for another release?

@lydell
Copy link
Member

lydell commented Oct 17, 2019

@jackkoppa We started the release process for 1.19.0 a while ago, but realized that we need to get a few more PRs in to make a stable release. We’ve been doing a lot of work since then and want to make a release as soon as possible!

@jackkoppa
Copy link

thanks @lydell, really appreciate it!

@netzpixel
Copy link

Any updates? I know it's just been two weeks, but I desperately need this feature!

@gluons
Copy link

gluons commented Oct 31, 2019

#6486

It's coming.

@alexander-akait
Copy link
Member

Yep, release will be in near future, i think we can close this issue /cc @lydell

@lydell
Copy link
Member

lydell commented Nov 5, 2019

Let’s close it once 1.19 is out. Maybe today.

@lipis lipis mentioned this issue Nov 5, 2019
12 tasks
@gluons
Copy link

gluons commented Nov 6, 2019

Keep track of #6787. 👀

@lydell
Copy link
Member

lydell commented Nov 9, 2019

1.19 is released! 🎉

@lydell lydell closed this as completed Nov 9, 2019
@lydell lydell removed the help wanted We're a small group who can't get to every issue promptly. We’d appreciate help fixing this issue! label Nov 9, 2019
@lock lock bot added the locked-due-to-inactivity Please open a new issue and fill out the template instead of commenting. label Feb 7, 2020
@lock lock bot locked as resolved and limited conversation to collaborators Feb 7, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
lang:vue Issues affecting Vue locked-due-to-inactivity Please open a new issue and fill out the template instead of commenting. type:option request Issues requesting a new option. We generally don’t accept these unless there is technical necessity.
Projects
None yet
Development

Successfully merging a pull request may close this issue.