IanVS / prettier-plugin-sort-imports

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

Pragma & Comment handling #166

Closed therealgilles closed 3 months ago

therealgilles commented 3 months ago

Your Environment

Describe the bug Here is the code:

/** @jsxImportSource @emotion/react */
'use client'
/**
 * Filename.tsx
 *
 */

import { ... } from '...'

It is transformed by this plugin as follows:

/** @jsxImportSource @emotion/react */
'use client'

/**
 * Filename.tsx
 *
 */
import { ... } from '...'

and that seems unexpected.

To Reproduce See above.

Expected behavior The comment is left where it was. The plugin honor the empty line specified at the top of importOrder.

Screenshots, code sample, etc See above.

Configuration File (cat .prettierrc, prettier.config.js, .prettier.js) Here is the importOrder:

  importOrder: [
    '',
    '<BUILT_IN_MODULES>',
    '<THIRD_PARTY_MODULES>',
    '',
    '^@(/.*)$',
    '^(?!.*[.]css$)[./].*$',
    '.css$',
  ],

Not sure exactly how to fix this.

fbartho commented 3 months ago

If you don't want a gap after 'use client'; -- you can remove your blank group from the front of your importOrder array:

 importOrder: [
-    '',
    '<BUILT_IN_MODULES>',
    '<THIRD_PARTY_MODULES>',
    '',
    '^@(/.*)$',
    '^(?!.*[.]css$)[./].*$',
    '.css$',
  ],

Does that do what you want @therealgilles?

therealgilles commented 3 months ago

The expected the code to stay like this:

/** @jsxImportSource @emotion/react */
'use client'
/**
 * Filename.tsx
 *
 */

import { ... } from '...'

It feels like the plugin is re-organized my comment, which I did not ask it to. But not a big deal.

IanVS commented 3 months ago

My guess is that the 'use client' pragma is breaking our "top of file" comment detection, and the jsdoc block is being treated as a leading comment on the first import. We can try looking into this, but it's not going to be a super-high priority.

therealgilles commented 3 months ago

It's alright, I'll close this issue. Thank you for taking the time to assist me and giving my issue some thoughts. With RSC, pragmas are going to become more common.