2014-11-12 3 views
-1

Этот код от узла узла templatizer.Почему это && не короткое замыкание?

if (i === 3 && node.type === "ExpressionStatement" && node.expression.callee.object.name === "buf" && 
     node.expression.arguments.length === 1 && node.expression.arguments[0].type === "Literal") { 

     // save the simple string 
     simpleString = node.expression.arguments[0].value; 
     cnt++; 
    } 

Значение node.type является «ОбъявлениеПеременной», поэтому логическое выражение ложно, и поэтому node.expression не должны даже быть оценены, но это, кажется, так ...

TypeError: Cannot read property 'object' of undefined at /node_modules/templatizer/lib/simplifyTemplate.js:34:89 at Array.forEach (native)

Это означает, что ' объект 'в' вызываемом '. expression и callee обе не определены. Как только я перехожу к аварийному завершению узла.

EDIT

Я думаю, что Javascript работает отлично, и, возможно, некоторые асинхронной код приводит к странным результатам образуют отладчик. Если я ставлю console.log в верхней части цикла он иногда дает выход, который имеет смысл ...

/usr/local/bin/node bin/www 
count: 1 
i:0 
node.type: VariableDeclaration 
expression: undefined 
node.expression.callee: undefined 

count: 2 
i:1 
node.type: VariableDeclaration 
expression: undefined 
node.expression.callee: undefined 

count: 3 
i:2 
node.type: VariableDeclaration 
expression: undefined 
node.expression.callee: undefined 

count: 4 
i:3 
node.type: ExpressionStatement 
expression: [object Object] 
node.expression.callee: [object Object] 

count: 5 
i:4 
node.type: ReturnStatement 
expression: undefined 
node.expression.callee: undefined 

count: 6 
i:0 
node.type: VariableDeclaration 
expression: undefined 
node.expression.callee: undefined 

count: 7 
i:1 
node.type: VariableDeclaration 
expression: undefined 
node.expression.callee: undefined 

count: 8 
i:2 
node.type: VariableDeclaration 
expression: undefined 
node.expression.callee: undefined 

count: 9 
i:3 
node.type: ExpressionStatement 
expression: [object Object] 
node.expression.callee: undefined 


/Users/ME/WebstormProjects/MySite/node_modules/templatizer/lib/simplifyTemplate.js:41 
= 3 && node.type === "ExpressionStatement" && node.expression.callee.object.na 
                    ^
TypeError: Cannot read property 'object' of undefined 
    at /Users/ME/WebstormProjects/MySite/node_modules/templatizer/lib/simplifyTemplate.js:41:89 
    at Array.forEach (native) 
    at module.exports (/Users/ME/WebstormProjects/MySite/node_modules/templatizer/lib/simplifyTemplate.js:15:18) 
    at /Users/ME/WebstormProjects/MySite/node_modules/templatizer/templatizer.js:111:20 
    at Array.forEach (native) 
    at module.exports (/Users/ME/WebstormProjects/MySite/node_modules/templatizer/templatizer.js:95:15) 
    at Object.<anonymous> (/Users/ME/WebstormProjects/MySite/app.js:23:1) 
    at Module._compile (module.js:456:26) 
    at Object.Module._extensions..js (module.js:474:10) 
    at Module.load (module.js:356:32) 

Process finished with exit code 8 

Так выглядит как ошибка в модуле.

+2

«' node.type' is 'VariableDeclaration' - это то, что вам сказал ваш отладчик, или просто то, что вы ожидаете от него? –

+0

В каком файле вы конкретно ссылаетесь? – lxg

+4

докажите, что претендуйте на нас: console log эти условные значения перед тем, как войти в оператор if, и покажите нам, что в них. –

ответ

0

Похоже, что отладчик показывал значения из другой части выполнения, кроме той, которая вызвала сбой. Разумеется, это должно выполняться асинхронно. Так что ошибка в модуле. Временное исправление заключалось в добавлении дополнительной проверки к условному.

if (i === 3 && node.type === "ExpressionStatement" && node.expression.callee && node.expression.callee.object.name === "buf" && 
       node.expression.arguments.length === 1 && node.expression.arguments[0].type === "Literal") 

Я открыл issue.

Смежные вопросы