microsoft / dts-gen

dts-gen creates starter TypeScript definition files for any module or library.
MIT License
2.44k stars 101 forks source link

Proxy that override toString() could lead to TypeError from indexOf() #145

Closed Suisse00 closed 2 years ago

Suisse00 commented 5 years ago

I was trying to create the definition for an addon of https://github.com/sequelize/sequelize 5.21.1 which would end up generating the sequelize definition as well.

the exception

Exception has occurred: TypeError TypeError: Cannot read property 'indexOf' of undefined at isNativeFunction (D:\Windows\Suisse\AppData\Roaming\npm\node_modules\dts-gen\bin\lib\index.js:358:25) at getParameterListAndReturnType (D:\Windows\Suisse\AppData\Roaming\npm\node_modules\dts-gen\bin\lib\index.js:302:9) at getResult (D:\Windows\Suisse\AppData\Roaming\npm\node_modules\dts-gen\bin\lib\index.js:135:30) at getTopLevelDeclarations (D:\Windows\Suisse\AppData\Roaming\npm\node_modules\dts-gen\bin\lib\index.js:129:17) at getResult (D:\Windows\Suisse\AppData\Roaming\npm\node_modules\dts-gen\bin\lib\index.js:197:35) at getTopLevelDeclarations (D:\Windows\Suisse\AppData\Roaming\npm\node_modules\dts-gen\bin\lib\index.js:129:17) at getResult (D:\Windows\Suisse\AppData\Roaming\npm\node_modules\dts-gen\bin\lib\index.js:153:17) at getTopLevelDeclarations (D:\Windows\Suisse\AppData\Roaming\npm\node_modules\dts-gen\bin\lib\index.js:129:17) at Object.generateIdentifierDeclarationFile (D:\Windows\Suisse\AppData\Roaming\npm\node_modules\dts-gen\bin\lib\index.js:73:20) at Object. (D:\Windows\Suisse\AppData\Roaming\npm\node_modules\dts-gen\bin\lib\run.js:62:24) at Module._compile (internal/modules/cjs/loader.js:685:14) at Object.Module._extensions..js (internal/modules/cjs/loader.js:699:10) at Module.load (internal/modules/cjs/loader.js:598:32) at tryModuleLoad (internal/modules/cjs/loader.js:537:12) at Function.Module._load (internal/modules/cjs/loader.js:529:3) at Function.Module.runMain (internal/modules/cjs/loader.js:741:12) at startup (internal/bootstrap/node.js:285:19) at bootstrapNodeJSCore (internal/bootstrap/node.js:739:3)

After investigating I found out the issue is when Proxy override the toString() method and return undefined.

Reproduction steps (from sequelize)

npm i -g sequelize use my only changes from 5.21.1 - https://github.com/Suisse00/sequelize/commit/eb1c7b144ea708f6277e860777efeff64c0713f6 dst-gen -m sequelize

Reproduction steps (POC) poc.js class Dummy { static toString(...args) { return undefined; } }

new Proxy(Dummy, { get(target, p) { return target[p]; }})

command

dts-gen --expression-file poc.js

dst-gen --version 0.5.8 sequelize 5.21.1 (with patch)

Suisse00 commented 5 years ago

I did fix my errors by updating dts-gen, I have no idea if it will break something else but that could be a start. Unfortunately, I updated the .js instead of the .ts, and I can't find the equivalent of Object.toString(X) in TS so... good luck!