un-ts / prettier

:package: Opinionated but Incredible Prettier plugins.
https://prettier.vercel.app
MIT License
277 stars 26 forks source link

[prettier-plugin-autocorrect] No changes are made #276

Open queengooborg opened 1 year ago

queengooborg commented 1 year ago

After installing the plugin and telling Prettier to use it, none of the desired changes are produced for our Markdown files. When doing a little debugging, it appears that the print() function isn't getting called.

JounQin commented 1 year ago

Can you provide a reproduction?

queengooborg commented 1 year ago

Of course -- here's a basic script to reproduce the issue:

import assert from "node:assert";
import prettier from "prettier";
import autocorrect from "autocorrect-node";

const input =
  "**`:host`** [CSS](/zh-CN/docs/Web/CSS) [伪类](/zh-CN/docs/Web/CSS/Pseudo-classes) 选择内部使用了该CSS的[影子DOM(shadow DOM)](/zh-CN/docs/Web/API/Web_components/Using_shadow_DOM)的影子宿主(shadow host)——换句话说,这允许你从其影子DOM内部选择自定义元素。";
const expected = autocorrect.format(input);

const actual = await prettier.format(input, {
  parser: "markdown",
  plugins: ["prettier-plugin-autocorrect"],
});

assert.deepEqual(actual, expected);

(This is with Prettier v3.0.0 and prettier-plugin-autocorrect v0.4.1.)

JounQin commented 1 year ago

OK, the problem is this prettier does not recognize to use prettier-plugin-autocorrect as its parser

import assert from "node:assert";
import prettier from "prettier";
import autocorrect from "autocorrect-node";

const input =
  "**`:host`** [CSS](/zh-CN/docs/Web/CSS) [伪类](/zh-CN/docs/Web/CSS/Pseudo-classes) 选择内部使用了该CSS的[影子DOM(shadow DOM)](/zh-CN/docs/Web/API/Web_components/Using_shadow_DOM)的影子宿主(shadow host)——换句话说,这允许你从其影子DOM内部选择自定义元素。";
const expected = autocorrect.formatFor(input, 'test.md'); // `formatFor` is required

const actual = await prettier.format(input, {
  parser: "autocorrect", // override default inferred parser
  plugins: ["prettier-plugin-autocorrect"],
  filepath: "test.md", // a filepath is required
});

assert.deepEqual(actual, expected);

However, this will make the default markdown printer unusable, I need to find a better solution for this.

JounQin commented 11 months ago

I got the idea that we can detect the default parser for files in the autocorrect plugin automatically and run prettier without autocorrect first, and run autocorrect.formatFor after then.