为单个文件禁用 prettier

Disable prettier for a single file

我需要为 单个文件(API URLs 文件)禁用 prettier Vs 代码中的项目。实际上,我需要每个 API 及其 URL 都在一行中,但 prettier 将它们分成两行。

之前

export const GET_SEARCH_TEACHERS = params => myexampleFunction_app_base(`teachers/search/${params.search}`);

之后

export const GET_SEARCH_TEACHERS = params =>
myexampleFunction_app_base(`teachers/search/${params.search}`);

感谢 evolutionxbox,到目前为止找到了几个解决方案。

忽略文件或文件夹

要从格式中排除文件,请将条目添加到项目 root 中的 .prettierignore 文件或设置 --ignore-path CLI 选项。 .prettierignore 使用 gitignore 语法。

/app/src/scripts/example.js
/app/src/folder/

根据扩展忽略

要根据扩展名排除文件,您也可以将条目添加到 .prettierignore 文件

*.html.erb

忽略行

JavaScript

// prettier-ignore 的 JavaScript 注释将从格式化中排除抽象语法树中的下一个节点。

    matrix(
      1, 0, 0,
      0, 1, 0,
      0, 0, 1
    )

    // prettier-ignore
    matrix(
      1, 0, 0,
      0, 1, 0,
      0, 0, 1
    )

将转换为:

    matrix(1, 0, 0, 0, 1, 0, 0, 0, 1);

    // prettier-ignore
    matrix(
      1, 0, 0,
      0, 1, 0,
      0, 0, 1
    )

JSX

    <div>
      {/* prettier-ignore */}
      <span     ugly  format=''   />
    </div>

更多:https://prettier.io/docs/en/ignore.html

使用扩展程序

我们可以在您需要时使用扩展程序在特定页面上切换格式,如 prettier。

格式切换 https://marketplace.visualstudio.com/items?itemName=tombonnike.vscode-status-bar-format-toggle

如果你希望 repo 中的某个文件永远不会被 prettier 格式化,你可以将它添加到 .prettierignore 文件中:Disable Prettier for one file

来自文档:

To exclude files from formatting, create a .prettierignore file in the root of your project. .prettierignore uses gitignore syntax.

Example:

# Ignore artifacts: 
build 
coverage

# Ignore all HTML files:
*.html 

在您的存储库的根目录中创建 .prettierignore 文件并添加您要忽略的文件夹的名称并添加您要忽略并保存的文件的完整路径。

使用 .gitignore 格式更新你的文件 你也可以在更漂亮的网站上阅读它 https://prettier.io/docs/en/ignore.html#ignoring-files

另一种选择是使用更漂亮的 block-like 开关,以禁用文件中“块”的格式设置。
例如,在函数定义开始之前添加 // prettier-ignore 将禁用该函数的更漂亮格式。
同样,如果将此行放在 if 语句之上,则只会忽略 if 块。

基本上一个块由一对 { } 匹配的大括号表示。

... (code up here is formatted by prettier)

// prettier-ignore
function noPrettierFormattingInHere(){
  ...
}

... (code down here is formatted by prettier)