Skip to content

[reactive-element] addInitializer adds initializer to superclass #3373

@christophe-g

Description

@christophe-g

Which package(s) are affected?

Lit Core (lit / lit-html / lit-element / reactive-element)

Description

While looking #3296 a bit more in depth, I realize that the culprit is not @lit-labs/context but ReactiveElement.addInitializer, which will add intializers to the superclass if it already contains its own initializers.

Documentation states:

Initializers are stored per-constructor. Adding an initializer to a subclass does not add it to a superclass. Since initializers are run in constructors, initializers will run in order of the class hierarchy, starting with superclasses and progressing to the instance's class.

This is not what happens as addInitializer only checks if this._initializer exists on the Class prototype chain. In case a superclass already has one, it will be added to it (the superclass). We should have a hasOwnProperty check there.

   /*
   * Initializers are stored per-constructor. Adding an initializer to a
   * subclass does not add it to a superclass. Since initializers are run in
   * constructors, initializers will run in order of the class hierarchy,
   * starting with superclasses and progressing to the instance's class.
   *
   * @nocollapse
   */
  static addInitializer(initializer: Initializer) {
    this._initializers ??= [];
    this._initializers.push(initializer);
  }

Reproduction

  • go to this playground
  • See that the base class has 2 initializers (while it shoul donly have one)

Workaround

none

Is this a regression?

No or unsure. This never worked, or I haven't tried before.

Affected versions

Failing in 2.4.0

Browser/OS/Node environment

NA

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    Status

    ✅ Done

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions