简体   繁体   中英

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:

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). 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.

You can use ESLint for that purpose, a one-liner approach for the above example would look like this:

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. 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 --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 . To know more about eslint and other helpful command line flags see here and here .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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