Tl;dr version:
Using TernJS
for autocompletion purposes, I want to use a rule on a function, that taking a string as parameter, in order to return a type with the name same with the param given, for example:
foo("TypeA") returns +TypeA
foo("TypeB") returns +TypeB
In simple words, I am searching whether or not there is a notation in Tern that can give you a type using its name.
Long version:
Using CodeMirror
in combination with TernJS
set up in NodeJS
, I am trying to simulate a custom factory behavior and provide similar capabilities in JavaScript in ES6:
//Module: Test.js
module.exports = (function() {
var test = {};
//....
test.classA = class ClassA {
constructor() {
//....
},
foo() {}
//....
}
test.factoryClass = class Factory {
constructor() {
//....
}
register(name, classData) {
//....
}
createInstance(name) {
//....
}
}
return test;
})();
Now, what I want to do is this: If I have registered ClassA by doing:
var test = require("Test");
var factory = new test.factoryClass();
factory.register("ClassA", classA);
And I create a class instance of it:
var classAInst = factory.createInstance("ClassA");
I want in the editor to be able to get the methods of the ClassA
instance.
classAInst.<AutoCompletion-KeyStroke> //This should give the completion list containing `foo`.
Now, I know that in order to get this, I need to have generated the TernJS Inference Engine rules in the .json
definitions, so for ClassA and Factory I have something like:
{
"Test" : {
"ClassA": {
"!type": "fn()"
"prototype": {
"foo": {
"!type": "fn() -> string"
}
}
}
//....
"Factory": {
"prototype": {
"register": {
"!type": "fn(className: string, classDefinition: object)"
},
"createInstance": {
"!type": "<This is what I need to specify properly>"
}
}
}
}
}
The problem is that I figured out how to form the rules by inspecting the ecma6.json
file in the TernJS repo, as I was unable to find any documentation of the inference engine.
I tried doing something like this, attempting to tell the engine to index the type in a way, but it does not work (I suppose that []
is used for array definition only):
"createInstance": {
"!type": "fn(name: string) -> Test[!0]"
}
So, any help on this would be appreciated. Any link or reference to the inference engine manual would be very helpful too, as I did not manage to find such information.