trivago/prettier-plugin-sort-imports

Exception thrown when imported type has same name as local value

OliverJAsh opened this issue · 5 comments

Your Environment

  • Prettier version: 2.8.4
  • node version 16.3.0:
  • package manager: yarn@2
  • IDE: VS Code

Describe the bug

The following code sample produces 0 type errors but causes Prettier to throw:

types.ts:

export type Demo = () => null;

main.ts:

import { Demo } from './types';

const Demo: Demo = () => null

export {}

To Reproduce

See above.

Expected behavior

No error, seeing as this works fine when disabling the Prettier plugin.

Screenshots, code sample, etc

image

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

None.

Error log

["ERROR" - 12:14:21] Identifier 'Demo' has already been declared. (3:6)
  1 | import { Demo } from './types';
  2 |
  3 | const Demo: Demo = () => null;
  4 |
  5 | export {}
SyntaxError: Identifier 'Demo' has already been declared. (3:6)
  1 | import { Demo } from './types';
  2 |
  3 | const Demo: Demo = () => null;
  4 |
  5 | export {}
    at instantiate (/Users/oliverash/Development/temp/prettier-sort-imports-plugin-bug/node_modules/@trivago/prettier-plugin-sort-imports/node_modules/@babel/parser/src/parse-error/credentials.js:61:22)
    at toParseError (/Users/oliverash/Development/temp/prettier-sort-imports-plugin-bug/node_modules/@trivago/prettier-plugin-sort-imports/node_modules/@babel/parser/src/parse-error.js:58:12)
    at Parser.raise (/Users/oliverash/Development/temp/prettier-sort-imports-plugin-bug/node_modules/@trivago/prettier-plugin-sort-imports/node_modules/@babel/parser/src/tokenizer/index.js:1736:19)
    at TypeScriptScopeHandler.checkRedeclarationInScope (/Users/oliverash/Development/temp/prettier-sort-imports-plugin-bug/node_modules/@trivago/prettier-plugin-sort-imports/node_modules/@babel/parser/src/util/scope.js:153:19)

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

  • I'm willing to fix this bug 🥇

This issue only happens when @trivago/prettier-plugin-sort-imports is enabled.

You are defining Demo as a type and after that re-defining as const.

Can you try this :

types.ts:

export type Demo = () => null;

main.ts:

import { Demo as DemoType } from './types';

const Demo: DemoType = () => null

export {}

Feel free to re-open if the issue exists.

You are defining Demo as a type and after that re-defining as const.

I understand that, but this is in fact valid TypeScript. If we use tsc or prettier without this plugin, there is no error.

byara commented

Dear @OliverJAsh the error you are referring to is thrown from @babel/parser which we use to get the AST and all the imports and relevant information regarding the file. Basically the code has an issue according to @babel/parser (you can see it here). The plugin has no opinion about your code.

I don't think this would be an issue if the plugin parsed the AST using TypeScript rather than Babel.

Actually, shouldn't the plugin use the same parser specified via Prettier's --parser option?