Skip to content

[Bug]: Imports used inside class with decorator 2203 aren't recognized correctly when targets >= 'es2022' #12005

@ForkKILLET

Description

@ForkKILLET

Version

System:
  OS: Linux 6.12 cpe:/o:nixos:nixos:25.11 25.11 (Xantusia)
  CPU: (6) x64 AMD Ryzen 5 4500U with Radeon Graphics
  Memory: 4.16 GB / 15.00 GB
  Container: Yes
  Shell: 5.9 - /run/current-system/sw/bin/zsh
Browsers:
  Firefox: 143.0.1
  Firefox Developer Edition: 143.0.1
npmPackages:
  @rspack/cli: ^1.5.8 => 1.5.8 
  @rspack/core: ^1.5.8 => 1.5.8

Details

Input

In a.ts we export a function f.

In index.ts, we import f, define a random 2203 decorator id, define a class Test decorated by id referencing f and call f.

// a.ts
export const f = () => console.log('f() called')
// index.ts
import { f } from './a'

const id = <T>(x: T): T => x

@id
export class Test {
  method() {
    f()
  }
}

f()

The correct output should be:

// ...
const f = ()=>console.log('f() called');
// ...
const id = (x)=>x;
let _Test;
class Test {
    method() {
        f();
    }
}
({ c: [_Test, _initClass] } = _apply_decs_2203_r(Test, [], [
    id
]));
_initClass();
f();

However, when target is set to 'es2022' or higher, the output becomes:

// ...
const a_f = ()=>console.log('f() called');
// ...
const id = (x)=>x;
let _Test;
class Test {
    static{
        ({ c: [_Test, _initClass] } = _apply_decs_2203_r(this, [], [
            id
        ]));
    }
    method() {
        f(); // !
    }
    static{
        _initClass();
    }
}
a_f();

And if we remove the call to f from index.ts, const a_f = ... disappears;

if we remove the decorator id, it compiles correctly.

In other words, imports used inside class with decorator 2203 aren't recognized correctly under this target.

(SWC output is correct)

Reproduce link

https://github.com/ForkKILLET/rslib-repro-deco-2203

Reproduce Steps

  1. Clone the reproduction repo.
  2. Run pnpm i
  3. Modify target to 'es2022' or higher in code.
  4. Run pnpm exec rspack build.
  5. See the output in dist-rspack/.

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions