question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Allow easier integration with tools like eslint

See original GitHub issue

This is coming from https://github.com/jlongster/prettier/issues/40#issuecomment-272360783

Right now we take an input string and return an output string. The problem is that if anyone wants to integrate with other tools that deal with ASTs, they will have to parse the string again. For large files this performance hit is not really acceptable.

A very common case will probably be eslint. I’d like to make sure we provide the right APIs to make it easy to integrate with tools like that.

Regarding eslint: there are 2 things it does. It has semantic rules that can be checked given an AST (the actual formatting doesn’t matter). It also has formatting rules, which can fixed automatically with --fix. These are two very different use cases and I’m mostly thinking of the former. It seems like the only reason people would want to pass the output of prettier into eslint is to drop semicolons. If people want to use eslint’s formatting options, it’ll have to double-parse because we need to print first and then it needs to parse the output. Then people could use --fix to tweak the output (this seems like a pretty convoluted/slow process though).

For all the semantic checking, I should be able to pass my AST into eslint first, and then print it afterwards, which is the other way around. @hzoo can eslint fix any of the semantic rules? If so, how does it do it? Does it edit the AST in place, create a new AST, etc?

I think all we need to do is introduce a new API method: formatAST. This would take an AST instead of a string. This would allow people to use other parsers (if available), do any AST wrangling beforehand, and just give it to us. It’s much more performant than generating the string and then us parsing it again.

This allows integration with any other tooling that deals with ASTs, and it’s up the editor plugin to do that integration.

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Reactions:9
  • Comments:5 (3 by maintainers)

github_iconTop GitHub Comments

3reactions
robwisecommented, Jan 20, 2017

From https://github.com/jlongster/prettier/issues/296#issuecomment-273601563 from @jlongster:

I don’t recommend using this [Prettier] with eslint’s formatting rules. You should get rid of the formatting rules and keep the other rules that enforce semantic behaviors.

If a developer configures eslint’s formatting rules to mirror what Prettier is outputting anyway, then what is the argument against leaving those rules enabled? That way, CI and editors with linter integration can enforce Prettier’s style (in case a developer isn’t using format-on-save and forgot to format their code or something).

What’s more, in those cases where their eslint rules conflict with Prettier’s style in a way that isn’t configurable, if the rules in question are eslint-auto-fixable, they can still benefit from Prettier by running Prettier first and eslint second.

Which brings me to my point: I strongly believe that Prettier should be configurable enough that it won’t force people to go against https://github.com/airbnb/javascript. It’s got almost 46,000 stars and has gone a long way towards promoting a consistent DX across projects.

If this would require a massive amount of work, maybe it would be a gray area given your vision of an opinionated, convention-over-configuration type tool. However, if we limit the changes to those Prettier rules that are incompatible with that project and that aren’t Prettier-configurable and aren’t eslint-auto-fixable, there aren’t that many:

  • Airbnb/javascript wants parentheses around ternaries but Prettier removes them
  • ~Airbnb/javascript has spaces in curly braces but no spaces in straight brackets but Prettier doesn’t differentiate between the two; it’s either both have spaces or neither have spaces~ (eslint auto-fixes this)

Although I admit I probably have not yet discovered all of the conflicts yet, so far there aren’t a huge amount of changes required to make Prettier 100% compatible the eslint airbnb/javscript preset assuming eslint-auto-fix is on and run afterwards.

Here is an example of using Prettier first and then eslint afterwards where I am missing some trailing commas and also Prettier is putting spaces in the square brackets, and then eslint autofixes them back so it’s happy in the end:

0reactions
vjeuxcommented, Jan 27, 2017

We’re good on this front, people have created the plugins and we are featuring them on the README 😃

Read more comments on GitHub >

github_iconTop Results From Across the Web

Modern, faster alternatives to ESLint - LogRocket Blog
In this article, we will look at specific open source libraries that either serve as alternatives to ESLint or can be integrated with...
Read more >
Integrating and Enforcing Prettier & ESLint
There are many tools for ensuring consistency and improving the quality of JavaScript code. At first were using tools like JSLint, JSHint and ......
Read more >
Getting Started with ESLint - Pluggable JavaScript Linter
A pluggable and configurable linter tool for identifying and reporting on patterns in JavaScript. Maintain your code quality with ease.
Read more >
ESLint: The Essential Facts About Essential Front End Tools
Even a simple To–Do–List app can easily require a bunch of tools—ESLint, Babel, Webpack, to name a few—and packages just to get started....
Read more >
How To Lint and Format Code with ESLint in Visual Studio Code
ESLint is a linter that you can integrate into your Visual Studio Code setup in order to ensure code integrity. In this tutorial,...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found