Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Proposal decorators initializers not called if class contains an abstract field #17541

@SCLeoX

Description

@SCLeoX

💻

  • Would you like to work on a fix?

How are you using Babel?

Other (Next.js, Gatsby, vue-cli, ...)

Input code

export function reproduction() {
  let initializerWasCalled = false;

  function decorator(method: any, ctx: ClassMethodDecoratorContext<any, any>) {
    ctx.addInitializer(function () {
      console.info("Initializer called!");
      initializerWasCalled = true;
    });
    return method;
  }

  abstract class A {
    abstract a: number; // <-- Note, commenting out this completely unrelated abstract field fixes the issue (i.e. the initializer will be called.) Removing the "abstract" also works.

    @decorator
    method() {}
  }

  class B extends A {
    a = 1;
  }
  const b = new B();

  console.info("Was initializer called?:", initializerWasCalled); // false, expected: true
}

I even have a playground link

Configuration file name

No response

Configuration

I am using vite, but see the link above, was able to reproduce online.

import react from "@vitejs/plugin-react";
import { defineConfig } from "vite";

// https://vite.dev/config/
export default defineConfig({
  plugins: [
    react({
      babel: {
        plugins: [["@babel/plugin-proposal-decorators", { version: "2023-11" }]],
      },
    }),
  ],
});

Current and expected behavior

If a class contains an abstract field, the initializers added by method decorators on that class are not getting called.

Either removing the field, or turning the field into a non abstract field fixes the issue. See my code sample for a minimal reproduction.

Environment

  System:
    OS: Windows 10 10.0.19045
  Binaries:
    Node: 23.5.0 - C:\Program Files\nodejs\node.EXE
    npm: 10.9.2 - C:\Program Files\nodejs\npm.CMD  
    bun: 1.2.10 - ~\.bun\bin\bun.EXE
  npmPackages:
    @babel/plugin-proposal-decorators: ^7.28.0 => 7.28.0
    eslint: ^9.36.0 => 9.37.0

Possible solution

No response

Additional context

No response

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions