Skip to content

Commit

Permalink
add $$host
Browse files Browse the repository at this point in the history
  • Loading branch information
tanhauhau committed Mar 16, 2020
1 parent 40c5df5 commit 571011b
Show file tree
Hide file tree
Showing 10 changed files with 51 additions and 2 deletions.
9 changes: 9 additions & 0 deletions src/compiler/compile/Component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,9 @@ export default class Component {
if (variable) {
variable.referenced = true;
} 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({
name,
injected: true,
Expand Down Expand Up @@ -655,6 +658,12 @@ export default class Component {
name,
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] === '$') {
if (name === '$' || name[1] === '$') {
this.error(node as any, {
Expand Down
1 change: 1 addition & 0 deletions src/compiler/compile/render_dom/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -412,6 +412,7 @@ export default function dom(

body.push(b`
function ${definition}(${args}) {
${component.var_lookup.has('$$host') ? 'const $$host = $$self' : null}
${rest}
${reactive_store_declarations}
Expand Down
2 changes: 1 addition & 1 deletion src/compiler/compile/utils/reserved_keywords.ts
Original file line number Diff line number Diff line change
@@ -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) {
return reserved_keywords.has(name);
Expand Down
6 changes: 5 additions & 1 deletion test/custom-elements/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,11 @@ describe('custom-elements', function() {

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);
});

Expand Down
11 changes: 11 additions & 0 deletions test/custom-elements/samples/$$host/main.svelte
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<svelte:options tag="custom-element"/>

<script>
export function getHost() {
return $$host;
}
export const host = $$host;
</script>

{typeof $$host}
12 changes: 12 additions & 0 deletions test/custom-elements/samples/$$host/test.js
Original file line number Diff line number Diff line change
@@ -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');
}
4 changes: 4 additions & 0 deletions test/runtime/samples/$$host-2/_config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
export default {
compileOptions: {},
error: "$$host is for custom element. Did you forget the 'customElement: true' compile option?"
};
1 change: 1 addition & 0 deletions test/runtime/samples/$$host-2/main.svelte
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{$$host}
4 changes: 4 additions & 0 deletions test/runtime/samples/$$host/_config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
export default {
compileOptions: {},
error: "$$host is for custom element. Did you forget the 'customElement: true' compile option?"
};
3 changes: 3 additions & 0 deletions test/runtime/samples/$$host/main.svelte
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<script>
export const host = $$host;
</script>

0 comments on commit 571011b

Please sign in to comment.