mockdeep / typewiz

Automatically discover and add missing types in your TypeScript code
https://medium.com/@urish/manual-typing-is-no-fun-introducing-typewiz-58e3e8813f4c
1.1k stars 46 forks source link

Make Typewiz robust to errors from .toString() user code #84

Closed zxti closed 5 years ago

zxti commented 5 years ago

The value.toString() call below can cause Typewiz to crash, since user code can override .toString() with arbitrary broken code. I ran into this trying to use the Ant library, for instance—rc-editor-mention overrides the class method Mention.toString.

function getTypeName(value, nest = 0) {
       ...
        if (value instanceof Function) {
            let argsStr = value.toString().split('=>')[0];
urish commented 5 years ago

Good catch, the issue reproduces with the following test case (added inside type-collector-snippet.spec.ts):

            it('should gracefully deal with toString() method throwing an error', () => {
                function f() { /* noop */ }
                f.toString = () => { throw new Error('Dont call this method'); };

                expect($_$twiz.typeName(f)).toBe(
                    '() => any',
                );
            });

Would you be interested in working on a PR that fixes this?

zxti commented 5 years ago

Thanks for going ahead and fixing this!