From 4a403dffbf3cdc49b82c55efb09a4c8bd1b32f08 Mon Sep 17 00:00:00 2001 From: "Marcus R. Brown" Date: Thu, 21 Jul 2016 00:26:18 -0700 Subject: [PATCH] Fix code generation for rt-template siblings. (#164) * Clean up the list of propTemplates fixtures. * Subtract the child index of a rt-template tag by the number of rt-template tags seen. Fixes wix/react-templates#163. --- src/reactTemplates.js | 3 ++- test/data/propTemplates/siblingTemplates.rt | 12 ++++++++++ .../data/propTemplates/siblingTemplates.rt.js | 22 +++++++++++++++++++ test/src/rt.valid.spec.js | 8 ++++++- 4 files changed, 43 insertions(+), 2 deletions(-) create mode 100644 test/data/propTemplates/siblingTemplates.rt create mode 100644 test/data/propTemplates/siblingTemplates.rt.js diff --git a/src/reactTemplates.js b/src/reactTemplates.js index 120cd60..55b1c29 100644 --- a/src/reactTemplates.js +++ b/src/reactTemplates.js @@ -116,6 +116,7 @@ function generateInjectedFunc(context, namePrefix, body, params) { } function generateTemplateProps(node, context) { + let templatePropCount = 0; const propTemplateDefinition = context.options.propTemplates[node.name]; const propertiesTemplates = _(node.children) .map((child, index) => { @@ -141,7 +142,7 @@ function generateTemplateProps(node, context) { } if (templateProp) { - _.assign(templateProp, {childIndex: index, content: _.find(child.children, {type: 'tag'})}); + _.assign(templateProp, {childIndex: index - templatePropCount++, content: _.find(child.children, {type: 'tag'})}); } return templateProp; diff --git a/test/data/propTemplates/siblingTemplates.rt b/test/data/propTemplates/siblingTemplates.rt new file mode 100644 index 0000000..21204d7 --- /dev/null +++ b/test/data/propTemplates/siblingTemplates.rt @@ -0,0 +1,12 @@ +
+ +
Brother: {brother}
+
+ +
Sister: {sister}
+
+
Separator
+ +
Other: {other}
+
+
diff --git a/test/data/propTemplates/siblingTemplates.rt.js b/test/data/propTemplates/siblingTemplates.rt.js new file mode 100644 index 0000000..58f5009 --- /dev/null +++ b/test/data/propTemplates/siblingTemplates.rt.js @@ -0,0 +1,22 @@ +define([ + 'react', + 'lodash' +], function (React, _) { + 'use strict'; + function templateProp11(brother) { + return React.createElement('div', {}, 'Brother: ', brother); + } + function templateProp22(sister) { + return React.createElement('div', {}, 'Sister: ', sister); + } + function templateProp33(other) { + return React.createElement('div', {}, 'Other: ', other); + } + return function () { + return React.createElement('div', { + 'templateProp1': templateProp11.bind(this), + 'templateProp2': templateProp22.bind(this), + 'templateProp3': templateProp33.bind(this) + }, React.createElement('div', {}, 'Separator')); + }; +}); diff --git a/test/src/rt.valid.spec.js b/test/src/rt.valid.spec.js index 7ceff23..5eb01f7 100644 --- a/test/src/rt.valid.spec.js +++ b/test/src/rt.valid.spec.js @@ -49,7 +49,13 @@ module.exports = { }, modules: 'amd' }; - const files = ['propTemplates/simpleTemplate.rt', 'propTemplates/templateInScope.rt', 'propTemplates/implicitTemplate.rt', 'propTemplates/twoTemplates.rt']; + const files = [ + 'simpleTemplate.rt', + 'templateInScope.rt', + 'implicitTemplate.rt', + 'twoTemplates.rt', + 'siblingTemplates.rt' + ].map(file => path.join('propTemplates', file)); testFiles(t, files, options); });