Skip to content

Commit

Permalink
complain if named slots other than direct descendant of component
Browse files Browse the repository at this point in the history
  • Loading branch information
tanhauhau committed Mar 5, 2020
1 parent fd378f2 commit 0e219ab
Show file tree
Hide file tree
Showing 11 changed files with 56 additions and 19 deletions.
30 changes: 12 additions & 18 deletions src/compiler/compile/nodes/Element.ts
Original file line number Diff line number Diff line change
Expand Up @@ -278,7 +278,7 @@ export default class Element extends Node {
}

validate_attributes() {
const { component } = this;
const { component, parent } = this;

const attribute_map = new Map();

Expand Down Expand Up @@ -395,26 +395,20 @@ export default class Element extends Node {
component.slot_outlets.add(name);
}

let ancestor = this.parent;
do {
if (ancestor.type === 'InlineComponent') break;
if (ancestor.type === 'Element' && /-/.test(ancestor.name)) break;

if (ancestor.type === 'IfBlock' || ancestor.type === 'EachBlock') {
const type = ancestor.type === 'IfBlock' ? 'if' : 'each';
const message = `Cannot place slotted elements inside an ${type}-block`;
if (parent.type === 'IfBlock' || parent.type === 'EachBlock') {
const type = parent.type === 'IfBlock' ? 'if' : 'each';
const message = `Cannot place slotted elements inside an ${type}-block`;

component.error(attribute, {
code: `invalid-slotted-content`,
message
});
}
} while (ancestor = ancestor.parent);
component.error(attribute, {
code: `invalid-slotted-content`,
message
});
}

if (!ancestor) {
if (!(parent.type === 'InlineComponent' || (parent.type === 'Element' && /-/.test(parent.name)))) {
component.error(attribute, {
code: `invalid-slotted-content`,
message: `Element with a slot='...' attribute must be a descendant of a component or custom element`
message: `Element with a slot='...' attribute must be a direct descendant of a component or custom element`,
});
}
}
Expand All @@ -425,7 +419,7 @@ export default class Element extends Node {
message: `The 'is' attribute is not supported cross-browser and should be avoided`
});
}

attribute_map.set(attribute.name, attribute);
});

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<slot name="slot2"></slot>
3 changes: 3 additions & 0 deletions test/runtime/samples/component-slot-nested-error-2/_config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export default {
error: [`Element with a slot='...' attribute must be a direct descendant of a component or custom element`]
};
11 changes: 11 additions & 0 deletions test/runtime/samples/component-slot-nested-error-2/main.svelte
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<script>
import Nested from "./Nested.svelte";
</script>

<Nested>
<div slot="slot1">
<div>
<div slot="slot2" />
</div>
</div>
</Nested>
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<slot name="slot2"></slot>
3 changes: 3 additions & 0 deletions test/runtime/samples/component-slot-nested-error-3/_config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export default {
error: [`Element with a slot='...' attribute must be a direct descendant of a component or custom element`]
};
11 changes: 11 additions & 0 deletions test/runtime/samples/component-slot-nested-error-3/main.svelte
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<script>
import Nested from "./Nested.svelte";
</script>

<Nested>
<div>
<div>
<div slot="slot2" />
</div>
</div>
</Nested>
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<slot name="slot2"></slot>
3 changes: 3 additions & 0 deletions test/runtime/samples/component-slot-nested-error/_config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export default {
error: [`Element with a slot='...' attribute must be a direct descendant of a component or custom element`]
};
9 changes: 9 additions & 0 deletions test/runtime/samples/component-slot-nested-error/main.svelte
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<script>
import Nested from "./Nested.svelte";
</script>

<Nested>
<div slot="slot1">
<div slot="slot2" />
</div>
</Nested>
2 changes: 1 addition & 1 deletion test/validator/samples/slot-attribute-invalid/errors.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[{
"code": "invalid-slotted-content",
"message": "Element with a slot='...' attribute must be a descendant of a component or custom element",
"message": "Element with a slot='...' attribute must be a direct descendant of a component or custom element",
"start": {
"line": 1,
"column": 5,
Expand Down

0 comments on commit 0e219ab

Please sign in to comment.