1

我正在尝试使用Tern对一些 Javascript 代码执行类型推断。但是,类型推断似乎没有在代码旁边使用 JSDoc 注释。

我使用代码/** @type {Foo} */ let x;作为问题的示例。在Tern 网站的演示页面(使用 CodeMirror)上,编辑器能够推断出类型xFoo.

然而,当通过节点在本地运行时,我得到了这个:{ type: '?', exprName: 'x' }.

这是一个复制问题的片段:

const tern = require('tern');

const ternServer = new tern.Server({
    plugins: {
        doc_comment: {
            strong: true
        }
    }
});

const js = `/** @type {Foo} */ let x;`;
ternServer.addFile("main", js);
ternServer.request({
    query: {
        type: "type",
        file: "main",
        start: js.length - 2,
        end: js.length - 2
    }
}, console.log);

否则,Tern 在类型推断方面工作得非常好。在使用 JSDoc 注释时,它似乎不适用于我初始化和调用它的方式。

我什至将doc_comment插件设置为strong,这意味着 JSDoc 类型优于通常推断的类型,但无济于事。

任何想法如何让它发挥作用?

4

1 回答 1

1

事实证明,您必须导入doc_comment插件才能使用它。否则,为 tern 服务器设置 plugins 选项不会做任何事情。

只需添加require("tern/plugin/doc_comment");到文件顶部即可解决问题。

于 2017-09-02T13:15:17.010 回答