15

In this answer here, the OP's issue is syntactically incorrect code:

https://stackoverflow.com/a/52009987/1068446

So I've reproduced the error, in a simplified output:

function foo(x) {
  return x%2 ===0; // x is an even number; 
}

if (function foo(2)) {
  console.log("we got here"); 
}

However, the error message isn't particularly meaningful:

{
  "message": "Uncaught SyntaxError: Unexpected number",
  "filename": "https://stacksnippets.net/js",
  "lineno": 17,
  "colno": 18
}

There is no line 17 in this code.

If I run this code with Node.js on my own machine, I get:

davidj@davidj-laptop:~/git-workspace/dsandbox$ node syntax.js
/home/davidj/git-workspace/dsandbox/syntax.js:5
  if (function foo(2)) {
                   ^

SyntaxError: Unexpected number
    at new Script (vm.js:74:7)

This is a more useful error. The error points correctly to the line of the syntax error.

8
  • 10
    In other words, one might say there are two issues: (1) The embedded snippet's error lineno does not correspond to the line number users see in the editor (2) The embedded snippet's error does not print the actual line content (nor the character) at which the error occurs. A fix for either would be useful. Commented Aug 25, 2018 at 2:50
  • I believe both Chrome and Firefox allow you to click on the error, and it would take you to the problematic line in the source code. Commented Aug 25, 2018 at 2:54
  • @NisargShah For code in the snippet iframe, that works in Chrome, but not Firefox. For most other code that works in both browsers. However, this question is about what's output within the iframe, such that it is easily visible to peopel reading the post and clicking "Run code snippet".
    – Makyen Mod
    Commented Aug 25, 2018 at 6:22
  • @Makyen Yup, if the browser's not helping, this might be confusing for some people. Both Codepen and JSFiddle are highlighting the error in the editor itself. So yeah, StackSnippet does have some limitations. Side note: Edge is also pointing to the right line in debugger. So it's weird to see Firefox confused on that one. Commented Aug 25, 2018 at 6:49
  • 2
    @dwjohnston Deviating a bit from topic but your answer on that question is not relevant. The OP is trying to do Boolean(function abc() {}) which is not a syntax error, whereas you are responding with why Boolean(function abc()) is incorrect. Commented Aug 25, 2018 at 7:00
  • 4
    Is this a bug, feature-request or support question? In fact, what is the question?
    – user692942
    Commented Aug 26, 2018 at 8:31
  • I've noticed this as well, my solution was not to use stackoverflow built in editor Commented Aug 26, 2018 at 16:44
  • 3
    Given that they use the information provided by the UA, this is the normal output. You have to remember that your code is inserted inline in a document, at the end of the <body>. So the line-no is actually correct, even if misleading. To fix it would require to know in which <script> tag the code the error thrown in was, because you can insert <script> tags in the HTML part too. And to do this is not a trivial task. Or, they would have to run this script as an external script, but this would mean from a blob URI and hence yet an other thing to include in every snippet...
    – Kaiido
    Commented Aug 27, 2018 at 1:42

0

Browse other questions tagged .