Skip to content

Error: Cannot read properties of undefined (reading 'map') #22

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

Open
matthew-dean opened this issue Oct 1, 2024 · 2 comments
Open

Error: Cannot read properties of undefined (reading 'map') #22

matthew-dean opened this issue Oct 1, 2024 · 2 comments

Comments

@matthew-dean
Copy link

It looks like this plugin is using deprecated SWC classes, which may be why it's triggering an error within SWC.

For instance, I'm getting this error:

[vite-plugin-deadfile-pre] Cannot read properties of undefined (reading 'map')
file: {redacted}/src/scripts/lib/intercom/IntercomProxy.ts

  file: src/scripts/lib/intercom/IntercomProxy.ts
  at ImportVisitor.visitTsFnParameters (node_modules/.pnpm/@[email protected]/node_modules/@swc/core/Visitor.js:590:23)

However, the error doesn't seem to be related to the file itself. If I go to the SWC playground, and compile with the same SWC version, no error occurs.

The file that is failing is:

import type { IntercomSettings } from './IntercomSettings'
import { Intercom } from './intercom-js.js'

export interface IIntercomProxy {
  isIntercomLoaded: () => boolean
  isAdminImpersonatingUser: () => boolean
  isKillSwitchEnabled: () => boolean
  shouldCallIntercom: () => boolean
  update: (key: string, value: any) => void
  updateObject: (obj: any) => void
  trackEvent: (eventName: any, eventMetadata: any) => void
  action: (actionName: string) => void
}

// eslint-disable-next-line prefer-const
export let intercomProxy: IntercomProxy

export class IntercomProxy implements IIntercomProxy {
  static readonly Update: string = 'update'
  static readonly TrackEvent: string = 'trackEvent'

  settings: Partial<IntercomSettings>

  constructor(settings?: Record<string, any>) {
    this.settings = settings ?? {}
    // Assume that any instantiation of IntercomProxy with different
    // settings replaces the singleton's settings
    // eslint-disable-next-line @typescript-eslint/no-this-alias
    if (intercomProxy) {
      intercomProxy.settings = this.settings
    }
  }

  // don't do anything unless library loaded fine
  isIntercomLoaded(): boolean {
    return typeof Intercom !== 'undefined'
  }

  isAdminImpersonatingUser(settings?: Partial<IntercomSettings>): boolean {
    const set = settings ?? this.settings
    return set?.isImpersonatedAdmin ?? false
  }

  // just while we get comfortable..
  isKillSwitchEnabled(): boolean {
    return false
  }

  // any reason to not allow sending things to intercom
  shouldCallIntercom(): boolean {
    return this.isIntercomLoaded() && !this.isAdminImpersonatingUser() && !this.isKillSwitchEnabled()
  }

  // update the user attribute value
  update(aName: string, aValue: any): void {
    if (this.shouldCallIntercom()) {
      const updateParams: any = {}
      updateParams[aName] = aValue
      Intercom(IntercomProxy.Update, updateParams)
    }
  }

  updateObject(obj: any): void {
    if (this.shouldCallIntercom()) {
      Intercom(IntercomProxy.Update, obj)
    }
  }

  // we use for page views and other actions/ feature access
  trackEvent(eventName: any, eventMetadata?: any): void {
    if (this.shouldCallIntercom()) {
      Intercom(IntercomProxy.TrackEvent, eventName, eventMetadata)
    }
  }

  // Eg shutdown, boot
  action(actionName: any): void {
    if (this.shouldCallIntercom()) {
      Intercom(actionName)
    }
  }
}

intercomProxy = new IntercomProxy()

export interface IIntercomDataFormatter {
  // TODO: define the object that this returns.
  formatListOfKeysToSendToIntercom: (keysList: any[]) => any
  // TODO: go find some dictionary interface in typescript and pass that.
  formatDictionaryToSendToIntercom: (keyValsList: any) => any
}

export class IntercomDataFormatter implements IIntercomDataFormatter {
  formatListOfKeysToSendToIntercom(keysList: any): any {
    // send role keys to intercom: key is attribute, make "true" value
    const keyObj: any = {}
    if (typeof keysList !== 'undefined' && keysList != null && keysList !== '') {
      for (let i = 0; i < keysList.length; i++) {
        keyObj[keysList[i]] = true
      }
    }
    return keyObj
  }

  formatDictionaryToSendToIntercom: any = function(keyValsList: any) {
    const keyValObj: any = {}
    if (typeof keyValsList === 'undefined' || keyValsList == null || keyValsList === '') {
      return keyValObj
    }

    for (let i = 0; i < keyValsList.length; i++) {
      keyValObj[keyValsList[i].Key] = keyValsList[i].Value
    }

    return keyValObj
  }
}
@stauren
Copy link
Owner

stauren commented Dec 3, 2024

Thanks for the feedback, I'll look into it.

@stauren
Copy link
Owner

stauren commented Dec 5, 2024

I copied this file to an empty project and mocked './IntercomSettings.ts' and './intercom-js.js', the error can not be reproduced after vite build complete.

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

No branches or pull requests

2 participants