IanVS / prettier-plugin-sort-imports

An opinionated but flexible prettier plugin to sort import statements
Apache License 2.0
984 stars 25 forks source link

Empty line between some groups are not working #145

Closed ivanafanasyeu closed 8 months ago

ivanafanasyeu commented 8 months ago

Your Environment

Describe the bug

//.prettier.rc
//...
   "importOrder": ["<BUILTIN_MODULES>", "<THIRD_PARTY_MODULES>", "^@", "^[../]", "", "^[./]", "", "<TYPES>"],
  //...

Result

'use client';

import { translateSegment } from '../utils';
import { useLocale } from './useLocale';

To Reproduce

  1. Just install the package in next.js project and add config from the bug description.

Expected behavior Separate line between "../" and "./"

Screenshots, code sample, etc

Screenshot 2024-02-06 at 10 38 54 Screenshot 2024-02-06 at 10 39 09

Configuration File (cat .prettierrc, prettier.config.js, .prettier.js)

{
    "$schema": "https://json.schemastore.org/prettierrc",
    "arrowParens": "always",
    "semi": true,
    "quoteProps": "consistent",
    "bracketSameLine": true,
    "endOfLine": "lf",
    "singleAttributePerLine": true,
    "printWidth": 120,
    "tabWidth": 4,
    "singleQuote": true,
    "useTabs": false,
    "plugins": ["@ianvs/prettier-plugin-sort-imports"],
    "importOrder": ["<BUILTIN_MODULES>", "<THIRD_PARTY_MODULES>", "^@", "^[../]", "", "^[./]", "", "<TYPES>"],
    "importOrderParserPlugins": ["typescript", "jsx", "decorators-legacy"]
}

Error log

Contribute to @ianvs/prettier-plugin-sort-imports

IanVS commented 8 months ago

Hi, thanks for the issue. I think the problem is that your regex is not what you expect.

The regex [../] will match the character . or / (see https://regex101.com/r/Hh40PX/2)

What you want is ^\.\.\/, which matches the pattern ../ exactly, and only at the start. (https://regex101.com/r/Hh40PX/3).

Give that a shot and let me know if you're still having problems.

ivanafanasyeu commented 8 months ago
Screenshot 2024-02-06 at 16 18 46

@IanVS you're right, but maybe you know is it addable to package.json?

IanVS commented 8 months ago

You need to also remove the [] characters, as I showed in the example in my link.

IanVS commented 8 months ago

And if json is not happy, you may need to convert it to a javascript file. https://prettier.io/docs/en/configuration

ivanafanasyeu commented 8 months ago

Yeah, with .mjs it's working