Node: Syntax error message sometimes highlights the wrong token for reserved words

Created on 6 Jan 2019  路  4Comments  路  Source: nodejs/node

  • Version: v11.6.0
  • Platform: Linux (linux-4.20.arch1-1-ARCH)

When attempting to declare a variable with a reserved word as an identifier name, the SyntaxError message thrown will sometimes highlight the wrong token. For example:

'use strict';
let public = 0;

The above code throws the following error:

let public = 0;
^^^

SyntaxError: Unexpected strict mode reserved word
    at new Script (vm.js:84:7)
    at createScript (vm.js:264:10)
    at Object.runInThisContext (vm.js:312:10)
    at Module._compile (internal/modules/cjs/loader.js:684:28)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:732:10)
    at Module.load (internal/modules/cjs/loader.js:620:32)
    at tryModuleLoad (internal/modules/cjs/loader.js:560:12)
    at Function.Module._load (internal/modules/cjs/loader.js:552:3)
    at Function.Module.runMain (internal/modules/cjs/loader.js:774:12)
    at executeUserCode (internal/bootstrap/node.js:342:17)

However, changing the let to a const throws this error instead:

const public = 0;
      ^^^^^^

SyntaxError: Unexpected strict mode reserved word
    at new Script (vm.js:84:7)
    at createScript (vm.js:264:10)
    at Object.runInThisContext (vm.js:312:10)
    at Module._compile (internal/modules/cjs/loader.js:684:28)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:732:10)
    at Module.load (internal/modules/cjs/loader.js:620:32)
    at tryModuleLoad (internal/modules/cjs/loader.js:560:12)
    at Function.Module._load (internal/modules/cjs/loader.js:552:3)
    at Function.Module.runMain (internal/modules/cjs/loader.js:774:12)
    at executeUserCode (internal/bootstrap/node.js:342:17)

Some additional notes about when this happens, in case it helps:

  • It only occurs in strict mode; non-strict-mode code gets underlined correctly
  • It only occurs with let declarations, not const or var
  • It does not occur with identifier names of let or strict, presumably because they're treated differently according to the ECMA spec
  • For identifiers true, false, and null, the bug still occurs with a let declaration, but the error message thrown by const and var declarations becomes SyntaxError: Unexpected token instead of SyntaxError: Unexpected strict mode reserved word
V8 Engine confirmed-bug

Most helpful comment

Reported on V8 side: https://crbug.com/v8/10649
We need to fix this on our side.

All 4 comments

This is likely a V8 bug. /cc @nodejs/v8

@gsathya

Ping @nodejs/v8

Reported on V8 side: https://crbug.com/v8/10649
We need to fix this on our side.

Was this page helpful?
0 / 5 - 0 ratings