ggascoigne / prettier-plugin-import-sort

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

File path should be forwarded to import-sort #22

Closed fsmaia closed 3 years ago

fsmaia commented 3 years ago

Currently, we are sending dummy.${extension} as the file path to import-sort.

This may lead to bugs when an import-sort style relies on the filename for something, like in the following example:

export default (styleApi: IStyleAPI, file: string): IStyleItem[] => {
  const { isInstalledModule } = styleApi;
  console.log(isInstalledModule(file)); // should be the actual file instead of `dummy.${ext}`
};

The file path is available in the prettier preprocess options:

preprocess(text, options) {
  console.log(options.filepath);
}

Type reference took from: https://github.com/DefinitelyTyped/DefinitelyTyped/blob/master/types/prettier/index.d.ts:

export interface ParserOptions<T = any> extends RequiredOptions {
  locStart: (node: T) => number;
  locEnd: (node: T) => number;
  originalText: string;
}

export interface RequiredOptions extends doc.printer.Options {
  //...
  /**
   * Specify the input filepath. This will be used to do parser inference.
   */
  filepath: string;