javascriptpaperjs

Finding location of syntax errors in paper.js


In case a paperjs script contains a syntax error, this kind of error is is displayed:

Uncaught SyntaxError: Unexpected token (242:31) (at paper-full.min.js:32:208210)
    at k (paper-full.min.js:32:208210)
    at ve (paper-full.min.js:32:224901)
    at Oe (paper-full.min.js:32:231197)
    at Ae (paper-full.min.js:32:229397)
    at paper-full.min.js:32:228839
    at paper-full.min.js:32:228850
    at Me (paper-full.min.js:32:228980)
    at Me (paper-full.min.js:32:229053)
    at Pe (paper-full.min.js:32:228429)
    at Le (paper-full.min.js:32:231909)

I found a method to make it slightly more readable, by including paper.js rather than paper.min.js:

paper-full.js:15781 Uncaught SyntaxError: Unexpected token (238:31)
    at raise (paper-full.js:15781:12)
    at unexpected (paper-full.js:16422:2)
    at parseExprAtom (paper-full.js:16868:4)
    at parseExprSubscripts (paper-full.js:16788:25)
    at parseMaybeUnary (paper-full.js:16774:13)
    at parseExprOps (paper-full.js:16740:21)
    at parseMaybeConditional (paper-full.js:16727:13)
    at parseMaybeAssign (paper-full.js:16713:13)
    at parseMaybeAssign (paper-full.js:16719:17)
    at parseExpression (paper-full.js:16702:13)

...but it still does not help: no clue is provided about the location of error into MY script, these data are only about paper.js library.

Adding "debugger;" line at beginning of script does not work, because the script is not executed at all: paper.js acorn preprocessor detects a syntax error before execution, and it stops everything.

Some tests alowed me a better estimating approximate location of the error: I modified parseTopLevel() function adding a couple of console.log():

  function parseTopLevel(program) {
    lastStart = lastEnd = tokPos;
    if (options.locations) lastEndLoc = new line_loc_t;
    inFunction = strict = null;
    labels = [];
    readToken();
console.log("START",startNode().start)    

    var node = program || startNode(), first = true;
    if (!program) node.body = [];
    while (tokType !== _eof) {
      var stmt = parseStatement();
      node.body.push(stmt);
      if (first && isUseStrict(stmt)) setStrict(true);
      first = false;
console.log("STMT=",stmt);      
    }
    return finishNode(node, "Program");
  }

The result is:

START 1832

STMT= node_t {type: 'ExpressionStatement', start: 4481, end: 4497, range: Array(2), expression: node_t}
STMT= node_t {type: 'FunctionDeclaration', start: 4500, end: 6910, range: Array(2), id: node_t, …}
....
....
STMT= node_t {type: 'ExpressionStatement', start: 6921, end: 6937, range: Array(2), expression: node_t}

paper-full.js:15781 Uncaught SyntaxError: Unexpected token (238:31)
    at raise (paper-full.js:15781:12)
    at unexpected (paper-full.js:16421:2)
    at parseExprAtom (paper-full.js:16867:4)
    at parseExprSubscripts (paper-full.js:16787:25)
    at parseMaybeUnary (paper-full.js:16773:13)
    at parseExprOps (paper-full.js:16739:21)
    at parseMaybeConditional (paper-full.js:16726:13)
    at parseMaybeAssign (paper-full.js:16712:13)
    at parseMaybeAssign (paper-full.js:16718:17)
    at parseExpression (paper-full.js:16701:13)

Last line before the error message shows the error location: start = 6921, end = 6937

Problem is that... it's not true: the real location is not 6921 but 7984, which is NOT 6921 + 1832; the error position is counted in a variant of my source which I cannot understand: maybe without empty lines, maybe without commented lines,.... but everything I tested did not work.

So question is: how do I calculate error position starting from that "6921"?

How is this number assigned?

Or is there another method to find the line of the error?

In my specific case, the error is caused by this line:

camData.segments.forEach( (s) => { console.log(s.y, 10 * Math.sin(s.y) - 2)});

Weird thing is that this line works perfectly from console...


Solution

  • Found the solution: in line

    paper-full.js:15781 Uncaught SyntaxError: Unexpected token (238:31)
    

    the "238:31" stands for "character 31 of line 238 counting from <SCRIPT> statement in the HTML source"; in case of runtime error, there is a clickable link which opens the source at the culprit line, but in case of syntax error there is no link.

    This means that if the script is inside an HTML page after some HTML code, you must remove the HTML lines to find the right line in your text editor.