ggascoigne / prettier-plugin-import-sort

Prettier plugin to pass javascript and typescript through import-sort
MIT License
110 stars 11 forks source link

Errors thrown when running against unknown file types #24

Closed atdrago closed 3 years ago

atdrago commented 3 years ago

Hello! First off, I just want to say thank you for this plugin! We've found this to be the best solution for sorting imports at my company and are really thankful for it! We've added it to several projects now.

One thing I'm noticing is that after adding the plugin, running Prettier against unsupported file types throws an error: https://github.com/ggascoigne/prettier-plugin-import-sort/blob/master/src/index.js#L25

Our situation is that we want to use Prettier with Husky and lint-staged. This means Prettier is run against any file that is staged, pre-commit. If any of those files doesn't have a configuration in the importSort config, prettier-plugin-import-sort will throw an error, and we aren't able to commit.

I'm wondering if there's a way to configure it not to behave this way, or if you'd consider a PR that either removes these errors, or adds a "silent" mode (or whatever verbiage you prefer) that prevents them.

ggascoigne commented 3 years ago

We use it with husky and lint-staged too, but I imagine that we must have different paths configured somewhere along the line. FYI we're using:

  "lint-staged": {
    "*.{js,json,md,yml,yaml}": [
      "prettier --write"
    ],
    "{src,cypress}/**/*.{json,css,md,yml,yaml}": [
      "prettier --write"
    ],
    "{src,cypress,scripts}/**/*.{js,jsx,ts,tsx}": [
      "eslint --max-warnings 0 --fix",
      "prettier --write"
    ]
  },
  "prettier": {
    "semi": false,
    "singleQuote": true
  },
  "importSort": {
    ".js, .jsx, .ts, .tsx": {
      "style": "module"
    }
  },

Off the top of my head, I'm not sure why running prettier on say, package.json, isn't giving me the same error too - but it doesn't. My guess is that since importSort only extends the typescript and bable parsers, it generally just doesn't go through that path.

That said, there's clearly a way that this is breaking and I'd be more than happy to consider a PR if you feel like writing one.

atdrago commented 3 years ago

Thanks for the quick response! That is very strange! I would expect you to see the same errors.

Sorry, I should've posted this in the beginning. Here's my current setup:

"importSort": {
  ".js, .jsx, .ts, .tsx": {
    "style": "atdrago",
    "parser": "typescript"
  }
},
"lint-staged": {
  "*.{md,js,json,jsx,ts,tsx,yml}": [
    "prettier --write"
  ]
},

With this, I get errors for any files with md, json, or yml extensions. I've also tried just using "style": "module", with and without a "parser", but no luck.

Here are some version numbers: Prettier: 2.3.1 Husky: 6.0.0 lint-staged: 11.0.0

I'm not able to reproduce this in a project that is using: Prettier: 2.2.1 Husky: 3.1.0 lint-staged: 9.5.0

At this point, I'm thinking it is probably either something somewhere in this project's config, or something that has changed in one of those versions of the 3 plugins. I'll update this thread after I've investigated more!

atdrago commented 3 years ago

As I assumed, it was a project configuration issue. Somebody had modified the .prettierrc to include "parser": "typescript". Once removed, I no longer see this issue.

More info, and an explicit warning against this, found here: https://prettier.io/docs/en/configuration.html#setting-the-parserdocsenoptionshtmlparser-option

Thanks again for this awesome plugin, and apologies for wasting your time. Closing this now.

ggascoigne commented 3 years ago

no worries, glad that you got it sorted out.