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

Add support for prettierIgnore key in package.json #12672

Closed
wants to merge 9 commits into from
Closed

Add support for prettierIgnore key in package.json #12672

wants to merge 9 commits into from

Conversation

mataha
Copy link

@mataha mataha commented Apr 20, 2022

Description

Most of what I've wanted to write about had already been discussed in #3460; to keep it brief:

package.json is used more and more to store configuration for various development tools (including their gitignore-like files, if any) in lieu of dotfiles. Prettier provides a way to do that for its options, but not for the list of files to ignore itself.

This PR tries to amend that. Implementation is similar to ESLint - via prettierIgnore key.

Checklist

  • I’ve added tests to confirm my change works.
  • (If changing the API or CLI) I’ve documented the changes I’ve made (in the docs/ directory).
  • (If the change is user-facing) I’ve added my changes to changelog_unreleased/*/XXXX.md file following changelog_unreleased/TEMPLATE.md.
  • I’ve read the contributing guidelines.

Try the playground for this PR

@mataha mataha marked this pull request as ready for review April 21, 2022 02:37
@mataha
Copy link
Author

mataha commented Jun 11, 2022

Anyone?

@trim21
Copy link

trim21 commented Jun 14, 2022

any progress 👀?

@@ -227,8 +227,13 @@ function format(context, input, opt) {
}

async function createIgnorerFromContextOrDie(context) {
const filepath = context.argv.filepath
Copy link
Member

@fisker fisker Jul 18, 2022

Choose a reason for hiding this comment

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

Where is this filepath come from? There is no filepath flag in CLI at all.

Copy link
Author

Choose a reason for hiding this comment

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

There is - as --stdin-filepath - and it's used here as well.

Copy link
Member

Choose a reason for hiding this comment

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

filepath only exists when format stdin, when formatting files, it doesn't exist.

Copy link
Member

Choose a reason for hiding this comment

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

See formatFiles function, it calls createIgnorerFromContextOrDie on first line. I don't think it will work.

@hendrikheil
Copy link

I'd love to see this land. It would allow us to have pretterignore settings shared across projects. As we can already share settings, but not ignored files (https://prettier.io/docs/en/configuration.html#sharing-configurations)

Is there anything we could contribute to accelerate this?

@kevinwolfcr
Copy link

I would suggest adding this as an option within the configuration file (i.e. .prettierrc.json). That way, it can be included in shareable configs.

@nickmccurdy
Copy link
Member

nickmccurdy commented Sep 2, 2022

I don't think sharing ignored files would be a good practice, as ignored files tend to vary more based on a project's tooling usage than its style guide, and we wouldn't want to couple something unrelated to style to a config. For reference, ESLint shareable configs also don't allow sharing ignores. If you do have several projects with the same tooling, you can simply copy the .prettierignore (or with this, the package.json).

@kevinwolfcr
Copy link

For reference, ESLint shareable configs also don't allow sharing ignores.

Yes, they do.

@mataha
Copy link
Author

mataha commented Sep 7, 2022

I would suggest adding this as an option within the configuration file (i.e. .prettierrc.json). That way, it can be included in shareable configs.

That's a different feature for a different PR.

const ignoreContent = !ignorePath
? null
: getFileContentOrNull.sync(path.resolve(ignorePath));
createIgnorer.sync = function (filePath, ignorePath, withNodeModules) {
Copy link
Member

Choose a reason for hiding this comment

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

Let's change base to next branch, so we don't need support sync version.

Copy link
Author

Choose a reason for hiding this comment

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

Done.

@mataha mataha changed the base branch from main to next September 9, 2022 10:32
@ivodolenc
Copy link

It might not be a bad idea to implement this feature directly in the prettier.config file.

So when we want to share the settings we have all the options inside one file. This is exactly what eslint have.

For example, I can specify in package.json custom path for eslint.config.js and prettier.config.js and clean up the root as much as possible (no need for additional ignore files and custom ignore options inside package.json):

{
  "name": "package.json",
  "type": "module",

  "prettier": "./.config/prettier.config.cjs",
  "eslintConfig": {
    "extends": "./.config/eslint.config.cjs"
  }
}

and in one file I have all settings, including ignorePatterns like this:

// .config/eslint.config.js

module.exports = {
  root: true,

  extends: [],

  plugins: [],

  ignorePatterns: [
    '.DS_Store',
    'node_modules'
    // ...
  ]
}

This would be a huge win to put ignore directly inside prettier config:

// .config/prettier.config.js

module.exports = {
  singleQuote: true,
  trailingComma: 'none',
  
  plugins: [],

  ignorePatterns: [
    '.DS_Store',
    'node_modules'
    // ...
  ]
}

What do you think?

@kachkaev
Copy link
Member

kachkaev commented Nov 2, 2022

@mataha would you be interested to continue this PR? @ivodolenc's suggestion to use ignorePatterns makes sense – it' be great to see more alignment between ESLint and Prettier!

📖 Docs on ingorePatterns in ESLint config

@aensley
Copy link

aensley commented Nov 3, 2022

I second @ivodolenc's suggestion: #12672 (comment)

It would be really great to be able to specify ignorePatterns in the prettier config.

@Standard8
Copy link

Note, as this appears to have stalled out, I've started working on adding the options to the configuration. As a first step, I've added command line arguments to echo ESLint - as they were quite easy to do, and I've just posted that in #14223

@fisker fisker changed the base branch from next to main April 23, 2023 01:08
@wallpants
Copy link

Is there anything I can do to help get this over the finish line? In a world where we already have a ton of config files in the root of our projects, I'm struggling to understand why we can't specify patterns to ignore in our prettier config file and we're forced to create a new file just for that.

@Standard8
Copy link

Is there anything I can do to help get this over the finish line? In a world where we already have a ton of config files in the root of our projects, I'm struggling to understand why we can't specify patterns to ignore in our prettier config file and we're forced to create a new file just for that.

As discussed elsewhere, I think it would be much better to add an option in the .prettierrc.js file rather than package.json, this would also align it better with ESLint (which I believe was one of the aims).

I tried doing this previously but hit some issues, and despite asking never got any feedback from the developers, even when pinging the devs directly, so I gave up.

Feel free to take my work and try moving it forward though, maybe you'll have more success.

This is a rather dumb implementation (for now).

Implements #3460

Signed-off-by: mataha <mataha@users.noreply.github.com>
With a sample `package.json`.

Signed-off-by: mataha <mataha@users.noreply.github.com>
Not sure if more are necessary?

Signed-off-by: mataha <mataha@users.noreply.github.com>
Signed-off-by: mataha <mataha@users.noreply.github.com>
Signed-off-by: mataha <mataha@users.noreply.github.com>
Signed-off-by: mataha <mataha@users.noreply.github.com>
Signed-off-by: mataha <mataha@users.noreply.github.com>
Signed-off-by: mataha <mataha@users.noreply.github.com>
@mataha
Copy link
Author

mataha commented Oct 2, 2023

I'm in a position in which I can commit some time to this again. I'd like to clarify some things beforehand though.

@mataha would you be interested to continue this PR? @ivodolenc's suggestion to use ignorePatterns makes sense – it' be great to see more alignment between ESLint and Prettier!

📖 Docs on ingorePatterns in ESLint config

Again, I believe that's a different feature for a different PR. The core tenet of this is that one can share prettier configuration between different projects or even monorepo packages, yet be able to selectively choose which files should be formatted at the project level, e.g. plumbing for package managers, test specifications, generated files... As someone here said, ignorable files tend to be influenced by a project's tooling more than anything - two entities sharing such config might not necessarily use the same package manager, among other things. ignorePatterns does not achieve this - it's applied on a configuration level, thus applicable to every package using said config.

That said, the main pain point of this PR continues to be how Prettier should behave upon encountering a package.json:

  • stop scanning
  • continue scanning until filesystem root, merge all sections
  • continue scanning if section is malformed or missing

I'll have to rewrite this as several things have changed since then (e.g. ignore facility has moved to src/utils).

@mataha
Copy link
Author

mataha commented Oct 3, 2023

Can't run tests at all due to jestjs/jest#7914.

@mataha mataha closed this Oct 3, 2023
@mataha mataha deleted the feature/prettierignore-package-json branch October 3, 2023 17:11
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.

None yet