Skip to content

acorn-loose throws SyntaxError on some numeric separators with ecmaVersion 2021 #1363

@denis-sokolov

Description

@denis-sokolov

Unexpectedly thrown SyntaxError from acorn-loose parse when using invalid numeric separators with ecmaVersion 2021 and above:

> looseParse("0_", { ecmaVersion: 2021 })
Uncaught:
[SyntaxError: Numeric separator is not allowed in legacy octal numeric literals (1:1)
] {
  pos: 1,
  loc: Position { line: 1, column: 1 },
  raisedAt: 1
}

Does not throw with ecmaVersion 2020 and below:

> looseParse("0_", { ecmaVersion: 2020 })

Node {
  type: 'Program',
  start: 0,
  end: 2,
  body: [ Node { type: 'EmptyStatement', start: 2, end: 2 } ],
  sourceType: 'script'
}

Note that the same happens with input 1_, but the error is different (“Numeric separator is not allowed at the last of digits”).

Errors are marked as recoverable in the source code:

acorn/acorn/src/tokenize.js

Lines 466 to 468 in 82233bf

if (isLegacyOctalNumericLiteral) this.raiseRecoverable(this.pos, "Numeric separator is not allowed in legacy octal numeric literals")
if (lastCode === 95) this.raiseRecoverable(this.pos, "Numeric separator must be exactly one underscore")
if (i === 0) this.raiseRecoverable(this.pos, "Numeric separator is not allowed at the first of digits")

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions