prettier-plugin-sort-imports icon indicating copy to clipboard operation
prettier-plugin-sort-imports copied to clipboard

`TypeError` within `checkBlockScopedCollisions`: `this.hub` is undefined

Open willsawyerrrr opened this issue 2 years ago • 4 comments

Your Environment

  • Prettier version: 2.8.8
  • node version: v18.1.0
  • package manager: [email protected]
  • IDE: VSCode/Vim (independent of editor)

Describe the bug

When importing a symbol and declaring a duplicate identifier, checkBlockScopedCollisions is called, correctly identifying the duplicate identifier; however, when attempting to throw this.hub.buildError, this.hub is undefined.

To Reproduce

  1. Create the following file1.js:
export const a = "foo";
  1. Create the following file2.js:
import { a } from "./file1";

const a = "bar";
  1. Execute prettier with file2.js:
npx prettier file2.js

Expected behavior

A SyntaxError should be raised, similarly to the result of executing Prettier with the following file3.js:

const a = "foo";

const a = "bar";

Screenshots, code sample, etc

N/A

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

N/A

Error log

[error] file2.js: TypeError: Cannot read properties of undefined (reading 'buildError')
[error]     at Scope.checkBlockScopedCollisions (./node_modules/@babel/traverse/lib/scope/index.js:473:22)
[error]     at Scope.registerBinding (./node_modules/@babel/traverse/lib/scope/index.js:634:16)
[error]     at Scope.registerDeclaration (./node_modules/@babel/traverse/lib/scope/index.js:575:14)
[error]     at Object.BlockScoped (./node_modules/@babel/traverse/lib/scope/index.js:292:12)
[error]     at Object.newFn (./node_modules/@babel/traverse/lib/visitors.js:218:17)
[error]     at NodePath._call (./node_modules/@babel/traverse/lib/path/context.js:53:20)
[error]     at NodePath.call (./node_modules/@babel/traverse/lib/path/context.js:36:14)
[error]     at NodePath.visit (./node_modules/@babel/traverse/lib/path/context.js:100:31)
[error]     at TraversalContext.visitQueue (./node_modules/@babel/traverse/lib/context.js:103:16)
[error]     at TraversalContext.visitMultiple (./node_modules/@babel/traverse/lib/context.js:72:17)

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

  • [ ] I'm willing to fix this bug 🥇

willsawyerrrr avatar May 04 '23 08:05 willsawyerrrr

Experiencing the same issue.

I think the solution here is bumping the @babel/traverse version. Currently it's hard locked to 7.17.3 but latest is 7.22.5

ian avatar Jun 15 '23 17:06 ian

Nope - that didn't do it.

Tracking upstream issue here: https://github.com/babel/babel/issues/8689

ian avatar Jun 15 '23 17:06 ian

I had the same issue with a conflicting named function in a TS file.

import { InitialData } from './file-a.ts';

// The function identical name in this file caused the conflict
export default function InitialData(): InitialData {}

I think this is the related Babel bug: https://github.com/babel/babel/issues/8617

patric-eberle avatar Oct 09 '23 07:10 patric-eberle

I was importing a type called Contact and had a component also called Contact that caused this issue

juxnpxblo avatar Dec 20 '23 16:12 juxnpxblo