Skip to content

[Bug]: syntaxes allowed in non-strict mode are rejected even in CJS files #7009

@sapphi-red

Description

@sapphi-red

Reproduction link or steps

REPL

What is expected?

No error happens

What is actually happening?

The following error happens:

Error: Build failed with 1 error:

[PARSE_ERROR] Error: Cannot assign to 'arguments' in strict mode
   ╭─[ index.cjs:5:1 ]
   │
 5 │ arguments = 1
   │ ────┬────  
   │     ╰────── 
───╯

System Info

REPL 1.0.0-beta.50

Any additional comments?

This code is valid in non-strict mode (sloppy mode). While ESM is always in strict mode, CJS is not. So this should not cause an error.

The fix is to use the correct ModuleKind when parsing:

  • For ModuleDefFormat::CJS || Cts || CjsPackageJson, use ModuleKind::Script
  • For ModuleDefFormat::EsmMjs || EsmMts || EsmPackageJson, use ModuleKind::Module
  • For ModuleDefFormat::Unknown, use ModuleKind::Unambiguous

It was tried in #7003, but is blocked by oxc-project/oxc#15541

Metadata

Metadata

Assignees

No one assigned

    Type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions