[英]How to apply only single rule or set of rules with prettier (or prettierx)
Would like to use prettier (or prettierx or similar tool) in the following way:想通过以下方式使用 prettier(或 prettierx 或类似工具):
npx prettier --no-defaults --no-semi --write test.js
where --no-defaults
is a hypothetical option to ignore & not apply any other rules except for those specified (ONLY apply the --no-semi
rule/option in this case).其中--no-defaults
是一个假设选项,用于忽略 & 不应用除指定规则之外的任何其他规则(在这种情况下仅应用--no-semi
规则/选项)。 In otherwords, I only want to run prettier and apply a single rule or the set of rules I specify.换句话说,我只想运行更漂亮并应用单个规则或我指定的一组规则。
I read through the docs and seems there is no obvious way to do this;我通读了文档,似乎没有明显的方法可以做到这一点; I realize prettierx is supposed to be less opinionated but also does not seem to offer a way to run without applying its own defaults.我意识到 prettierx 应该不那么固执己见,但似乎也没有提供一种在不应用自己的默认值的情况下运行的方法。
You can use ESLint for that purpose, a one-liner approach for the above example would look like this:您可以为此目的使用 ESLint,上述示例的单行方法如下所示:
eslint --fix --rule 'semi: [error, never]' test.js
To define multiple rules, separate them using commas, or use the option multiple times.要定义多个规则,请使用逗号分隔它们,或多次使用该选项。 If adding multiple rules it's recommended to add them to a dedicated file like eslintrc.json
or declare them in package.json
where they will be automatically parsed by eslint, thus eliminating the need for rule option in command line.如果添加多个规则,建议将它们添加到eslintrc.json
类的专用文件中,或者在package.json
中声明它们,它们将由 eslint 自动解析,从而无需在命令行中使用 rule 选项。 More on configuration here .更多关于配置在这里。
Caution: Providing rules through command line will merge them with existing eslint config if present in your project, in order to avoid that you can add a flag like this:注意:通过命令行提供规则会将它们与现有的 eslint 配置合并(如果存在于您的项目中),以避免您可以添加这样的标志:
eslint --no-eslintrc --fix --rule 'semi: [error, never]' test.js
Most of the layout and formatting rules are fixable by eslint, a comprehensive list is given here .大多数布局和格式规则都可以通过 eslint 修复,这里给出了一个完整的列表。 To know more about eslint and other helpful command line flags see here and here .要了解有关 eslint 和其他有用的命令行标志的更多信息,请参见此处和此处。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.