Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

I have this error: #1892

Closed
sergiodn opened this issue Mar 17, 2015 · 10 comments
Closed

I have this error: #1892

sergiodn opened this issue Mar 17, 2015 · 10 comments

Comments

@sergiodn
Copy link

Hi. At some point of my project, this message have appeard:

TypeError: Property 'visitundefined' of object #<Object> is not a function

Please report this entire error and stack trace to https://github.com/jadejs/jade/issues
   at Object.Compiler.visitNode (/home/sergio/dev/myapp/node_modules/jade/lib/compiler.js:225:37)
   at Object.Compiler.visit (/home/sergio/dev/myapp/node_modules/jade/lib/compiler.js:212:10)
   at Object.Compiler.visitBlock (/home/sergio/dev/myapp/node_modules/jade/lib/compiler.js:295:12)
   at Object.Compiler.visitNode (/home/sergio/dev/myapp/node_modules/jade/lib/compiler.js:225:37)
   at Object.Compiler.visit (/home/sergio/dev/myapp/node_modules/jade/lib/compiler.js:212:10)
   at Object.Compiler.compile (/home/sergio/dev/myapp/node_modules/jade/lib/compiler.js:66:10)
   at parse (/home/sergio/dev/myapp/node_modules/jade/lib/index.js:114:19)
   at Object.exports.compile (/home/sergio/dev/myapp/node_modules/jade/lib/index.js:205:16)
   at handleTemplateCache (/home/sergio/dev/myapp/node_modules/jade/lib/index.js:174:25)
   at Object.exports.renderFile (/home/sergio/dev/myapp/node_modules/jade/lib/index.js:380:10) 
@ForbesLindesay
Copy link
Member

Interesting, is there any chance you could give us a copy of the jade file that results in this error?

@sergiodn
Copy link
Author

The code above, transforms an url into a call in a controller. The controller returns a promise that results in a call to the view with the given params (the error is inres.render('./' + p.name + '/' + p.method + '.jade', data); ): thanks

var system = require('es6-module-loader').System;

export class Router {
    static loadParams(url) {
        var m = url.replace(/^\//, '').split('/');
        var name = m && m.length > 0 && m[0] ? m[0] : 'index';

        var p = {
            name: name,
            controller: name.capitalize() + 'Controller',
            method: m && m.length > 1 ? m[1] : 'index',
            params: {}
        };
        if (m) {
            var l = m.length;

            if (l > 1 && l < 4 && !isNaN(m[l - 1])) {
                p.params.id = m[l - 1];
            }
            else {
                for (var i = 2; i < l; i += 2) {
                    p.params[m[i]] = (i + 1) < l ? m[i + 1] : '';
                }
            }
        }
        return p;
    }

    // Processamento do routeamento por defeito
    static process(req, res, next) {
        var p = Router.loadParams(req.path);

        system.import('./app/ctr/' + p.name)
            .then((ctr) => {
                ctr = new ctr[p.controller](req, res);
                ctr[p.method](p.params).then((data) => {
                    res.render('./' + p.name + '/' + p.method + '.jade', data);
                });
            })
            .catch((err) => {
                console.log('Error', err);
                next();
            });
    }
} 

@ForbesLindesay
Copy link
Member

What would be extremely useful would be a copy of the jade file that causes the error. What appears to be happening is that the jade parser is producing an invalid AST that the jade compiler is therefore unable to compile, but without the original source, I have no way to reproduce the issue.

If you aren't comfortable posting the jade file online, you can send it to me privately if you prefer (forbes [at] lindesay.co.uk)?

@sergiodn
Copy link
Author

That's ok, but the file is extremely simple. I think the error is because
of es6-module-loader. Thanks

ObrigadoSaudações,Sérgio
Humansoft

2015-03-18 15:54 GMT+00:00 Forbes Lindesay [email protected]:

What would be extremely useful would be a copy of the jade file that
causes the error. What appears to be happening is that the jade parser is
producing an invalid AST that the jade compiler is therefore unable to
compile, but without the original source, I have no way to reproduce the
issue.

If you aren't comfortable posting the jade file online, you can send it to
me privately if you prefer (forbes [at] lindesay.co.uk)?


Reply to this email directly or view it on GitHub
#1892 (comment).

@fathyb
Copy link

fathyb commented Mar 19, 2015

Exactly the same error happened to me :

TypeError: Property 'visitundefined' of object #<Object> is not a function

Please report this entire error and stack trace to https://github.com/jadejs/jade/issues
    at Object.Compiler.visitNode (node_modules/jade/lib/compiler.js:225:37)
    at Object.Compiler.visit (node_modules/jade/lib/compiler.js:212:10)
    at Object.Compiler.visitBlock (node_modules/jade/lib/compiler.js:295:12)
    at Object.Compiler.visitNode (node_modules/jade/lib/compiler.js:225:37)
    at Object.Compiler.visit (node_modules/jade/lib/compiler.js:212:10)
    at Object.Compiler.compile (node_modules/jade/lib/compiler.js:66:10)
    at parse (node_modules/jade/lib/index.js:114:19)
    at Object.exports.compile (node_modules/jade/lib/index.js:205:16)
    at handleTemplateCache (node_modules/jade/lib/index.js:174:25)
    at Object.exports.renderFile (node_modules/jade/lib/index.js:380:10)

I use jade with express, and it throw this error on every pages independently of the jade file used. I don't understand why, I don't think I've made any changes with jade files or something related.

Nothing on Google.. Maybe it's update related?

EDIT :

Ok I feel stupid now, a ugly Object prototyping was the cause of the problem :

Object.prototype.forEach = function(callback) {
    for(var j in this) {
        callback.apply(this[j], [j]);
    }
}

Removing it fixed everything, hope it'll help.

@ForbesLindesay
Copy link
Member

Interesting.. I think that's probably caused by this code:

