Skip to content

Commit a759443

Browse files
committed
fix(processRecursive): Fix async AND! recursive partial inclusion
1 parent 02b3598 commit a759443

File tree

4 files changed

+30
-16
lines changed

4 files changed

+30
-16
lines changed

core/lib/decompose.js

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ const smh = require('./style_modifier_hunter');
88
const addPattern = require('./addPattern');
99
const jsonCopy = require('./json_copy');
1010
const getPartial = require('./get');
11-
const processRecursive = require('./processRecursive');
1211

1312
const lineage_hunter = new lh();
1413
const list_item_hunter = new lih();
@@ -17,15 +16,19 @@ const style_modifier_hunter = new smh();
1716

1817
function expandPartials(foundPatternPartials, patternlab, currentPattern) {
1918

19+
// these needs to be inside the function call, unless there is a better way to handle the recursion
20+
const processRecursive = require('./processRecursive');
21+
2022
logger.debug(`found partials for ${currentPattern.patternPartial}`);
2123

2224
// determine if the template contains any pattern parameters. if so they
2325
// must be immediately consumed
2426
return parameter_hunter.find_parameters(currentPattern, patternlab).then(() => {
2527

2628
//do something with the regular old partials
27-
for (var i = 0; i < foundPatternPartials.length; i++) {
28-
var partial = currentPattern.findPartial(foundPatternPartials[i]);
29+
foundPatternPartials.forEach((foundPartial) => {
30+
31+
var partial = currentPattern.findPartial(foundPartial);
2932
var partialPath;
3033

3134
//identify which pattern this partial corresponds to
@@ -37,8 +40,6 @@ function expandPartials(foundPatternPartials, patternlab, currentPattern) {
3740
}
3841
}
3942

40-
console.log(processRecursive);
41-
4243
//recurse through nested partials to fill out this extended template.
4344
processRecursive(partialPath, patternlab).then(() => { //eslint-disable-line no-loop-func
4445
//complete assembly of extended template
@@ -48,12 +49,16 @@ function expandPartials(foundPatternPartials, patternlab, currentPattern) {
4849

4950
//if partial has style modifier data, replace the styleModifier value
5051
if (currentPattern.stylePartials && currentPattern.stylePartials.length > 0) {
51-
style_modifier_hunter.consume_style_modifier(cleanPartialPattern, foundPatternPartials[i], patternlab);
52+
style_modifier_hunter.consume_style_modifier(cleanPartialPattern, foundPartial, patternlab);
5253
}
5354

54-
currentPattern.extendedTemplate = currentPattern.extendedTemplate.replace(foundPatternPartials[i], cleanPartialPattern.extendedTemplate);
55+
//this is what we came here for
56+
logger.debug(`within ${currentPattern.patternPartial}, replacing extendedTemplate partial ${foundPartial} with ${cleanPartialPattern.patternPartial}'s extededTemplate`);
57+
currentPattern.extendedTemplate = currentPattern.extendedTemplate.replace(foundPartial, cleanPartialPattern.extendedTemplate);
5558
});
56-
}
59+
});
60+
}).catch((reason) => {
61+
logger.error(reason);
5762
});
5863
}
5964

@@ -66,6 +71,7 @@ function expandPartials(foundPatternPartials, patternlab, currentPattern) {
6671
*/
6772
module.exports = function (pattern, patternlab, ignoreLineage) {
6873

74+
//set the extendedTemplate to operate on later if we find partials to replace
6975
pattern.extendedTemplate = pattern.template;
7076

7177
//find how many partials there may be for the given pattern
@@ -106,5 +112,8 @@ module.exports = function (pattern, patternlab, ignoreLineage) {
106112
addPattern(pattern, patternlab);
107113
});
108114

109-
return Promise.all([expandPartialPromise, lineagePromise, addPromise]);
115+
return Promise.all([expandPartialPromise, lineagePromise, addPromise])
116+
.catch((reason) => {
117+
logger.error(reason);
118+
});
110119
};

core/lib/object_factory.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,7 @@ Pattern.prototype = {
113113
render: function (data, partials) {
114114

115115
if (!this.extendedTemplate) {
116+
console.log('setting extendedTemplate because it didnt seem to be set. this is NEW CODE')
116117
this.extendedTemplate = this.template;
117118
}
118119

core/lib/processRecursive.js

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
"use strict";
22

3+
const logger = require('./log');
34
const decompose = require('./decompose');
45

56
module.exports = function (file, patternlab) {
6-
//find current pattern in patternlab object using var file as a partial
7+
8+
//find current pattern in patternlab object using file as a partial
79
var currentPattern, i;
810

911
for (i = 0; i < patternlab.patterns.length; i++) {
@@ -19,5 +21,8 @@ module.exports = function (file, patternlab) {
1921
if (currentPattern.engine === null) { return Promise.resolve(); }
2022

2123
//call our helper method to actually unravel the pattern with any partials
22-
return decompose(currentPattern, patternlab);
24+
return decompose(currentPattern, patternlab)
25+
.catch((reason)=> {
26+
logger.error(reason);
27+
});
2328
};

test/processRecursive_tests.js

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -37,14 +37,13 @@ tap.test('process_pattern_recursive recursively includes partials', function (te
3737

3838
Promise.all([p1, p2]).then(() => {
3939
//act
40-
console.log('????');
41-
processRecursive('00-test' + path.sep + '00-foo.mustache', patternlab).then(() => {
40+
processRecursive(fooPatternPath, patternlab).then(() => {
4241
//assert
4342
var expectedValue = 'bar';
44-
test.equals(fooPattern.extendedTemplate.replace(/\s\s+/g, ' ').replace(/\n/g, ' ').trim(), expectedValue.trim());
43+
test.equals(util.sanitized(fooPattern.extendedTemplate), util.sanitized(expectedValue));
4544
test.end();
46-
});
47-
});
45+
}).catch(test.threw);
46+
}).catch(test.threw);
4847
});
4948

5049
/*

0 commit comments

Comments
 (0)