docascode / type2docfx

A tool to convert json format output from TypeDoc to universal reference model for DocFx to consume.
Apache License 2.0
17 stars 10 forks source link

Improve "Function Types" support #6

Open fvoronin opened 6 years ago

fvoronin commented 6 years ago

type2docfx (v0.9.79) has a problem with function types described with an interface. For example, we have a module:

export interface ObservableGetter<T> {
    (): T;
}
export interface ObservableSetter<T> {
    (v: T): void;
}

export interface ObservableProperty<T> extends ObservableSetter<T>, ObservableGetter<T> {
}

export default class Observable {
    static accessor<T>(name: string): ObservableProperty<T>;
    static accessor(name: string) {
        return function(v?) {
            if (arguments.length) {
                // call internal set method
            } else {
                // call internal get method
            }
        }
    }
}

Run typedoc (v0.11.1) on it with json output and get result api.json.

If we pass that api.json to type2docfx, we get an error:

> type2docfx api.json .\
Class: test.Observable
 - Method: test.Observable.accessor
Interface: test.ObservableGetter
Interface: test.ObservableProperty
...\type2docfx\dist\jsonTraverse.js:220
    method.syntax.parameters = fillParameters(node.signatures[signatureIndex].parameters);
                             ^

TypeError: Cannot set property 'parameters' of undefined
    at extractInformationFromSignature (...\type2docfx\dist\jsonTraverse.js:220:30)
    at traverse (...\type2docfx\dist\jsonTraverse.js:199:17)
    at ...\type2docfx\dist\jsonTraverse.js:210:17
    at Array.forEach (<anonymous>)
    at Object.traverse (...\type2docfx\dist\jsonTraverse.js:205:23)
    at Object.<anonymous> (...\type2docfx\dist\main.js:78:20)
    at Module._compile (module.js:649:30)
    at Object.Module._extensions..js (module.js:660:10)
    at Module.load (module.js:561:32)
    at tryModuleLoad (module.js:501:12)
stratospheres commented 6 years ago

Having the same problem. Not entirely sure why, but the "syntax" property isn't initialized in this case. Just adding an initializer in the jsonTraverse.ts file in the extractInformationFromSignature seems to fix (but might not be the best way)...

// init syntax object if it's not already there...
method.syntax = method.syntax || {};