简体   繁体   English

如何让 Prettier 忽略一段代码?

[英]How to make Prettier to ignore a block of code?

Say we have a line of code:假设我们有一行代码:

const a = 'a'; const b = 'b';

and we wouldn't want it to be formatter by Prettier.我们不希望它成为 Prettier 的格式化程序。

What I've tried so far:到目前为止我已经尝试过:

1) 1)

// prettier-ignore
const a = 'a'; const b = 'b';
// prettier-ignore-start
const a = 'a'; const b = 'b';
// prettier-ignore-end

In both cases it gets transformed into:在这两种情况下,它都会转化为:

const a = 'a';
const b = 'b';

So how to ignore a block of code?那么如何忽略一段代码呢?

It sucks, but this isnt currently supported.这很糟糕,但目前不支持。

While this doesnt work for OP's example, one option is to add the prettier-ignore comment to the start of each line to be ignored, using the /* */ comment style, eg.虽然这不适用于 OP 的示例,但一种选择是将prettier-ignore注释添加到要忽略的每一行的开头,使用/* */注释样式,例如。

/* prettier-ignore */ import { SomeTypeA, SomeTypeB, SomeTypeC } from './some-long-folder-name/super-long-import';
/* prettier-ignore */ import { someFnA, someFnB, someFnC, } from './another-long-folder-name/super-long-import';

In my opinion, this is easier on the eyes than the alternatives;在我看来,这比其他选择更容易让人眼前一亮;

Letting prettier reformat the imports:让 prettier 重新格式化导入:

import {
    SomeTypeA,
    SomeTypeB,
    SomeTypeC,
} from './some-long-folder-name/super-long-import';
import {
    someFnA,
    someFnB,
    someFnC,
} from './another-long-folder-name/super-long-import';

Specifying the // prettier-ignore comment above each import:在每个导入上方指定// prettier-ignore注释:

// prettier-ignore
import { SomeTypeA, SomeTypeB, SomeTypeC } from './some-long-folder-name/super-long-import';
// prettier-ignore
import { someFnA, someFnB, someFnC, } from './another-long-folder-name/super-long-import';

The reason why // prettier-ignore does not work here is that // prettier-ignore will exclude the next node in the abstract syntax tree from formatting (see https://prettier.io/docs/en/ignore.html ). // prettier-ignore在这里不起作用的原因是// prettier-ignore将从格式化中排除抽象语法树中的下一个节点(参见https://prettier.io/docs/en/ignore.html )。

In your case the next node would only be const a = 'a';在您的情况下,下一个节点只会是const a = 'a'; . . For instance,例如,

// prettier-ignore
const a = 
  'a';

would be preserved after formatting.格式化后会保留。

If you want to keep both assignments in one line without changing the prettier configuration for your whole file, you could use destructuring assignment if you are using es6 or node:如果您想将两个分配保留在一行而不更改整个文件的更漂亮的配置,如果您使用的是 es6 或节点,则可以使用解构分配

const [a, b] = ["a", "b"];

Sometimes multiple statements can be wrapped in a block with // prettier-ignore in front of it:有时可以将多个语句包装在一个块中,并在其前面加上// prettier-ignore

// prettier-ignore
{
abcRouter('/api/abc', server);
xRouter  ('/api/x', server);
}

Of course, that doesn't make sense for block-level const declarations, but you wrote that was not your actual code and just an example.当然,这对于块级const声明没有意义,但你写的不是你的实际代码,只是一个例子。 So that's a solution that works in some but not in all cases.所以这是一个在某些情况下但并非在所有情况下都有效的解决方案。 Overall, the strategy is to wrap multiple things in one thing that can be prettier-ignore d.总体而言,该策略是将多个东西包装在一个可以prettier-ignore的东西中。

Another option is to move all the code you don't want to format (eg, because it's generated) to a separate file excluded by .prettierignore .另一种选择是将您不想格式化的所有代码(例如,因为它已生成)移动到.prettierignore排除的单独文件中。

prettier-ignore-start and prettier-ignore-end are supported only in Markdown . prettier-ignore-start和 prettier prettier-ignore-end仅在 Markdown中受支持。

You can ignore the whole method你可以忽略整个方法

// prettier-ignore
function myFunc() {
    const a = 'a'; const b = 'b';
    return a + b;
}

Actually I tried only on typescript.实际上我只尝试过 typescript。 But I think should works on JS as well.但我认为也应该适用于 JS。

You can ignore a code block is not supported at the moment, your option is is to only use // prettier-ignore您可以忽略目前不支持的代码块,您的选择是仅使用// prettier-ignore

example below:下面的例子:

// prettier-ignore
const a = 'a';
const b = 'b';

// prettier-ignore
function xyz() {
 console.log({a, b})
}

known issue -> https://github.com/prettier/prettier/issues/5287已知问题-> https://github.com/prettier/prettier/issues/5287

Docs -> https://prettier.io/docs/en/ignore.html#javascript文档-> https://prettier.io/docs/en/ignore.html#javascript

To disable the auto line break in the VS code.禁用 VS 代码中的自动换行符。

Open vs code => Code => Preferences => Settings => In the search field type: Prettier Now from the provided Prettier settings, choose the Prettier: Print Width打开 vs code => Code => Preferences => Settings => 在搜索字段中输入:Prettier 现在从提供的 Prettier 设置中,选择 Prettier: Print Width

r/vscode - To disable the auto line break in the VS code. r/vscode - 禁用 VS 代码中的自动换行。 Screenshot截屏

And Instead of 80, you can make it a big number.而不是 80,你可以把它变成一个大数字。 Eg: 999999 Now you don't have to worry about the line break.例如:999999 现在您不必担心换行了。

https://www.reddit.com/r/vscode/comments/lkz3d7/to_disable_the_auto_line_break_in_the_vs_code/ https://www.reddit.com/r/vscode/comments/lkz3d7/to_disable_the_auto_line_break_in_the_vs_code/

What worked for me was using this对我有用的是使用这个

<!-- prettier-ignore -->
{% extends "main/base.html" %}
{% load humanize static %}
{% block base_content %}
<!-- prettier-ignore-end -->

This was for django and Vue CDN这适用于 django 和 Vue CDN

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

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