Skip to content

Commit fdebc96

Browse files
fixed: Handling of inherited slots
1 parent 9a36b78 commit fdebc96

File tree

3 files changed

+57
-9
lines changed

3 files changed

+57
-9
lines changed

src/generators/template/bindings/tag.js

Lines changed: 21 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ import build from '../builder.js'
2424
import { builders } from '../../../utils/build-types.js'
2525
import compose from 'cumpa'
2626
import { simplePropertyNode } from '../../../utils/custom-ast-nodes.js'
27+
import { hasChildrenNodes, isSlotNode } from '../checks.js'
2728

2829
/**
2930
* Find the slots in the current component and group them under the same id
@@ -64,16 +65,27 @@ function buildSlot(id, sourceNode, sourceFile, sourceCode) {
6465
...sourceNode,
6566
attributes: getNodeAttributes(sourceNode),
6667
}
67-
const [html, bindings] = build(cloneNode, sourceFile, sourceCode)
6868

69-
return builders.objectExpression([
70-
simplePropertyNode(BINDING_ID_KEY, builders.literal(id)),
71-
simplePropertyNode(BINDING_HTML_KEY, builders.literal(html)),
72-
simplePropertyNode(
73-
BINDING_BINDINGS_KEY,
74-
builders.arrayExpression(bindings),
75-
),
76-
])
69+
// If the node is an empty slot we do not create the html key (https://github.com/riot/riot/issues/3055)
70+
const [html, bindings] =
71+
isSlotNode(cloneNode) && !hasChildrenNodes(cloneNode)
72+
? [null, null]
73+
: build(cloneNode, sourceFile, sourceCode)
74+
75+
return builders.objectExpression(
76+
[
77+
simplePropertyNode(BINDING_ID_KEY, builders.literal(id)),
78+
html
79+
? simplePropertyNode(BINDING_HTML_KEY, builders.literal(html))
80+
: null,
81+
bindings
82+
? simplePropertyNode(
83+
BINDING_BINDINGS_KEY,
84+
builders.arrayExpression(bindings),
85+
)
86+
: null,
87+
].filter(Boolean),
88+
)
7789
}
7890

7991
/**

src/generators/template/checks.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -261,3 +261,4 @@ export const hasIfAttribute = compose(Boolean, findIfAttribute)
261261
export const hasEachAttribute = compose(Boolean, findEachAttribute)
262262
export const hasIsAttribute = compose(Boolean, findIsAttribute)
263263
export const hasKeyAttribute = compose(Boolean, findKeyAttribute)
264+
export const hasChildrenNodes = (node) => node?.nodes?.length > 0

test/generators/template.spec.js

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -919,6 +919,41 @@ describe('Generators - Template', () => {
919919
expect(output[BINDING_ATTRIBUTES_KEY]).to.have.length(0)
920920
})
921921

922+
it('Empty inherited slots do not create the HTML key', () => {
923+
const source = '<my-tag><slot name="default" slot="default"/></my-tag>'
924+
925+
const { template } = parse(source)
926+
927+
const input = tagBinding(template, 'expr0', FAKE_SRC_FILE, source)
928+
const output = evaluateOutput(input)
929+
const defaultSlot = output.slots[0]
930+
931+
expect(output[BINDING_SELECTOR_KEY]).to.be.equal('[expr0]')
932+
expect(output[BINDING_TYPE_KEY]).to.be.equal(bindingTypes.TAG)
933+
expect(output[BINDING_EVALUATE_KEY]()).to.be.equal('my-tag')
934+
expect(defaultSlot[BINDING_HTML_KEY]).to.be.not.ok
935+
expect(defaultSlot[BINDING_ID_KEY]).to.be.equal('default')
936+
})
937+
938+
it('Not empty inherited slots do create the HTML key', () => {
939+
const source =
940+
'<my-tag><slot name="default" slot="default">Hello there</slot></my-tag>'
941+
942+
const { template } = parse(source)
943+
944+
const input = tagBinding(template, 'expr0', FAKE_SRC_FILE, source)
945+
const output = evaluateOutput(input)
946+
const defaultSlot = output.slots[0]
947+
948+
expect(output[BINDING_SELECTOR_KEY]).to.be.equal('[expr0]')
949+
expect(output[BINDING_TYPE_KEY]).to.be.equal(bindingTypes.TAG)
950+
expect(output[BINDING_EVALUATE_KEY]()).to.be.equal('my-tag')
951+
expect(defaultSlot[BINDING_HTML_KEY]).to.be.equal(
952+
'<slot expr34="expr34" name="default" slot="default"></slot>',
953+
)
954+
expect(defaultSlot[BINDING_ID_KEY]).to.be.equal('default')
955+
})
956+
922957
it('Tag binding on a custom input element', () => {
923958
const source = '<input is="bar" value="1"/>'
924959
const { template } = parse(source)

0 commit comments

Comments
 (0)