IanVS / prettier-plugin-sort-imports

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

prettier-ignore should not move the import line past code #50

Closed KholdStare closed 1 year ago

KholdStare commented 1 year ago

Your Environment

Describe the bug

Imports are moved around code even with // prettier-ignore. The order relative to other imports is preserved, but code gets shoved below all the imports (or imports get shoved above code, depending on how you look at it). This can pop up when mocking some functionality that another module depends on. For example https://www.npmjs.com/package/firestore-jest-mock requires running mockGoogleCloudFirestore before any import of @google-cloud/firestore. See the @google-cloud/firestore compatibility section on the package page

To Reproduce

someCode();

// prettier-ignore
import * as fs from "node:fs";

Expected behavior

Any import line with // prettier-ignore should not budge, even relative to code.

Screenshots, code sample, etc

As an example from our codebase:

mockGoogleCloudFirestore({
  database: {
    markets: [{ id: exampleBetMarket.marketAddress, ...exampleBetMarket }],
  },
});

// prettier-ignore
import { FirebaseUpdater, DocumentDBData, DocumentDB } from "./firebaseupdater";

becomes

// prettier-ignore
import { FirebaseUpdater, DocumentDBData, DocumentDB } from "./firebaseupdater";

mockGoogleCloudFirestore({
  database: {
    markets: [{ id: exampleBetMarket.marketAddress, ...exampleBetMarket }],
  },
});

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

IanVS commented 1 year ago

@blutorange i think you may have looked into this at one time. If so, do you recall what the challenge was?

villanuevadani commented 1 year ago

Any progress on this topic? @IanVS @blutorange @KholdStare

IanVS commented 1 year ago

I don't see any mention in npmjs.com/package/firestore-jest-mock about putting code before imports. Technically, imports always run before any of the code in the module, so even if you put code above the imports, it will still run afterwards.

I think the only way to achieve what you want is to use a dynamic import() statement so that you can run some code beforehand.