I'm trying to use Tern to perform type inference on some Javascript code. However, the type inference doesn't seem to be using the JSDoc comments alongside the code.
I'm using the code /** @type {Foo} */ let x;
as an example of the problem. On the Tern website's demo page (which uses CodeMirror), the editor is able to infer that the type of x
is Foo
.
Yet, when run locally via node, I get this back: { type: '?', exprName: 'x' }
.
Here's a snippet that replicates the issue:
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 has otherwise been working perfectly fine for type inference. It's when using the JSDoc comments that it doesn't seem to work with the way I've initialized and called it.
I even set the doc_comment
plugin to strong
, which means that JSDoc types are preferred over normally inferred types, but to no avail.
Any ideas how to get this to work?
As it turns out, you have to import the
doc_comment
plugin in order to use it. Otherwise, setting the plugins option for the tern server won't do anything.Simply adding
require("tern/plugin/doc_comment");
to the top of the file solved the problem.