Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Plugin crashes reading file: Cannot read properties of undefined (reading 'buildError') #287

Open
1 task
jspradlin1-chwy opened this issue Mar 14, 2024 · 3 comments

Comments

@jspradlin1-chwy
Copy link

jspradlin1-chwy commented Mar 14, 2024

Your Environment

  • Prettier version: 3.1.0
  • node version 20.11.1
  • package manager: [email protected]
  • IDE: [CLI, IntelliJ]
  • typescript: 4.7.2

Describe the bug

plugin throws an error when parsing a specific file.

That file contains an import for QuoteSelectPet, and that same name is used elsewhere in the file as const QuoteSelectionPet.

To Reproduce

parse this file:

// quote-selection-pets.ts
import { Factory } from 'rosie';

import { InsurancePlanCode } from '@src/helpers/plan-constants.helper';
import {
  CoInsurancePercentage,
  DeductibleDollars,
} from '@src/helpers/quote-product.helper';

// this import is the problem
import {
  Financials,
  QuoteSelectionPet,
  SelectedQuoteProduct,
} from '@src/@types/quotes';

import freeze from '@test/helpers/freeze';
import SelectedQuoteProductsFactory, {
  SelectableProductCodes,
} from '@test/fixtures/selected-quote-products';

const QuoteSelectionPet = new Factory<QuoteSelectionPet>()

class QuoteSelectionPetsFactory {
  build(
    name: 'QuoteSelectionPet',
    attrs?: Partial<QuoteSelectionPet>,
    options?: {
      selectedProductKeys?: SelectableProductCodes[];
      withEmployeeDiscount?: boolean;
      withMultipetDiscount?: boolean;
      withBundleDiscount?: boolean;
      selectedDeductible?: DeductibleDollars;
    },
  ): QuoteSelectionPet {
    return freeze(QuoteSelectionPet.build(attrs, options));
  }
}

export default new QuoteSelectionPetsFactory();

Expected behavior

Prettier (with the help of the plugin) should be able to:
1 - Detect when the imports aren't sorted properly (without --write)
or
2 - sort the imports properly (with --write)

Screenshots, code sample, etc
image

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

  "prettier": {
    "semi": true,
    "trailingComma": "all",
    "singleQuote": true,
    "importOrder": [
      "^@src/(.*)(?<!css)$",
      "^@test/(.*)$",
      "^[./]",
      "^(.*)\\.s?css$"
    ],
    "importOrderSeparation": true,
    "importOrderSortSpecifiers": true,
    "plugins": [
      "prettier-plugin-java",
      "@trivago/prettier-plugin-sort-imports"
    ]
  },

Error log

~/IdeaProjects/hc-pet-insurance  % npx prettier --write src/test/js/fixtures/quote-selection-pets.ts
src/test/js/fixtures/quote-selection-pets.ts
[error] src/test/js/fixtures/quote-selection-pets.ts: TypeError: Cannot read properties of undefined (reading 'buildError')
[error]     at Scope.checkBlockScopedCollisions (/Users/jspradlin1/IdeaProjects/hc-pet-insurance/node_modules/@trivago/prettier-plugin-sort-imports/node_modules/@babel/traverse/lib/scope/index.js:399:22)
[error]     at Scope.registerBinding (/Users/jspradlin1/IdeaProjects/hc-pet-insurance/node_modules/@trivago/prettier-plugin-sort-imports/node_modules/@babel/traverse/lib/scope/index.js:535:16)
[error]     at Scope.registerDeclaration (/Users/jspradlin1/IdeaProjects/hc-pet-insurance/node_modules/@trivago/prettier-plugin-sort-imports/node_modules/@babel/traverse/lib/scope/index.js:487:14)
[error]     at Object.BlockScoped (/Users/jspradlin1/IdeaProjects/hc-pet-insurance/node_modules/@trivago/prettier-plugin-sort-imports/node_modules/@babel/traverse/lib/scope/index.js:254:12)
[error]     at Object.newFn (/Users/jspradlin1/IdeaProjects/hc-pet-insurance/node_modules/@trivago/prettier-plugin-sort-imports/node_modules/@babel/traverse/lib/visitors.js:192:17)
[error]     at NodePath._call (/Users/jspradlin1/IdeaProjects/hc-pet-insurance/node_modules/@trivago/prettier-plugin-sort-imports/node_modules/@babel/traverse/lib/path/context.js:46:20)
[error]     at NodePath.call (/Users/jspradlin1/IdeaProjects/hc-pet-insurance/node_modules/@trivago/prettier-plugin-sort-imports/node_modules/@babel/traverse/lib/path/context.js:32:14)
[error]     at NodePath.visit (/Users/jspradlin1/IdeaProjects/hc-pet-insurance/node_modules/@trivago/prettier-plugin-sort-imports/node_modules/@babel/traverse/lib/path/context.js:82:31)
[error]     at TraversalContext.visitQueue (/Users/jspradlin1/IdeaProjects/hc-pet-insurance/node_modules/@trivago/prettier-plugin-sort-imports/node_modules/@babel/traverse/lib/context.js:86:16)
[error]     at TraversalContext.visitMultiple (/Users/jspradlin1/IdeaProjects/hc-pet-insurance/node_modules/@trivago/prettier-plugin-sort-imports/node_modules/@babel/traverse/lib/context.js:61:17)

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

  • I'm willing to fix this bug 🥇
@AaronTrazona
Copy link

I encounter this issue too.

@mikitabut
Copy link

Same

@MohammadKurjieh
Copy link

I get this error when I have a variable and an imported type with the same type. Renaming one of them resolves the issue temporarily.

Example:

import type { User } from './userModal';
// ...
type UserModalType = Omit<User, 'id'>;
// ...
const User: UserModalType = {
 // ...
}

Temporary Fix:

import type { User as UserModal } from './userModal';
// ...
type UserModalType = Omit<UserModal, 'id'>;
// ...
const User: UserModalType = {
 // ...
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants