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

Bug: Error thrown when iterating a dynamically created anonymous array #18054

Closed
elyran opened this issue Jan 9, 2018 · 4 comments
Closed

Bug: Error thrown when iterating a dynamically created anonymous array #18054

elyran opened this issue Jan 9, 2018 · 4 comments
Labels
invalid Issues and PRs that are invalid.

Comments

@elyran
Copy link

elyran commented Jan 9, 2018

  • Version: v9.3.0
  • Platform: Windows 10 64-bit
  • Subsystem: require

error-test.json

{
  "a": 1,
  "b": 2
}

error-test.js

const json = require('./error-test.json')
console.log(json)
[ 11, 22 ].forEach((value) => console.log(value))

Output:

E:\tmp>node error-test.js
{ a: 1, b: 2 }
E:\tmp\error-test.js:4
[ 11, 22 ].forEach((value) => console.log(value))
^

TypeError: Cannot read property '22' of undefined
at Object. (E:\tmp\error-test.js:4:1)
at Module._compile (module.js:660:30)
at Object.Module._extensions..js (module.js:671:10)
at Module.load (module.js:573:32)
at tryModuleLoad (module.js:513:12)
at Function.Module._load (module.js:505:3)
at Function.Module.runMain (module.js:701:10)
at startup (bootstrap_node.js:194:16)
at bootstrap_node.js:618:3

[Solution A] Assigning the array a variable name first wouldn't throw an error:

const json = require('./error-test.json')
console.log(json)
const values = [ 11, 22 ]
values.forEach((value) => console.log(value))

[Solution B] Adding a semicolon to the console.log(json) call would fix the error. That is:
console.log(json);
And that makes me wonder if the error is in the require module/subsystem or maybe it's related to the array.

Thanks!

@vsemozhetbyt
Copy link
Contributor

vsemozhetbyt commented Jan 9, 2018

I think the cause is the semicolon omission, your code is parsed in this way:

console.log(json)[ 11, 22 ].forEach();

ie:

console.log(json) -> undefined
undefined[ 11, 22 ] ->  undefined[ 22 ]

@gireeshpunathil
Copy link
Member

simplest recreate:

console.log()
[0]

while the missing semi-colon is understood to be the root cause, I would say the v8's source parser could do better here than coalescing them together to make a single expression. reference

@gireeshpunathil
Copy link
Member

interestingly, mozilla console also behaves the same. Looking for any documented evidence that support this behavior

@bnoordhuis bnoordhuis added the invalid Issues and PRs that are invalid. label Jan 9, 2018
@bnoordhuis
Copy link
Member

That's ASI (automatic semicolon insertion) for you. Working per spec, not a bug. Closing.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
invalid Issues and PRs that are invalid.
Projects
None yet
Development

No branches or pull requests

4 participants