Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: use "prettier-tslint" and "prettier-eslint" when they exist #54

Closed
wants to merge 6 commits into from

Conversation

aleclarson
Copy link

When the scm root has ./node_modules/prettier-tslint and/or ./node_modules/prettier-eslint, we use them on any applicable modules.

We prefer prettier-eslint for .js, .jsx, and .mjs modules. If prettier-eslint does not exist, we try prettier-tslint and then prettier alone.

We prefer prettier-tslint for .ts and .tsx. If prettier-tslint does not exist, we try prettier-eslint and then prettier alone.

All other file types use prettier only.

@aleclarson
Copy link
Author

PS: You can add me as a maintainer here, too. ;)

function loadFormat(rootDirectory, formatName, exportName) {
let entry;
try {
entry = require.resolve(join(rootDirectory, 'node_modules', formatName));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This won't work with yarn PnP.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are you saying it breaks pretty-quick for Yarn PnP installs, or just that prettier-eslint and prettier-tslint won't be found?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Have you actually tested this with Yarn PnP?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The latter. Projects using PnP don't have node_modules

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Okay, that's fine. We can patch PnP support for this feature in a later patch.

@azz
Copy link
Member

azz commented Dec 16, 2018

Thinking I'd favour a more explicit API, like pretty-quick --tslint, pretty-quick --eslint. It will save a small amount of IO for users who don't use those tools, and users who have e.g. prettier-eslint still in node_modules (maybe unused) will be less confused when the behaviour changes after upgrading this package.

We can also make it work with PnP that way.

@aleclarson
Copy link
Author

We can also make it work with PnP that way.

Why would replacing implicit loading with an explicit flag make PnP work? We already wrap the require.resolve call in a try..catch block, so PnP shouldn't choke on it.

@azz
Copy link
Member

azz commented Dec 17, 2018

We can just do if (opts.eslint) require('prettier-eslint'), shouldn't need to hardcode node_modules.

@aleclarson
Copy link
Author

aleclarson commented Dec 17, 2018

Can you explain why someone would want pretty-quick to not use prettier-eslint and/or prettier-tslint when they are installed?

We can remove the hard-coded node_modules, but I don't get why adding new flags is ideal.

@aleclarson
Copy link
Author

aleclarson commented Dec 17, 2018

I've improved the implicit behavior by ensuring the resolved prettier-tslint and/or prettier-eslint paths are never globally-installed versions. Basically, they must be in your package.json to signal to pretty-quick that you're using them.

src/formatFiles.js Outdated Show resolved Hide resolved
);

const format =
(/\.(mjs|jsx?)$/.test(file) && (eslintFormat || tslintFormat)) ||
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This won't work for files with shebangs (which prettier supports). Maybe using prettier.getFileInfo would be better?

Copy link
Author

@aleclarson aleclarson Dec 17, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You're talking about executable files with no file extension that contain Javascript and begin with a shebang, yes?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Absolutely.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's support shebangs in a later patch. No biggie if they don't get linted for now.

When the scm root has "./node_modules/prettier-tslint" and/or
"./node_modules/prettier-eslint", we use them on applicable modules.
So we can use the `getFileInfo` export.
@aleclarson
Copy link
Author

aleclarson commented Dec 23, 2018

I've added shebang support (using prettier.getFileInfo), which requires Prettier v1.13.0 or higher.
I've updated the peer/dev dependencies on prettier to match.

Like I said, we can add Yarn PnP support in the future. I think we would take this approach. Specifically, importing pnpapi to determine if a repository is using PnP, and then calling pnp.resolveRequest.

LGTM! :)

@aleclarson
Copy link
Author

Hmm.. Looks like Travis is using a version of Prettier that doesn't include getFileInfo.

Even though I updated yarn.lock. 🤔

@azz Could you take a look?

Copy link
Member

@azz azz left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry I haven't had a chance to look at the build issue. Christmas is a busy time! Left a few comments

package.json Outdated Show resolved Hide resolved
format === prettierFormat
? format(input, ((options.filepath = file), options))
: format({
text: input,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you add a test for this code path?

Copy link
Author

@aleclarson aleclarson Dec 27, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've tested locally (and you can too) with:

git clone -b feat1-test https://github.com/aleclarson/pretty-quick
cd pretty-quick
cat README.md

I'll try to get around to writing a test when I can.


const parser = getFileInfo.sync(file).inferredParser;
const format =
(parser === 'babylon' && (eslintFormat || tslintFormat)) ||
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Or flow

Copy link
Author

@aleclarson aleclarson Dec 27, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does eslint support flow? edit: Nope!

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

babylon supports flow language too. You can write JavaScript and still use the flow parser.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Okay, but passing Flow-typed JavaScript into eslint does not work.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are you saying there's a chance that file will contain Flow-typed JavaScript when parser === 'babylon'?

@azz
Copy link
Member

azz commented Dec 27, 2018

I deleted the build caches and restarted the build. Hopefully that helps.

- rename "isSupportedExtension" function to "isFileSupported"
- use `prettier.getFileInfo` to detect shebangs
- upgrade prettier to 1.15.0+ (where shebang support was added)
@aleclarson
Copy link
Author

aleclarson commented Dec 27, 2018

Once tests are added, this is ready to merge.

Things to fix later:

  • add Yarn PnP support
  • get prettier-eslint to work with shebang modules

@MichaelDeBoey
Copy link
Contributor

Any news on this one? 🤔

Ping @aleclarson @azz

@JounQin
Copy link
Member

JounQin commented Jan 16, 2024

No thanks, but you can looking forward to https://github.com/prettier-eslint/prettier-plugin.

@JounQin JounQin closed this Jan 16, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants