简体   繁体   English

更漂亮 + VS 代码。 返回语句后在网页上打印分号(.js)

[英]Prettier + VS Code. Printing semicolons on webpage after return statements (.js)

Prettier v8.1.0 out-of-the-box (no .prettierrc files) Prettier v8.1.0 开箱即用(无.prettierrc文件)

I am writing in .js files.我正在写.js文件。 Prettier is adding semicolon after return statements. Prettier 在 return 语句后添加分号。 These are showing up in my development environment webpages.这些显示在我的开发环境网页中。

--example - 例子

return (
    <div className="not-found">
      <h1>Oops...</h1>
      <h2>That page cannot be found.</h2>
      <p>
        Go back to the
        <Link href="/">
          <a>Homepage</a>
        </Link>
      </p>
    </div>
  );

I have extensively researched this, and nobody else seems to be experiencing this issue (or at least writing about it on Google Search).我对此进行了广泛的研究,似乎没有其他人遇到过这个问题(或者至少在谷歌搜索上写过)。 Please note the amount of discussions I have had to filter through looking for this answer, but only to find people arguing about those whom use semicolons after every line and those who don't.请注意我不得不通过寻找这个答案来过滤大量的讨论,但结果却发现人们在争论那些在每一行之后使用分号的人和那些不使用分号的人。

How do I fix this without disabling semicolon auto-insertion all-together?如何在不完全禁用分号自动插入的情况下解决此问题?

Currently there is no option to do this automatically, and according to the Prettier docs , there never will be.目前没有自动执行此操作的选项,而且根据Prettier 文档,永远不会有。

Prettier has a few options because of history.由于历史原因,Prettier 有一些选择。 But we won't add more of them.但我们不会添加更多。

... ...

Now that Prettier is mature enough and we see it adopted by so many organizations and projects, the research phase is over.现在 Prettier 已经足够成熟,我们看到它被许多组织和项目采用,研究阶段已经结束。 We have enough confidence to conclude that Prettier reached a point where the set of options should be “frozen”我们有足够的信心得出结论,Prettier 达到了应该“冻结”选项集的地步


In your case you could tell Prettier to not format return statements at all manually, using an ignore-comment you can add the comment // prettier-ignore above to tell Prettier to skip over your return statement when formatting.在您的情况下,您可以告诉 Prettier 根本不要手动格式化return语句,使用ignore-comment ,您可以在上面添加注释// prettier-ignore以告诉 Prettier 在格式化时跳过您的 return 语句。

With:和:

// prettier-ignore
return [
  "this", "that"
,
      "here", "there"
]

Without:没有:

return ["this", "that", "here", "there"];
  1. Lejlun's answer is correct (and marked as so), but didn't solve the problem. Lejlun 的回答是正确的(并标记为正确),但没有解决问题。 You do not fix this problem without disabling semicolon auto-insertion all-together.如果不完全禁用分号自动插入,就无法解决此问题。 Period.时期。

Solution #1: Ditch the semicolons in javascript files all-together with Prettier (using a.prettierrc.xyz config file in root by setting "semi": to false).解决方案 #1:将 javascript 文件中的分号与 Prettier 一起丢弃(通过将“semi”设置为 false,在根目录中使用 a.prettierrc.xyz 配置文件)。 Enbrace the way code looks cleaner without all the semicolon "noise".拥抱没有分号“噪音”的代码看起来更干净的方式。

Solution #2: Stop using the auto-format-on-save option all-together, whilst also trying to remember to remove the extra semicolon manually every single time, and deal with potential bugs.解决方案#2:完全停止使用保存时自动格式化选项,同时还要记住每次都手动删除多余的分号,并处理潜在的错误。

Solution #3: Use a.prettierignore file or insert comments into every file (above each individual function block) where you want Prettier to not 'touch'.解决方案 #3:使用 .prettierignore 文件或将注释插入到每个文件(在每个单独的 function 块上方)中,您希望 Prettier 不“触摸”这些地方。 Then format all of the returns' inners manually.然后手动格式化所有返回的内部。 Deal with judgement by peers as to why you go to such extreme measures to make your method work instead of just quitting using semis in js code.处理同行对你 go 为什么采取如此极端的措施来使你的方法起作用而不是仅仅退出在 js 代码中使用 semis 的判断。

Personally, I like #1.就个人而言,我喜欢#1。 I hope this helps someone in the near/far future.我希望这能在不久/遥远的将来对某人有所帮助。 Thanks Lejlun!谢谢乐伦! And nice input Scotty!好的输入 Scotty!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM