简体   繁体   中英

Why does this && not short-circuit?

This code is from the node module 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++;
    }

The value of node.type is 'VariableDeclaration' so the logical expression is false and so node.expression shouldn't even be evaluated but it appears to be so...

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

It means the 'object' in 'callee'. expression and callee are both undefined. As soon as I step over the conditional node crashes.

EDIT

I think Javascript works fine and perhaps some async code is resulting in strange results form the debugger. If I put a console.log at the top of the loop it sometimes gives output that makes sense...

/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

So looks like a bug in the module.

Seems that the debugger was showing values from a different part of the execution than the one that caused the crash. Surely it must be running things asynchronously somewhere. So a bug in the module. Temporary fix was to add an extra check to the conditional.

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") 

I opened an issue .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM