禁用单个文件更漂亮

问题描述 投票:0回答:7

我需要在 Vs-code 项目中禁用 single file(API URL 文件)的 prettier。实际上,我需要每个 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}`);
javascript reactjs ecmascript-6 visual-studio-code prettier
7个回答
85
投票

如果您希望存储库中的某个文件永远不会被 Prettier 格式化,您可以将其添加到 .prettierignore 文件中:对一个文件禁用 Prettier

来自文档:

要从格式中排除文件,请在以下位置创建一个 .prettierignore 文件: 你的项目的根目录。 .prettierignore 使用 gitignore 语法。

示例:

# Ignore artifacts: 
build 
coverage

# Ignore all HTML files:
*.html 

54
投票

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

忽略文件或文件夹

要从格式中排除文件,请将条目添加到项目

.prettierignore
中的
root
文件或设置
--ignore-path
CLI 选项。
.prettierignore
使用 gitignore 语法。

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

根据扩展名忽略

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

.prettierignore
文件中

*.html.erb

忽略线条

JavaScript

JavaScript 注释

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

    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

使用扩展程序

当您需要时,我们可以使用扩展程序来切换特定页面上的格式,例如更漂亮。

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


11
投票

另一种选择是使用更漂亮的块状切换,以禁用文件内“块”的格式。
例如,在函数定义开始之前添加

// prettier-ignore
将禁用该函数的更漂亮格式。
同样,如果将该行放在
if
语句上方,则仅忽略 if 块。

基本上,一个块由一对

{
}
匹配的大括号表示。

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

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

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

5
投票

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

使用 .gitignore 格式更新您的文件 您也可以在 Prettier 网站上阅读相关内容 https://prettier.io/docs/en/ignore.html#ignoring-files


1
投票

。 React 项目的 prettierignore

build/
node_modules/
internals/generators/
internals/scripts/
package-lock.json
yarn.lock
package.json
coverage

0
投票

要对特定文件禁用 Prettier,只需添加以下代码

// prettier-ignore

示例:

const { gql } = require("@apollo/client");

// prettier-ignore

export const INSERT_RECHARGE_REGISTER = gql`

`

0
投票

您可以在文件顶部使用它:

/* eslint-disable prettier/prettier */

© www.soinside.com 2019 - 2024. All rights reserved.