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

Run prettier based on glob, not languageId #837

Closed
remcohaszing opened this issue May 20, 2019 · 3 comments
Closed

Run prettier based on glob, not languageId #837

remcohaszing opened this issue May 20, 2019 · 3 comments
Labels
locked Please open a new issue and fill out the template instead of commenting.

Comments

@remcohaszing
Copy link

When prettier is run in CI, it looks somewhat like this:

yarn prettier --check '**/*.{html,json,md,scss,yaml,yml}'

However, in VSCode, prettier runs based on the languageId. This means there is an inconsistent behaviour between running prettier in CI and in the editor. (This applies on other plugins, such as ESLint, as well. IMHO this is incorrect behaviour.)

A practical example:

I recently installed the Kubernetes extension. This means prettier VSCode no longer runs on my Helm chart files, as their language id is now helm. In CI, Prettier is being run, because the files are named Foo.yaml.

I suggest to add a glob option. This makes prettier run based on file globs instead of language ids. This will allow VSCode prettier to run on files, even if plugins use different language ids. This would also implicitly fix #757.

@kachkaev
Copy link
Member

@remcohaszing I tried this trick yesterday and it works pretty well so far:

{
  "scripts": {
    "fix:prettier": "prettier --write \"**/*\"",
    "lint:prettier": "prettier --check \"**/*\""
  },
  "husky": {
    "hooks": {
      "pre-commit": "lint-staged"
    }
  },
  "lint-staged": {
    "**/*": [
      "prettier --write",
      "git add"
    ]
  },
}

↑ uses **/* everywhere

Then in .prettierignore I have:

## Please check .eslintignore and .gitignore when changing this file

## file extensions
*.*
LICENSE
!*.js
!*.jsx
!*.json
!*.md
!*.mdx
!*.ts
!*.tsx
!*.yml

## copied from .gitignore
## - npm / yarn
*-debug.log
node_modules
package-lock.json
yarn-error.log

## - temporary local files
build
lib


## shared between eslint and prettier
foo/bar

Having extensions (globs) whitelisted in .prettierignore instead of package.json helps the editor and other tools like lint-staged skip the same files. There is only one source of truth for what's handled by the tool and what's not, which makes things much easier to manage. Feel free to have a look at a project where I tried the above; it also applies the same trick for configuring ESLint: https://gitlab.com/kachkaev/website-graphql-server/

@ntotten
Copy link
Member

ntotten commented Aug 9, 2019

This is how the VS Code API works. All formatters are registered based on language ID. One solution is to register the file extensions you are working with as a language that prettier and vscode know about.

    "files.associations": {
        "*.myphp": "php"
    }

@ntotten ntotten closed this as completed Aug 9, 2019
@github-actions
Copy link

This issue has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.

@github-actions github-actions bot added the locked Please open a new issue and fill out the template instead of commenting. label Apr 12, 2020
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Apr 12, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
locked Please open a new issue and fill out the template instead of commenting.
Projects
None yet
Development

No branches or pull requests

3 participants