-
-
Notifications
You must be signed in to change notification settings - Fork 4.5k
Disable embedded language formatting by default and make it configurable #5588
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
Comments
Why don’t you want Prettier to format the HTML inside the template strings? Does it break your code’s syntax? |
@j-f1 It was breaking tests for autocomplete because the tests compare the input HTML to the autocompleted HTML, and it was breaking the smart "hover" functionality (selecting an open tag highlights the closing tag, and vice versa) because the cursor would be in the wrong character position in the text due to the added indentation. The HTML is still valid and displays properly, but it is impossible to test the hover functionality because we don't know where in the string the tags are. |
Workaround:
|
@lydell Yes I can see that this would work, as Prettier only checks for the |
Yeah, I only mentioned it as a workaround, not a solution. |
Perhaps we should keep single-line HTML literals single-line? How about renaming your template tag |
The issue here isn't about HTML. It's about Prettier assuming that the contents of certain tagged template literals is safe to format, while in theory it obviously isn't. For the The question is what to do about it. Should be there be a way to opt out? Or is it OK to ask people to change their tag name? I remember @bakkot having commented on this early on (EDIT: #2337 (comment) and #2337 (comment) might be what I'm thinking of, not sure), but I can't find it now. I did find #4424 though which is a bit related. |
Similar to a configuration setting for this would be something to tell Prettier what parser any given tagged template literal should use. I'm using AngularJS in template literals, for example. |
IMHO the user never expects a formatter to behave differently based on the content of an identifier, so this is a bug. Naming a variable differently should not affect how the file is formatted (except for if the length changes); and in this case, the name of the tag not only changes the formatting of the file but actually changes the contents of the literal string. Not only should the contents of a string never be changed by a formatter, but that behavior shouldn't be enabled or disabled based on the name of the template tag. I think the best solution here is very simple: only format the contents of templates for a list of tag names that the user explicitly enables this behavior for. Whether the list should be empty by default, or |
Empirically, many users do expect this. I agree with you overall (see my comments linked above), just saying, this claim is wrong as a question of fact. |
It’s not that easy, though, because there’s also non-identifier-only stuff such as |
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
Feel difficult to config |
Could we add an option to allow automatic formatting for If possible I'd also like some sort of mode that automatically enables both the existing embedded formatting and new embedded formatting features like what I'm suggesting for |
@bschlenk One dirty hack is to use: import { svg as html } from "lit-html"; |
@Coder-256 Prettier now formats this onto one line. const foo = html`<p>test</p>`; Does that fix your issue? |
I am just facing this issue, but I would like to be able to have a normal html formatting in a template literal instead of a oneliner. In bigger html template literals it is not a good behaviour |
@somahargitai Please describe the problem you have. When the html template gets longer, prettier does turn it into multiple lines. |
Agree this should be turned off by default — different |
I am currently in a dead-lock between either having mjml-template-literal to enable inline mjml syntax support and using a tag named |
9E88
Chiming in to +1 support for custom tags. I'd like to be able to use a plugin to support |
This comment was marked as duplicate.
This comment was marked as duplicate.
I'm working on a Prettier plugin called prettier-plugin-embed to customize embedded languages formatting. It's not production ready and lacks tests, but I think it's time to let more users try this plugin. To simply put:
Check the README file for a more detailed introduction: https://github.com/Sec-ant/prettier-plugin-embed#readme Hope you find it helpful ❤️ |
@Sec-ant thanks so much for this! I can confirm, this works well with formatting SQL inside tagged template literals with |
Hey awesome people, any way to push this forward please? |
You can try to use https://github.com/Sec-ant/prettier-plugin-embed |
Yes it does, the problem I am having now is that this
becomes
and it breaks the |
Prettier 1.15.2
Playground link
Input:
Output:
Expected behavior:
I would like a way to disable HTML template string formatting for a js/ts file, but I still want to format the file using Prettier (I just want the template strings to be treated as regular template strings rather than being formatted as HTML). In other words, I would like a way to disable the function here:
prettier/src/language-js/embed.js
Lines 548 to 565 in 2247ce1
Other than using
// prettier-ignore
for every single template string, I do not think there is currently any way to do this.My use case is creating test cases for an editor extension that provides autocomplete for HTML; so I want the tests themselves to be formatted as TypeScript, but I don't want to have Prettier format the HTML inside the template strings. Basically, something like
htmlWhitespaceSensitivity: "ignore"
but that only applies for template strings (I also have Vue files that I want to format).The text was updated successfully, but these errors were encountered: