Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Confusing error when missing initializer in const declaration #6362

Closed
rhalff opened this issue Apr 24, 2016 · 3 comments
Closed

Confusing error when missing initializer in const declaration #6362

rhalff opened this issue Apr 24, 2016 · 3 comments
Labels
v8 engine Issues and PRs related to the V8 dependency.

Comments

@rhalff
Copy link

rhalff commented Apr 24, 2016

  • Version: v5.11.0
  • Platform: Ubuntu x86_64

Whenever a constant is left uninitialized (and no semicolons are used) the resulting error is confusing.

Currently it will report anything specified after it as an error.

I would have expected the same error message as the browser / d8 throws.

Test Script:

$ cat test.js 
'use strict'

function test () {
  const uninitialized
  console.log('test')
}

Node v5.11.0:

$ node test.js 
/srv/psichi/git/nodule/template/test.js:5
  console.log('test')
  ^^^^^^^

SyntaxError: Unexpected identifier
    at exports.runInThisContext (vm.js:53:16)
    at Module._compile (module.js:387:25)
    at Object.Module._extensions..js (module.js:422:10)
    at Module.load (module.js:357:32)
    at Function.Module._load (module.js:314:12)
    at Function.Module.runMain (module.js:447:10)
    at startup (node.js:148:18)
    at node.js:405:3

d8 & browser:
Using d8 (v5.1.0) or the chromium browser (v49.0.2623.87) the error message is much more concise

$ d8 test.js 
test.js:4: SyntaxError: Missing initializer in const declaration
  const uninitialized
        ^^^^^^^^^^^^^
SyntaxError: Missing initializer in const declaration

Babel:
Babel throws the same kind of error, but at least provides some more context:

$ babel-node test.js
/home/rhalff/.nvm/versions/node/v5.11.0/lib/node_modules/babel-cli/node_modules/babel-core/lib/transformation/file/index.js:556
      throw err;
      ^

SyntaxError: /srv/psichi/git/nodule/template/test.js: Unexpected token (5:2)
  3 | function test () {
  4 |   const uninitialized
> 5 |   console.log('test')
    |   ^
  6 | }
  7 | 
@ChALkeR
Copy link
Member

ChALkeR commented Apr 24, 2016

I don't see how this is confusing.
Until the next line, there is no error there.

It could have been

const uninitialized
                    = 10;

So, the unexpected token is on the line after const.

If you put semicolons after the lines, it would tell you that a semicolon is unexpected:

  const uninitialized;
                     ^

SyntaxError: Unexpected token ;

Nevertheless, the new behaviour (as seen in v8 5.1.0) is more clear, of course.

@ChALkeR
Copy link
Member

ChALkeR commented Apr 24, 2016

This is done inside v8.

In Node.js 6.0 rc4, with v8 5.0, the error message is in line with what you have on d8 5.1:

  const uninitialized
        ^^^^^^^^^^^^^
SyntaxError: Missing initializer in const declaration

@ChALkeR ChALkeR closed this as completed Apr 24, 2016
@ChALkeR ChALkeR added the v8 engine Issues and PRs related to the V8 dependency. label Apr 24, 2016
@rhalff
Copy link
Author

rhalff commented Apr 24, 2016

Ok, thanks for the feedback.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
v8 engine Issues and PRs related to the V8 dependency.
Projects
None yet
Development

No branches or pull requests

2 participants