All Projects → prettier → Prettier Eslint

prettier / Prettier Eslint

Licence: mit
Code ➡️ prettier ➡️ eslint --fix ➡️ Formatted Code ✨

Programming Languages

javascript
184084 projects - #8 most used programming language

Projects that are alternatives of or similar to Prettier Eslint

Prettier Atom
An atom package for the prettier formatter.
Stars: ✭ 750 (-78.17%)
Mutual labels:  eslint, prettier, formatter
Prettier Eslint Cli
CLI for prettier-eslint
Stars: ✭ 451 (-86.87%)
Mutual labels:  eslint, prettier, formatter
Typescript Express Starter
🚀 TypeScript Express Starter
Stars: ✭ 238 (-93.07%)
Mutual labels:  eslint, prettier, formatter
Mevn Cli
Light speed setup for MEVN(Mongo Express Vue Node) Apps
Stars: ✭ 696 (-79.74%)
Mutual labels:  eslint, prettier, hacktoberfest
Poetic
Automatically install and maintain ESLint, Prettier, EditorConfig and Airbnb rules for JavaScript, TypeScript and React.
Stars: ✭ 165 (-95.2%)
Mutual labels:  eslint, prettier, formatter
Eslint Config Wesbos
No-Sweat™ Eslint and Prettier Setup - with or without VS Code
Stars: ✭ 2,293 (-33.25%)
Mutual labels:  eslint, prettier, hacktoberfest
prettier-eslint-atom
DEPRECATED IN FAVOR OF prettier-atom + ESLint integration
Stars: ✭ 64 (-98.14%)
Mutual labels:  formatter, eslint, prettier
react16-seed-with-apollo-graphql-scss-router4-ssr-tests-eslint-prettier-docker-webpack3-hot
Seed to create your own project using React with Apollo GraphQL client
Stars: ✭ 19 (-99.45%)
Mutual labels:  eslint, prettier
gatsby-starter-antoine
My opinionated Gatsby.js starter
Stars: ✭ 17 (-99.51%)
Mutual labels:  eslint, prettier
vite-react-ts-tailwind-firebase-starter
Starter using Vite + React + TypeScript + Tailwind CSS. And already set up Firebase(v9), Prettier and ESLint.
Stars: ✭ 108 (-96.86%)
Mutual labels:  eslint, prettier
awesome-address-book
This project shows a basic address book built with ReactJS, Redux Toolkit and Typescript 📖
Stars: ✭ 20 (-99.42%)
Mutual labels:  eslint, prettier
nextjs-baseweb
Slightly opinionated scaffold of Next.js and Base Web.
Stars: ✭ 46 (-98.66%)
Mutual labels:  eslint, prettier
eslint-config-adjunct
A reasonable collection of plugins to use alongside your main esLint configuration
Stars: ✭ 39 (-98.86%)
Mutual labels:  eslint, prettier
node-backend-template
A template for NodeJS backend projects
Stars: ✭ 19 (-99.45%)
Mutual labels:  eslint, prettier
react-component-library-lerna
Build your own React component library managed with lerna, presented with storybook and published in private npm registry.
Stars: ✭ 55 (-98.4%)
Mutual labels:  eslint, prettier
next-pwa-template
Next.js progressive web app template
Stars: ✭ 266 (-92.26%)
Mutual labels:  eslint, prettier
OSAPI
👋 OSAPI 是依靠通用性后台管理平台搭建的API管理平台,基于 vue3、Nestjs 技术栈实现,包含 RBAC 角色权限模块、数据展示、编辑等模块。
Stars: ✭ 32 (-99.07%)
Mutual labels:  eslint, prettier
walrus
🎉 Cli development framework.
Stars: ✭ 17 (-99.51%)
Mutual labels:  eslint, prettier
create-react-typescript-app
Integrating Reactjs with Typescript + ESLint
Stars: ✭ 20 (-99.42%)
Mutual labels:  eslint, prettier
happy-web
Project built during Rocketseat's Next Level Week #3
Stars: ✭ 19 (-99.45%)
Mutual labels:  eslint, prettier

prettier-eslint

Formats your JavaScript using prettier followed by eslint --fix

