question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Disable embedded language formatting by default and make it configurable

See original GitHub issue

Prettier 1.15.2 Playground link

--parser babylon

Input:

const foo = html`<p>test
</p>`;

Output:

const foo = html`
  <p>test</p>
`;

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:

https://github.com/prettier/prettier/blob/2247ce1aab604bf1b95b3d010c08d48b003286c7/src/language-js/embed.js#L548-L565

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).

Issue Analytics

  • State:open
  • Created 5 years ago
  • Reactions:20
  • Comments:48 (25 by maintainers)

github_iconTop GitHub Comments

11reactions
thorn0commented, Aug 27, 2020

The plan is to extend the --embedded-language-formatting option and implement support for something like this:

{
  overrides: [
    {
      file: "*.js",
      options: {
        "embedded-language-formatting": [
          { comment: "js", parser: "babel" },
          { callee: "html", parser: "html" },
        ],
      },
    },
  ];
}

Also a way to specify whether these definitions are an addition to the auto behavior or a replacement for it will be needed.

First we need to decide on the format of this configuration. If anybody wants to write a proposal, it’ll be really appreciated.

7reactions
YarnSpherecommented, Dec 3, 2019

I don’t know if this has been mentioned before, but how about a generic config such as formatTaggedTemplates which receives an object mapping the tag name to the parser to use.

The default could be something like:

{
  "formatTaggedTemplates": {
    "gql": "graphql"
  }
}

This would not only allow users to disable unintended formatting but also to configure Prettier to format tagged templates with arbitrary tags, for example:

{
  "formatTaggedTemplates": {
    "ng": "angular"
  }
}

would make Prettier format a template with tag ng as Angular-specific HTML.

To disable all template formatting we would use {"formatTaggedTemplates": {}} or maybe also {"formatTaggedTemplates": false}.

As a CLI option it could be specified with prettier --formatTaggedTemplates gql=graphql,ng=angular, prettier --formatTaggedTemplates none, or something similar.

This seems like a more generic solution than the others mentioned in this thread. Thoughts?

Read more comments on GitHub >

github_iconTop Results From Across the Web

Prettier 2.1: new --embedded-language-formatting option and ...
This option allows you to switch between the default behavior ( auto ) and disabling this feature entirely ( off ). It applies...
Read more >
Configure report settings - Power BI Client APIs - Microsoft Learn
Use the localeSettings property to specify the language and the formatting of the embedded report: The language property defines the language ...
Read more >
How can I configure Prettier & VS Code's settings.json file to ...
You need to configure your default formatter in a generalized way to be able to format JavaScript & HTML. Your trying to specify...
Read more >
Language Configuration Guide - Visual Studio Code
json : Note: If your language configuration file name is or ends with language-configuration.json , you will get autocompletion and validation in VS...
Read more >
Creating and Enforcing a Code Formatting Standard with ...
While you can build your clang-format configuration from scratch, it's much easier to start with an existing style and make your modifications.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found