christopherthielen / typedoc-plugin-external-module-name

Specify the Typedoc Module of a file using @module annotation

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

TypeError: Cannot read property 'filter' of undefined

raineorshine opened this issue · comments

There was a recent change to my codebase that is causing typedoc-plugin-external-module-name to crash.

I will investigate more closely to determine why parent.children is undefined in onBeginResolve.

> typedoc --options typedoc.json

Loaded plugin typedoc-plugin-external-module-name

Using TypeScript 4.3.2 from /Users/raine/projects/em/node_modules/typescript/lib
/Users/raine/projects/em/node_modules/typedoc-plugin-external-module-name/typedoc-plugin-external-module-name.js:187
                let mergeTarget = parent.children.filter((ref) => ref.kind === renaming.kind && ref.name === nameParts[nameParts.length - 1])[0];
                                                  ^

TypeError: Cannot read property 'filter' of undefined
    at /Users/raine/projects/em/node_modules/typedoc-plugin-external-module-name/typedoc-plugin-external-module-name.js:187:51
    at Array.forEach (<anonymous>)
    at ExternalModuleNamePlugin.onBeginResolve (/Users/raine/projects/em/node_modules/typedoc-plugin-external-module-name/typedoc-plugin-external-module-name.js:170:32)
    at triggerEvents (/Users/raine/projects/em/node_modules/typedoc/dist/lib/utils/events.js:128:43)
    at triggerApi (/Users/raine/projects/em/node_modules/typedoc/dist/lib/utils/events.js:110:13)
    at eventsApi (/Users/raine/projects/em/node_modules/typedoc/dist/lib/utils/events.js:21:18)
    at Converter.trigger (/Users/raine/projects/em/node_modules/typedoc/dist/lib/utils/events.js:264:13)
    at Converter.resolve (/Users/raine/projects/em/node_modules/typedoc/dist/lib/converter/converter.js:164:14)
    at Converter.convert (/Users/raine/projects/em/node_modules/typedoc/dist/lib/converter/converter.js:91:30)
    at CliApplication.convert (/Users/raine/projects/em/node_modules/typedoc/dist/lib/application.js:79:39)

I was able to fix the problem in my mapping function, but it may still be a good idea to have a guard that prevents the plugin from crashing when parent.children is undefined.

The issues is indirectly caused by adding parallel .native.ts files to the project:

  • foo.ts
  • foo.native.ts

I am not sure why exactly that plus my mapping function is leading to parent.children being undefined.

Here is my mapping function:

module.exports = (explicit, implicit, path) => {
  // make index.js files root modules (e.g. action-creators.index.js => action-creators)
  if (path.includes("index.js") || path.includes("index.ts")) return implicit

  // check for files which are placed in subdirectories of src
  if (implicit !== ".") {
    const matches = path.match(RegExp(`${implicit}/.*`))
    if (!matches) return
    const endIndex = matches[0].lastIndexOf(".")
    // return directoryName.fileName (e.g. "action-creators.alert")
    return matches[0].slice(0,endIndex).replace(/\//g, ".")
  }
}

I was able to simply ignore native modules as a quick fix:

if (path.includes('.native.')) return