Build Status Code Coverage version downloads MIT License

All Contributors PRs Welcome Donate Code of Conduct Roadmap Examples

Watch on GitHub Star on GitHub Tweet

The problem

The fix feature of eslint is pretty great and can auto-format/fix much of your code according to your ESLint config. prettier is a more powerful automatic formatter. One of the nice things about prettier is how opinionated it is. Unfortunately, it's not opinionated enough and/or some opinions differ from my own. So after prettier formats the code, I start getting linting errors.

This solution

This formats your code via prettier, and then passes the result of that to eslint --fix. This way you can get the benefits of prettier's superior formatting capabilities, but also benefit from the configuration capabilities of eslint.

For files with an extension of .css, .less, .scss, or .json this only runs prettier since eslint cannot process those.

Installation

This module is distributed via npm which is bundled with node and should be installed as one of your project's devDependencies:

npm install --save-dev prettier-eslint

Usage

Example

const format = require("prettier-eslint");

// notice, no semicolon in the original text
const sourceCode = "const {foo} = bar";

const options = {
  text: sourceCode,
  eslintConfig: {
    parserOptions: {
      ecmaVersion: 7,
    },
    rules: {
      semi: ["error", "never"],
    },
  },
  prettierOptions: {
    bracketSpacing: true,
  },
  fallbackPrettierOptions: {
    singleQuote: false,
  },
};

const formatted = format(options);

// notice no semicolon in the formatted text
formatted; // const { foo } = bar

options

text (String)

The source code to format.

filePath (?String)

The path of the file being formatted can be used to override eslintConfig (eslint will be used to find the relevant config for the file).

eslintConfig (?Object)

The config to use for formatting with ESLint. Can be overridden with filePath.

prettierOptions (?Object)

