javascriptbabeljsbabel-parser

Why can't @babel/parser parse objects?


When i input an object in astexplorer like below code, this will get a mistake: Missing semicolon. (3:9), what is that mean? @babel/parser can't parse object correctly?

{
  name: 'test',
  company: 'test company',
}

mistake


Solution

  • Your code is interpreted as a block statement, not an object literal. However, the contents of the block are not valid statements, hence the error.

    If you wrap the code in parenthesis to force the evaluation as an expression (and thus as an object literal) it works as expected:

    ({
      name: 'test',
      company: 'test company',
    })