Skip to content

Commit 0cc5722

Browse files
authored
Bug: Reserve data= In Shorthand Subtemplates (#188)
1 parent 06f0fac commit 0cc5722

3 files changed

Lines changed: 31 additions & 2 deletions

File tree

packages/compiler/src/template-compiler.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -652,6 +652,11 @@ class TemplateCompiler {
652652
each(dataMatches, (match, index) => {
653653
let name = match[1].trim();
654654
let value = match[2].trim();
655+
// `data` is the reserved blob arg, not a reactive key
656+
if (name === 'data') {
657+
templateInfo.data = value;
658+
return;
659+
}
655660
data[name] = value;
656661
});
657662
// standard notation defaults to reactive data

packages/compiler/test/compiler.test.js

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1306,6 +1306,30 @@ describe('TemplateCompiler', () => {
13061306
expect(ast).toEqual(expectedAST);
13071307
});
13081308

1309+
it('should route shorthand data= to node.data, not reactiveData', () => {
1310+
const compiler = new TemplateCompiler();
1311+
const ast = compiler.compile(`{>child data=getCardData}`);
1312+
expect(ast).toEqual([
1313+
{ type: 'template', name: "'child'", data: 'getCardData', reactiveData: {} },
1314+
]);
1315+
});
1316+
1317+
it('should split mixed shorthand args into data and reactiveData', () => {
1318+
const compiler = new TemplateCompiler();
1319+
const ast = compiler.compile(`{>child data=getRow extra=foo}`);
1320+
expect(ast).toEqual([
1321+
{ type: 'template', name: "'child'", data: 'getRow', reactiveData: { extra: 'foo' } },
1322+
]);
1323+
});
1324+
1325+
it('should produce equivalent data binding for shorthand vs verbose', () => {
1326+
const compiler = new TemplateCompiler();
1327+
const shorthand = compiler.compile(`{>child data=getRow}`);
1328+
const verbose = compiler.compile(`{> template name='child' data=getRow}`);
1329+
expect(shorthand[0].data).toBe(verbose[0].data);
1330+
expect(shorthand[0].reactiveData ?? {}).toEqual(verbose[0].reactiveData ?? {});
1331+
});
1332+
13091333
it('should compile a template with a partial and reactive data', () => {
13101334
const compiler = new TemplateCompiler();
13111335
const template = `

packages/renderer/test/browser/subtree-misc.test.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -336,8 +336,8 @@ RENDERING_ENGINES.forEach(engine => {
336336
tagName: tag,
337337
template: [
338338
'{#snippet leaf}<span>{value}</span>{/snippet}',
339-
'{#snippet branch}{>leaf value=data}{/snippet}',
340-
'{>branch data=(getLabel)}',
339+
'{#snippet branch}{>leaf value=label}{/snippet}',
340+
'{>branch label=(getLabel)}',
341341
].join(''),
342342
defaultState: { count: 0 },
343343
createComponent: ({ state }) => ({

0 commit comments

Comments
 (0)