The options to pass for formatting with prettier. If not provided, prettier-eslint will attempt to create the options based on the eslintConfig (whether that's provided or derived via filePath). You can also provide some of the options and have the remaining options derived via your eslint config. This is useful for options like parser.

NOTE: these options override the eslint config. If you want the fallback options to be used only in the case that the rule cannot be inferred from eslint, see "fallbackPrettierOptions" below.

fallbackPrettierOptions (?Object)

The options to pass for formatting with prettier if prettier-eslint is not able to create the options based on the the eslintConfig (whether that's provided or derived via filePath). These options will only be used in the case that the corresponding eslint rule cannot be found and the prettier option has not been manually defined in prettierOptions. If the fallback is not given, prettier-eslint will just use the default prettier value in this scenario.

logLevel (?Enum: ['trace', 'debug', 'info', 'warn', 'error', 'silent'])

prettier-eslint does quite a bit of logging if you want it to. Pass this to set the number of logs you want to see. Default is process.env.LOG_LEVEL || 'warn'.

eslintPath (?String)

By default, prettier-eslint will try to find the relevant eslint (and prettier) module based on the filePath. If it cannot find one, then it will use the version that prettier-eslint has installed locally. If you'd like to specify a path to the eslint module you would like to have prettier-eslint use, then you can provide the full path to it with the eslintPath option.

prettierPath (?String)

This is basically the same as eslintPath except for the prettier module.

prettierLast (?Boolean)

By default, prettier-eslint will run prettier first, then eslint --fix. This is great if you want to use prettier, but override some of the styles you don't like using eslint --fix.

An alternative approach is to use different tools for different concerns. If you provide prettierLast: true, it will run eslint --fix first, then prettier. This allows you to use eslint to look for bugs and/or bad practices, and use prettier to enforce code style.

throws

prettier-eslint will only propagate parsing errors when either prettier or eslint fails. In addition to propagating the errors, it will also log a specific message indicating what it was doing at the time of the failure.

Note: prettier-eslint will not show any message regarding broken rules in either prettier or eslint.

Technical details

Code ➡️ prettier ➡️ eslint --fix ➡️ Formatted Code

inferring prettierOptions via eslintConfig

The eslintConfig and prettierOptions can each be provided as an argument. If the eslintConfig is not provided, then prettier-eslint will look for them based on the fileName (if no fileName is provided then it uses process.cwd()). Once prettier-eslint has found the eslintConfig, the prettierOptions are inferred from the eslintConfig. If some of the prettierOptions have already been provided, then prettier-eslint will only infer the remaining options. This inference happens in src/utils.js.

An important thing to note about this inference is that it may not support your specific eslint config. So you'll want to check src/utils.js to see how the inference is done for each option (what rule(s) are referenced, etc.) and make a pull request if your configuration is supported.

Defaults if you have all of the relevant ESLint rules disabled (or have ESLint disabled entirely via /* eslint-disable */ then prettier options will fall back to the prettier defaults:

{
  printWidth: 80,
  tabWidth: 2,
  singleQuote: false,
  trailingComma: 'none',
  bracketSpacing: true,
  semi: true,
  useTabs: false,
  // prettier-eslint doesn't currently support
  // inferring these two (Pull Requests welcome):
  parser: 'babylon',
  jsxBracketSameLine: false,
}

Troubleshooting

debugging issues

There is a lot of logging available with prettier-eslint. When debugging, you can use one of the logLevels to get a better idea of what's going on. If you're using prettier-eslint-cli then you can use the --log-level trace, if you're using the Atom plugin, then you can open the developer tools and enter: process.env.LOG_LEVEL = 'trace' in the console, then run the format. You'll see a bunch of logs that should help you determine whether the problem is prettier, eslint --fix, how prettier-eslint infers your prettier options, or any number of other things. You will be asked to do this before filing issues, so please do 😄

NOTE: When you're doing this, it's recommended that you only run this on a single file because there are a LOT of logs :)

eslint-disable-line

While using // eslint-disable-line, sometimes you may get linting errors after the code has been processed by this module. That is because prettier changes this:

// prettier-ignore
if (x) { // eslint-disable-line
}

to this:

if (x) {
  // eslint-disable-line
}

And the eslint --fix wont change it back. You can notice that // eslint-disable-line has moved to a new line. To work around this issue, you can use //eslint-disable-next-line instead of // eslint-disable-line like this:

// eslint-disable-next-line
if (x) {
}

Inspiration

Other Solutions

None that I'm aware of. Feel free to file a PR if you know of any other solutions.

Related

  • prettier-eslint-cli - Command Line Interface
  • prettier-atom - Atom plugin (check the "ESlint integration" checkbox in settings)
  • prettier-vscode - Visual Studio Code plugin (set prettier.eslintIntegration: true in settings)
  • eslint-plugin-prettier - ESLint plugin. While prettier-eslint uses eslint --fix to change the output of prettier, eslint-plugin-prettier keeps the prettier output as-is and integrates it with the regular ESLint workflow.
  • prettier-eslint-webpack-plugin - Prettier ESlint Webpack Plugin

Contributors

Thanks goes to these people (emoji key):


Kent C. Dodds

💻 📖 🚇 ⚠️

Gyandeep Singh

👀

Igor Pnev

🐛

Benjamin Tan

💬 👀

Eric McCormick

💻 📖 ⚠️

Simon Lydell

📖

Tom McKearney

📖 💡

Patrik Åkerstrand

💻

Lochlan Bunn

💻

Daniël Terwiel

🔌 🔧

Robin Malfait

🔧

Michael McDermott

💻

Adam Stankiewicz

💻

Stephen John Sorensen

💻

Brian Di Palma

🐛 💻

Rob Wise

📖 💻

Patryk Peas

🐛 💻 ⚠️

Thijs Koerselman

🐛 💻 ⚠️

Enrique Caballero

🐛 💻

Łukasz Moroz

🐛 ⚠️

Simon Fridlund

💬 🐛 💻 📖 💡 🤔 🚇 🔌 👀 📢 ⚠️ 🔧

Oliver Joseph Ash

🐛 💻

Mark Palfreeman

📖

Alex Taylor

💻 ⚠️

Xianming Zhong

⚠️

Lewis Liu

💻

Hamza Hamidi

💻 🤔 🚧 🔧 👀

Rajiv Ranjan Singh

💻

Igor

🚧

This project follows the all-contributors specification. Contributions of any kind welcome!

LICENSE

MIT

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].