tern icon indicating copy to clipboard operation
tern copied to clipboard

!known_modules breaks the capability to use !proto with node type

Open angelozerr opened this issue 10 years ago • 5 comments
trafficstars

I have a problem with my tern express plugin since I'm using "!known_modules". Tern express defines a Request which extends node http.IncomingMessage.

"!define": {
  request: {
        Request: {
          "!type": "fn()",
          prototype : {
            "!proto" : "http.IncomingMessage.prototype",
}

See https://github.com/angelozerr/tern-node-express/blob/master/node-express.js#L196

With tern 0.13.0 which used !node to extend node tern plugin, it worked well, now with !known_modules it doesn't work. I have debugged and it seems that it works because in 0.13.0, cx.paths stores http.IncomingMessage and http.IncomingMessage.prototype although 0.16.0 stores only http.IncomingMessage.

So in 0.13.0 https://github.com/ternjs/tern/blob/master/lib/def.js#L310 can return the cached http.IncomingMessage.prototype, 0.16.0 cannot do that.

My initial need is to use http.IncomingMessage.prototype in a custom node tern plugin.

angelozerr avatar Oct 28 '15 10:10 angelozerr

@marijnh I have finxed my problem by copying node definitions inside cx.localDefs for node-express :

function preLoadDef(data) {
    var cx = infer.cx(), localDefs = cx.localDefs;
    if (cx.definitions["node"] && data["!define"] && data["!name"]== "node-express") {
      // copy node definition to localDefs to support "!proto" : "http.IncomingMessage.prototype", for request.Request
      for (var def in cx.definitions["node"]) {
        cx.localDefs[def] = cx.definitions["node"][def];
      }
    }
  }

It works great, but I don't know if it's a clean mean.

angelozerr avatar Oct 28 '15 14:10 angelozerr

Did this ever work, though? The http in your path refers, if I understand correctly, to Node's built-in http module. That was never visible from another definition set, as far as I understand it.

We'll need a way to specify dependencies between JSON definitions, but that doesn't exist yet.

marijnh avatar Nov 09 '15 09:11 marijnh

Did this ever work, though?

@marijnh I know it's not clean mean, because we expose node modules as global (and it's not like this, that node works). But it's the only mean that I have found to fix problem with express.

We'll need a way to specify dependencies between JSON definitions, but that doesn't exist yet.

Yes it should be very cool if we could specify dependencies like:

"!define": {
  request: {
        Request: {
          "!type": "fn()",
          prototype : {
            "!proto" : "!exports.http.IncomingMessage.prototype",
}

angelozerr avatar Nov 09 '15 09:11 angelozerr

Hey guys, now I am trying to create a plugin for a framework that is an extension of express... so I need to define the prototype for one of my types as "!proto" : "application.Application.prototype" (which is coming from node-express plugin from @angelozerr)

Is there a way to accomplish that or that is still unsupported? (I have tried to do the preLoad defs but that does not really seem to do any difference)

Regards Orlando

orlandoibm avatar Apr 21 '16 17:04 orlandoibm

There's still no way to declare dependencies between .json files, but in a plugin you can have your code load dependencies and then refer to the defs they produce.

marijnh avatar Jun 07 '16 10:06 marijnh