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

Tagged templates formatting adding indentation #120

Open
DianomiJH opened this issue Jul 19, 2024 · 3 comments
Open

Tagged templates formatting adding indentation #120

DianomiJH opened this issue Jul 19, 2024 · 3 comments
Labels
bug Something isn't working

Comments

@DianomiJH
Copy link

Hi!

It's probably a setup issue on my part, but I've been banging my head against a wall with this formatting issue

this is the code i'd expect not to change indentation (added a couple of nested ifs to add indentation):

const markdown = String.raw
function getMd() {
  if (true) {
    if (true) {
      return markdown`
# Hello

**This is some bold text**
      `
    }
  }
}

this is my prettier config:

{
  "semi": false,
  "singleQuote": false,
  "trailingComma": "all",
  "plugins": ["prettier-plugin-embed"],
  "noEmbeddedMultiLineIndentation": ["markdown", "md"]
}

expected output would be unchanged from the original but this is what I'm seeing:

const markdown = String.raw
function getMd() {
  if (true) {
    if (true) {
      return markdown`
      # Hello

      **This is some bold text**
      `
    }
  }
}

here's a sandbox link:

https://codesandbox.io/p/devbox/3v7s4t?file=%2Fsrc%2FApp.jsx%3A4%2C1-15%2C2

Thanks!

@DianomiJH
Copy link
Author

DianomiJH commented Jul 19, 2024

I tried to do some debugging in my node_modules, and as far as i could tell the condition on this line:

resolvedOptions.noEmbeddedMultiLineIndentation?.includes(commentOrTag)

is returning true as i'd expect

@Sec-ant Sec-ant added the bug Something isn't working label Jul 19, 2024
@Sec-ant
Copy link
Owner

Sec-ant commented Jul 19, 2024

Should be a bug, will look into it, thanks for reporting

@DianomiJH
Copy link
Author

I'm by no means an expert here, but i think this is a valid test:

import * as prettier from "prettier";
import { expect, it } from "vitest";

const pluginPath = "dist/index.js";

it("Doesn't add extra indentation to markdown when noEmbeddedMultiLineIndentation is set", async () => {
  expect(true).toBe(true);
  const code = `
  function foo() {
    const md = /* markdown */ \`
# Title

## Subtitle

Paragraph

- List
- List

\`
    }
      `;
  const res = await prettier.format(code, {
    parser: "babel",
    plugins: [pluginPath],
    noEmbeddedMultiLineIndentation: ["markdown"],
  });
  expect(res).toBe(
    "function foo() {\n  const md = /* markdown */ `\n# Title\n\n## Subtitle\n\nParagraph\n\n- List\n- List\n`;\n}\n",
  );
});

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants