Skip to content

Commit

Permalink
Fix bug when calling freeze when freezing
Browse files Browse the repository at this point in the history
Previously, when an attacher (or transformer?) called `.freeze`,
this could cause plugins to be invoked multiple times.
This commit fixed that. 😬

Closes remarkjs/remark-lint#131.
  • Loading branch information
wooorm committed Feb 28, 2017
1 parent 8da7530 commit f7b592f
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 3 deletions.
7 changes: 4 additions & 3 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ function unified() {
var transformers = trough();
var namespace = {};
var frozen = false;
var freezeIndex = -1;

/* Data management. */
processor.data = data;
Expand Down Expand Up @@ -95,7 +96,6 @@ function unified() {
* In essence, always invoke this when exporting a
* processor. */
function freeze() {
var index = -1;
var values;
var plugin;
var options;
Expand All @@ -105,8 +105,8 @@ function unified() {
return processor;
}

while (++index < attachers.length) {
values = attachers[index];
while (++freezeIndex < attachers.length) {
values = attachers[freezeIndex];
plugin = values[0];
options = values[1];
transformer = null;
Expand All @@ -127,6 +127,7 @@ function unified() {
}

frozen = true;
freezeIndex = Infinity;

return processor;
}
Expand Down
21 changes: 21 additions & 0 deletions test/freeze.js
Original file line number Diff line number Diff line change
Expand Up @@ -78,5 +78,26 @@ test('freeze()', function (t) {
'`process` can be invoked on frozen interfaces'
);

t.test('should freeze once, even for nested calls', function (st) {
st.plan(2);

var index = 0;
var p = unified()
.use(plugin)
.use({plugins: [freezingPlugin]})
.use({plugins: [freezingPlugin]})
.freeze();

p().freeze();

function plugin() {
st.pass('Expected: ' + String(index++));
}

function freezingPlugin() {
this.freeze();
}
});

t.end();
});

0 comments on commit f7b592f

Please sign in to comment.