      // hoist mixins
      for (var name in this.mixins)
        ast.unshift(this.mixins[name]);

in the parser, which (for your example) would try and "hoist" forEach as a mixin onto your AST. Because the function isn't a jade AST node, it doesn't have a type, so when the compiler attempts to compile it, it gets undefined for the type. It therefore tries to run visitundefined, which fails.

@TimothyGu
Copy link
Member

@ForbesLindesay does this mean that we need a hasOwnProperty?

@fathyb
Copy link

fathyb commented Mar 20, 2015

Using hasOwnProperty fix it :

// hoist mixins
for (var name in this.mixins)
  if(this.mixins.hasOwnProperty(name))
    ast.unshift(this.mixins[name]);

@ForbesLindesay
Copy link
Member

We should just use Object.keys like we do elsewhere (which is equivalent)

@TimothyGu
Copy link
Member

In current master:

Object.prototype.forEach = function(callback) {
    for(var j in this) {
            callback.apply(this[j], [j]);
        }
}

var jade = require('jade')
jade.compile('doctype html')

shows

/home/timothy_gu/jade/lib/index.js:109
    throw err;
          ^
TypeError: callback.apply is not a function
    at Object.forEach (/home/timothy_gu/jade-cli/a.js:3:22)
    at Parser.loadPlugins (/home/timothy_gu/jade-code-gen/node_modules/with/node_modules/acorn-globals/node_modules/acorn/dist/acorn.js:1616:7)
    at new Parser (/home/timothy_gu/jade-code-gen/node_modules/with/node_modules/acorn-globals/node_modules/acorn/dist/acorn.js:1558:10)
    at Object.parse (/home/timothy_gu/jade-code-gen/node_modules/with/node_modules/acorn-globals/node_modules/acorn/dist/acorn.js:937:10)
    at reallyParse (/home/timothy_gu/jade-code-gen/node_modules/with/node_modules/acorn-globals/index.js:29:18)
    at findGlobals (/home/timothy_gu/jade-code-gen/node_modules/with/node_modules/acorn-globals/index.js:42:11)
    at addWith (/home/timothy_gu/jade-code-gen/node_modules/with/index.js:44:28)
    at Object.Compiler.compile (/home/timothy_gu/jade-code-gen/index.js:121:12)
    at generateCode (/home/timothy_gu/jade-code-gen/index.js:25:39)
    at compileBody (/home/timothy_gu/jade/lib/index.js:83:14)

The cause of this bug is acorn, which fills its options.plugins with an empty object, which isn't really empty now we add a method to its prototype. And, when accessing options.plugins it uses an unprotected for-in loop, causing this bug to occur.

There is really nothing we can fix here in jade now, so closing.

@TimothyGu TimothyGu added this to the 2.0.0 milestone Aug 15, 2015
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

4 participants