Node: Exception message for assert(0) depends on whitespace

Created on 10 Dec 2019  路  5Comments  路  Source: nodejs/node

  • Version: 10, 12
  • Platform: macOS High Sierra 10.13.6
  • Subsystem: Assert


Under Node 10 and Node 12, generating an exception from an assertion failure returns a different assertion failure message depending on differences in whitespace. It seems that with a certain whitespace configuration we see the Node 8 assertion failure message 0 == true, other configurations will generate the Node 10 message The expression evaluated to a falsy value. This issue only seems to affect the exception message, all other behaviour seems consistent.

This will affect anything that runs minified source.

I have included a simple reproduction of the issue here

assert confirmed-bug

All 5 comments

@AndrewFinlay thank you for the report. This should indeed not fail to create the better error message.

The left curly bracket without a line break before assert would cause this issue:

try { assert(0)
} catch (err) {}

function test() { assert(0)
}

It looks like the expression parse is intentionally started at the line start. Modifying it from start to the actual offset seems to fix these cases, but not sure if it breaks others.

https://github.com/nodejs/node/blob/7629fb25f2c97de9926f71724072ad2b2af27c85/lib/assert.js#L235-L239

// line 235
-let start = 0
+let start = offset

@pd4d10 no

https://github.com/nodejs/node/blob/7629fb25f2c97de9926f71724072ad2b2af27c85/lib/assert.js#L239-L242

 node = parseExpressionAt(code, start, { ecmaVersion: 11 });

this line will throw error with message Unexpected token (1:2) and the code are

try {         assert(condition, message)
  } catch (e) {
    throw e
  }
}

// Now run tests

console.log('Call assert proxy with slightly minified whitespace')
try {
  assertProxySlightlyMinified(0)
  console.error(`assertProxySlightlyMinified(0); failed to cause an exception`)
} catch (e) {
  console.log(`assertProxySlightlyMinified(0); generated exception: ${e}`)
}

console.log('Finished tests')

and this code will not throw error

    assert(condition, message)
  } catch (e) {
    throw e
  }
}

const assertProxySlightlyMinified = (condition, message) => {
  try {         assert(condition, message)
  } catch (e) {
    throw e
  }
}

// Now run tests

console.log('Call assert proxy with slightly minified whitespace')
try {
  assertProxy(0)
  console.error(`assertProxySlightlyMinified(0); failed to cause an exception`)
} catch (e) {
  console.log(`assertProxySlightlyMinified(0); generated exception: ${e}`)
}

console.log('Finished tests')

@Himself65 @pd4d10 is correct about that. The start is set to zero to include assert or what ever name the user named it. It would otherwise only show ok().

I am currently looking into it (there is an easy solution but that would waste a lot of CPU time, so I am trying to find a proper fix).

Was this page helpful?
0 / 5 - 0 ratings

Related issues

srl295 picture srl295  路  3Comments

cong88 picture cong88  路  3Comments

Brekmister picture Brekmister  路  3Comments

danielstaleiny picture danielstaleiny  路  3Comments

willnwhite picture willnwhite  路  3Comments