Skip to content

Commit

Permalink
Merge pull request #76 from straker/noCarryover
Browse files Browse the repository at this point in the history
fix(*): prevent leaks from prior runs
  • Loading branch information
straker committed Dec 21, 2019
2 parents 831a86b + ff1ff8f commit cd0fef8
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 11 deletions.
17 changes: 7 additions & 10 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ var Handlebars = require('handlebars');

var generate = require('./lib/generate');
var parseComments = require('./lib/parseComments');
var tags = require('./lib/tags');
var defaultTags = require('./lib/tags');
var utils = require('./lib/utils');

/**
Expand Down Expand Up @@ -86,13 +86,7 @@ function livingcss(source, dest, options) {
options.loadcss = (typeof options.loadcss === 'undefined' ? true : options.loadcss);

// add custom tags
for (var tag in options.tags) {
if (!options.tags.hasOwnProperty(tag)) {
continue;
}

tags[tag] = options.tags[tag];
}
var tags = Object.assign({}, defaultTags, options.tags);

return Promise.all([
// read the handlebars template
Expand Down Expand Up @@ -166,9 +160,12 @@ function livingcss(source, dest, options) {

// wait until all promises have returned (either rejected or resolved) before
// returning the last promise
return Promise.all(promises);
return Promise.all(promises).then(function() {
defaultTags.resetForwardReference();
});
})
.catch(function(err) {
defaultTags.resetForwardReference();

// in a gulp pipeline we don't want to log the error as gulp-util will do that
// for us
Expand All @@ -183,5 +180,5 @@ function livingcss(source, dest, options) {
module.exports = livingcss;
module.exports.generate = generate;
module.exports.parseComments = parseComments;
module.exports.tags = tags;
module.exports.tags = defaultTags;
module.exports.utils = utils;
7 changes: 6 additions & 1 deletion lib/tags.js
Original file line number Diff line number Diff line change
Expand Up @@ -417,4 +417,9 @@ var tags = {
tags.code = tags.example; // @code and @example generate the same structure

module.exports = tags;
module.exports.forwardReferenceSections = forwardReferenceSections;
module.exports.forwardReferenceSections = forwardReferenceSections;
module.exports.resetForwardReference = function resetForwardReference() {
for (var name in forwardReferenceSections) {
delete forwardReferenceSections[name];
}
}
51 changes: 51 additions & 0 deletions test/integration.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ var expect = require('chai').expect;
var sinon = require('sinon');
var path = require('path');
var livingcss = require('../index');
var tags = require('../lib/tags');

describe('livingcss', function() {

Expand Down Expand Up @@ -69,4 +70,54 @@ describe('livingcss', function() {
});
});

it('should not carry over undefined section error from previous run', function() {
expect(Object.keys(tags.forwardReferenceSections).length).to.equal(0);
});

it('should use custom tags', function(done) {
var file = path.join(__dirname, 'data/simple-tag.css');
var options = {
preprocess: function(context) {
return false;
},
tags: {
tagName: function() {
return false;
}
}
};

sinon.spy(options.tags, 'tagName');

livingcss(file, '.', options).then(function() {
expect(options.tags.tagName.called).to.be.true;
done();
})
.catch(function(e) {
done(e);
});
});

it('should not modify original tags', function(done) {
var file = path.join(__dirname, 'data/section.css');
var options = {
preprocess: function(context) {
return false;
},
tags: {
foo: function() {
return false;
}
}
};

livingcss(file, '.', options).then(function() {
expect(tags.foo).to.not.exist;
done();
})
.catch(function(e) {
done(e);
});
});

});

0 comments on commit cd0fef8

Please sign in to comment.