pull/4534/head
Tan Li Hau 6 years ago
parent 40c5df51a2
commit 571011baa4

@ -187,6 +187,9 @@ export default class Component {
if (variable) { if (variable) {
variable.referenced = true; variable.referenced = true;
} else if (is_reserved_keyword(name)) { } else if (is_reserved_keyword(name)) {
if (name === '$$host' && !this.compile_options.customElement) {
throw new Error(`$$host is for custom element. Did you forget the 'customElement: true' compile option?`);
}
this.add_var({ this.add_var({
name, name,
injected: true, injected: true,
@ -655,6 +658,12 @@ export default class Component {
name, name,
injected: true, injected: true,
}); });
if (name === '$$host' && !this.compile_options.customElement) {
this.error(node as any, {
code: 'illegal-host',
message: `$$host is for custom element. Did you forget the 'customElement: true' compile option?`
});
}
} else if (name[0] === '$') { } else if (name[0] === '$') {
if (name === '$' || name[1] === '$') { if (name === '$' || name[1] === '$') {
this.error(node as any, { this.error(node as any, {

@ -412,6 +412,7 @@ export default function dom(
body.push(b` body.push(b`
function ${definition}(${args}) { function ${definition}(${args}) {
${component.var_lookup.has('$$host') ? 'const $$host = $$self' : null}
${rest} ${rest}
${reactive_store_declarations} ${reactive_store_declarations}

@ -1,4 +1,4 @@
export const reserved_keywords = new Set(["$$props", "$$restProps"]); export const reserved_keywords = new Set(["$$props", "$$restProps", "$$host"]);
export function is_reserved_keyword(name) { export function is_reserved_keyword(name) {
return reserved_keywords.has(name); return reserved_keywords.has(name);

@ -105,7 +105,11 @@ describe('custom-elements', function() {
const page = await browser.newPage(); const page = await browser.newPage();
page.on('console', (type, ...args) => { page.on('console', async (consoleMessage) => {
const type = consoleMessage.type();
const args = await Promise.all(
consoleMessage.args().map(arg => arg.jsonValue())
)
console[type](...args); console[type](...args);
}); });

@ -0,0 +1,11 @@
<svelte:options tag="custom-element"/>
<script>
export function getHost() {
return $$host;
}
export const host = $$host;
</script>
{typeof $$host}

@ -0,0 +1,12 @@
import * as assert from 'assert';
import './main.svelte';
export default async function (target) {
target.innerHTML = '<custom-element></custom-element>';
const el = target.querySelector('custom-element');
assert.equal(el.getHost(), el);
assert.equal(el.host, el);
assert.equal(el.shadowRoot.textContent, 'object');
}

@ -0,0 +1,4 @@
export default {
compileOptions: {},
error: "$$host is for custom element. Did you forget the 'customElement: true' compile option?"
};

@ -0,0 +1,4 @@
export default {
compileOptions: {},
error: "$$host is for custom element. Did you forget the 'customElement: true' compile option?"
};

@ -0,0 +1,3 @@
<script>
export const host = $$host;
</script>
Loading…
Cancel
Save