IanVS / prettier-plugin-sort-imports

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

Ignored import order config with `@`-packages #122

Closed paolorossig closed 11 months ago

paolorossig commented 11 months ago

Your Environment

Describe the bug

Third party modules that starts with @ (e.g.: @headlessui/react) are placed at the top of the imports, ignoring any importOrder configuration.

To Reproduce

Expected behavior

So the expected ordered output of my imports is:

import { Fragment } from 'react'
import Link from 'next/link'
import { usePathname } from 'next/navigation'
import { Popover, Transition } from '@headlessui/react'
import clsx from 'clsx'

but the actual output looks like this:

import { Popover, Transition } from '@headlessui/react'
import { Fragment } from 'react'
import Link from 'next/link'
import { usePathname } from 'next/navigation'
import clsx from 'clsx'

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

module.exports = {
  semi: false,
  singleQuote: true,
  plugins: [
    '@ianvs/prettier-plugin-sort-imports',
    'prettier-plugin-tailwindcss',
  ],
  importOrder: [
    'react',
    '^next(.*)$',
    '<THIRD_PARTY_MODULES>',
    '',
    '^[.]',
  ],
}

Error log

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

IanVS commented 11 months ago

I think this is happening because the importOrder is a regex, and @headless/react is matching react, and it comes before react because @ comes before r alphabetically. Can you try changing your 'react' to '^react', and see if that makes a difference?

paolorossig commented 11 months ago

Thanks for the fast help @IanVS. It was not clear to me that I needed to put the ^ for exact match. actually this solved the problem.