From fd1b5cf20630627d138667284d7b6ff101872271 Mon Sep 17 00:00:00 2001 From: Gianluca Guarini Date: Fri, 22 Nov 2024 23:09:23 +0100 Subject: [PATCH] updated: avoid adding a template fallback for empty slots --- src/generators/template/bindings/slot.js | 20 ++++++++++++-------- test/generators/template.spec.js | 9 +++++++++ 2 files changed, 21 insertions(+), 8 deletions(-) diff --git a/src/generators/template/bindings/slot.js b/src/generators/template/bindings/slot.js index 063798f..967db68 100644 --- a/src/generators/template/bindings/slot.js +++ b/src/generators/template/bindings/slot.js @@ -1,6 +1,7 @@ import { BINDING_ATTRIBUTES_KEY, BINDING_NAME_KEY, + BINDING_TEMPLATE_KEY, BINDING_TYPES, BINDING_TYPE_KEY, DEFAULT_SLOT_NAME, @@ -11,6 +12,7 @@ import { createBindingAttributes, createSelectorProperties, createTemplateProperty, + getChildrenNodes, getName, getNodeAttributes, } from '../utils.js' @@ -63,14 +65,16 @@ export default function createSlotBinding( ), ), simplePropertyNode(BINDING_NAME_KEY, builders.literal(slotName)), - createTemplateProperty( - createNestedBindings( - sourceNode, - sourceFile, - sourceCode, - selectorAttribute, - ), - ), + getChildrenNodes(sourceNode).length + ? createTemplateProperty( + createNestedBindings( + sourceNode, + sourceFile, + sourceCode, + selectorAttribute, + ), + ) + : simplePropertyNode(BINDING_TEMPLATE_KEY, builders.nullLiteral()), ...createSelectorProperties(selectorAttribute), ]) } diff --git a/test/generators/template.spec.js b/test/generators/template.spec.js index d3dde74..4083b2c 100644 --- a/test/generators/template.spec.js +++ b/test/generators/template.spec.js @@ -1293,6 +1293,15 @@ describe('Generators - Template', () => { expect(output[NAME_ATTRIBUTE]).to.be.equal('default') }) + it('Slot without fallback', () => { + const source = '' + const { template } = parse(source) + const input = slotBinding(template, 'expr0', FAKE_SRC_FILE, source) + const output = evaluateOutput(input) + + expect(output[BINDING_TEMPLATE_KEY]).to.be.not.ok + }) + it('Slot fallback html', () => { const source = '
  • {item}
'