Skip to content

Error with new Decorator Plugin and HTMLElement instance with newย #8750

@pabloalmunia

Description

@pabloalmunia

Current Behavior

This class extend from HTMLImport run perfectly (it's very simple):

class Counter extends HTMLElement {
  constructor() {
    super();
  }
}

window.customElements.define('my-counter', Counter);
const counter = new Counter();

Now, I use @littledan example decorator:

// Define the class as a custom element with the given tag name
function defineElement(tagName) {
  // In order for a decorator to take an argument, it takes that argument
  // in the outer function and returns a different function that's called
  // when actually decorating the class (manual currying).
  return function(classDescriptor) {
    let { kind, elements } = classDescriptor;
    console.assert(kind == "class");
    return {
      kind,
      elements,
      // This callback is called once the class is otherwise fully defined
      finisher(klass) {
        window.customElements.define(tagName, klass);
      }
    };
  };
}

@defineElement('my-counter')
class Counter extends HTMLElement {
  constructor() {
    super();
  }
}

const counter = new Counter();

When that code is transformed with the new Decorator plugin (thank @nicolo-ribaudo) and run it on a browser, this error is displayed:

Uncaught TypeError: Failed to construct 'HTMLElement': Please use the 'new' operator, this DOM object constructor cannot be called as a function.
    at new Counter (<anonymous>:100:73)
    at <anonymous>:116:15

Expected behavior/code

The decoration don't change the Custom Element behavior and must be run without problems.

Babel Configuration (.babelrc.js)

const presets = [ "@babel/preset-env" ];
const plugins = [
  [ "@babel/plugin-proposal-decorators", { "legacy": false, decoratorsBeforeExport: false } ]
];
module.exports = { presets, plugins };

Environment

  • Babel version(s): 7.1.0

Metadata

Metadata

Assignees

No one assigned

    Labels

    Spec: DecoratorsoutdatedA closed issue/PR that is archived due to age. Recommended to make a new issue

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions