Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

compile time error on slotted content inside if/each blocks. closes #849 #852

Merged
merged 1 commit into from
Sep 16, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion src/validate/html/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ const meta = new Map([[':Window', validateWindow]]);
export default function validateHtml(validator: Validator, html: Node) {
const refs = new Map();
const refCallees: Node[] = [];
const stack: Node[] = [];
const elementStack: Node[] = [];

function visit(node: Node) {
Expand All @@ -21,7 +22,7 @@ export default function validateHtml(validator: Validator, html: Node) {
return meta.get(node.name)(validator, node, refs, refCallees);
}

validateElement(validator, node, refs, refCallees, elementStack);
validateElement(validator, node, refs, refCallees, stack, elementStack);
} else if (node.type === 'EachBlock') {
if (validator.helpers.has(node.context)) {
let c = node.expression.end;
Expand All @@ -40,7 +41,9 @@ export default function validateHtml(validator: Validator, html: Node) {

if (node.children) {
if (node.type === 'Element') elementStack.push(node);
stack.push(node);
node.children.forEach(visit);
stack.pop();
if (node.type === 'Element') elementStack.pop();
}

Expand Down
23 changes: 18 additions & 5 deletions src/validate/html/validateElement.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ export default function validateElement(
node: Node,
refs: Map<string, Node[]>,
refCallees: Node[],
stack: Node[],
elementStack: Node[]
) {
const isComponent =
Expand Down Expand Up @@ -189,11 +190,23 @@ export default function validateElement(
}
}

if (attribute.name === 'slot' && !isComponent && isDynamic(attribute)) {
validator.error(
`slot attribute cannot have a dynamic value`,
attribute.start
);
if (attribute.name === 'slot' && !isComponent) {
let i = stack.length;
while (i--) {
const parent = stack[i];
if (parent.type === 'Element' && validator.components.has(parent.name)) break;
if (parent.type === 'IfBlock' || parent.type === 'EachBlock') {
const message = `Cannot place slotted elements inside an ${parent.type === 'IfBlock' ? 'if' : 'each'}-block`;
validator.error(message, attribute.start);
}
}

if (isDynamic(attribute)) {
validator.error(
`slot attribute cannot have a dynamic value`,
attribute.start
);
}
}
}
});
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
[{
"message": "Cannot place slotted elements inside an each-block",
"loc": {
"line": 3,
"column": 7
},
"pos": 43
}]
15 changes: 15 additions & 0 deletions test/validator/samples/component-slotted-each-block/input.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<Nested>
{{#each things as thing}}
<div slot='foo'>{{thing}}</div>
{{/each}}
</Nested>

<script>
import Nested from './Nested.html';

export default {
components: {
Nested
}
};
</script>
8 changes: 8 additions & 0 deletions test/validator/samples/component-slotted-if-block/errors.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
[{
"message": "Cannot place slotted elements inside an if-block",
"loc": {
"line": 3,
"column": 7
},
"pos": 31
}]
15 changes: 15 additions & 0 deletions test/validator/samples/component-slotted-if-block/input.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<Nested>
{{#if thing}}
<div slot='foo'>{{thing}}</div>
{{/if}}
</Nested>

<script>
import Nested from './Nested.html';

export default {
components: {
Nested
}
};
</script>