fix(dev-server-hmr): handle multiple assigned variables

This commit is contained in:
Lars den Bakker
2020-11-30 08:28:48 +01:00
parent cce9e11e10
commit afadd6fa2c
3 changed files with 27 additions and 1 deletions

View File

@@ -0,0 +1,5 @@
---
'@open-wc/dev-server-hmr': patch
---
handle multiple assigned variables

View File

@@ -169,7 +169,11 @@ function babelPluginWcHmr() {
}
if (decoratedCustomElement.isIdentifier()) {
maybeInjectRegister(callExpr.parentPath, decoratedCustomElement.node.name);
let assignExpr = callExpr.parentPath;
while (assignExpr && assignExpr.isAssignmentExpression()) {
assignExpr = assignExpr.parentPath;
}
maybeInjectRegister(assignExpr, decoratedCustomElement.node.name);
}
}
}

View File

@@ -112,4 +112,21 @@ __$wc_hmr$__.register(import.meta.url, Foo);
Foo = __decorate([defineElement('x-foo')], Foo);`);
});
it('compiled decorator with a double assigned variable', () => {
const code = `
function __decorate() {}
let Foo = Foo_1 = class extends HTMLElement {};
Foo = Foo_1 = __decorate([customElement('x-foo')], Foo);`;
const result = transform(code, { decorators: [{ name: 'customElement' }] });
expect(result).to.equal(`${banner}
function __decorate() {}
let Foo = Foo_1 = class extends HTMLElement {};
__$wc_hmr$__.register(import.meta.url, Foo);
Foo = Foo_1 = __decorate([customElement('x-foo')], Foo);`);
});
});